Ok, i will see all the links soon..
In the previous code, i done modifications.
here is the code
Code: Select all
#include <RTClock.h>
RTClock rtclock (RTCSEL_LSE); // initialise Low speed External clock for RTCclock
uint32 tt;
tm_t mtt;
uint32 last_ms = 0;
uint32 period = 0;
#define LED_PIN PC13
double count = 0;
const char * months[] = {"Dummy", "Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec" };
const char * delim = " :";
char s[128]; // for sprintf
char buf[25];
const char * weekdays[] = {"Mon", "Tue", "Wed", "Thu", "Fri", "Sat", "Sun"};
// This function is called in the attachSecondsInterrpt
void blink ()
{
digitalWrite(LED_PIN, (~ digitalRead(LED_PIN)) & 1);
uint32_t ms = millis();
if (last_ms == 0)
{
last_ms = ms;
}
else
{
period = ms - last_ms;
last_ms = ms;
}
}
uint8_t str2month(const char * d)
{
uint8_t i = 13;
while ( (--i) && strcmp(months[i], d) != 0 );
return i;
}
void ParseBuildTimestamp(tm_t & mt)
{
// Timestamp format: "Dec 8 2017, 22:57:54"
sprintf(s, "Timestamp: %s, %s\n", __DATE__ , __TIME__);
Serial.print(s);
char * token = strtok(s, delim); // get first token
while ( token != NULL )
{
uint8_t m = str2month((const char*)token);
if ( m > 0 )
{
mt.month = m;
Serial.print(" month: "); Serial.println(mt.month);
token = strtok(NULL, delim); // get next token
mt.day = atoi(token);
Serial.print(" day: "); Serial.println(mt.day);
token = strtok(NULL, delim); // get next token
mt.year = atoi(token) - 1970;
Serial.print(" year: "); Serial.println(mt.year);
token = strtok(NULL, delim); // get next token
mt.hour = atoi(token);
Serial.print(" hour: "); Serial.println(mt.hour);
token = strtok(NULL, delim); // get next token
mt.minute = atoi(token);
Serial.print(" minute: "); Serial.println(mt.minute);
token = strtok(NULL, delim); // get next token
mt.second = atoi(token);
Serial.print(" second: "); Serial.println(mt.second);
}
token = strtok(NULL, delim);
}
sprintf(s, "Build: %02d-%02d-%02d %02d:%02d:%02d\n", mt.year + 1970, mt.month, mt.day, mt.hour, mt.minute, mt.second);
Serial.println(s);
}
void setup()
{
delay(5000);
pinMode(LED_PIN, OUTPUT);
ParseBuildTimestamp(mtt); //setting initial time. here, instant compilation time is considered as initial time setup
tt = rtclock.makeTime(mtt) + 8; //since compilation time is 8sec delayed when compared with actual time, so adding 8seconds with the compilation time here.
// Serial.println("make time value 1 is:: "); Serial.println(tt); //printing the adjusted time
rtclock.setTime(tt);
rtclock.attachSecondsInterrupt(blink);// Call blink
}
void loop()
{
if (rtclock.getTime() != tt)
{
tt = rtclock.getTime(); //gets the instatnt time
Serial.print("___________time is: ");
Serial.println(tt);
Serial.print("period : ");
Serial.println(period);
rtclock.breakTime(rtclock.now(), mtt); //gets the instant time in day,month,year,hour,minute,seconds
sprintf(s, "RTC timestamp: %s %u %u, %s, %02u:%02u:%02u\n",
months[mtt.month], mtt.day, mtt.year + 1970, weekdays[mtt.weekday], mtt.hour, mtt.minute, mtt.second);
Serial.print(s);
}
}
OUTPUT
Code: Select all
12:44:13.168 -> Timestamp: Feb 6 2020, 12:44:05
12:44:13.168 -> month: 2
12:44:13.168 -> day: 6
12:44:13.168 -> year: 50
12:44:13.168 -> hour: 12
12:44:13.168 -> minute: 44
12:44:13.168 -> second: 5
12:44:13.168 -> Build: 2020-02-06 12:44:05
12:44:13.168 ->
12:44:14.187 -> ___________time is: 1580993054
12:44:14.187 -> period : 1000
12:44:14.187 -> RTC timestamp: Feb 6 2020, Thu, 12:44:14
12:44:15.175 -> ___________time is: 1580993055
12:44:15.175 -> period : 1000
12:44:15.175 -> RTC timestamp: Feb 6 2020, Thu, 12:44:15
12:44:16.160 -> ___________time is: 1580993056
12:44:16.160 -> period : 1000
12:44:16.160 -> RTC timestamp: Feb 6 2020, Thu, 12:44:16
12:44:17.181 -> ___________time is: 1580993057
12:44:17.181 -> period : 1002
12:44:17.181 -> RTC timestamp: Feb 6 2020, Thu, 12:44:17
12:44:18.166 -> ___________time is: 1580993058
12:44:18.166 -> period : 1000
12:44:18.166 -> RTC timestamp: Feb 6 2020, Thu, 12:44:18
12:44:19.161 -> ___________time is: 1580993059
12:44:19.161 -> period : 1003
12:44:19.161 -> RTC timestamp: Feb 6 2020, Thu, 12:44:19
12:44:20.181 -> ___________time is: 1580993060
12:44:20.181 -> period : 1000
12:44:20.181 -> RTC timestamp: Feb 6 2020, Thu, 12:44:20
12:44:21.169 -> ___________time is: 1580993061
12:44:21.169 -> period : 1003
12:44:21.169 -> RTC timestamp: Feb 6 2020, Thu, 12:44:21
12:44:22.191 -> ___________time is: 1580993062
12:44:22.191 -> period : 1000
12:44:22.191 -> RTC timestamp: Feb 6 2020, Thu, 12:44:22
12:44:23.171 -> ___________time is: 1580993063
12:44:23.171 -> period : 1003
12:44:23.171 -> RTC timestamp: Feb 6 2020, Thu, 12:44:23
12:44:24.191 -> ___________time is: 1580993064
12:44:24.191 -> period : 1000
12:44:24.191 -> RTC timestamp: Feb 6 2020, Thu, 12:44:24
12:44:25.183 -> ___________time is: 1580993065
12:44:25.183 -> period : 1003
12:44:25.183 -> RTC timestamp: Feb 6 2020, Thu, 12:44:25
12:44:26.173 -> ___________time is: 1580993066
12:44:26.173 -> period : 1000
12:44:26.173 -> RTC timestamp: Feb 6 2020, Thu, 12:44:26
12:44:27.195 -> ___________time is: 1580993067
12:44:27.195 -> period : 1002
12:44:27.195 -> RTC timestamp: Feb 6 2020, Thu, 12:44:27
12:44:28.184 -> ___________time is: 1580993068
12:44:28.184 -> period : 1000
12:44:28.184 -> RTC timestamp: Feb 6 2020, Thu, 12:44:28
12:44:29.185 -> ___________time is: 1580993069
12:44:29.185 -> period : 1003
12:44:29.185 -> RTC timestamp: Feb 6 2020, Thu, 12:44:29
12:44:30.178 -> ___________time is: 1580993070
12:44:30.178 -> period : 1000
12:44:30.178 -> RTC timestamp: Feb 6 2020, Thu, 12:44:30
12:44:31.203 -> ___________time is: 1580993071
12:44:31.203 -> period : 1003
12:44:31.203 -> RTC timestamp: Feb 6 2020, Thu, 12:44:31
12:44:32.188 -> ___________time is: 1580993072
12:44:32.188 -> period : 1000
12:44:32.188 -> RTC timestamp: Feb 6 2020, Thu, 12:44:32
I got output period as 1001,1003,1004. If i let the controller to run for longer time, the
period value changes to
1500. If this continuous, it would definitely show wrong time in few days.
What is the reason for this change in period? How could i rectify it?