forked from nettigo/DS18B20
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathDS18B20.cpp
249 lines (198 loc) · 5.58 KB
/
DS18B20.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
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
#include "DS18B20.h"
// Exception handler
// Prints the line number of the exception and block the program, when the function returns false
void __check(bool value, uint16_t line)
{
if (value)
return;
Serial.print(F("EXCEPTION at line: "));
Serial.println(line);
while(1);
}
// Constructor
// Argument: Pointer to OneWire object
// Return: New DS18B20 object
DS18B20::DS18B20(OneWire *oneWire)
{
_oneWire = oneWire;
_quality = 0;
_samePowerType = false;
_powerType = false;
_beginConversionTime = 0;
}
// Setup for all ds19b20 sensors in 1-Wire bus.
// Argument: quality - measurement resolution in bits (from 9 to 12)
// Return:
// true - if all operations were successful
// false - when the bus is physically damaged
// - when devices not respond
// - when device address is not valid
// - when not detect any device
bool DS18B20::begin(uint8_t quality)
{
_quality = constrain(quality, 9, 12);
uint8_t address[8];
uint8_t devices = 0;
uint8_t parasiteDevices = 0;
uint32_t beginResetTimeout = millis();
while(!_oneWire->reset())
{
uint32_t elapsedResetTimeout = millis() - beginResetTimeout;
if (elapsedResetTimeout > 1000)
return false;
}
_oneWire->reset_search();
while (_oneWire->search(address))
{
if (OneWire::crc8(address, 7) != address[7])
return false;
if (address[0] != 0x28)
continue;
if (!_sendQuality(address))
return false;
_powerType = _receivePowerType(address);
if (!_powerType)
parasiteDevices++;
devices++;
}
if (parasiteDevices == devices || parasiteDevices == 0)
_samePowerType = true;
if (devices == 0)
return false;
return true;
}
// Request for temperature measurements on all devices
// Return:
// - true - if operation were successful
// - false - if devices have different ways of power (combinations of normal and parasite in one bus)
// - if devices not responding
bool DS18B20::request()
{
if (!_samePowerType)
return false;
if (!_oneWire->reset())
return false;
_oneWire->skip();
_oneWire->write(0x44, !_powerType);
_beginConversionTime = millis();
return true;
}
// Request for temperature measurements on device
// Argument: Pointer to an array of device address
// Return:
// - true - if operation were successful
// - false - if device not responding
bool DS18B20::request(uint8_t *address)
{
if (!_samePowerType)
{
_powerType = _receivePowerType(address);
}
if (!_oneWire->reset())
return false;
_oneWire->select(address);
_oneWire->write(0x44, !_powerType);
_beginConversionTime = millis();
return true;
}
// Request for temperature measurements on device
// Argument: Pointer to an array in flash memory of device address
// Return:
// - true - if operation were successful
// - false - if device not responding
bool DS18B20::request(const __FlashStringHelper *_address)
{
uint8_t address[8];
_readFlashAddress(_address, address);
return request(address);
}
// Checks if devices completed the measurement
// Return:
// - true - the measurement was completed
// - false - the measurement wasn't completed
bool DS18B20::available(void)
{
uint32_t durationTime[] = {94, 188, 375, 750};
uint32_t elapsedTime = millis() - _beginConversionTime;
bool timeout = elapsedTime >= durationTime[_quality-9];
if (_powerType)
{
bool ready = _oneWire->read_bit();
if (ready || timeout)
return true;
}
if (timeout)
return true;
return false;
}
// Read temperature from device
// Argument: Pointer to an array of device address
// Return: temperature in degrees Celsius
// If the temperature is TEMP_ERROR value - measurement failed because:
// - the bus is physically damaged
// - devices not respond
// - when data from the device is not valid
// - when not detect device of thad address
float DS18B20::readTemperature(uint8_t *address)
{
uint8_t scratchpad[9];
if (!_sendCommand(address, 0xbe))
return TEMP_ERROR;
_oneWire->read_bytes(scratchpad, 9);
if (OneWire::crc8(scratchpad, 8) != scratchpad[8])
return TEMP_ERROR;
float quality[] = {0.5, 0.25, 0.125, 0.0625};
uint8_t shift[] = {3, 2, 1, 0};
int16_t raw = word(scratchpad[1], scratchpad[0]);
raw >>= shift[_quality-9];
return raw * quality[_quality-9];
}
// Read temperature from device
// Argument: Pointer to an array in flash memory of device address
// Return: temperature in degrees Celsius
// If the temperature is TEMP_ERROR value - measurement failed because:
// - the bus is physically damaged
// - devices not respond
// - when data from the device is not valid
// - when not detect device of thad address
float DS18B20::readTemperature(const __FlashStringHelper *_address)
{
uint8_t address[8];
_readFlashAddress(_address, address);
return readTemperature(address);
}
// private methods
bool DS18B20::_sendCommand(uint8_t *address, uint8_t command)
{
if (!_oneWire->reset())
return false;
_oneWire->select(address);
_oneWire->write(command);
return true;
}
bool DS18B20::_sendQuality(uint8_t *address)
{
if (!_sendCommand(address, 0x4e))
return false;
_oneWire->write(0);
_oneWire->write(0);
uint8_t quality = _quality;
quality -= 9;
quality <<= 5;
quality |= 0b00011111;
_oneWire->write(quality);
return true;
}
bool DS18B20::_receivePowerType(uint8_t *address)
{
_sendCommand(address, 0xb4);
return _oneWire->read();
}
void DS18B20::_readFlashAddress(const __FlashStringHelper *_address, uint8_t *address)
{
const uint8_t *pgmAddress PROGMEM = (const uint8_t PROGMEM *) _address;
for (uint8_t i=0; i<8; i++)
{
address[i] = pgm_read_byte(pgmAddress++);
}
}