Teensy 3.2 RTC not syncing

snowface

Member
Hi,
I have a Teensy 3.2 with CR2032 battery and 32.768 kHz 12.5 PF crystal attached. I am running the example TimeTeensy3 code, but the Teensy doesn't seem to be syncing with internal RTC. Can you help we debug this? I have done the exact same setup on two different Teensy 3.2.
Following is the code:

Code:
/*
 * TimeRTC.pde
 * example code illustrating Time library with Real Time Clock.
 * 
 */

#include <TimeLib.h>

void setup()  {
  // set the Time library to use Teensy 3.0's RTC to keep time
  setSyncProvider(getTeensy3Time);

  Serial.begin(115200);
  while (!Serial);  // Wait for Arduino Serial Monitor to open
  delay(100);
  Serial.println(timeStatus());
  if (timeStatus()!= 1) {
    Serial.println("Unable to sync with the RTC");
  } else {
    Serial.println("RTC has set the system time");
  }
}

void loop() {
  if (Serial.available()) {
    time_t t = processSyncMessage();
    if (t != 0) {
      Teensy3Clock.set(t); // set the RTC
      setTime(t);
    }
  }
  digitalClockDisplay();  
  delay(1000);
}

void digitalClockDisplay() {
  Serial.println(timeStatus());
  // 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(); 
}

time_t getTeensy3Time()
{
  return Teensy3Clock.get();
}

/*  code to process time sync messages from the serial port   */
#define TIME_HEADER  "T"   // Header tag for serial time sync message

unsigned long processSyncMessage() {
  unsigned long pctime = 0L;
  const unsigned long DEFAULT_TIME = 1357041600; // Jan 1 2013 

  if(Serial.find(TIME_HEADER)) {
     pctime = Serial.parseInt();
     return pctime;
     if( pctime < DEFAULT_TIME) { // check the value is a valid time (greater than Jan 1 2013)
       pctime = 0L; // return 0 to indicate that the time is not valid
     }
  }
  return pctime;
}

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);
}

And here is the output:
Code:
2
Unable to sync with the RTC
2
15:11:02 1 2 2022
2
15:11:03 1 2 2022
2
15:11:04 1 2 2022
 
From what I understand timeStatus() ==2 is
Code:
timeNeedsSync  Time's clock is set, but the sync has failed, so it may not be accurate.
 
Looking at the source, the timeStatus values are
Code:
typedef enum {timeNotSet, timeNeedsSync, timeSet
}  timeStatus_t ;
So the value of 2 is "timeSet", maybe if statement should read
if (timeStatus() != timeSet) {
as shown in File > Examples > Time > TimeTeensy3

and your sketch is probably working. Though you should confirm that the RTC is properly soldered and working. Here is your sketch modified to print system time (16mhz crystal) and RTC time (32khz crystal).
Code:
/*
   TimeRTC.pde
   example code illustrating Time library with Real Time Clock.

*/

#include <TimeLib.h>
[COLOR="#FF0000"]#include <time.h>[/COLOR]

void setup()  {
  // set the Time library to use Teensy 3.0's RTC to keep time
  setSyncProvider(getTeensy3Time);

  Serial.begin(115200);
  while (!Serial);  // Wait for Arduino Serial Monitor to open
  delay(100);
  Serial.println(timeStatus());
 [COLOR="#FF0000"] if (timeStatus() != timeSet) {[/COLOR]
    Serial.println("Unable to sync with the RTC");
  } else {
    Serial.println("RTC has set the system time");
  }
}

void loop() {
  if (Serial.available()) {
    time_t t = processSyncMessage();
    if (t != 0) {
      Teensy3Clock.set(t); // set the RTC
      setTime(t);
      Serial.println("RTC has set the system time");
    }
  }
  digitalClockDisplay();
  [COLOR="#FF0000"]time_t secs = Teensy3Clock.get();   // read RTC
  Serial.print("RTC ");
  Serial.print(ctime(&secs));[/COLOR]

  delay(1000);
}

void digitalClockDisplay() {
  //Serial.println(timeStatus());
  // 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();
}

time_t getTeensy3Time()
{
  return Teensy3Clock.get();
}

/*  code to process time sync messages from the serial port   */
#define TIME_HEADER  "T"   // Header tag for serial time sync message

unsigned long processSyncMessage() {
  unsigned long pctime = 0L;
  const unsigned long DEFAULT_TIME = 1357041600; // Jan 1 2013

  if (Serial.find(TIME_HEADER)) {
    pctime = Serial.parseInt();
    return pctime;
    if ( pctime < DEFAULT_TIME) { // check the value is a valid time (greater than Jan 1 2013)
      pctime = 0L; // return 0 to indicate that the time is not valid
    }
  }
  return pctime;
}

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);
}
The teensy 3 IDE sets the system time and RTC time to the compile time of the sketch (if the RTC hasn't been set). The example sketch can update the time from the Serial port using Processing sketch (see https://www.pjrc.com/teensy/td_libs_Time.html). Or from UNIX bash, time can be set with the following incantation
Code:
TZ_adjust=-5;  echo T$(($(date +%s)+60*60*$TZ_adjust)) > /dev/ttyACM0

You should also verify your RTC battery is working, by removing power (unplug USB) from T3.2 for a few minutes, then repower and confirm RTC clock kept ticking.
 
Back
Top