cancel
Showing results for 
Search instead for 
Did you mean: 

Problem reading the RTC calendar after re writing it.

Santiago_Deliotte
Associate II

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...

1 ACCEPTED SOLUTION

Accepted Solutions

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

View solution in original post

2 REPLIES 2

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 II

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

Thanks a lot.