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:

Advertisements

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.

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.

Learn how to use the Real-time Clock (RTC) of STM32

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:

Loading

Read more:


MKDas

Mithun K. Das. B.Sc. in Electrical and Electronic Engineering (EEE) from KUET. Senior Embedded Systems Designer at a leading international company. Welcome to my personal blog! I share articles on various electronics topics, breaking them down into simple and easy-to-understand explanations, especially for beginners. My goal is to make learning electronics accessible and enjoyable for everyone. If you have any questions or need further assistance, feel free to reach out through the Contact Us page. Thank you for visiting, and happy learning!

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

Leave a Reply

Avatar placeholder

Your email address will not be published. Required fields are marked *