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