Century bit in hardware is not handled
max688182 opened this issue · 0 comments
max688182 commented
The DS3231 uses a century bit in bit 7 of the month register(x05).
If this bit is set, as could happen if the clock was set by another library, the bcd2dec() in getDateTime will get the month wrong. example, May which is 05 will be read as 85.
As this lib always assumes years > 2k it never sets the century bit, this is only an issue if the clock chip has been set elsewhere and the century bit was used.
A possible solution is to change getDateTime() to something like the following. (other changes to handle the century bit will also be needed)
RTCDateTime DS3231::getDateTime(void)
{
int values[7];
int century = 0;
int n;
Wire.beginTransmission(DS3231_ADDRESS);
#if ARDUINO >= 100
Wire.write(DS3231_REG_TIME);
#else
Wire.send(DS3231_REG_TIME);
#endif
Wire.endTransmission();
Wire.requestFrom(DS3231_ADDRESS, 7);
while(!Wire.available()) {};
for (int i = 6; i >= 0; i--)
{
#if ARDUINO >= 100
n = Wire.read();
#else
n = Wire.receive();
#endif
if (i == 1) {
values[1] = bcd2dec(n & 0x1F);
century = (n & 0x80) >> 7;
} else
values[i] = bcd2dec(n);
}
Wire.endTransmission();
if (century == 1) {
t.year = values[0] + 2000;
} else {
t.year = values[0] + 1900;
}
t.month = values[1];
t.day = values[2];
t.dayOfWeek = values[3];
t.hour = values[4];
t.minute = values[5];
t.second = values[6];
t.unixtime = unixtime();
return t;
}