@@ -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;
0 commit comments