I have a DS1307 hooked up to an Arduino and I'm getting some odd readings, I was getting seconds reading way above 59.
My code (after modifying)
The modification is the and 0x7f on the seconds variable.
This is the printout I get in the serial window,
The fact the high bit of Seconds is set means the Clock Halt bit is set and yet, the clock is running.
Anyone got any ideas to explain this?
Mike.
Edit, for completeness, here's BCD2dec
My code (after modifying)
Code:
void getRTC(){
uint8_t myhour,mymins,mysecs,myday,mymonth,myyear;
Wire.begin();
Wire.beginTransmission(0x68); //read the RTC.
Wire.write(0);
Wire.endTransmission(false); //don't release the bus
Wire.requestFrom(0x68,8);
uint8_t temp = Wire.read();
Serial.println(temp,HEX);
temp&=0x7f;
mysecs=BCD2dec(temp);
mymins=BCD2dec(Wire.read());
myhour=BCD2dec(Wire.read());
Wire.read(); //location[3] not used = weekday
myday=BCD2dec(Wire.read());
mymonth=BCD2dec(Wire.read());
myyear=BCD2dec(Wire.read())+2000;
setTime(myhour,mymins,mysecs,myday,mymonth,myyear);
Serial.print("Hour =");Serial.println(myhour);
Serial.print("min =");Serial.println(mymins);
Serial.print("Sec =");Serial.println(mysecs);
}
This is the printout I get in the serial window,
Code:
D8 //the HEX value returned from seconds register
Hour =3
min =57
Sec =58 //the seconds after the AND with 0x7f
D9
Hour =3
min =57
Sec =59
80
Hour =3
min =58
Sec =0
80
Hour =3
min =58
Sec =0
81
Hour =3
min =58
Sec =1
Anyone got any ideas to explain this?
Mike.
Edit, for completeness, here's BCD2dec
Code:
uint8_t BCD2dec(uint8_t dat){
return((dat>>4)*10+(dat&0x0f));
}