cancel
Showing results for 
Search instead for 
Did you mean: 

STM32U5 RTC min and sec count to more than 59

APede.1
Associate II

Hi

I am trying to get the RTC to work on the STM32U5 Nucleo board.

But when I read out the time I sometimes get a second and minute value that is more than 59. Any ideas as to how that happens?

I have checked that it is not because I write a higher value to the register.

I use LSI to run the RTC. 

 My init code

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.OutPutRemap = RTC_OUTPUT_REMAP_NONE;

hrtc.Init.OutPutPolarity = RTC_OUTPUT_POLARITY_HIGH;

hrtc.Init.OutPutType = RTC_OUTPUT_TYPE_OPENDRAIN;

hrtc.Init.OutPutPullUp = RTC_OUTPUT_PULLUP_NONE;

if (HAL_RTC_Init(&hrtc) != HAL_OK) {

Error_Handler();

}

struct tm ts;

ts = *localtime(&NewUTCtime);

RTC_TimeTypeDef newTime = { 0 };

RTC_DateTypeDef newDate = { 0 };

newTime.Hours = ts.tm_hour;

newTime.Minutes = ts.tm_min;

newTime.Seconds = ts.tm_sec;

newTime.DayLightSaving = RTC_DAYLIGHTSAVING_NONE;

newTime.StoreOperation = RTC_STOREOPERATION_RESET;

if (HAL_RTC_SetTime(&hrtc, &newTime, RTC_FORMAT_BCD) != HAL_OK) {

Error_Handler();

}

newDate.WeekDay = ts.tm_wday + 1; // 1-7

newDate.Month = ts.tm_mon + 1; // 1-12

newDate.Date = ts.tm_mday; // 1-31

newDate.Year = ts.tm_year - 100; // <99

if (HAL_RTC_SetDate(&hrtc, &newDate, RTC_FORMAT_BCD) != HAL_OK) {

Error_Handler();

}

0693W00000GX2L1QAL.png0693W00000GX2L1QAL.png

3 REPLIES 3
APede.1
Associate II

-

> RTC_FORMAT_BCD

I don't use Cube, but it appears that this requires the individual struct fields to have in BCD format.

Similarly, on the screenshot, if you read out time and use RTC_FORMAT_BCD, individual fields will be BCD, i.e. minutes = 33(decimal) = 0x21 (BCD); seconds = 69 (decimal) = 0x45 (BCD). That matches value read from the RTC->TR register, stored in tmpreg: it's 08:21:45.

JW

Little mistake Jan convert decimal values to hex valus 33 = 0x21 , but to BCD is 33 = 0x33 ... then values for format BCD is value bigger as binary for >9...