Hexter48044
Member
In my new project using a Teensy 4.0 RTC, I need to determine if the battery has died and the RTC time is incorrect. If it is, certain actions must be taken to set the correct time.
In preperation, I did cut the trace to separate VIN from VUSB. I coded a small test program to display the Epoch time for the internal RTC on a cold startup without a battery connected. After programming the Teensy, I brought up the Serial Monitor console, left it display active, unplugged the Teensy power for 30 minutes and re applied power. Upon startup, my program shows:
Epoch time = 1546300801 01/01/49(2019) 00:00:01
Is my assumption incorrect, that the Epoch time should default to 00:00:00 on 1/1/1970?
Any guidance would be most appreciated.
My sample code is:
//
// Display Teensy RTC time on Powerup with no battery
//
#include <TimeLib.h>
void setup () {
Serial.begin(115200);
while (!Serial);
time_t tt = Teensy3Clock.get();
GetRTC();
if (tt == 0) {
// if Epoch is 0, Teesny RTC lost time
Serial.println(" RTC has lost time - take action");
}
else
{
Serial.print(" RTC is running");
}
}
void loop () {
}
void GetRTC() {
time_t tt = Teensy3Clock.get();
Serial.print("Epoch time = ");Serial.print(tt);
tmElements_t tmtm;
breakTime( tt, tmtm);
Serial.print(" ");
printDigits(tmtm.Month),Serial.print("/");
printDigits(tmtm.Day),Serial.print("/");
Serial.print(tmtm.Year),Serial.print("(");
Serial.print(tmtm.Year+1970),Serial.print(") ");
printDigits(tmtm.Hour),Serial.print(":");
printDigits(tmtm.Minute),Serial.print(":");
printDigits(tmtm.Second),Serial.print(" ");
}
void printDigits(int digits){
if(digits < 10)
Serial.print('0');
Serial.print(digits);
}
In preperation, I did cut the trace to separate VIN from VUSB. I coded a small test program to display the Epoch time for the internal RTC on a cold startup without a battery connected. After programming the Teensy, I brought up the Serial Monitor console, left it display active, unplugged the Teensy power for 30 minutes and re applied power. Upon startup, my program shows:
Epoch time = 1546300801 01/01/49(2019) 00:00:01
Is my assumption incorrect, that the Epoch time should default to 00:00:00 on 1/1/1970?
Any guidance would be most appreciated.
My sample code is:
//
// Display Teensy RTC time on Powerup with no battery
//
#include <TimeLib.h>
void setup () {
Serial.begin(115200);
while (!Serial);
time_t tt = Teensy3Clock.get();
GetRTC();
if (tt == 0) {
// if Epoch is 0, Teesny RTC lost time
Serial.println(" RTC has lost time - take action");
}
else
{
Serial.print(" RTC is running");
}
}
void loop () {
}
void GetRTC() {
time_t tt = Teensy3Clock.get();
Serial.print("Epoch time = ");Serial.print(tt);
tmElements_t tmtm;
breakTime( tt, tmtm);
Serial.print(" ");
printDigits(tmtm.Month),Serial.print("/");
printDigits(tmtm.Day),Serial.print("/");
Serial.print(tmtm.Year),Serial.print("(");
Serial.print(tmtm.Year+1970),Serial.print(") ");
printDigits(tmtm.Hour),Serial.print(":");
printDigits(tmtm.Minute),Serial.print(":");
printDigits(tmtm.Second),Serial.print(" ");
}
void printDigits(int digits){
if(digits < 10)
Serial.print('0');
Serial.print(digits);
}