Skip to content

Commit 9968238

Browse files
committed
Use the temperature conversion method from adafruit#303 instead.
It's basically the same but maybe a bit safer due to explicit conversion to 16-bit first.
1 parent 51b233e commit 9968238

File tree

1 file changed

+3
-4
lines changed

1 file changed

+3
-4
lines changed

src/RTC_DS3231.cpp

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -114,10 +114,9 @@ void RTC_DS3231::writeSqwPinMode(Ds3231SqwPinMode mode) {
114114
float RTC_DS3231::getTemperature() {
115115
uint8_t buffer[2] = {DS3231_TEMPERATUREREG, 0};
116116
i2c_dev->write_then_read(buffer, 1, buffer, 2);
117-
// First convert the full 10 bits into a signed integer, then multiply by 0.25 to get
118-
// a signed float of the number of degrees.
119-
int16_t signedVal = ((int8_t)buffer[0] << 2) + (buffer[1] >> 6);
120-
return signedVal * 0.25;
117+
// Fix for negative temperatures https://github.com/adafruit/RTClib/pull/303
118+
int16_t temp = uint16_t(buffer[0]) << 8 | buffer[1];
119+
return temp * (1 / 256.0);
121120
}
122121

123122
/**************************************************************************/

0 commit comments

Comments
 (0)