Teensy 3.5 set RTC without rebooting teensy

Status
Not open for further replies.

palle

New member
Hi,
ive a little trouble understanding the teensy 3.5 RTC code from the TimeLib and how it sets the RTC from Systemtime.
Iam Using the TimeTeensy3 code to get and set the RTC. It works if i change my System time , reload the code into the teensy and disconnect power (without having the RTC backed by Vbat). After ithe teensy is up again it shows me the updated system time.
however i would like to change the RTC time without the need to completly disconnect power from the RTC and teensy.
I understand that there is the possibility with sending a serial command for example: "T1357041600" for Jan 1 2013 and it gets the RTC updated by processSyncMessage() . but as this is elapsed seconds from 1970, its a bit cumbersome to set a time. It would be way easier and less error-prone if i could update from system time e.g. sending "settime" via serial and it would grab the actual system time and reload it into the RTC .
But so far i couldn get it to work to do so....would appreciate any help!

code i used from the timelib:
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);       // I understand it that this is what grabs the system tiem and sets the teensy RTC?

  Serial.begin(115200);
  while (!Serial);  // Wait for Arduino Serial Monitor to open
  delay(100);
  if (timeStatus()!= timeSet) {
    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();           // I understand this is what grabs the Txxxxx serial command and updates the RTC without the need to reset the power
    if (t != 0) {
      Teensy3Clock.set(t); // set the RTC
      setTime(t);
    }
  }
  digitalClockDisplay();  
  delay(1000);
}

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

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);
}
 
I understand that there is the possibility with sending a serial command for example: "T1357041600" for Jan 1 2013 and it gets the RTC updated by processSyncMessage() . but as this is elapsed seconds from 1970, its a bit cumbersome to set a time. It would be way easier and less error-prone if i could update from system time e.g. sending "settime" via serial and it would grab the actual system time and reload it into the RTC .

I suppose what is considered cumbersome or error-prone is a matter of perspective and opinion. If software is doing the work on both sides, personally I'd consider a single number to be much simpler than 6 or 7 numbers.

But if you really want to use a human readable date format (regardless of whether that's error prone or not), you certainly can. Obviously you first need to craft code to parse the 6 numbers from the text. Don't skip testing that part, as any errors in your parsing code will make the rest of this project very frustrating.

Once you have the 6 numbers, TimeLib.h provides this function.

Code:
  void    setTime(int hr,int min,int sec,int day, int month, int yr);

That only sets the time within the Time library. But then you can call now() to get the library's time as a 32 bit number, which you would then use with Teensy3Clock.set(). For example:

Code:
  setTime(hr, min, sec, day, month, yr);
  Teensy3Clock.set(now());
 
Status
Not open for further replies.
Back
Top