adafruit / RTClib

A fork of Jeelab's fantastic RTC Arduino library
MIT License
795 stars 705 forks source link

RTC_DS3231: correctly handle negative temperatures #303

Open edgar-bonet opened 4 months ago

edgar-bonet commented 4 months ago

As reported in issue #287, the method RTC_DS3231::getTemperature() fails on negative temperatures. This is because the conversion from raw bytes to a float-typed temperature assumes the value is always positive, wheres the datasheet specifies it is stored as a signed, two's complement integer.

This pull request fixes the method by changing the conversion code to this:

  int16_t temp = uint16_t(buffer[0]) << 8 | buffer[1];
  return temp * (1 / 256.0);

Some points worth noting:

Tested down to −27.00°C by @i440bx.

Fixes #287.

BillyGriffiths commented 2 months ago

We ran into the same problem with negative temps. Do you have any idea when this will be merged?

edgar-bonet commented 2 months ago

@BillyGriffiths: No idea. This repo seems practically unmaintained. The last change to the source code of the library was the merge of pull request #257, on 2022-08-04. Since then, it has only had minute changes that do not touch the source code of the library itself.