Teensy 4.0 RTC

Status
Not open for further replies.

gh_iot

New member
I’m using the Teensy 4.0 RTC, and I have some strange issue, with keeping the time, the problem is as follows: I’m setting the time using the serial interface (view code), and the Teensy is displaying it correctly, up until now everything is ok…. But as I’m disconnecting the power from the Teensy and reconnecting it, it is losing one hour, the minutes and the seconds are ok, it is only the hour segment that is missing the time, and it is always losing one hour. Does anyone have any idea?

Teensy 4.0 rtc.jpg

Arduino code:

#include <TimeLib.h> //Teensy time lib

#define led 14
#define display Serial1 // nextion display

int long blink_time; // blinking led
int long time_display_cycle;

void setup() {
Serial.begin(115200); Serial.setTimeout(50);
display.begin(115200);
setSyncProvider(getTeensy3Time);
pinMode(led, OUTPUT);
Serial.println("- = Program Starts = - ");
}

void loop() {
led_blink(100); // blinking the LED connected on pin 14
display_clock();
set_clock();
}

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

void led_blink(int t) {
if (millis() - blink_time >= t) {
digitalWrite(led, !digitalRead(led));
blink_time = millis();
}
}

void set_clock() { //setting the time using the serial interface, typ 1 for entering time setting mode
if (Serial.available() > 0) {
int data_type;
data_type = Serial.parseInt();
if (data_type == 1) {// 1-->clock setting
Serial.println(Serial.parseInt());
Serial.println(" -= Time Setting =- ");
//=============
Serial.print("Enter Hour :.....");
while (Serial.available() == 0) {
}
int Hour = int (Serial.parseInt());
Serial.println(Hour);
//=============
Serial.print("Enter Minutes:.....");
while (Serial.available() == 0) {
}
int Minute = int (Serial.parseInt());
Serial.println(Minute);
//=============
Serial.print("Day Of Month :.....");
while (Serial.available() == 0) {
}
int Day_month = int (Serial.parseInt());
Serial.println(Day_month);
//=============
Serial.print("Month :.....");
while (Serial.available() == 0) {
}
int Month = int (Serial.parseInt());
Serial.println(Month);
//=============
Serial.print("Year :.....");
while (Serial.available() == 0) {
}
int Year = int (Serial.parseInt());
Serial.println(Year);
//=============
// hours, minutes, seconds, days, months, years
setTime(Hour, Minute, 0, Day_month, Month, Year);
}
else {
Serial.println("");
Serial.println("- = Unknown data type = -");
}
Serial.println("====================================");
time_display_cycle = 0;
display_clock();
}
}

void display_clock() {
if (millis() - time_display_cycle >= 1000) { // display the time once a second
String _minute;
String _second;
String Day;

if (second() < 10) {
_second = "0" + String(second());
}
else {
_second = String(second());
}

if (minute() < 10) {
_minute = "0" + String(minute());
}
else {
_minute = String(minute());
}

switch (weekday()) {
case 1:
Day = "Sun";
break;
case 2:
Day = "Mon";
break;
case 3:
Day = "Tue";
break;
case 4:
Day = "Wed";
break;
case 5:
Day = "Thu";
break;
case 6:
Day = "Fri";
break;
case 7:
Day = "Sat";
break;
}

String txt = String(day()) + "/" + String(month()) + "/" + String(year()) + " " +
String(hour()) + ":" + _minute + " " + Day;
Serial.println(txt);

String txt_hmi = String(hour()) + ":" + _minute + ":" + _second;
display.print("t0.txt="); // update nextion display
display.write(0x22);
display.print(txt_hmi);
display.write(0x22);
display.write(0xff);
display.write(0xff);
display.write(0xff);

time_display_cycle = millis();
}
}
 
Which version of Teensy Loader are you using. In the small Teensy window, click Help > About (or Teensy > About if using Macintosh) to see the version.

A bug with daylight savings time was fixed in version 1.52. If you have an older version, update to 1.52.
 
Status
Not open for further replies.
Back
Top