Learn how to use the Real-time Clock (RTC) of STM32 microcontrollers to keep accurate time even when the device is powered off. Check out an example code and explore the RTC peripheral’s various features.
Disclaimer:
Handling electricity carries inherent risks. It’s essential to have the appropriate skills to manage it safely. Proceed at your own risk, as the author disclaims responsibility for any misuse, harm, or errors. All content on this website is unique and copyrighted; please avoid unauthorized copying. While most articles are open-source for your benefit, feel free to use the knowledge provided. If you find our resources helpful, consider purchasing available materials to support our work.
For assistance or guidance, leave a comment below; the author is committed to helping. Some articles may contain affiliate links that support the author with a commission at no additional cost to you. Thank you for your understanding and support.
Real-time clocks (RTCs) are an essential component of many electronic devices that require accurate timekeeping. RTCs are used in applications such as alarms, event scheduling, data logging, and more. STM32 microcontrollers come with built-in RTC peripherals that provide accurate and reliable timekeeping, even when the device is powered off. In this article, we will discuss the RTC peripheral of STM32 microcontrollers and their various features.
Table of Contents
RTC Overview:
The RTC peripheral of STM32 microcontrollers provides a 32-bit counter, which can be used to count seconds, minutes, hours, and days. The counter can be used to keep track of time and date, and it can be configured to generate an interrupt at specific intervals. The RTC can also be configured to generate a wakeup signal to the microcontroller when the device is in low-power mode.
STM32 RTC Configuration:
The RTC peripheral of STM32 microcontrollers can be configured using the STM32CubeIDE software. The STM32CubeIDE provides an RTC configuration tool that allows users to configure various parameters such as time, date, and alarm settings. Users can also configure the RTC to use an external crystal oscillator for better accuracy.
RTC Example Code:
Here is an example code that demonstrates the basic RTC functionality of STM32 microcontrollers. This code uses the STM32CubeIDE and the STM32F4 Discovery board.
#include "main.h" #include "stm32f4xx_hal.h" #include "stdio.h" RTC_HandleTypeDef hrtc; void SystemClock_Config(void); static void MX_GPIO_Init(void); static void MX_RTC_Init(void); int main(void) { HAL_Init(); SystemClock_Config(); MX_GPIO_Init(); MX_RTC_Init(); RTC_TimeTypeDef sTime = {0}; RTC_DateTypeDef sDate = {0}; sTime.Hours = 12; sTime.Minutes = 0; sTime.Seconds = 0; if (HAL_RTC_SetTime(&hrtc, &sTime, RTC_FORMAT_BIN) != HAL_OK) { Error_Handler(); } sDate.WeekDay = RTC_WEEKDAY_FRIDAY; sDate.Month = RTC_MONTH_FEBRUARY; sDate.Date = 5; sDate.Year = 21; if (HAL_RTC_SetDate(&hrtc, &sDate, RTC_FORMAT_BIN) != HAL_OK) { Error_Handler(); } while (1) { RTC_DateTypeDef getDate = {0}; RTC_TimeTypeDef getTime = {0}; if (HAL_RTC_GetTime(&hrtc, &getTime, RTC_FORMAT_BIN) != HAL_OK) { Error_Handler(); } if (HAL_RTC_GetDate(&hrtc, &getDate, RTC_FORMAT_BIN) != HAL_OK) { Error_Handler(); } printf("%02d:%02d:%02d\n", getTime.Hours, getTime.Minutes, getTime.Seconds); printf("%02d/%02d/%02d\n", getDate.Date, getDate.Month, getDate.Year); HAL_Delay(1000); } } ...
Circuits you need for RTC:
To use the RTC, you must use a backup power option with a supercapacitor or battery. You can follow this diagram:
Hope this article will help you. See you soon. Thanks
For Professional Designs or Help:
Read more:
- How to interface micro SD cards with STM32
- I2C communication with STM32
- SPI communication with STM32
- Introducing RTOS with stm32
- Internal Temperature of STM32
2 Comments
MUSA · 16/06/2023 at 7:35 pm
hi
time does not increase when i de-energize the circuit. I’ll be glad, if you help me
MKDas · 18/06/2023 at 6:50 pm
use battery for the RTC