AnsweredAssumed Answered

Using the RTC

Question asked by goverdovskiy.andrey on Mar 20, 2015
Latest reply on Nov 20, 2017 by ERRAOUI Abdelhakim
I'm having issues making the RTC work as expected.

This is the code which is called periodically:

RTC_TimeTypeDef currentTime;
HAL_RTC_GetTime(&hrtc, &currentTime, FORMAT_BIN);
  
char uartMsg[32] = "Time: ";
char time[16] = {0};
sprintf(time, "%d:%d:%d %d\n", currentTime.Hours, currentTime.Minutes, currentTime.Seconds, currentTime.SubSeconds);
strcat(uartMsg, time);
HAL_UART_Transmit(&huart1, (uint8_t *)uartMsg, strlen(uartMsg), 10000);

The output is always something like this:

Time: 0:0:0 29
Time: 0:0:0 27
Time: 0:0:0 25
Time: 0:0:0 24

With hours, minutes and seconds staying at 0, while sub-seconds oscillate periodically between 0 and 255.

RTC initialization code generated by CubeMX:

void MX_RTC_Init(void)
{
  RTC_TimeTypeDef sTime;
  RTC_DateTypeDef sDate;
  RTC_AlarmTypeDef sAlarm;
 
  /* Initialize RTC and set the Time and Date */
  hrtc.Instance = RTC;
  hrtc.Init.HourFormat = RTC_HOURFORMAT_24;
  hrtc.Init.AsynchPrediv = 127;
  hrtc.Init.SynchPrediv = 255;
  hrtc.Init.OutPut = RTC_OUTPUT_DISABLE;
  hrtc.Init.OutPutPolarity = RTC_OUTPUT_POLARITY_HIGH;
  hrtc.Init.OutPutType = RTC_OUTPUT_TYPE_OPENDRAIN;
  HAL_RTC_Init(&hrtc);
 
  sTime.Hours = 0;
  sTime.Minutes = 0;
  sTime.Seconds = 0;
  sTime.SubSeconds = 0;
  sTime.TimeFormat = RTC_HOURFORMAT12_AM;
  sTime.DayLightSaving = RTC_DAYLIGHTSAVING_NONE;
  sTime.StoreOperation = RTC_STOREOPERATION_RESET;
  HAL_RTC_SetTime(&hrtc, &sTime, FORMAT_BIN);
 
  sDate.WeekDay = RTC_WEEKDAY_MONDAY;
  sDate.Month = RTC_MONTH_JANUARY;
  sDate.Date = 1;
  sDate.Year = 0;
  HAL_RTC_SetDate(&hrtc, &sDate, FORMAT_BIN);
 
  /* Enable the Alarm A */
  sAlarm.AlarmTime.Hours = 0;
  sAlarm.AlarmTime.Minutes = 0;
  sAlarm.AlarmTime.Seconds = 0;
  sAlarm.AlarmTime.SubSeconds = 0;
  sAlarm.AlarmTime.TimeFormat = RTC_HOURFORMAT12_AM;
  sAlarm.AlarmTime.DayLightSaving = RTC_DAYLIGHTSAVING_NONE;
  sAlarm.AlarmTime.StoreOperation = RTC_STOREOPERATION_RESET;
  sAlarm.AlarmMask = RTC_ALARMMASK_NONE;
  sAlarm.AlarmSubSecondMask = RTC_ALARMSUBSECONDMASK_ALL;
  sAlarm.AlarmDateWeekDaySel = RTC_ALARMDATEWEEKDAYSEL_DATE;
  sAlarm.AlarmDateWeekDay = 1;
  sAlarm.Alarm = RTC_ALARM_A;
  HAL_RTC_SetAlarm(&hrtc, &sAlarm, FORMAT_BIN);
}

With different values ofAsynchPredivandSynchPrediv, hours, minutes and seconds are set to different initial values (not all 0's as by default), but they never change with time not matter what I try.

I'm using STM32F051 and RTC is clocked by 40kHz LSI.

What's the problem?

Outcomes