Detecting Stale RTC time

Status
Not open for further replies.
Hello,

I'm working on a logging product that needs to have an up to date time via the battery backed up RTC or no time at all.

I have a GPS module running to update the time, but this can take some time to lock on (or not at all in metal buildings) if the unit is powered off for a long time.

On a Teensy 3.5 I am simulating a dead RTC backup battery by disconnecting it (having set the time previously), remove the GPS module and turn the Teensy off for a few minutes

On power up of the Teensy, I find that the RTC using Teensy3Clock.get() returns a stale time (looks like the last time the RTC was set). In non logging situations, this may be desirable, but I need to know if the RTC is essentially stale so that I can not log anything with a [very] stale time (I can accept the normal drift over time).

So, my question is this:
Is there a way to know that the RTC that the time is stale. For example, is there a way to check the Vbat voltage so that if it's zero or less than say 2v, I can assume the backup battery is dead and the RTC could be stale?

Many thanks
Gary



Code:
#include <TimeLib.h>

void setup() {
  Serial.begin(9600);
  while (!Serial); // Wait for monitor
    
  setTime(0); // Zero time
  Serial.printf("Initial now: %ld\n", now());
  digitalClockDisplay();
  
  setTime(Teensy3Clock.get());
  Serial.printf("\nNow after RTC get: %ld\n", now());
  digitalClockDisplay();
  
}

void loop() {
  // put your main code here, to run repeatedly:

}

void digitalClockDisplay() {
  // digital clock display of the time
  Serial.print(hour());
  printDigits(minute());
  printDigits(second());
  Serial.print(" ");
  Serial.print(day());
  Serial.print(" ");
  Serial.print(month());
  Serial.print(" ");
  Serial.print(year()); 
  Serial.println(); 
}

void printDigits(int digits){
  // utility function for digital clock display: prints preceding colon and leading 0
  Serial.print(":");
  if(digits < 10)
    Serial.print('0');
  Serial.print(digits);
}
 
Hello,

I'm working on a logging product that needs to have an up to date time via the battery backed up RTC or no time at all.

I have a GPS module running to update the time, but this can take some time to lock on (or not at all in metal buildings) if the unit is powered off for a long time.

On a Teensy 3.5 I am simulating a dead RTC backup battery by disconnecting it (having set the time previously), remove the GPS module and turn the Teensy off for a few minutes

On power up of the Teensy, I find that the RTC using Teensy3Clock.get() returns a stale time (looks like the last time the RTC was set). In non logging situations, this may be desirable, but I need to know if the RTC is essentially stale so that I can not log anything with a [very] stale time (I can accept the normal drift over time).

So, my question is this:
Is there a way to know that the RTC that the time is stale. For example, is there a way to check the Vbat voltage so that if it's zero or less than say 2v, I can assume the backup battery is dead and the RTC could be stale?

Many thanks
Gary

Best thing is to remove (comment) the clock setting part from the core setup routine (lines 1100 - 1130 in mk20dx128.c) and do the RTC setting on demand after starting the program (needs some sort of menu in startup())

AFAIK, schematic shows that VBAT has always 3.3V once there is power, a battery provides 3 V only when Teensy is not powered (and therefore not running)
 
Thanks @WMXZ. That cured it - I had not considered that the RTC might have been set based on the last binary compile time (that also explains some inconsistency in the time's I saw during testing).

Now, when the battery is disconnected, the RTC always starts from 0 (..1970...), perfect.

Thanks also for the shallow dive into the underlying system. I will have to remember to make that change in future versions.

Gary
 
In setup() you could check the time against EEPROM saved value in some fashion. When you get a GPS locked time you could update the EEPROM. Anytime setup sees it older than that in EEPROM you would know it isn't valid and set a flag. That would save modifying the core.
 
I haven't used the RTC on Teensy but I am well familiar with the concepts. Can you check that the RTC is advancing at the same rate as real time? In other words, is the RTC advancing at the rate of 1 second per second (within some error limits)? Perhaps use a smaller time interval?
 
Status
Not open for further replies.
Back
Top