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: Electricity is always dangerous. Proper skill is required to work with electricity. Do work at your own risk. The author will not be responsible for any misuse or harmful act or any mistake you make. The contents of this website are unique and copyright protected. Kindly don’t do any nonsensical act of copying and claiming it as yours. Most of the articles published here are kept open-source to help you. Take the knowledge for free and use it, but if you are interested you can buy the ready resources offered here. If you need any help or guidance feel free to comment below, the author will try to help you. Also, there can be affiliation links in the article. Which will not affect you anyway, but allows the author with some commission. So please don’t take it otherwise. Thanks.

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.

Advertisements

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.

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

Also let me know what you want to get as the next article, comment below!

Read more:


MKDas

Mithun K. Das; B. Sc. in EEE from KUET; Head of R&D @ M's Lab Engineering Solution. "This is my personal blog. I post articles on different subjects related to electronics in the easiest way so that everything becomes easy for all, especially for beginners. If you have any questions, feel free to ask through the contact us page." Thanks.

0 Comments

Leave a Reply

Avatar placeholder

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

%d bloggers like this: