Skip to main content
Associate III
November 17, 2023
Solved

RTC not rolling over at midnight

Using the RTC on a NUCLEO-G071RB (HAL) I have a program the prints out the time, date and temperature in degrees Centigrade. I left it running overnight and discovered in the morning that the hours had not reset to 00:00 at midnight and neither had the date rolled forward by a day.
I'm not experienced in embedded systems, in fact this is my first attempt at programming any MCU, but I thought this rollover was implemented in the HAL library.
I'm using functions HAL_RTC_SetTime & HAL_RTC_SetDate to initialise the clock to 10 seconds before midnight) and then HAL_RTC_GetTime and HAL_RTC_GetDate before printing to the serial monitor.

I'm not asking to fix my code (attached) but would appreciate help in knowing where to look. Thanks

This topic has been closed for replies.
waclawek.jan
waclawek.janBest answer
Super User
November 17, 2023

Always start debugging with reading out and checking the relevant peripheral registers, here, RTC registers.

You'll find that you have RTC_CR.FMT set, i.e. RTC is in AM/PM (12-hour) mode, as a consequence of using uninitialized local variable as an initialization struct when calling Cube/HAL functions. Here, it's unitialized fields of hrtc in calling HAL_RTC_Init() (it's a weird call, as you've already called it in MX_RTC_Init()).

JW

3 replies

waclawek.jan
waclawek.janBest answer
Super User
November 17, 2023

Always start debugging with reading out and checking the relevant peripheral registers, here, RTC registers.

You'll find that you have RTC_CR.FMT set, i.e. RTC is in AM/PM (12-hour) mode, as a consequence of using uninitialized local variable as an initialization struct when calling Cube/HAL functions. Here, it's unitialized fields of hrtc in calling HAL_RTC_Init() (it's a weird call, as you've already called it in MX_RTC_Init()).

JW

DabboAuthor
Associate III
November 18, 2023

Thanks Jan, that seems to have solved the problem. I initialised my gtime and gdate structs to {0} and now it works.

waclawek.jan
Super User
November 18, 2023

Hi Dabbo,

I was talking about the hrtc struct, defined locally in main().

JW