-
Notifications
You must be signed in to change notification settings - Fork 487
/
DallasTemperature.cpp
1122 lines (890 loc) · 31.8 KB
/
DallasTemperature.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
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
// This library is free software; you can redistribute it and/or
// modify it under the terms of the GNU Lesser General Public
// License as published by the Free Software Foundation; either
// version 2.1 of the License, or (at your option) any later version.
#include "DallasTemperature.h"
// for Particle support
// yield() is not a standard function, but instead wraps Particle process
// https://community.particle.io/t/syscall-yield-operation/40708/2
#if defined(PLATFORM_ID) // Only defined if a Particle device
inline void yield() {
Particle.process();
}
#elif ARDUINO >= 100
#include "Arduino.h"
#else
extern "C" {
#include "WConstants.h"
}
#endif
// OneWire commands
#define STARTCONVO 0x44 // Tells device to take a temperature reading and put it on the scratchpad
#define COPYSCRATCH 0x48 // Copy scratchpad to EEPROM
#define READSCRATCH 0xBE // Read from scratchpad
#define WRITESCRATCH 0x4E // Write to scratchpad
#define RECALLSCRATCH 0xB8 // Recall from EEPROM to scratchpad
#define READPOWERSUPPLY 0xB4 // Determine if device needs parasite power
#define ALARMSEARCH 0xEC // Query bus for devices with an alarm condition
// Scratchpad locations
#define TEMP_LSB 0
#define TEMP_MSB 1
#define HIGH_ALARM_TEMP 2
#define LOW_ALARM_TEMP 3
#define CONFIGURATION 4
#define INTERNAL_BYTE 5
#define COUNT_REMAIN 6
#define COUNT_PER_C 7
#define SCRATCHPAD_CRC 8
// DSROM FIELDS
#define DSROM_FAMILY 0
#define DSROM_CRC 7
// 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
#define MAX_CONVERSION_TIMEOUT 750
// Alarm handler
#define NO_ALARM_HANDLER ((AlarmHandler *)0)
DallasTemperature::DallasTemperature() {
#if REQUIRESALARMS
setAlarmHandler(NO_ALARM_HANDLER);
#endif
useExternalPullup = false;
}
DallasTemperature::DallasTemperature(OneWire* _oneWire) : DallasTemperature() {
setOneWire(_oneWire);
}
bool DallasTemperature::validFamily(const uint8_t* deviceAddress) {
switch (deviceAddress[DSROM_FAMILY]) {
case DS18S20MODEL:
case DS18B20MODEL:
case DS1822MODEL:
case DS1825MODEL:
case DS28EA00MODEL:
return true;
default:
return false;
}
}
/*
* Constructs DallasTemperature with strong pull-up turned on. Strong pull-up is mandated in DS18B20 datasheet for parasitic
* power (2 wires) setup. (https://datasheets.maximintegrated.com/en/ds/DS18B20.pdf, p. 7, section 'Powering the DS18B20').
*/
DallasTemperature::DallasTemperature(OneWire* _oneWire, uint8_t _pullupPin) : DallasTemperature(_oneWire) {
setPullupPin(_pullupPin);
}
void DallasTemperature::setPullupPin(uint8_t _pullupPin) {
useExternalPullup = true;
pullupPin = _pullupPin;
pinMode(pullupPin, OUTPUT);
deactivateExternalPullup();
}
void DallasTemperature::setOneWire(OneWire* _oneWire) {
_wire = _oneWire;
devices = 0;
ds18Count = 0;
parasite = false;
globalBitResolution = 9;
waitForConversion = true;
checkForConversion = true;
autoSaveScratchPad = true;
}
// initialise the bus
void DallasTemperature::begin(void) {
DeviceAddress deviceAddress;
_wire->reset_search();
devices = 0; // Reset the number of devices when we enumerate wire devices
ds18Count = 0; // Reset number of DS18xxx Family devices
while (_wire->search(deviceAddress)) {
if (validAddress(deviceAddress)) {
devices++;
if (validFamily(deviceAddress)) {
ds18Count++;
if (!parasite && readPowerSupply(deviceAddress))
parasite = true;
uint8_t b = getResolution(deviceAddress);
if (b > globalBitResolution) globalBitResolution = b;
}
}
}
}
// returns the number of devices found on the bus
uint8_t DallasTemperature::getDeviceCount(void) {
return devices;
}
uint8_t DallasTemperature::getDS18Count(void) {
return ds18Count;
}
// returns true if address is valid
bool DallasTemperature::validAddress(const uint8_t* deviceAddress) {
return (_wire->crc8((uint8_t*)deviceAddress, 7) == deviceAddress[DSROM_CRC]);
}
// finds an address at a given index on the bus
// returns true if the device was found
bool DallasTemperature::getAddress(uint8_t* deviceAddress, uint8_t index) {
uint8_t depth = 0;
_wire->reset_search();
while (depth <= index && _wire->search(deviceAddress)) {
if (depth == index && validAddress(deviceAddress))
return true;
depth++;
}
return false;
}
// attempt to determine if the device at the given address is connected to the bus
bool DallasTemperature::isConnected(const uint8_t* deviceAddress) {
ScratchPad scratchPad;
return isConnected(deviceAddress, scratchPad);
}
// attempt to determine if the device at the given address is connected to the bus
// also allows for updating the read scratchpad
bool DallasTemperature::isConnected(const uint8_t* deviceAddress,
uint8_t* scratchPad) {
bool b = readScratchPad(deviceAddress, scratchPad);
return b && !isAllZeros(scratchPad) && (_wire->crc8(scratchPad, 8) == scratchPad[SCRATCHPAD_CRC]);
}
bool DallasTemperature::readScratchPad(const uint8_t* deviceAddress,
uint8_t* scratchPad) {
// send the reset command and fail fast
int b = _wire->reset();
if (b == 0)
return false;
_wire->select(deviceAddress);
_wire->write(READSCRATCH);
// Read all registers in a simple loop
// byte 0: temperature LSB
// byte 1: temperature MSB
// byte 2: high alarm temp
// byte 3: low alarm temp
// byte 4: DS18S20: store for crc
// DS18B20 & DS1822: configuration register
// byte 5: internal use & crc
// byte 6: DS18S20: COUNT_REMAIN
// DS18B20 & DS1822: store for crc
// byte 7: DS18S20: COUNT_PER_C
// DS18B20 & DS1822: store for crc
// byte 8: SCRATCHPAD_CRC
for (uint8_t i = 0; i < 9; i++) {
scratchPad[i] = _wire->read();
}
b = _wire->reset();
return (b == 1);
}
void DallasTemperature::writeScratchPad(const uint8_t* deviceAddress,
const uint8_t* scratchPad) {
_wire->reset();
_wire->select(deviceAddress);
_wire->write(WRITESCRATCH);
_wire->write(scratchPad[HIGH_ALARM_TEMP]); // high alarm temp
_wire->write(scratchPad[LOW_ALARM_TEMP]); // low alarm temp
// DS1820 and DS18S20 have no configuration register
if (deviceAddress[DSROM_FAMILY] != DS18S20MODEL)
_wire->write(scratchPad[CONFIGURATION]);
if (autoSaveScratchPad)
saveScratchPad(deviceAddress);
else
_wire->reset();
}
// returns true if parasite mode is used (2 wire)
// returns false if normal mode is used (3 wire)
// if no address is given (or nullptr) it checks if any device on the bus
// uses parasite mode.
// See issue #145
bool DallasTemperature::readPowerSupply(const uint8_t* deviceAddress)
{
bool parasiteMode = false;
_wire->reset();
if (deviceAddress == nullptr)
_wire->skip();
else
_wire->select(deviceAddress);
_wire->write(READPOWERSUPPLY);
if (_wire->read_bit() == 0)
parasiteMode = true;
_wire->reset();
return parasiteMode;
}
// set resolution of all devices to 9, 10, 11, or 12 bits
// if new resolution is out of range, it is constrained.
void DallasTemperature::setResolution(uint8_t newResolution) {
globalBitResolution = constrain(newResolution, 9, 12);
DeviceAddress deviceAddress;
_wire->reset_search();
for (uint8_t i = 0; i < devices; i++) {
if(_wire->search(deviceAddress) && validAddress(deviceAddress)) {
setResolution(deviceAddress, globalBitResolution, true);
}
}
}
/* PROPOSAL */
// set resolution of a device to 9, 10, 11, or 12 bits
// if new resolution is out of range, 9 bits is used.
bool DallasTemperature::setResolution(const uint8_t* deviceAddress,
uint8_t newResolution, bool skipGlobalBitResolutionCalculation) {
bool success = false;
// DS1820 and DS18S20 have no resolution configuration register
if (deviceAddress[DSROM_FAMILY] == DS18S20MODEL) {
success = true;
} else {
// handle the sensors with configuration register
newResolution = constrain(newResolution, 9, 12);
uint8_t newValue = 0;
ScratchPad scratchPad;
// we can only update the sensor if it is connected
if (isConnected(deviceAddress, scratchPad)) {
// MAX31850 has no resolution configuration register
// this is also a hack as the MAX31850 Coversion time is 100ms max.
// use a low res (~10 by spec, but 9 might work) for faster blocking read times.
if (deviceAddress[DSROM_FAMILY] == DS1825MODEL && scratchPad[CONFIGURATION] & 0x80 ) {
success = true;
} else {
switch (newResolution) {
case 12:
newValue = TEMP_12_BIT;
break;
case 11:
newValue = TEMP_11_BIT;
break;
case 10:
newValue = TEMP_10_BIT;
break;
case 9:
default:
newValue = TEMP_9_BIT;
break;
}
// if it needs to be updated we write the new value
if (scratchPad[CONFIGURATION] != newValue) {
scratchPad[CONFIGURATION] = newValue;
writeScratchPad(deviceAddress, scratchPad);
}
// done
success = true;
}
}
}
// do we need to update the max resolution used?
if (skipGlobalBitResolutionCalculation == false) {
globalBitResolution = newResolution;
if (devices > 1) {
DeviceAddress deviceAddr;
_wire->reset_search();
for (uint8_t i = 0; i < devices; i++) {
if (globalBitResolution == 12) break;
if (_wire->search(deviceAddr) && validAddress(deviceAddr)) {
uint8_t b = getResolution(deviceAddr);
if (b > globalBitResolution) globalBitResolution = b;
}
}
}
}
return success;
}
// returns the global resolution
uint8_t DallasTemperature::getResolution() {
return globalBitResolution;
}
// returns the current resolution of the device, 9-12
// returns 0 if device not found
uint8_t DallasTemperature::getResolution(const uint8_t* deviceAddress) {
// DS1820 and DS18S20 have no resolution configuration register
if (deviceAddress[DSROM_FAMILY] == DS18S20MODEL)
return 12;
ScratchPad scratchPad;
if (isConnected(deviceAddress, scratchPad)) {
// MAX31850 has no resolution configuration register
if (deviceAddress[DSROM_FAMILY] == DS1825MODEL && scratchPad[CONFIGURATION] & 0x80)
return 12;
switch (scratchPad[CONFIGURATION]) {
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;
}
// sets the value of the waitForConversion flag
// TRUE : function requestTemperature() etc returns when conversion is ready
// FALSE: function requestTemperature() etc returns immediately (USE WITH CARE!!)
// (1) programmer has to check if the needed delay has passed
// (2) but the application can do meaningful things in that time
void DallasTemperature::setWaitForConversion(bool flag) {
waitForConversion = flag;
}
// gets the value of the waitForConversion flag
bool DallasTemperature::getWaitForConversion() {
return waitForConversion;
}
// sets the value of the checkForConversion flag
// TRUE : function requestTemperature() etc will 'listen' to an IC to determine whether a conversion is complete
// FALSE: function requestTemperature() etc will wait a set time (worst case scenario) for a conversion to complete
void DallasTemperature::setCheckForConversion(bool flag) {
checkForConversion = flag;
}
// gets the value of the waitForConversion flag
bool DallasTemperature::getCheckForConversion() {
return checkForConversion;
}
bool DallasTemperature::isConversionComplete() {
uint8_t b = _wire->read_bit();
return (b == 1);
}
// sends command for all devices on the bus to perform a temperature conversion
DallasTemperature::request_t DallasTemperature::requestTemperatures() {
DallasTemperature::request_t req = {};
req.result = true;
_wire->reset();
_wire->skip();
_wire->write(STARTCONVO, parasite);
// ASYNC mode?
req.timestamp = millis();
if (!waitForConversion)
return req;
blockTillConversionComplete(globalBitResolution, req.timestamp);
return req;
}
// sends command for one device to perform a temperature by address
// returns FALSE if device is disconnected
// returns TRUE otherwise
DallasTemperature::request_t DallasTemperature::requestTemperaturesByAddress(const uint8_t* deviceAddress) {
DallasTemperature::request_t req = {};
uint8_t deviceBitResolution = getResolution(deviceAddress);
if (deviceBitResolution == 0) {
req.result = false;
return req; //Device disconnected
}
_wire->reset();
_wire->select(deviceAddress);
_wire->write(STARTCONVO, parasite);
req.timestamp = millis();
// ASYNC mode?
req.result = true;
if (!waitForConversion)
return req;
blockTillConversionComplete(deviceBitResolution, req.timestamp);
return req;
}
// Continue to check if the IC has responded with a temperature
void DallasTemperature::blockTillConversionComplete(uint8_t bitResolution, unsigned long start) {
if (checkForConversion && !parasite) {
while (!isConversionComplete() && (millis() - start < MAX_CONVERSION_TIMEOUT))
yield();
} else {
unsigned long delms = millisToWaitForConversion(bitResolution);
activateExternalPullup();
delay(delms);
deactivateExternalPullup();
}
}
// Continue to check if the IC has responded with a temperature
void DallasTemperature::blockTillConversionComplete(uint8_t bitResolution) {
unsigned long start = millis();
blockTillConversionComplete(bitResolution, start);
}
// Continue to check if the IC has responded with a temperature
void DallasTemperature::blockTillConversionComplete(uint8_t bitResolution, DallasTemperature::request_t req) {
if (req.result)
blockTillConversionComplete(bitResolution, req.timestamp);
}
// returns number of milliseconds to wait till conversion is complete (based on IC datasheet)
uint16_t DallasTemperature::millisToWaitForConversion(uint8_t bitResolution) {
switch (bitResolution) {
case 9:
return 94;
case 10:
return 188;
case 11:
return 375;
default:
return 750;
}
}
// returns number of milliseconds to wait till conversion is complete (based on IC datasheet)
uint16_t DallasTemperature::millisToWaitForConversion() {
return millisToWaitForConversion(globalBitResolution);
}
// Sends command to one device to save values from scratchpad to EEPROM by index
// Returns true if no errors were encountered, false indicates failure
bool DallasTemperature::saveScratchPadByIndex(uint8_t deviceIndex) {
DeviceAddress deviceAddress;
if (!getAddress(deviceAddress, deviceIndex)) return false;
return saveScratchPad(deviceAddress);
}
// Sends command to one or more devices to save values from scratchpad to EEPROM
// If optional argument deviceAddress is omitted the command is send to all devices
// Returns true if no errors were encountered, false indicates failure
bool DallasTemperature::saveScratchPad(const uint8_t* deviceAddress) {
if (_wire->reset() == 0)
return false;
if (deviceAddress == nullptr)
_wire->skip();
else
_wire->select(deviceAddress);
_wire->write(COPYSCRATCH, parasite);
// Specification: NV Write Cycle Time is typically 2ms, max 10ms
// Waiting 20ms to allow for sensors that take longer in practice
if (!parasite) {
delay(20);
} else {
activateExternalPullup();
delay(20);
deactivateExternalPullup();
}
return _wire->reset() == 1;
}
// Sends command to one device to recall values from EEPROM to scratchpad by index
// Returns true if no errors were encountered, false indicates failure
bool DallasTemperature::recallScratchPadByIndex(uint8_t deviceIndex) {
DeviceAddress deviceAddress;
if (!getAddress(deviceAddress, deviceIndex)) return false;
return recallScratchPad(deviceAddress);
}
// Sends command to one or more devices to recall values from EEPROM to scratchpad
// If optional argument deviceAddress is omitted the command is send to all devices
// Returns true if no errors were encountered, false indicates failure
bool DallasTemperature::recallScratchPad(const uint8_t* deviceAddress) {
if (_wire->reset() == 0)
return false;
if (deviceAddress == nullptr)
_wire->skip();
else
_wire->select(deviceAddress);
_wire->write(RECALLSCRATCH, parasite);
// Specification: Strong pullup only needed when writing to EEPROM (and temp conversion)
unsigned long start = millis();
while (_wire->read_bit() == 0) {
// Datasheet doesn't specify typical/max duration, testing reveals typically within 1ms
if (millis() - start > 20) return false;
yield();
}
return _wire->reset() == 1;
}
// Sets the autoSaveScratchPad flag
void DallasTemperature::setAutoSaveScratchPad(bool flag) {
autoSaveScratchPad = flag;
}
// Gets the autoSaveScratchPad flag
bool DallasTemperature::getAutoSaveScratchPad() {
return autoSaveScratchPad;
}
void DallasTemperature::activateExternalPullup() {
if (useExternalPullup)
digitalWrite(pullupPin, LOW);
}
void DallasTemperature::deactivateExternalPullup() {
if (useExternalPullup)
digitalWrite(pullupPin, HIGH);
}
// sends command for one device to perform a temp conversion by index
DallasTemperature::request_t DallasTemperature::requestTemperaturesByIndex(uint8_t deviceIndex) {
DeviceAddress deviceAddress;
getAddress(deviceAddress, deviceIndex);
return requestTemperaturesByAddress(deviceAddress);
}
// Fetch temperature for device index
float DallasTemperature::getTempCByIndex(uint8_t deviceIndex) {
DeviceAddress deviceAddress;
if (!getAddress(deviceAddress, deviceIndex)) {
return DEVICE_DISCONNECTED_C;
}
return getTempC((uint8_t*) deviceAddress);
}
// Fetch temperature for device index
float DallasTemperature::getTempFByIndex(uint8_t deviceIndex) {
DeviceAddress deviceAddress;
if (!getAddress(deviceAddress, deviceIndex)) {
return DEVICE_DISCONNECTED_F;
}
return getTempF((uint8_t*) deviceAddress);
}
// reads scratchpad and returns fixed-point temperature, scaling factor 2^-7
int32_t DallasTemperature::calculateTemperature(const uint8_t* deviceAddress,
uint8_t* scratchPad) {
int32_t fpTemperature = 0;
// looking thru the spec sheets of all supported devices, bit 15 is always the signing bit
// Detected if signed
int32_t neg = 0x0;
if (scratchPad[TEMP_MSB] & 0x80)
neg = 0xFFF80000;
// detect MAX31850
// The temp range on a MAX31850 can far exceed other models, causing an overrun @ 256C
// Based on the spec sheets for the MAX31850, bit 7 is always 1
// Whereas the DS1825 bit 7 is always 0
// DS1825 - https://datasheets.maximintegrated.com/en/ds/DS1825.pdf
// MAX31850 - https://datasheets.maximintegrated.com/en/ds/MAX31850-MAX31851.pdf
if (deviceAddress[DSROM_FAMILY] == DS1825MODEL && scratchPad[CONFIGURATION] & 0x80 ) {
//Serial.print(" Detected MAX31850");
if (scratchPad[TEMP_LSB] & 1) { // Fault Detected
if (scratchPad[HIGH_ALARM_TEMP] & 1) {
//Serial.println("open detected");
return DEVICE_FAULT_OPEN_RAW;
}
else if (scratchPad[HIGH_ALARM_TEMP] >> 1 & 1) {
//Serial.println("short to ground detected");
return DEVICE_FAULT_SHORTGND_RAW;
}
else if (scratchPad[HIGH_ALARM_TEMP] >> 2 & 1) {
//Serial.println("short to Vdd detected");
return DEVICE_FAULT_SHORTVDD_RAW;
}
else {
// We don't know why there's a fault, exit with disconnect value
return DEVICE_DISCONNECTED_RAW;
}
}
// We must mask out bit 1 (reserved) and 0 (fault) on TEMP_LSB
fpTemperature = (((int32_t) scratchPad[TEMP_MSB]) << 11)
| (((int32_t) scratchPad[TEMP_LSB] & 0xFC) << 3)
| neg;
} else {
fpTemperature = (((int16_t) scratchPad[TEMP_MSB]) << 11)
| (((int16_t) scratchPad[TEMP_LSB]) << 3)
| neg;
}
/*
DS1820 and DS18S20 have a 9-bit temperature register.
Resolutions greater than 9-bit can be calculated using the data from
the temperature, and COUNT REMAIN and COUNT PER °C registers in the
scratchpad. The resolution of the calculation depends on the model.
While the COUNT PER °C register is hard-wired to 16 (10h) in a
DS18S20, it changes with temperature in DS1820.
After reading the scratchpad, the TEMP_READ value is obtained by
truncating the 0.5°C bit (bit 0) from the temperature data. The
extended resolution temperature can then be calculated using the
following equation:
COUNT_PER_C - COUNT_REMAIN
TEMPERATURE = TEMP_READ - 0.25 + --------------------------
COUNT_PER_C
Hagai Shatz simplified this to integer arithmetic for a 12 bits
value for a DS18S20, and James Cameron added legacy DS1820 support.
See - http://myarduinotoy.blogspot.co.uk/2013/02/12bit-result-from-ds18s20.html
*/
if ((deviceAddress[DSROM_FAMILY] == DS18S20MODEL) && (scratchPad[COUNT_PER_C] != 0)) {
fpTemperature = (((fpTemperature & 0xfff0) << 3) - 32
+ (((scratchPad[COUNT_PER_C] - scratchPad[COUNT_REMAIN]) << 7)
/ scratchPad[COUNT_PER_C])) | neg;
}
return fpTemperature;
}
// returns temperature in 1/128 degrees C or DEVICE_DISCONNECTED_RAW if the
// device's scratch pad cannot be read successfully.
// the numeric value of DEVICE_DISCONNECTED_RAW is defined in
// DallasTemperature.h. It is a large negative number outside the
// operating range of the device
int32_t DallasTemperature::getTemp(const uint8_t* deviceAddress) {
ScratchPad scratchPad;
if (isConnected(deviceAddress, scratchPad))
return calculateTemperature(deviceAddress, scratchPad);
return DEVICE_DISCONNECTED_RAW;
}
// returns temperature in degrees C or DEVICE_DISCONNECTED_C if the
// device's scratch pad cannot be read successfully.
// the numeric value of DEVICE_DISCONNECTED_C is defined in
// DallasTemperature.h. It is a large negative number outside the
// operating range of the device
float DallasTemperature::getTempC(const uint8_t* deviceAddress) {
return rawToCelsius(getTemp(deviceAddress));
}
// returns temperature in degrees F or DEVICE_DISCONNECTED_F if the
// device's scratch pad cannot be read successfully.
// the numeric value of DEVICE_DISCONNECTED_F is defined in
// DallasTemperature.h. It is a large negative number outside the
// operating range of the device
float DallasTemperature::getTempF(const uint8_t* deviceAddress) {
return rawToFahrenheit(getTemp(deviceAddress));
}
// returns true if the bus requires parasite power
bool DallasTemperature::isParasitePowerMode(void) {
return parasite;
}
// IF alarm is not used one can store a 16 bit int of userdata in the alarm
// registers. E.g. an ID of the sensor.
// See github issue #29
// note if device is not connected it will fail writing the data.
void DallasTemperature::setUserData(const uint8_t* deviceAddress,
int16_t data) {
// return when stored value == new value
if (getUserData(deviceAddress) == data)
return;
ScratchPad scratchPad;
if (isConnected(deviceAddress, scratchPad)) {
scratchPad[HIGH_ALARM_TEMP] = data >> 8;
scratchPad[LOW_ALARM_TEMP] = data & 255;
writeScratchPad(deviceAddress, scratchPad);
}
}
int16_t DallasTemperature::getUserData(const uint8_t* deviceAddress) {
int16_t data = 0;
ScratchPad scratchPad;
if (isConnected(deviceAddress, scratchPad)) {
data = scratchPad[HIGH_ALARM_TEMP] << 8;
data += scratchPad[LOW_ALARM_TEMP];
}
return data;
}
// note If address cannot be found no error will be reported.
int16_t DallasTemperature::getUserDataByIndex(uint8_t deviceIndex) {
DeviceAddress deviceAddress;
getAddress(deviceAddress, deviceIndex);
return getUserData((uint8_t*) deviceAddress);
}
void DallasTemperature::setUserDataByIndex(uint8_t deviceIndex, int16_t data) {
DeviceAddress deviceAddress;
getAddress(deviceAddress, deviceIndex);
setUserData((uint8_t*) deviceAddress, data);
}
// Convert float Celsius to Fahrenheit
float DallasTemperature::toFahrenheit(float celsius) {
return (celsius * 1.8f) + 32.0f;
}
// Convert float Fahrenheit to Celsius
float DallasTemperature::toCelsius(float fahrenheit) {
return (fahrenheit - 32.0f) * 0.555555556f;
}
// convert from raw to Celsius
float DallasTemperature::rawToCelsius(int32_t raw) {
if (raw <= DEVICE_DISCONNECTED_RAW)
return DEVICE_DISCONNECTED_C;
// C = RAW/128
return (float) raw * 0.0078125f;
}
// Convert from Celsius to raw returns temperature in raw integer format.
// The rounding error in the conversion is smaller than 0.01°C
// where the resolution of the sensor is at best 0.0625°C (in 12 bit mode).
// Rounding error can be verified by running:
// for (float t=-55.; t<125.; t+=0.01)
// {
// Serial.println( DallasTemperature::rawToCelsius(DallasTemperature::celsiusToRaw(t))-t, 4 );
// }
int16_t DallasTemperature::celsiusToRaw(float celsius) {
return static_cast<uint16_t>( celsius * 128.f );
}
// convert from raw to Fahrenheit
float DallasTemperature::rawToFahrenheit(int32_t raw) {
if (raw <= DEVICE_DISCONNECTED_RAW)
return DEVICE_DISCONNECTED_F;
// C = RAW/128
// F = (C*1.8)+32 = (RAW/128*1.8)+32 = (RAW*0.0140625)+32
return ((float) raw * 0.0140625f) + 32.0f;
}
// Returns true if all bytes of scratchPad are '\0'
bool DallasTemperature::isAllZeros(const uint8_t * const scratchPad, const size_t length) {
for (size_t i = 0; i < length; i++) {
if (scratchPad[i] != 0) {
return false;
}
}
return true;
}
#if REQUIRESALARMS
/*
ALARMS:
TH and TL Register Format
BIT 7 BIT 6 BIT 5 BIT 4 BIT 3 BIT 2 BIT 1 BIT 0
S 2^6 2^5 2^4 2^3 2^2 2^1 2^0
Only bits 11 through 4 of the temperature register are used
in the TH and TL comparison since TH and TL are 8-bit
registers. If the measured temperature is lower than or equal
to TL or higher than or equal to TH, an alarm condition exists
and an alarm flag is set inside the DS18B20. This flag is
updated after every temperature measurement; therefore, if the
alarm condition goes away, the flag will be turned off after
the next temperature conversion.
*/
// sets the high alarm temperature for a device in degrees Celsius
// accepts a float, but the alarm resolution will ignore anything
// after a decimal point. valid range is -55C - 125C
void DallasTemperature::setHighAlarmTemp(const uint8_t* deviceAddress,
int8_t celsius) {
// return when stored value == new value
if (getHighAlarmTemp(deviceAddress) == celsius)
return;
// make sure the alarm temperature is within the device's range
if (celsius > 125)
celsius = 125;
else if (celsius < -55)
celsius = -55;
ScratchPad scratchPad;
if (isConnected(deviceAddress, scratchPad)) {
scratchPad[HIGH_ALARM_TEMP] = (uint8_t) celsius;
writeScratchPad(deviceAddress, scratchPad);
}
}
// sets the low alarm temperature for a device in degrees Celsius
// accepts a float, but the alarm resolution will ignore anything
// after a decimal point. valid range is -55C - 125C
void DallasTemperature::setLowAlarmTemp(const uint8_t* deviceAddress,
int8_t celsius) {
// return when stored value == new value
if (getLowAlarmTemp(deviceAddress) == celsius)
return;
// make sure the alarm temperature is within the device's range
if (celsius > 125)
celsius = 125;
else if (celsius < -55)
celsius = -55;
ScratchPad scratchPad;
if (isConnected(deviceAddress, scratchPad)) {
scratchPad[LOW_ALARM_TEMP] = (uint8_t) celsius;
writeScratchPad(deviceAddress, scratchPad);
}
}
// returns a int8_t with the current high alarm temperature or
// DEVICE_DISCONNECTED for an address
int8_t DallasTemperature::getHighAlarmTemp(const uint8_t* deviceAddress) {
ScratchPad scratchPad;
if (isConnected(deviceAddress, scratchPad))
return (int8_t) scratchPad[HIGH_ALARM_TEMP];
return DEVICE_DISCONNECTED_C;
}
// returns a int8_t with the current low alarm temperature or
// DEVICE_DISCONNECTED for an address
int8_t DallasTemperature::getLowAlarmTemp(const uint8_t* deviceAddress) {
ScratchPad scratchPad;
if (isConnected(deviceAddress, scratchPad))
return (int8_t) scratchPad[LOW_ALARM_TEMP];
return DEVICE_DISCONNECTED_C;
}
// resets internal variables used for the alarm search
void DallasTemperature::resetAlarmSearch() {
alarmSearchJunction = -1;
alarmSearchExhausted = 0;
for (uint8_t i = 0; i < 7; i++) {
alarmSearchAddress[i] = 0;
}
}
// This is a modified version of the OneWire::search method.
//
// Also added the OneWire search fix documented here:
// http://www.arduino.cc/cgi-bin/yabb2/YaBB.pl?num=1238032295
//
// Perform an alarm search. If this function returns a '1' then it has
// enumerated the next device and you may retrieve the ROM from the
// OneWire::address variable. If there are no devices, no further
// devices, or something horrible happens in the middle of the
// enumeration then a 0 is returned. If a new device is found then
// its address is copied to newAddr. Use
// DallasTemperature::resetAlarmSearch() to start over.
bool DallasTemperature::alarmSearch(uint8_t* newAddr) {
uint8_t i;
int8_t lastJunction = -1;
uint8_t done = 1;
if (alarmSearchExhausted)
return false;
if (!_wire->reset())
return false;
// send the alarm search command
_wire->write(0xEC, 0);
for (i = 0; i < 64; i++) {
uint8_t a = _wire->read_bit();
uint8_t nota = _wire->read_bit();
uint8_t ibyte = i / 8;
uint8_t ibit = 1 << (i & 7);
// I don't think this should happen, this means nothing responded, but maybe if
// something vanishes during the search it will come up.
if (a && nota)
return false;
if (!a && !nota) {
if (i == alarmSearchJunction) {
// this is our time to decide differently, we went zero last time, go one.
a = 1;
alarmSearchJunction = lastJunction;