Skip to main content
GSalz.1
Associate
Feb 26, 2023 5:19 PM UTC
Question

REAL TIME CLOCK (RTC) Not working correctly.

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.

4 replies

waclawek.jan
Super User
Feb 26, 2023 9:06 PM UTC

> at times SKIPS seconds. WHY?

Because:

> Using HAL library set up RTC

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

JW

GSalz.1
GSalz.1Author
Associate
Feb 27, 2023 2:14 AM UTC

Thanks 

Sent from AOL on Android

[edit: moderation removed the automatically inserted email thread]

waclawek.jan
Super User
Feb 26, 2023 9:11 PM UTC

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.1
GSalz.1Author
Associate
Feb 27, 2023 2:10 AM UTC

Thank u

Sent from AOL on Android

[edit: moderation removed the automatically inserted email thread]