You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Temperature Registers (11h–12h)
Temperature is represented as a 10-bit code with a resolution of 0.25°C and is accessible at location 11h and 12h. The
temperature is encoded in two’s complement format. The upper 8 bits, the integer portion, are at location 11h and the
lower 2 bits, the fractional portion, are in the upper nibble at location 12h.
Although variable buffer[0] holds a signed value in twos complement format, it has been declared as uint8_t. Casting the value in buffer[0] to float does not handle negative values correctly, and we find in practice that for example a temperature of -1 deg C which is read from the DS3231's registers as {255,0}, is returned by this function as 255 deg C, and not as -1 deg C.
Changing the last line of the function to the following corrects this.
From: https://datasheets.maximintegrated.com/en/ds/DS3231.pdf -
Although variable buffer[0] holds a signed value in twos complement format, it has been declared as uint8_t. Casting the value in buffer[0] to float does not handle negative values correctly, and we find in practice that for example a temperature of -1 deg C which is read from the DS3231's registers as {255,0}, is returned by this function as 255 deg C, and not as -1 deg C.
Changing the last line of the function to the following corrects this.
return (float) (*(int8_t *)buffer) + (buffer[1] >> 6) * 0.25f;
regards,
The text was updated successfully, but these errors were encountered: