Real Time Clock
RTC features and backup domain.
A Real Time Clock (RTC) is a dedicated peripheral inside a microcontroller that tracks calendar time (seconds, minutes, hours, date, month, year) continuously, even when the main CPU is powered down. Unlike general-purpose timers, the RTC is specifically designed for timekeeping with very low power consumption by using a 32.768 kHz crystal oscillator. This frequency is chosen because it is exactly 2 to the power of 15, allowing simple binary division down to a 1 Hz tick that increments the seconds counter. RTC is used in data loggers, event schedulers, battery-operated clocks, and any embedded system that needs awareness of real-world time.
Core Concept of RTC
The RTC peripheral uses a two-stage prescaler to divide the 32.768 kHz crystal frequency down to a 1 Hz tick. The first stage is an asynchronous prescaler with a 7-bit register (PREDIV_A) and the second stage is a synchronous prescaler with a 15-bit register (PREDIV_S). The product of both stages must equal 32768 to achieve exactly 1 Hz. The recommended configuration splits this as PREDIV_A = 127 and PREDIV_S = 255, giving (127+1) x (255+1) = 128 x 256 = 32768. This split is chosen to minimize power consumption, as the asynchronous stage runs at the slower divided rate.
The backup domain is a dedicated power rail connected to the VBAT pin on most STM32 and similar microcontrollers. When the main VDD supply is removed (device powered off or battery removed), the backup domain continues to receive power from a small coin cell or supercapacitor connected to VBAT. The RTC, backup registers, and LSE oscillator all reside in this backup domain. This is why an RTC can continue tracking time through power cycles without requiring a full software reconfiguration on every startup.
The RTC alarm feature allows the system to define a match condition on time, date, or day-of-week. When the real-time count matches the alarm register, a hardware interrupt or event is generated. This can wake the CPU from low-power sleep mode, making it useful for scheduling periodic tasks in battery-powered IoT devices where the CPU should stay asleep as long as possible and only wake at predefined times.
Mathematical Expression
The two-stage prescaler chain defines the sub-second resolution as well as the 1 Hz tick. The sub-second counter (SSR) counts downward from PREDIV_S to 0 for every second. This allows measurement of time with sub-second precision. The sub-second fraction value is calculated as: Sub-second fraction = (PREDIV_S - SSR) / (PREDIV_S + 1). If PREDIV_S = 255 and SSR = 128, then the fraction = (255 - 128) / 256 = 127/256 = approximately 0.496 seconds, meaning the current time is 0.496 seconds past the last whole second.
The total time represented in a timestamp is: T = HH x 3600 + MM x 60 + SS + fraction. This is relevant when logging sensor data with precise timestamps where knowing only whole seconds is insufficient. Many industrial data loggers rely on RTC sub-second resolution for synchronizing measurements from multiple sources.
Practical Understanding
One of the most important practical aspects of RTC is crystal accuracy. A standard 32.768 kHz crystal has a typical accuracy of plus or minus 20 ppm. Over one day (86400 seconds), this translates to an error of 86400 x 20 x 10^-6 = 1.728 seconds per day. For long-duration data logging applications, this drift must be compensated using digital calibration, a hardware feature in most microcontrollers that adds or subtracts pulses from the subsecond clock to correct drift.
The backup domain is write-protected by hardware. Before writing to the RTC or backup registers, the programmer must disable backup domain write protection by setting a specific bit in the power control register (PWR_CR). This prevents accidental writes that could corrupt the time registers due to noise or software bugs. After configuration, write protection should be re-enabled.
Given:
LSE crystal frequency = 32,768 Hz
PREDIV_A (async prescaler) = 127
PREDIV_S (sync prescaler) = 255
Crystal accuracy = 20 ppm
Time period for drift calculation = 1 day = 86,400 seconds
Why this formula applies:
Both prescaler stages divide LSE to achieve 1 Hz tick.
Prescaler product must equal LSE frequency.
Drift per day = total seconds x ppm error x 10^-6.
Formula:
f_tick = f_LSE / ((PREDIV_A + 1) x (PREDIV_S + 1))
Drift per day = 86400 x accuracy_ppm x 1e-6
Substitution:
f_tick = 32768 / ((127 + 1) x (255 + 1))
f_tick = 32768 / (128 x 256)
Drift = 86400 x 20 x 1e-6
Calculation:
128 x 256 = 32768
f_tick = 32768 / 32768 = 1 Hz (exact)
Drift = 86400 x 0.00002 = 1.728 seconds per day
Final Answer:
RTC tick frequency = 1 Hz (correctly configured)
Timekeeping drift = approximately 1.73 seconds per day at 20 ppmExam Tip: RTC uses LSE (32.768 kHz external crystal) or LSI (internal RC, less accurate). The prescaler product must equal 32768 for exact 1 Hz. A common MCQ asks which is more accurate for timekeeping: LSE is correct because LSI drifts with temperature.
- 32.768 kHz was chosen because it equals 2^15, allowing exact binary division down to 1 Hz.
- Two prescaler stages: PREDIV_A (7-bit async) and PREDIV_S (15-bit sync). Their product must equal LSE frequency.
- Backup domain includes RTC counter, calendar, alarms, and backup registers, all retained on VBAT.
- Crystal accuracy of 20 ppm causes approximately 1.73 seconds of drift per day without digital calibration.
- Write protection must be removed before configuring RTC registers and re-enabled after to prevent corruption.
Quick Revision
- RTC tracks real-world time continuously with very low power using 32.768 kHz crystal.
- Prescaler formula: f_tick = f_LSE / ((PREDIV_A + 1) x (PREDIV_S + 1)) must equal 1 Hz.
- Backup domain powered by VBAT keeps RTC running when main supply is off.
- LSE (external crystal) is more accurate than LSI (internal RC) for long-term timekeeping.
- Alarm registers can wake the CPU from sleep at a defined time.
- Drift at 20 ppm = 86400 x 20 x 10^-6 = 1.73 seconds per day.
- Exam trap: forgetting to disable write protection before RTC configuration is a common code bug and exam question.
RTC Module Practice
Test your knowledge on this topic!
Q1.Which oscillator frequency is standard for driving a Real-Time Clock (RTC) module to maintain accurate timekeeping?
Related Articles
Timer Basics
Up/Down counting, pre-scaler, auto-reload.
10 min read
SysTick Timer
System timer for OS heartbeats.
9 min read
PWM Generation
Pulse Width Modulation for motor control, LED dimming.
5 min read
Input Capture
Measuring pulse width and frequency.
4 min read
Output Compare
Generating precise delays and signals.
6 min read