Skip to main content
Tomohiro Shirane
Associate II
June 7, 2018
Question

STM32 RTC milliseconds setting

  • June 7, 2018
  • 9 replies
  • 7387 views
Posted on June 07, 2018 at 03:50

Hello,

I'm using the STM32 H753 Evaluation board, and trying to set the milliseconds by using HAL_RTC_SetTime function.

However, the HAL_RTC_SetTime function does not change the milliseconds(subseconds) value.

I've read the RTC synchronization chapter in the reference manual, but I don't understand it enough though.

If I would like to set the 400 milliseconds value, should I set the 0x190(= 400 decimal) into SUBFS field in RTC_SHIFTR and set the 0x3E7(= 999 decimal) into PREDIV_S field in RTC_PRER ?

Help would be appreciated.

Thanks.

Shirane.

    This topic has been closed for replies.

    9 replies

    waclawek.jan
    Super User
    June 7, 2018
    Posted on June 07, 2018 at 09:08

    Unless you want to use some nonstandard crystal, no.

    The subsecond register runs in units given by the crystal and asynchronous part of divider (i.e. 1/(fLSE/(PREDIV_A+1)), which in the normal case is equal to 1 second/(PREDIV_S+1) ) and you should set the shift register in the same units.

    In the default case, i.e. PREDIV_A = 0x007F, PREDIV_S = 0x00FF, to shift by 400ms you set SUBFS to 256*0.400≈102.

    JW

    Tomohiro Shirane
    Associate II
    June 13, 2018
    Posted on June 13, 2018 at 12:16

    Hello,

    I've tried setting SUBFS field in SHIFT register but I did't get expeced result.

    I've attached my code. The below is code snippet

    --------------------------------------------------------------

    [main.c] <- I've added the code that set SHIFTR after calling MX_RTC_Init();

    /* Initialize all configured peripherals */

    MX_GPIO_Init();

    MX_USART2_UART_Init();

    MX_RTC_Init();

    /* USER CODE BEGIN 2 */

    __HAL_RTC_WRITEPROTECTION_DISABLE(&hrtc);

    hrtc.Instance->SHIFTR = 0x00000190;// 400msec

    __HAL_RTC_WRITEPROTECTION_ENABLE(&hrtc);

    /* USER CODE END 2 */

    [rtc.c] <- set AsynchPredivas 31 andSynchPrediv as 999 in order to set the callendar clock into 1Hz.Currently I'm usingLSI(32KHz) as RTC clock.

    void MX_RTC_Init(void)

    {

    RTC_TimeTypeDef sTime;

    RTC_DateTypeDef sDate;

    /**Initialize RTC Only

    */

    hrtc.Instance = RTC;

    hrtc.Init.HourFormat = RTC_HOURFORMAT_24;

    hrtc.Init.AsynchPrediv = 31;

    hrtc.Init.SynchPrediv = 999;

    hrtc.Init.OutPut = RTC_OUTPUT_DISABLE;

    hrtc.Init.OutPutPolarity = RTC_OUTPUT_POLARITY_HIGH;

    hrtc.Init.OutPutType = RTC_OUTPUT_TYPE_OPENDRAIN;

    if (HAL_RTC_Init(&hrtc) != HAL_OK)

    {

    _Error_Handler(__FILE__, __LINE__);

    }

    /* USER CODE BEGIN RTC_Init 2 */

    /* USER CODE END RTC_Init 2 */

    /**Initialize RTC and set the Time and Date

    */

    sTime.Hours = 0x13;

    sTime.Minutes = 0x22;

    sTime.Seconds = 0x30;

    sTime.DayLightSaving = RTC_DAYLIGHTSAVING_NONE;

    sTime.StoreOperation = RTC_STOREOPERATION_RESET;

    if (HAL_RTC_SetTime(&hrtc, &sTime, RTC_FORMAT_BCD) != HAL_OK)

    {

    _Error_Handler(__FILE__, __LINE__);

    }

    /* USER CODE BEGIN RTC_Init 3 */

    --------------------------------------------------------------

    My question is that:

    • Whereshould I add the code that sets SHIFTR ? Inside ofHAL_RTC_SetTime function ?
    • Is thereany code that I should call(add) around 'hrtc.Instance->SHIFTR ' ?

    Thanks.

    Shirane

    ________________

    Attachments :

    main.c.zip : https://st--c.eu10.content.force.com/sfc/dist/version/download/?oid=00Db0000000YtG6&ids=0680X000006HxVr&d=%2Fa%2F0X0000000az6%2FuL7ROgzBTPUFtaflQ8XCIP8fwLSVomvBhV.B9S88ZXg&asPdf=false

    rtc.c.zip : https://st--c.eu10.content.force.com/sfc/dist/version/download/?oid=00Db0000000YtG6&ids=0680X000006HxVI&d=%2Fa%2F0X0000000ayx%2FtPf1npWlf4sv71iRsgEHSsyjggo0v5xurbKDNeLZu54&asPdf=false
    waclawek.jan
    Super User
    June 13, 2018
    Posted on June 13, 2018 at 12:47

    I've tried setting SUBFS field in SHIFT register but I did't get expeced result.

    What is the expected result and what was your observation?

    JW

    Tomohiro Shirane
    Associate II
    June 13, 2018
    Posted on June 13, 2018 at 13:27

    Hi,

    MX_RTC_Init() sets the Hours as 0x13, Minutes as 0x22 and Seconds as 0x30.

    And also, my code sets 400[msec] into 'hrtc.Instance->SHIFTR'.

    In main.c, printf() is called on each 100[msec], that shows the current Hours, Minutes, Seconds and Sub-seconds.

    >> printf('Hours = %d, Minutes = %d, Seconds = %d, SubSeconds = %d\n', time.Hours, time.Minutes, time.Seconds, msec);

    So, the expected result are:

    ----------------------------------------------

    Hours = 13, Minutes = 22, Seconds = 30, SubSeconds = 400

    Hours = 13, Minutes = 22, Seconds = 30, SubSeconds = 500

    Hours = 13, Minutes = 22, Seconds = 30, SubSeconds = 600

    ........

    ----------------------------------------------

    However, the current result are:

    ----------------------------------------------

    Hours = 13, Minutes = 22, Seconds = 30, SubSeconds = 0                   <-  SubSeconds starts with 0.

    Hours = 13, Minutes = 22, Seconds = 30, SubSeconds = 4294676       <-  Strange value ?

    Hours = 13, Minutes = 22, Seconds = 30, SubSeconds = 4294786      <-  Strange value ?

    Hours = 13, Minutes = 22, Seconds = 30, SubSeconds = 4294896      <-  Strange value ?

    Hours = 13, Minutes = 22, Seconds = 30, SubSeconds = 39

    ----------------------------------------------

    Thanks.

    Shirane