2024-07-23 09:36 PM - last edited on 2024-07-24 02:05 AM by SofLit
Greetings Everybody,
I have Nucleo - F207ZG board. I have attached external 3.0 V coin battery to VBat and GND. I am configuring board for RTC peripheral. After configuring RTC time and again rechecking after 1 day the time starts to move ahead or time advances by few seconds. After 3 weeks RTC shows 15 seconds more than the actual time.
Below is the code of main while loop
while (1)
{
if(_set_rtc_time)
{
RTC_CalendarConfig();
_set_rtc_time = 0;
}
RTC_CalendarShow(aShowTime, aShowDate);
lcd_put_cur(3,0);
memcpy(_BUFFER_0._BUFFER,(char *)aShowTime, 8);
lcd_text_justify(&_BUFFER_0);
lcd_send_string(_BUFFER_0._BUFFER_OUT);
}
static void MX_RTC_Init(void)
{
hrtc.Instance = RTC;
hrtc.Init.HourFormat = RTC_HOURFORMAT_24;
hrtc.Init.AsynchPrediv = 127;
hrtc.Init.SynchPrediv = 255;
hrtc.Init.OutPut = RTC_OUTPUT_DISABLE;
hrtc.Init.OutPutPolarity = RTC_OUTPUT_POLARITY_HIGH;
hrtc.Init.OutPutType = RTC_OUTPUT_TYPE_OPENDRAIN;
if((RCC->BDCR & RCC_BDCR_RTCEN) == 0)
{
if (HAL_RTC_Init(&hrtc) != HAL_OK)
{
Error_Handler();
}
}
}
Below is RTC show function code
/**
* @brief Display the current time and date.
* showtime : pointer to buffer
* showdate : pointer to buffer
* @retval None
*/
static void RTC_CalendarShow(uint8_t *showtime, uint8_t *showdate)
{
RTC_DateTypeDef sdatestructureget;
RTC_TimeTypeDef stimestructureget;
/* Get the RTC current Time */
HAL_RTC_GetTime(&hrtc, &stimestructureget, RTC_FORMAT_BIN);
/* Get the RTC current Date */
HAL_RTC_GetDate(&hrtc, &sdatestructureget, RTC_FORMAT_BIN);
/* Display time Format : hh:mm:ss */
sprintf((char *)showtime, "%2d:%2d:%2d", stimestructureget.Hours, stimestructureget.Minutes, stimestructureget.Seconds);
/* Display date Format : mm-dd-yy */
sprintf((char *)showdate, "%2d-%2d-%2d", sdatestructureget.Month, sdatestructureget.Date, 2000 + sdatestructureget.Year);
}
Solved! Go to Solution.
2024-07-24 11:24 PM
I don't use F2, but look for app notes like: AN2604 Application note STM32F101xx and STM32F103xx RTC calibration.
Besides, synchronisation with some external precise reference clock, GPS signal, ... seems possible.
hth
KnarfB
2024-07-23 11:08 PM
The board schematics say that the RTC crystal X2 is a NX3215SA32768KHZ.
The crystal's data sheet says Frequency tolerance -20 ... +20 ×10-6 @ +25°C
3 weeks = 1814400 seconds.
so, your observations are pretty much expected.
hth
KnarfB
2024-07-24 11:10 PM
Well thanks Knarf. Is there any other work around for this issue ?
2024-07-24 11:24 PM
I don't use F2, but look for app notes like: AN2604 Application note STM32F101xx and STM32F103xx RTC calibration.
Besides, synchronisation with some external precise reference clock, GPS signal, ... seems possible.
hth
KnarfB
2024-07-25 01:06 AM - edited 2024-07-25 01:13 AM
Use a more precise crystal or calibrate like @KnarfB said. Preferably both. I have a project where I measure the 512Hz RTC output with an accurate frequency counter and use that to calculate the required calibration values.
You can make the calculation iterative too and redo the calibration later to get better calibration values.
You can also use the time drift to calculate the calibration value too. If you know when you last synced your clock and know the current time you can calculate the calibration values too.
How to calibrate the RTC of the STM32F207ZG can be found in the reference manual.
2024-07-31 08:16 PM
Well i have not yet tried calibrating RTC, which i will do later. Your answer seems the possible way to get precise rtc clock and i have selected as an answer.
Thanks for your insight into RTC calibration.