Most of the STM32 devices have RTC (Real Time Clock) built in which can keep the track of the current time and date. RTC can be used for chronometers, alarm clocks, watches, small electronic agendas, and many other devices and today we are going to learn HOW to access internal RTC in STM32.
I will show you how to set up the time, Getting the time and displaying it on the LCD and also How to use the alarm feature. So let’s begin the Process…
Open CubeMx and follow me —->
The clock configuration is as follows
Next go to configuration tab and click on RTC
Generate the project and open it.
Some Insight into the CODE
We are going to set the time and date in the program itself. So first let’s write a function to set the time and date
The time should be entered in a HEX format. The time above is 10:20:30 and the date is 09-Aug-2018.
Also note that
HAL_RTCEx_BKUPWrite(&hrtc, RTC_BKP_DR1, 0x32F2);
is a backup register and is used to store time and date. It’s use is explained in the video attached in the last tab.
Now let’s write a function to read the time and date
The above function is self explanatory. sprintf is used to convert the values into the char so that we can display it on the LCD
display_time on LCD
The following function is used to set ALARM and we will also write another function to be called when this alarm get triggered.
ALARM Event Callback Function
This function is called when the alarm gets triggered and it will turn ON the LED at PA5.
YOU CAN DOWNLOAD FULL CODE AT THE END OF THIS POST