cancel
Showing results for 
Search instead for 
Did you mean: 

stm32f103c8t6 rtc. When I restart the power supply, I lose the date, but the time is correct (Keil),

A Bravos
Associate III

/* RTC init function */

static void MX_RTC_Init(void)

{

 /* USER CODE BEGIN RTC_Init 0 */

 /* USER CODE END RTC_Init 0 */

 RTC_TimeTypeDef sTime;

 RTC_DateTypeDef DateToUpdate;

 /* USER CODE BEGIN RTC_Init 1 */

 /* USER CODE END RTC_Init 1 */

   /**Initialize RTC Only

   */

 hrtc.Instance = RTC;

      if(HAL_RTCEx_BKUPRead(&hrtc,RTC_BKP_DR2) != 0x32F2)

   {

 hrtc.Init.AsynchPrediv = RTC_AUTO_1_SECOND;

 hrtc.Init.OutPut = RTC_OUTPUTSOURCE_ALARM;

 if (HAL_RTC_Init(&hrtc) != HAL_OK)

 {

   _Error_Handler(__FILE__, __LINE__);

 }

   /**Initialize RTC and set the Time and Date

   */

 sTime.Hours = 0x01;

 sTime.Minutes = 0x20;

 sTime.Seconds = 0x30;

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

 {

   _Error_Handler(__FILE__, __LINE__);

 }

   DateToUpdate.WeekDay = 0x07;

 DateToUpdate.Month = 0x04;

 DateToUpdate.Date = 0x25;

 DateToUpdate.Year = 0x20;

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

 {

   _Error_Handler(__FILE__, __LINE__);

 }

      HAL_PWR_EnableBkUpAccess();

      HAL_RTCEx_BKUPWrite(&hrtc,RTC_BKP_DR2,0x32F2);

   }

}

1 REPLY 1
TDK
Guru

> I lose the date, but the time is correct

How are you verifying this? Seems the code you linked only sets the time/date if the backup register isn't 0x32F2. And if it is, your program does nothing at all.

If you feel a post has answered your question, please click "Accept as Solution".