Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Century bit in hardware is not handled #3

Open
max688182 opened this issue May 14, 2015 · 0 comments
Open

Century bit in hardware is not handled #3

max688182 opened this issue May 14, 2015 · 0 comments

Comments

@max688182
Copy link

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;

}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant