Skip to main content
Visitor II
February 26, 2023
Question

REAL TIME CLOCK (RTC) Not working correctly.

  • February 26, 2023
  • 2 replies
  • 2180 views

Using HAL library set up RTC, but at times SKIPS seconds. WHY?

This is the setup:

static void MX_RTC_Init(void)

{

 /* USER CODE BEGIN RTC_Init 0 */

 /* USER CODE END RTC_Init 0 */

 RTC_TimeTypeDef sTime = {0};

 RTC_DateTypeDef sDate = {0};

 /* USER CODE BEGIN RTC_Init 1 */

 /* USER CODE END RTC_Init 1 */

 /** Initialize RTC Only

 */

 hrtc.Instance = RTC;

 hrtc.Init.HourFormat = RTC_HOURFORMAT_12;

 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 (HAL_RTC_Init(&hrtc) != HAL_OK)

 {

  Error_Handler();

 }

 /* USER CODE BEGIN Check_RTC_BKUP */

 /* USER CODE END Check_RTC_BKUP */

 /** Initialize RTC and set the Time and Date

 */

 sTime.Hours = 0x1;

 sTime.Minutes = 0x0;

 sTime.Seconds = 0x0;

 sTime.TimeFormat = RTC_HOURFORMAT12_AM;

 sTime.DayLightSaving = RTC_DAYLIGHTSAVING_NONE;

 sTime.StoreOperation = RTC_STOREOPERATION_RESET;

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

 {

  Error_Handler();

 }

 sDate.WeekDay = RTC_WEEKDAY_MONDAY;

 sDate.Month = RTC_MONTH_JANUARY;

 sDate.Date = 0x1;

 sDate.Year = 0x0;

 if (HAL_RTC_SetDate(&hrtc, &sDate, RTC_FORMAT_BCD) != HAL_OK)

 {

  Error_Handler();

 }

 /* USER CODE BEGIN RTC_Init 2 */

 /* USER CODE END RTC_Init 2 */

}

for main loop:

/* USER CODE BEGIN WHILE */

 while (1)

 {

 HAL_RTC_GetTime(&hrtc, &currentTime, RTC_FORMAT_BCD);

 HAL_RTC_GetDate(&hrtc, &currentDate, RTC_FORMAT_BCD);

 printf("This is date and time: %02d:%02d:%02d \n\r",currentTime.Hours, currentTime.Minutes, currentTime.Seconds);

  HAL_Delay(1000);

 /* USER CODE END WHILE */

  /* USER CODE BEGIN 3 */

 }

 /* USER CODE END 3 */

    This topic has been closed for replies.

    2 replies

    Super User
    February 26, 2023

    > at times SKIPS seconds. WHY?

    Because:

    > Using HAL library set up RTC

    http://efton.sk/STM32/gotcha/g116.html

    JW

    GSalz.1Author
    Visitor II
    February 27, 2023

    Thanks 

    Sent from AOL on Android

    [edit: moderation removed the automatically inserted email thread]

    Super User
    February 26, 2023

    Maybe I just misunderstood your complaint. If your problem is that the printf() does not print consecutively every second, the problem is the code in while(1) loop.

    Depending on the exact clocks setup, this

     >  HAL_Delay(1000);

    lasts 1000 to 1001 milliseconds.

    Also, the primary clock from which HAL_Delay() is derived, may be of different precision than the RTC clock.

    JW

    GSalz.1Author
    Visitor II
    February 27, 2023

    Thank u

    Sent from AOL on Android

    [edit: moderation removed the automatically inserted email thread]