cancel
Showing results for 
Search instead for 
Did you mean: 

Hi, I am new to STM32. I am trying to get the RTC working on a nucleo F302R8 board. But the seconds are giving weird returns. I am not sure that minutes work properly either.

GPapo.1
Associate

Here is the RTC initialization code:

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_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 (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 = 0x15;

 sTime.Minutes = 0x00;

 sTime.Seconds = 0x00;

 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_THURSDAY;

 sDate.Month = RTC_MONTH_AUGUST;

 sDate.Date = 0x8;

 sDate.Year = 0x1985;

 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 */

}

In main function I just have a loop that reads date, time, and prints time to the serial port.

 while (1)

 {

  /* USER CODE END WHILE */

  /* USER CODE BEGIN 3 */

 RTC_TimeTypeDef Time_Stamp;

 RTC_DateTypeDef sDate;

 HAL_RTC_GetDate(&hrtc,&sDate,RTC_FORMAT_BIN );

 HAL_RTC_GetTime(&hrtc,&Time_Stamp,RTC_FORMAT_BIN);

 sprintf(str,"%d:%d:%d:%d",Time_Stamp.Hours,Time_Stamp.Minutes,Time_Stamp.Seconds,Time_Stamp.SubSeconds);

 HAL_UART_Transmit_DMA(&huart2,&str,15);

 HAL_Delay(1000);

 }

The RTC clock input is set to LSE in system clock config:

 PeriphClkInit.RTCClockSelection = RCC_RTCCLKSOURCE_LSE;

Here is what I get from serial port:

15:0:0:255

15:0:0:0

15:0:1:0

15:0:1:0

15:0:1:0

15:0:1:0

15:0:1:1

15:0:1:1

15:0:1:1

15:0:1:1

15:0:1:1

15:0:1:1

15:0:1:2

15:0:1:2

15:0:1:2

15:0:14:2

15:0:14:2

15:0:14:2

15:0:14:2

15:0:14:3

15:0:14:3

15:0:14:3

15:0:14:3

15:0:14:3

15:0:14:4

15:0:14:4

15:0:14:4

15:0:26:4

15:0:26:4

15:0:26:4

15:0:26:5

15:0:26:5

15:0:26:5

15:0:26:5

15:0:26:5

15:0:26:5

15:0:26:5

15:0:26:6

15:0:26:6

15:0:38:6

15:0:38:6

15:0:38:6

15:0:38:6

15:0:38:7

15:0:43:7

15:0:43:7

15:0:45:7

15:0:45:7

15:0:45:7

15:0:45:8

15:0:45:8

15:0:50:8

15:0:50:8

15:0:50:8

15:0:53:8

15:0:53:9

15:0:53:9

15:0:53:9

15:0:57:9

15:0:57:9

15:0:57:9

15:0:57:9

15:0:57:10

15:0:57:10

15:0:57:10

15:1:4:10

15:1:4:10

15:1:4:10

15:1:7:11

15:1:7:11

15:1:7:11

15:1:7:11

15:1:7:11

15:1:7:11

15:1:7:12

15:1:14:12

15:1:14:12

15:1:14:12

15:1:14:12

15:1:14:12

15:1:14:13

15:1:14:13

15:1:14:13

15:1:22:13

15:1:22:13

15:1:22:13

15:1:22:14

15:1:22:14

15:1:27:14

15:1:27:14

Also note it printed more that 60 times before the minutes value updated from 0 to 1 minute... I know the delay function is not 100% accurate for measuring time but it shouldnt have such huge deviation. If I leave it running the minutes continue to increase but compared to my PC clock they have deviation if I leave it for 30 minutes or so.. The prediv values are correct for 32.768Khz clock:

 hrtc.Init.AsynchPrediv = 127;

 hrtc.Init.SynchPrediv = 255;

Any help would be highly apreciated. Thanx

1 ACCEPTED SOLUTION

Accepted Solutions

First read time, then date.

JW

View solution in original post

3 REPLIES 3

You're using BCD not binary 0x09 ticks to 0x10, 0x49 to 0x50, and 0x59 to 0x00

uint8_t str[32];

HAL_UART_Transmit_DMA(&huart2, &str, sprintf((char *)str,"%02d:%02d:%02d:%d\n",Time_Stamp.Hours,Time_Stamp.Minutes,Time_Stamp.Seconds,Time_Stamp.SubSeconds));

sprintf returns length of string

Tips, Buy me a coffee, or three.. PayPal Venmo
Up vote any posts that you find helpful, it shows what's working..

First read time, then date.

JW

GPapo.1
Associate

waclawek.jan thank you. it works now!