Skip to content

Commit 18258a8

Browse files
committed
Use a temperature conversion method adapted from adafruit#303
My previous method worked, but this one is better so I used it in the DS3231 and 3232, and for consistency use a similar method here.
1 parent 4f9187a commit 18258a8

File tree

1 file changed

+6
-5
lines changed

1 file changed

+6
-5
lines changed

src/RTC_RV3032.cpp

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -134,11 +134,12 @@ float RTC_RV3032::getTemperature() {
134134
uint8_t buffer[2] = {RV3032_TEMPERATURE, 0};
135135
i2c_dev->write_then_read(buffer, 1, buffer, 2);
136136
// Whereas the DS3232 is MSB-first, the RV3032 is LSB-first.
137-
// Also the fractional part is 4 bits instead of 2. Otherwise it's basically the same.
138-
// First we convert the full 12 bits into a signed integer, then multiply by 0.0625 to get
139-
// a signed float of the number of degrees.
140-
int16_t signedVal = ((int8_t)buffer[1] << 4) + (buffer[0] >> 4);
141-
return signedVal * 0.0625;
137+
// The fractional part is 4 bits instead of 2, but that doesn't matter for this method.
138+
// What does matter is the remaining 4 bits are used for flags, so unlike the DS* they
139+
// are not guaranteed to be zero, and must be explicitly ignored.
140+
// Otherwise it's broadly the same, so we use a similar method to https://github.com/adafruit/RTClib/pull/303
141+
int16_t temp = uint16_t(buffer[1]) << 8 | (buffer[0] & 0xf0);
142+
return temp * (1 / 256.0);
142143
}
143144

144145
/**************************************************************************/

0 commit comments

Comments
 (0)