cancel
Showing results for 
Search instead for 
Did you mean: 

RTC Time value

GUIRAT.Youssef
Associate III
Posted on November 28, 2016 at 10:59

Hi, 

I'm using STM32L476RG microcontroller and I'm configuring the RTC to get the instant time value. I'm using LSE with 32,786 KHz as a clock source for my RTC . I have followed this application note (

http://www.st.com/content/ccc/resource/technical/document/application_note/group0/71/b8/5f/6a/8e/d5/45/0a/DM00226326/files/DM00226326.pdf/jcr:content/translations/en.DM00226326.pdf

) to set correctly the initialization process as well as the PREDIV_SYNCH and PREDIV_ASYNCH values (which are respectively in my case 0x00FF and 0x7F ) . However, when I let my application run for too long ( 24 Hours ) and then I check the RTC Time value , I found a gap of 10 seconds ( difference between my RTC time and the desktop time ). This difference is so huge for me because I'm working on medical app. 

Here's my Init function:

&sharpdefine RTC_ASYNCH_PREDIV  0x7F

&sharpdefine RTC_SYNCH_PREDIV   0x00FF

  BSP_RTC_Status Status= RTC_OK;

 /**** Configure the RTC peripheral ******************/

  /* Configure RTC prescaler and RTC data registers */

  /* RTC configured as follow:

      - Hour Format    = Format 12

      - Asynch Prediv  = Value according to source clock

      - Synch Prediv   = Value according to source clock

      - OutPut         = Output Disable

      - OutPutPolarity = High Polarity

      - OutPutType     = Open Drain */

  __HAL_RTC_RESET_HANDLE_STATE(&RtcHandle);

  RtcHandle.Instance = RTC;

  RtcHandle.Init.HourFormat     = RTC_HOURFORMAT_12;

  RtcHandle.Init.AsynchPrediv   = RTC_ASYNCH_PREDIV;

  RtcHandle.Init.SynchPrediv    = RTC_SYNCH_PREDIV;

  RtcHandle.Init.OutPut         = RTC_OUTPUT_DISABLE;

  RtcHandle.Init.OutPutPolarity = RTC_OUTPUT_POLARITY_HIGH;

  RtcHandle.Init.OutPutType     = RTC_OUTPUT_TYPE_OPENDRAIN;

  if(HAL_RTC_Init(&RtcHandle)!= HAL_OK)

  {

    Status = RTC_ERROR;

  }

  __HAL_RTC_WRITEPROTECTION_DISABLE(&RtcHandle);

  RtcHandle.Instance->CR |= RTC_CR_BYPSHAD;

  /* Enable the write protection for RTC registers */

  __HAL_RTC_WRITEPROTECTION_ENABLE(&RtcHandle);

Any ideas please ?

#rtc #l4 #time #stm32
4 REPLIES 4
Posted on November 28, 2016 at 18:17

This is around 115ppm. More than one would expect from a common 32kHz crystal, but not that much that couldn't be explained by poor design of the oscillator - improper burden capacitors, long traces imposing further parasitics. The oscillator circuit is a low-power one, and can be easily thrown wrong even by flux residues.

Review AN2867.

Also, RTCs in most STM32 have ''fine tuning'' provisions, read the RTC chapter of RM.

JW

Walid FTITI_O
Senior II
Posted on November 29, 2016 at 10:05

Hi Elliot, 

As mentionned in the same application note that you mentioned AN4759, you should perform a dogital smooth calibration to compensate the RTC clock deviation which causes a time deviation. You would refer to following parts:

- 1.4 Digital Smooth Calibration

- 5 STM32L4 API and digital smooth calibrationapplication example

-Hannibal-

GUIRAT.Youssef
Associate III
Posted on November 30, 2016 at 15:32

Thanks for both of you !

I will do the smooth calibration and hope that I will be back with good news :))

damh
Associate II
Posted on June 20, 2017 at 17:07

a) disconnect debugger (or set DBGMCU_APB1FZR1 appropriate (DBG_RTC_STOP=0))

b) check, if your PC is the problem (most windows PCs without automatic RTC sync have this problem, difference of ~10-15s is normal)

c) check your ambient temperature (RTCs are working only well at ~20°C)