Skip to content

Commit 757b1ce

Browse files
committed
Convert API to 32-bit raw temperature values; read from the MAX31850
1 parent ab583b1 commit 757b1ce

2 files changed

Lines changed: 52 additions & 10 deletions

File tree

DallasTemperature.cpp

Lines changed: 48 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -604,11 +604,53 @@ float DallasTemperature::getTempFByIndex(uint8_t deviceIndex) {
604604
}
605605

606606
// reads scratchpad and returns fixed-point temperature, scaling factor 2^-7
607-
int16_t DallasTemperature::calculateTemperature(const uint8_t* deviceAddress,
607+
int32_t DallasTemperature::calculateTemperature(const uint8_t* deviceAddress,
608608
uint8_t* scratchPad) {
609609

610-
int16_t fpTemperature = (((int16_t) scratchPad[TEMP_MSB]) << 11)
611-
| (((int16_t) scratchPad[TEMP_LSB]) << 3);
610+
int32_t fpTemperature = 0;
611+
612+
// looking thru the spec sheets of all supported devices, bit 15 is always the signing bit
613+
// Detected if signed
614+
int32_t neg = 0x0;
615+
if (scratchPad[TEMP_MSB] & 0x80)
616+
neg = 0xFFF80000;
617+
618+
// detect MAX31850
619+
// The temp range on a MAX31850 can far exceed other models, causing an overrun @ 256C
620+
// Based on the spec sheets for the MAX31850, bit 7 is always 1
621+
// Whereas the DS1825 bit 7 is always 0
622+
// DS1825 - https://datasheets.maximintegrated.com/en/ds/DS1825.pdf
623+
// MAX31850 - https://datasheets.maximintegrated.com/en/ds/MAX31850-MAX31851.pdf
624+
625+
if (deviceAddress[DSROM_FAMILY] == DS1825MODEL && scratchPad[CONFIGURATION] & 0x80 ) {
626+
//Serial.print(" Detected MAX31850");
627+
if (scratchPad[TEMP_LSB] & 1) { // Fault Detected
628+
if (scratchPad[HIGH_ALARM_TEMP] & 1) {
629+
//Serial.println("open detected");
630+
return DEVICE_FAULT_OPEN_RAW;
631+
}
632+
else if (scratchPad[HIGH_ALARM_TEMP] >> 1 & 1) {
633+
//Serial.println("short to ground detected");
634+
return DEVICE_FAULT_SHORTGND_RAW;
635+
}
636+
else if (scratchPad[HIGH_ALARM_TEMP] >> 2 & 1) {
637+
//Serial.println("short to Vdd detected");
638+
return DEVICE_FAULT_SHORTVDD_RAW;
639+
}
640+
else {
641+
// We don't know why there's a fault, exit with disconnect value
642+
return DEVICE_DISCONNECTED_RAW;
643+
}
644+
}
645+
// We must mask out bit 1 (reserved) and 0 (fault) on TEMP_LSB
646+
fpTemperature = (((int32_t) scratchPad[TEMP_MSB]) << 11)
647+
| (((int32_t) scratchPad[TEMP_LSB] & 0xFC) << 3)
648+
| neg;
649+
} else {
650+
fpTemperature = (((int16_t) scratchPad[TEMP_MSB]) << 11)
651+
| (((int16_t) scratchPad[TEMP_LSB]) << 3)
652+
| neg;
653+
}
612654

613655
/*
614656
DS1820 and DS18S20 have a 9-bit temperature register.
@@ -649,7 +691,7 @@ int16_t DallasTemperature::calculateTemperature(const uint8_t* deviceAddress,
649691
// the numeric value of DEVICE_DISCONNECTED_RAW is defined in
650692
// DallasTemperature.h. It is a large negative number outside the
651693
// operating range of the device
652-
int16_t DallasTemperature::getTemp(const uint8_t* deviceAddress) {
694+
int32_t DallasTemperature::getTemp(const uint8_t* deviceAddress) {
653695

654696
ScratchPad scratchPad;
655697
if (isConnected(deviceAddress, scratchPad))
@@ -734,7 +776,7 @@ float DallasTemperature::toCelsius(float fahrenheit) {
734776
}
735777

736778
// convert from raw to Celsius
737-
float DallasTemperature::rawToCelsius(int16_t raw) {
779+
float DallasTemperature::rawToCelsius(int32_t raw) {
738780

739781
if (raw <= DEVICE_DISCONNECTED_RAW)
740782
return DEVICE_DISCONNECTED_C;
@@ -757,7 +799,7 @@ int16_t DallasTemperature::celsiusToRaw(float celsius) {
757799
}
758800

759801
// convert from raw to Fahrenheit
760-
float DallasTemperature::rawToFahrenheit(int16_t raw) {
802+
float DallasTemperature::rawToFahrenheit(int32_t raw) {
761803

762804
if (raw <= DEVICE_DISCONNECTED_RAW)
763805
return DEVICE_DISCONNECTED_F;

DallasTemperature.h

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -149,7 +149,7 @@ class DallasTemperature {
149149
bool requestTemperaturesByIndex(uint8_t);
150150

151151
// returns temperature raw value (12 bit integer of 1/128 degrees C)
152-
int16_t getTemp(const uint8_t*);
152+
int32_t getTemp(const uint8_t*);
153153

154154
// returns temperature in degrees C
155155
float getTempC(const uint8_t*);
@@ -255,13 +255,13 @@ class DallasTemperature {
255255
static float toCelsius(float);
256256

257257
// convert from raw to Celsius
258-
static float rawToCelsius(int16_t);
258+
static float rawToCelsius(int32_t);
259259

260260
// convert from Celsius to raw
261261
static int16_t celsiusToRaw(float);
262262

263263
// convert from raw to Fahrenheit
264-
static float rawToFahrenheit(int16_t);
264+
static float rawToFahrenheit(int32_t);
265265

266266
#if REQUIRESNEW
267267

@@ -308,7 +308,7 @@ class DallasTemperature {
308308
OneWire* _wire;
309309

310310
// reads scratchpad and returns the raw temperature
311-
int16_t calculateTemperature(const uint8_t*, uint8_t*);
311+
int32_t calculateTemperature(const uint8_t*, uint8_t*);
312312

313313

314314
// Returns true if all bytes of scratchPad are '\0'

0 commit comments

Comments
 (0)