I am using stm32f103c8 and External Clock Crystal (LSE). I connected the Vbat with a 1220 coin battery. But when I turn off VDD and turn on the micro again, the time and date are all zero. Why time is reset? Do I have to change the code too?
static void MX_RTC_Init(void)
{
RTC_TimeTypeDef sTime = {0};
RTC_DateTypeDef DateToUpdate = {0};
hrtc.Instance = RTC;
hrtc.Init.AsynchPrediv = RTC_AUTO_1_SECOND;
hrtc.Init.OutPut = RTC_OUTPUTSOURCE_ALARM;
if (HAL_RTC_Init(&hrtc) != HAL_OK)
{
Error_Handler();
}
if (HAL_RTC_SetTime(&hrtc, &sTime, RTC_FORMAT_BIN) != HAL_OK)
{
Error_Handler();
}
if (HAL_RTC_SetDate(&hrtc, &DateToUpdate, RTC_FORMAT_BIN) != HAL_OK)
{
Error_Handler();
}
}
I set the RTC time with interrupt.
1 Answer 1
That code sets time and date every time it is run. You obviously need to change it so that time and date is not set if it is already set and backed up by the battery.
-
\$\begingroup\$ Can you tell me exactly where should be changed please? \$\endgroup\$Mojtaba Miraki– Mojtaba Miraki2020年08月03日 13:42:49 +00:00Commented Aug 3, 2020 at 13:42
-
\$\begingroup\$ You better look at STM32 RTC examples and API documentation. \$\endgroup\$Justme– Justme2020年08月03日 13:47:56 +00:00Commented Aug 3, 2020 at 13:47
-
\$\begingroup\$ So if you know, tell me what should I do? I am confused. \$\endgroup\$Mojtaba Miraki– Mojtaba Miraki2020年08月03日 15:34:28 +00:00Commented Aug 3, 2020 at 15:34
-
\$\begingroup\$ I have never used the battery backup so I would have to figure it out myself too. You asked why the time is reset and if you have to change code, and those questions were easy to answer. Did you look at ST code examples and API documentation? \$\endgroup\$Justme– Justme2020年08月03日 16:11:28 +00:00Commented Aug 3, 2020 at 16:11
HAL_RTC_SetTime(...)
andHAL_RTC_SetDate(...)
with bothsTime
andDateToUpdate
both set to all zeroes? \$\endgroup\$