// read the RTC memory
void ReadRTC(){
// update the time vars from the RTC
byte hold;
A1Reg=0;
A2Reg=0;
Wire.beginTransmission(I2C_address);
Wire.write(0x00);
Wire.endTransmission();
// read all 19 bytes that have the data
Wire.requestFrom(I2C_address, 0x13);
second = BCD2Dec(Wire.read());
minute = BCD2Dec(Wire.read());
// if bit 6 of hour is set we have a 12 hour clock
hold=Wire.read();
if (hold & 0b01000000) {
// 12 hour clock
if(bitSet(hold,5)){
// it's PM
AM_PM=1;
}
else{
AM_PM=0;
}
hour = BCD2Dec(hold & 0b00011111);
}
else{
// 24 h clock
AM_PM=2;
hour = BCD2Dec(hold & 0b00111111);
}
// bit 7-3=0
DoW = BCD2Dec(Wire.read());
// bit 7-6=0
DoM = BCD2Dec(Wire.read());
// bit 7=century
month = BCD2Dec(Wire.read() & 0b01111111);
year = BCD2Dec(Wire.read());
// Alarm #1
hold=Wire.read(); // A1 seconds
if(bitRead(hold,7)){
bitSet(A1Reg,0);
}
hold=hold&0b01111111;
A1second=BCD2Dec(hold);
hold=Wire.read(); // A1 minutes
if(bitRead(hold,7)){
bitSet(A1Reg,1);
}
hold=hold&0b01111111;
A1minute=BCD2Dec(hold);
hold=Wire.read(); // A1 hours
if(bitRead(hold,7)){
bitSet(A1Reg,2);
}
if(bitRead(hold,6)){
// 12 hour clock - convert the time
// get AM/PM
if(bitRead(hold,5)){
// it's PM
A1AM_PM=1;
}
else{
A1AM_PM=0;
}
A1hour = BCD2Dec(hold & 0b00011111);
}
else{
// 24 h clock
A1AM_PM=2;
A1hour = BCD2Dec(hold & 0b00111111);
}
// DoM or Day depending on bit 6
hold=Wire.read(); // DY/DT
if(bitRead(hold,7)){
bitSet(A1Reg,3);
}
if(bitRead(hold,6)){
// it's day (DoW)
A1DY_DT=1;
A1DoM=0;
A1DoW = BCD2Dec(hold & 0b00001111);
}
else{
// it's date (DoM)
A1DY_DT=0;
A1DoW=0;
A1DoM = BCD2Dec(hold & 0b00111111);
}
//
// Alarm #2
A2minute=BCD2Dec(Wire.read()); // A2 minutes
hold=Wire.read(); // A2 hours
if(hold&0b01000000){
// 12 hour clock
A2AM_PM==hold & 0b00100000;
A2hour = BCD2Dec(hold & 0b00011111);
}
else{
// 24 h clock
A2AM_PM=2;
A2hour = BCD2Dec(hold & 0b00111111);
}
// DoM or Day depending on bit 6
hold=Wire.read();
if(hold&0b01000000){
// it's day (DoW)
A2DoM=0;
A2DoW = BCD2Dec(hold & 0b00001111);
}
else{
// it's date (DoM)
A2DoW=0;
A2DoM = BCD2Dec(hold & 0b00111111);
}
// control byte
cntb=Wire.read();
// control/status byte
statusb=Wire.read();
ageb=Wire.read(); // aging offset msb is sign
aging=ageb&0b01111111;
if(ageb&0b10000000){
aging=aging*-1;
}
// note that we are not explicitly requesting a conversion
temphi=Wire.read(); // Temp MSB
templo=Wire.read(); // Temp LSB
/* calculate the chip temperature */
degrees = temphi<< 8;
degrees |= templo;
degrees/=256;
}