STM32F103C8T6 RTC in Arduino ide

hi, I am using STM32F103C8T6 microcontroller to get current Time and date. I am using Arduino IDE for this project. Since the microcontroller has in-buid RTC, i am using it. I used RTClock.h library. I used the example program “Test_RTClock” . In the setup function, i initially set the epoch time by myself using rt.setTime() function. And i did not change anything in the loop function. So, as example, it should print the epoch time for every 1 second.
But, case(1),As i executed the code, i found that the microcontroller prints the correct time at first execution and it takes more than actual 1second to print the next RTCsecond. And again it takes more than actual 1second to Print the next second. and thus IF WE SEE, after few minutes, the MICROCONTROLLER’s time is very much delayed when compared to the actual time.
Also if i re-upload (case:2) the code, it prints actual time at first, and then the controller prints delayed time when compared with Actual time.
Also in both the cases, the delay is not same.

First time uploading the code: In first case it takes, 1115 milliseconds to chance a first second and not 1000msec. Then it takes 1405 milliseconds to chance the next second, then it takes 1200 milliseconds to change the next second. here, the millisecond change is random and not 1000ms.
Second time uploading the code: In this case it takes, 1100 milliseconds to chance a first second. Then it takes 1665 milliseconds to chance the next second, then it takes 1455 milliseconds to change the next second. here also, the milli second change is random.
we could see, on every upload, the RTCcounter takes different/random milliseconds and not the actual 1000millisecond.
What causes this random changes ? Does RTC counter doesnot work properly? Somebody kindly help me to fix this issue…

We don’t do Arduino here.
But try this on the Mbed compiler.
I use this to test the RTC and the fetch time.
On the STM32L152RE the fetch RTC time is 171uS.
You can also compare the RTC time with a watch or clock.

#include "mbed.h"

Serial pc(USBTX, USBRX);

int last_seconds,getTime_delay;
char timebuffer[32];

struct tm t;
time_t seconds;

// manual set RTC values
int second      =0;
int minute      =14;    // 0-59
int hour        =9;    // 2-23
int dayofmonth  =31;    // 1-31
int month       =1;     // 1-12
int year        =19;    // last 2 digits

void setRTC()
{
    t.tm_sec = (second);        // 0-59
    t.tm_min = (minute);        // 0-59
    t.tm_hour = (hour);         // 0-23
    t.tm_mday = (dayofmonth);   // 1-31
    t.tm_mon = (month-1);       // 0-11  "0" = Jan, -1 added for Mbed RCT clock format
    t.tm_year = ((year)+100);   // year since 1900,  current DCF year + 100 + 1900 = correct year
    set_time(mktime(&t));       // set RTC clock                
} 

void getTime()
{
    seconds = time(NULL);
    strftime(timebuffer,30,"%H:%M:%S %a %d %b %Y", localtime(&seconds));                 
}

Timer t1;

int main(){ 
 
pc.printf("\033[0m\033[2J\033[H\n        ----- RTC test -----\r\n\n\n");

if (time(NULL) < 1420070400) {setRTC();}    // set the RTC if power loss
//setRTC(); // manual set RTC register

t1.start();

while(1){        
    
    t1.reset();     
    getTime();
    getTime_delay = t1.read_us();
        
    while(seconds == last_seconds){            
        getTime();           
    }                
    last_seconds=seconds;
       
   // pc.printf("\033[2K\033[1A%s   getTime took %d us \r\n",timebuffer, getTime_delay);  // returns to beginning of line 
    pc.printf("%s   getTime took %d us \r\n",timebuffer, getTime_delay);    // returns to next line       
            
}

}

2 Likes

sure, i will try…
Thanks