forked from RobTillaart/DS18B20_INT
-
Notifications
You must be signed in to change notification settings - Fork 0
/
DS18B20_INT.cpp
172 lines (141 loc) · 3.78 KB
/
DS18B20_INT.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
//
// FILE: DS18B20_INT.cpp
// AUTHOR: [email protected]
// VERSION: 0.2.0
// DATE: 2017-07-25
// PUPROSE: library for DS18B20 temperature sensor - integer only.
// URL: https://github.com/RobTillaart/DS18B20_INT
// https://github.com/RobTillaart/DS18B20_RT
//
// HISTORY:
// 0.1.0 2017-07-25 initial version
// 0.1.1 2019-
// 0.1.2 2020-08-05 refactor / sync with DS18B20
// 0.1.3 2020-12-20 add Arduino-CI + unit test
// 0.1.4 2021-05-26 add OneWire.reset() to begin()
// 0.1.5 2021-06-16 add retries parameter to begin()
// 0.1.6 2021-10-03 add dependency + fix build-CI
// 0.1.7 2021-12-17 update library.json, license, minor edits
// 0.2.0 2022-06-23 fix #10 getTempCentiC() (thanks negroKiordi)
// fix reading sensor
#include "DS18B20_INT.h"
// OneWire commands
#define STARTCONVO 0x44
#define READSCRATCH 0xBE
#define WRITESCRATCH 0x4E
// Device resolution
#define TEMP_9_BIT 0x1F // 9 bit
#define TEMP_10_BIT 0x3F // 10 bit
#define TEMP_11_BIT 0x5F // 11 bit
#define TEMP_12_BIT 0x7F // 12 bit
DS18B20_INT::DS18B20_INT(OneWire* ow)
{
_oneWire = ow;
_addressFound = false;
_resolution = TEMP_9_BIT;
}
bool DS18B20_INT::begin(uint8_t retries)
{
_addressFound = false;
for (uint8_t rtr = retries; (rtr > 0) && (_addressFound == false); rtr--)
{
_oneWire->reset();
_oneWire->reset_search();
_deviceAddress[0] = 0x00;
_oneWire->search(_deviceAddress);
_addressFound = _deviceAddress[0] != 0x00 &&
_oneWire->crc8(_deviceAddress, 7) == _deviceAddress[7];
}
if (_addressFound)
{
_oneWire->reset();
_oneWire->select(_deviceAddress);
_oneWire->write(WRITESCRATCH);
// two dummy values for LOW & HIGH ALARM
_oneWire->write(0);
_oneWire->write(100);
_oneWire->write(_resolution); // lowest as we do only integer math.
_oneWire->reset();
}
return _addressFound;
}
void DS18B20_INT::requestTemperatures(void)
{
_oneWire->reset();
_oneWire->skip();
_oneWire->write(STARTCONVO, 0);
}
bool DS18B20_INT::isConversionComplete(void)
{
return (_oneWire->read_bit() == 1);
}
int16_t DS18B20_INT::getTempC(void)
{
int16_t rawTemperature = _readRaw();
rawTemperature >>= 4;
if (rawTemperature < -55) return DEVICE_DISCONNECTED;
return rawTemperature;
}
bool DS18B20_INT::getAddress(uint8_t* buf)
{
if (_addressFound)
{
for (uint8_t i = 0; i < 8; i++)
{
buf[i] = _deviceAddress[i];
}
}
return _addressFound;
}
void DS18B20_INT::setResolution(uint8_t bits)
{
uint8_t newRes = 0;
switch (bits)
{
case 12: newRes = TEMP_12_BIT; break;
case 11: newRes = TEMP_11_BIT; break;
case 10: newRes = TEMP_10_BIT; break;
default: newRes = TEMP_9_BIT; break;
}
if (newRes != _resolution)
{
_resolution = newRes;
begin();
}
}
uint8_t DS18B20_INT::getResolution()
{
switch (_resolution)
{
case TEMP_12_BIT: return 12;
case TEMP_11_BIT: return 11;
case TEMP_10_BIT: return 10;
case TEMP_9_BIT: return 9;
}
return 0;
}
int16_t DS18B20_INT::getTempCentiC(void)
{
int16_t rawTemperature = _readRaw();
// rawTemperature = rawTemperature * 100 / 16;
rawTemperature *= 25;
rawTemperature >>= 2;
// use at own risk.
// if (rawTemperature < -5500) return DEVICE_DISCONNECTED * 100;
return rawTemperature;
}
//////////////////////////////////////////////////
//
// PRIVATE
//
int16_t DS18B20_INT::_readRaw(void)
{
_oneWire->reset();
_oneWire->select(_deviceAddress);
_oneWire->write(READSCRATCH);
int16_t rawTemperature = ((int16_t)_oneWire->read());
rawTemperature |= _oneWire->read() << 8;
_oneWire->reset();
return rawTemperature;
}
// -- END OF FILE --