Anyone have any experience with a DS1307 running a little fast? I set the clock then a week later it is about 20 seconds fast, the next week it will be about 40 seconds fast. Any idea how to slow it down a bit?
#include <TimeLib.h>
#include <Wire.h>
#define DS1307_I2C_ADDRESS 0x68
byte decToBcd(byte val)
{
return((val/10*16) + (val%10));
}
byte bcdToDec(byte val)
{
return((val/16*10) + (val%16));
}
void setup() //runs once
{
Wire.begin();
Serial.begin(9600);
//setDS1307time(00,37,20,4,25,01,17); //set DS1307 Time&Date: seconds, minutes, hours, day, date, month, year; comment out after setting and re-upload
void setDS1307time(byte second, byte minute, byte hour, byte dayOfWeek, byte dayOfMonth, byte month, byte year)
{
Wire.beginTransmission(DS1307_I2C_ADDRESS);
Wire.write(0);
Wire.write(decToBcd(second));
Wire.write(decToBcd(minute));
Wire.write(decToBcd(hour));
Wire.write(decToBcd(dayOfWeek));
Wire.write(decToBcd(dayOfMonth));
Wire.write(decToBcd(month));
Wire.write(decToBcd(year));
Wire.endTransmission();
}
void readDS1307time(byte *second, byte *minute, byte *hour, byte *dayOfWeek, byte *dayOfMonth, byte *month, byte *year)
{
Wire.beginTransmission(DS1307_I2C_ADDRESS);
Wire.write(0);
Wire.endTransmission();
Wire.requestFrom(DS1307_I2C_ADDRESS, 7); //requesting 7 bytes from DS1307 beginning at register 00h
*second = bcdToDec(Wire. read() & 0x7f);
*minute = bcdToDec(Wire. read());
*hour = bcdToDec(Wire. read() & 0x3f);
*dayOfWeek = bcdToDec(Wire. read());
*dayOfMonth = bcdToDec(Wire. read());
*month = bcdToDec(Wire. read());
*year = bcdToDec(Wire. read());
}
void displayTime()
{
byte second, minute, hour, dayOfWeek, dayOfMonth, month, year;
readDS1307time(&second, &minute, &hour, &dayOfWeek, &dayOfMonth, &month, &year);
lcd.setCursor(0, 0);
lcd.print(month, DEC);
lcd.print("/");
lcd.print(dayOfMonth, DEC);
lcd.print("/");
lcd.print(year, DEC);
lcd.print(" ");
lcd.print(hour, DEC);
lcd.print(":");
if(minute < 10)
{
lcd.print("0");
}
lcd.print(minute, DEC);
lcd.print(":");
if(second < 10)
{
lcd.print("0");
}
lcd.print(second, DEC);
lcd.print(" ");
switch(dayOfWeek)
{
case 1:
lcd.print("Su ");
break;
case 2:
lcd.print("Mo ");
break;
case 3:
lcd.print("Tu ");
break;
case 4:
lcd.print("We ");
break;
case 5:
lcd.print("Th ");
break;
case 6:
lcd.print("Fr ");
break;
case 7:
lcd.print("Sa ");
break;
}
void loop()
{
displayTime();
}
Thanks for any advice.