@@ -21,7 +21,7 @@ DallasTemperature::DallasTemperature() {}
21
21
DallasTemperature::DallasTemperature (OneWire* _oneWire)
22
22
23
23
#if REQUIRESALARMS
24
- : _AlarmHandler(&defaultAlarmHandler)
24
+ : _AlarmHandler(&defaultAlarmHandler)
25
25
#endif
26
26
{
27
27
setOneWire (_oneWire);
@@ -101,19 +101,22 @@ bool DallasTemperature::isConnected(const uint8_t* deviceAddress)
101
101
// also allows for updating the read scratchpad
102
102
bool DallasTemperature::isConnected (const uint8_t * deviceAddress, uint8_t * scratchPad)
103
103
{
104
- readScratchPad (deviceAddress, scratchPad);
105
- return (_wire->crc8 (scratchPad, 8 ) == scratchPad[SCRATCHPAD_CRC]);
104
+ bool b = readScratchPad (deviceAddress, scratchPad);
105
+ return b && (_wire->crc8 (scratchPad, 8 ) == scratchPad[SCRATCHPAD_CRC]);
106
106
}
107
107
108
108
// read device's scratch pad
109
- void DallasTemperature::readScratchPad (const uint8_t * deviceAddress, uint8_t * scratchPad)
109
+ // returns false if it fails to reset the Wire bus
110
+ bool DallasTemperature::readScratchPad (const uint8_t * deviceAddress, uint8_t * scratchPad)
110
111
{
111
- // send the command
112
- _wire->reset ();
112
+ // send the reset command and fail fast
113
+ int b = _wire->reset ();
114
+ if (b == 0 ) return false ;
115
+
113
116
_wire->select (deviceAddress);
114
117
_wire->write (READSCRATCH);
115
118
116
- // TODO => collect all comments & use simple loop
119
+ // Read all registers in a simple loop
117
120
// byte 0: temperature LSB
118
121
// byte 1: temperature MSB
119
122
// byte 2: high alarm temp
@@ -126,53 +129,16 @@ void DallasTemperature::readScratchPad(const uint8_t* deviceAddress, uint8_t* sc
126
129
// byte 7: DS18S20: COUNT_PER_C
127
130
// DS18B20 & DS1822: store for crc
128
131
// byte 8: SCRATCHPAD_CRC
129
- //
130
- // for(int i=0; i<9; i++)
131
- // {
132
- // scratchPad[i] = _wire->read();
133
- // }
134
-
135
-
136
- // read the response
137
-
138
- // byte 0: temperature LSB
139
- scratchPad[TEMP_LSB] = _wire->read ();
140
-
141
- // byte 1: temperature MSB
142
- scratchPad[TEMP_MSB] = _wire->read ();
143
-
144
- // byte 2: high alarm temp
145
- scratchPad[HIGH_ALARM_TEMP] = _wire->read ();
146
-
147
- // byte 3: low alarm temp
148
- scratchPad[LOW_ALARM_TEMP] = _wire->read ();
149
-
150
- // byte 4:
151
- // DS18S20: store for crc
152
- // DS18B20 & DS1822: configuration register
153
- scratchPad[CONFIGURATION] = _wire->read ();
154
-
155
- // byte 5:
156
- // internal use & crc
157
- scratchPad[INTERNAL_BYTE] = _wire->read ();
158
-
159
- // byte 6:
160
- // DS18S20: COUNT_REMAIN
161
- // DS18B20 & DS1822: store for crc
162
- scratchPad[COUNT_REMAIN] = _wire->read ();
163
-
164
- // byte 7:
165
- // DS18S20: COUNT_PER_C
166
- // DS18B20 & DS1822: store for crc
167
- scratchPad[COUNT_PER_C] = _wire->read ();
168
-
169
- // byte 8:
170
- // SCTRACHPAD_CRC
171
- scratchPad[SCRATCHPAD_CRC] = _wire->read ();
132
+ for (uint8_t i = 0 ; i < 9 ; i++)
133
+ {
134
+ scratchPad[i] = _wire->read ();
135
+ }
172
136
173
- _wire->reset ();
137
+ b = _wire->reset ();
138
+ return (b == 1 );
174
139
}
175
140
141
+
176
142
// writes device's scratch pad
177
143
void DallasTemperature::writeScratchPad (const uint8_t * deviceAddress, const uint8_t * scratchPad)
178
144
{
@@ -187,7 +153,7 @@ void DallasTemperature::writeScratchPad(const uint8_t* deviceAddress, const uint
187
153
_wire->select (deviceAddress); // <--this line was missing
188
154
// save the newly written values to eeprom
189
155
_wire->write (COPYSCRATCH, parasite);
190
- delay (20 ); // <--- added 20ms delay to allow 10ms long EEPROM write operation (as specified by datasheet)
156
+ delay (20 ); // <--- added 20ms delay to allow 10ms long EEPROM write operation (as specified by datasheet)
191
157
if (parasite) delay (10 ); // 10ms delay
192
158
_wire->reset ();
193
159
}
@@ -402,7 +368,7 @@ float DallasTemperature::getTempCByIndex(uint8_t deviceIndex)
402
368
{
403
369
DeviceAddress deviceAddress;
404
370
if (!getAddress (deviceAddress, deviceIndex))
405
- return DEVICE_DISCONNECTED_C;
371
+ return DEVICE_DISCONNECTED_C;
406
372
return getTempC ((uint8_t *)deviceAddress);
407
373
}
408
374
@@ -411,16 +377,16 @@ float DallasTemperature::getTempFByIndex(uint8_t deviceIndex)
411
377
{
412
378
DeviceAddress deviceAddress;
413
379
if (!getAddress (deviceAddress, deviceIndex))
414
- return DEVICE_DISCONNECTED_F;
380
+ return DEVICE_DISCONNECTED_F;
415
381
return getTempF ((uint8_t *)deviceAddress);
416
382
}
417
383
418
384
// reads scratchpad and returns fixed-point temperature, scaling factor 2^-7
419
385
int16_t DallasTemperature::calculateTemperature (const uint8_t * deviceAddress, uint8_t * scratchPad)
420
386
{
421
387
int16_t fpTemperature =
422
- (((int16_t ) scratchPad[TEMP_MSB]) << 11 ) |
423
- (((int16_t ) scratchPad[TEMP_LSB]) << 3 );
388
+ (((int16_t ) scratchPad[TEMP_MSB]) << 11 ) |
389
+ (((int16_t ) scratchPad[TEMP_LSB]) << 3 );
424
390
425
391
/*
426
392
DS1820 and DS18S20 have a 9-bit temperature register.
@@ -448,11 +414,11 @@ int16_t DallasTemperature::calculateTemperature(const uint8_t* deviceAddress, ui
448
414
*/
449
415
450
416
if (deviceAddress[0 ] == DS18S20MODEL)
451
- fpTemperature = ((fpTemperature & 0xfff0 ) << 3 ) - 16 +
452
- (
453
- ((scratchPad[COUNT_PER_C] - scratchPad[COUNT_REMAIN]) << 7 ) /
454
- scratchPad[COUNT_PER_C]
455
- );
417
+ fpTemperature = ((fpTemperature & 0xfff0 ) << 3 ) - 16 +
418
+ (
419
+ ((scratchPad[COUNT_PER_C] - scratchPad[COUNT_REMAIN]) << 7 ) /
420
+ scratchPad[COUNT_PER_C]
421
+ );
456
422
457
423
return fpTemperature;
458
424
}
@@ -505,7 +471,7 @@ bool DallasTemperature::isParasitePowerMode(void)
505
471
TH and TL Register Format
506
472
507
473
BIT 7 BIT 6 BIT 5 BIT 4 BIT 3 BIT 2 BIT 1 BIT 0
508
- S 2^6 2^5 2^4 2^3 2^2 2^1 2^0
474
+ S 2^6 2^5 2^4 2^3 2^2 2^1 2^0
509
475
510
476
Only bits 11 through 4 of the temperature register are used
511
477
in the TH and TL comparison since TH and TL are 8-bit
@@ -576,7 +542,7 @@ void DallasTemperature::resetAlarmSearch()
576
542
alarmSearchJunction = -1 ;
577
543
alarmSearchExhausted = 0 ;
578
544
for (uint8_t i = 0 ; i < 7 ; i++)
579
- alarmSearchAddress[i] = 0 ;
545
+ alarmSearchAddress[i] = 0 ;
580
546
}
581
547
582
548
// This is a modified version of the OneWire::search method.
@@ -693,7 +659,7 @@ void DallasTemperature::processAlarms(void)
693
659
while (alarmSearch (alarmAddr))
694
660
{
695
661
if (validAddress (alarmAddr))
696
- _AlarmHandler (alarmAddr);
662
+ _AlarmHandler (alarmAddr);
697
663
}
698
664
}
699
665
@@ -710,11 +676,11 @@ void DallasTemperature::defaultAlarmHandler(const uint8_t* deviceAddress)
710
676
711
677
#endif
712
678
713
- // IF alarm is not used one can store a 16 bit int of userdata in the alarm
679
+ // IF alarm is not used one can store a 16 bit int of userdata in the alarm
714
680
// registers. E.g. an ID of the sensor.
715
681
// See github issue #29
716
682
717
- // note if device is not connected it will fail writing the data.
683
+ // note if device is not connected it will fail writing the data.
718
684
void DallasTemperature::setUserData (const uint8_t * deviceAddress, int16_t data)
719
685
{
720
686
ScratchPad scratchPad;
@@ -730,7 +696,7 @@ int16_t DallasTemperature::getUserData(const uint8_t* deviceAddress)
730
696
{
731
697
int16_t data = 0 ;
732
698
ScratchPad scratchPad;
733
- if (isConnected (deviceAddress, scratchPad))
699
+ if (isConnected (deviceAddress, scratchPad))
734
700
{
735
701
data = scratchPad[HIGH_ALARM_TEMP] << 8 ;
736
702
data += scratchPad[LOW_ALARM_TEMP];
@@ -770,7 +736,7 @@ float DallasTemperature::toCelsius(float fahrenheit)
770
736
float DallasTemperature::rawToCelsius (int16_t raw)
771
737
{
772
738
if (raw <= DEVICE_DISCONNECTED_RAW)
773
- return DEVICE_DISCONNECTED_C;
739
+ return DEVICE_DISCONNECTED_C;
774
740
// C = RAW/128
775
741
return (float )raw * 0.0078125 ;
776
742
}
@@ -779,7 +745,7 @@ float DallasTemperature::rawToCelsius(int16_t raw)
779
745
float DallasTemperature::rawToFahrenheit (int16_t raw)
780
746
{
781
747
if (raw <= DEVICE_DISCONNECTED_RAW)
782
- return DEVICE_DISCONNECTED_F;
748
+ return DEVICE_DISCONNECTED_F;
783
749
// C = RAW/128
784
750
// F = (C*1.8)+32 = (RAW/128*1.8)+32 = (RAW*0.0140625)+32
785
751
return ((float )raw * 0.0140625 ) + 32 ;
0 commit comments