In the code below, the function ReadDS3231(); displays a different time to that resulting from Clock.getTime(year, month, date, DoW, hour, minute, second);
Why would this be?
/*
DS3231_test.pde
Eric Ayars
4/11
Test/demo of read routines for a DS3231 RTC.
Turn on the serial monitor after loading this to check if things are
working as they should.
*/
#include <DS3231.h>
#include <Wire.h>
DS3231 Clock;
bool h12;
bool PM;
byte ADay, AHour, AMinute, ASecond, ABits;
bool ADy, A12h, Apm;
byte year, month, date, DoW, hour, minute, second;
void setup() {
// Start the I2C interface
Wire.begin();
// Start the serial interface
Serial.begin(9600);
}
void ReadDS3231()
{
int second,minute,hour,date,month,year,temperature;
second=Clock.getSecond();
minute=Clock.getMinute();
hour=Clock.getHour(h12, PM);
Serial.print(hour,DEC);
Serial.print(':');
Serial.print(minute,DEC);
Serial.print(':');
Serial.print(second,DEC);
Serial.print('\n');
}
void loop() {
ReadDS3231();
Serial.print('\n');
Clock.getTime(year, month, date, DoW, hour, minute, second);
Serial.print(hour, DEC);
Serial.print(":");
Serial.print(minute, DEC);
Serial.print(":");
Serial.println(second, DEC);
delay(1000);
}
Your sketch works as intended with my DS3231 with the displayed data from the Clock.getTime consistently one second more than the result from function ReadDS3231(). I see no time difference of 6 hours.
Looking at the DS3231 library code, and the structure of the Timekeeping Registers in the DS3231 it is hard to see where the 6 hour difference can come from if one of the readings is correct.
You may want to place 4.7K pull up resisters on the SDA and SCL lines. If you are getting glitchy data over the I2C bus this can help.
The sketch also works for me and I see no difference between the two times at all.
The apparent one second difference is only due to the way that the two times are printed:
19:30:11 // from ReadDS3231
19:30:11 // from Clock.getTime
19:30:12 // from ReadDS3231
19:30:12 // from Clock.getTime
19:30:13 // etc.
19:30:13
19:30:14
19:30:14
19:30:15
Clock.getTime is printed immediately after ReadDS3231 but they are separated by a blank line which makes it look like the times are one second apart but in fact this pair:
19:30:11 // from ReadDS3231
19:30:11 // from Clock.getTime
are printed in the same one second interval, and similarly for the other pairs.
The code should be fixed so that the blank line is printed between a ReadDS3231/Clock.getTime pair.