0
\$\begingroup\$

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.

asked Aug 3, 2020 at 11:29
\$\endgroup\$
4
  • 2
    \$\begingroup\$ Do you run that code every time the processor boots? It looks like that sets the date and time... \$\endgroup\$ Commented Aug 3, 2020 at 12:01
  • \$\begingroup\$ What do you think happens when you execute HAL_RTC_SetTime(...) and HAL_RTC_SetDate(...) with both sTime and DateToUpdate both set to all zeroes? \$\endgroup\$ Commented Aug 3, 2020 at 13:47
  • \$\begingroup\$ So what should I write here? \$\endgroup\$ Commented Aug 3, 2020 at 15:22
  • \$\begingroup\$ You haven't answered my question. \$\endgroup\$ Commented Aug 3, 2020 at 23:23

1 Answer 1

2
\$\begingroup\$

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.

answered Aug 3, 2020 at 12:32
\$\endgroup\$
4
  • \$\begingroup\$ Can you tell me exactly where should be changed please? \$\endgroup\$ Commented Aug 3, 2020 at 13:42
  • \$\begingroup\$ You better look at STM32 RTC examples and API documentation. \$\endgroup\$ Commented Aug 3, 2020 at 13:47
  • \$\begingroup\$ So if you know, tell me what should I do? I am confused. \$\endgroup\$ Commented 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\$ Commented Aug 3, 2020 at 16:11

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.