Skip to main content
Santiago_Deliotte
Associate
April 18, 2023
Solved

Problem reading the RTC calendar after re writing it.

  • April 18, 2023
  • 2 replies
  • 1746 views

Hello. I'm trying to make a code to show some menús (configuration and user menús) in a LCD screen, but something goes wrong when I try to write the year, and read it later, from the RTC calendar registers.

My main code begins with the cube MX function that sets a initial value to all the RTC calendar fields:

sTime.Hours = 0x09;
 sTime.Minutes = 0x30;
 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_FRIDAY;
 sDate.Month = RTC_MONTH_MARCH;
 sDate.Date = 0x31;
 sDate.Year = 0x23;
 
 if (HAL_RTC_SetDate(&hrtc, &sDate, RTC_FORMAT_BCD) != HAL_OK)
 {
 Error_Handler();
 }

Later, I can configure the time and date, using some enums to do it more human, in the exit section of a configuration menú:

if (new_exit == 1)
 {
 date_final.Date = day_enum;
 date_final.Month = month_enum;
 date_final.Year = year_enum;
 
 HAL_RTC_SetDate(&hrtc, &date_final, RTC_FORMAT_BIN);
 
 time_final.Hours = hour_enum;
 time_final.Minutes = minute_enum;
 time_final.Seconds = second_enum;
 
 HAL_RTC_SetTime(&hrtc, &time_final, RTC_FORMAT_BIN);
 
 new_entry = 1;
 new_exit = 0;
 }

For example, I can set the date = 31, month = 03, year = 23, and so on...

And finally, in an user menú, I wanted to show the time and date, reading the calendar again, using the following code:

void fm_calendar_get()
{
 __HAL_RTC_WRITEPROTECTION_DISABLE(&hrtc);
 HAL_RTC_WaitForSynchro(&hrtc);
 __HAL_RTC_WRITEPROTECTION_ENABLE(&hrtc);
 
 HAL_RTC_GetTime(&hrtc, &sTime, RTC_FORMAT_BIN);
 HAL_RTC_GetDate(&hrtc, &sDate, RTC_FORMAT_BIN);
}

But when I read the date, the year is always wrong, it's different than the year I selected before.

I think it is because of the BCD representation of the RTC, but I really don't know how to solve it.

I can give the whole code if it's needed, but it is really a mess, and you will need to configure some buttons to recreate the error...

This topic has been closed for replies.
Best answer by waclawek.jan

Initialize the whole struct used to write time and date.

Cube is open source. Single step it in debugger to see, what is happening.

JW

2 replies

waclawek.jan
waclawek.janBest answer
Super User
April 18, 2023

Initialize the whole struct used to write time and date.

Cube is open source. Single step it in debugger to see, what is happening.

JW

Santiago_Deliotte
Associate
April 19, 2023

You are totally right, it's the second time someone show me that web page, and I think it's fantastic.

Thanks a lot.