Table of Contents
Introducing STM32 RTC
The RTC (Real-Time Clock) in STM32 microcontrollers is a highly configurable peripheral designed to keep track of the current time and date, even when the main system is powered down. It is especially useful in low-power applications where accurate timekeeping must be maintained with minimal energy consumption. The RTC can operate independently from the main processor using a dedicated low-speed clock (typically LSE or LSI), and it can retain its state across resets and power cycles using a backup power source such as a coin cell battery connected to the VBAT pin.
The RTC is often used in applications like data logging, alarms, time stamping, clock displays, and wake-up scheduling in low-power modes.
Key Features of STM32 RTC
- Time and Calendar Functions
The RTC provides accurate tracking of time (hours, minutes, seconds) and calendar (date, month, year, day of the week) in either 24-hour or 12-hour format. - Alarm Functionality
Supports one or two independent alarms (Alarm A and Alarm B), which can trigger interrupts or wake up the device from low-power modes at a predefined time. - Tamper and Timestamp Detection
Capable of detecting external tamper events and logging timestamps, useful for applications requiring security or event tracking. - Backup Domain and Battery Operation
The RTC can run independently on a backup battery via the VBAT pin, ensuring continued timekeeping during power loss or system shutdown.
CUBEMX CONFIGURATION
Clock Configuration
Below is the image showing the clock configuration for this project.

I am using the Nucleo F446RE board and it has both, LSE and HSE crystals on board. The LSE is 32.768Khz and HSE is of 8MHz. The LSE crystal is used to provide the clock to the RTC whereas the HSE will be used to clock the rest of the system.
In the RCC configuration, enable the crystal oscillator for both LSE and HSE. The image above shows how the clock is configured such that the RTC gets the 32.768KHz, while the rest of the system runs at 180MHz.
If your controller board does not have this dedicated LSE crystal of 32.768KHz, you can use the Internal RC oscillator (LSI RC) too.
RTC Configuration
Below is the image showing the RTC configuration.

Activate the Clock Source, Calendar and enable at least one of the Alarm.
In the parameter configuration, I have set the 24Hr Time format and Data format is set to binary format. You can set any random values for the time, date and alarm. We will write functions to set up the values for these parameters in the code itself.
The Asynchronous and Synchronous Predivider values are used to calculate the value of the ck_spre. The values should be chosen in a way that the ck_spre value = 1. The formula to calculate ck_spre is shown below.
The RTCCLK is at 32768, so using the PREDIV_A value of 127 and PREDEV_S value of 255 in the above formula results in the ck_spre value =1.
We have also configured the Alarm, so make sure to enable the Alarm interrupt in the NVIC Tab. We will use the LED connected to pin PA5 for the Alarm signal, so let’s set the pin PA5 as output.

1 thought on “Internal RTC in STM32”
Test comment