Internal RTC in STM32

Most of the STM32 devices have RTC (Real Time Clock) built in which can keep the track of the current time and date. RTC can be used for chronometers, alarm clocks, watches, small electronic agendas, and many other devices and today we are going to learn HOW to access internal RTC in STM32.
I will show you how to set up the time, Getting the time and displaying it on the LCD and also How to use the alarm feature. So let’s begin the Process…

Open CubeMx and follow me —->

The clock configuration is as follows


Next go to configuration tab and click on RTC

We are going to set time and date in the code itself so just set random here

Generate the project and open it.

Some Insight into the CODE

We are going to set the time and date in the program itself. So first let’s write a function to set the time and date


void set_time (void) 
  RTC_TimeTypeDef sTime; 
  RTC_DateTypeDef sDate; 
  sTime.Hours = 0x10; // set hours 
  sTime.Minutes = 0x20; // set minutes 
  sTime.Seconds = 0x30; // set seconds 
  sTime.DayLightSaving = RTC_DAYLIGHTSAVING_NONE; 
  sTime.StoreOperation = RTC_STOREOPERATION_RESET; 
  if (HAL_RTC_SetTime(&hrtc, &sTime, RTC_FORMAT_BCD) != HAL_OK) 
    _Error_Handler(__FILE__, __LINE__); 
  sDate.WeekDay = RTC_WEEKDAY_THURSDAY; // 
  sDate.Month = RTC_MONTH_AUGUST; // 
  sDate.Date = 0x9; // date 
  sDate.Year = 0x18; // year 
  if (HAL_RTC_SetDate(&hrtc, &sDate, RTC_FORMAT_BCD) != HAL_OK) 
    _Error_Handler(__FILE__, __LINE__); 
  HAL_RTCEx_BKUPWrite(&hrtc, RTC_BKP_DR1, 0x32F2); // backup register 

The time should be entered in a HEX format. The time above is 10:20:30 and the date is 09-Aug-2018.

Also note that

HAL_RTCEx_BKUPWrite(&hrtc, RTC_BKP_DR1, 0x32F2);

is a backup register and is used to store time and date. It’s use is explained in the video attached in the last tab.

Now let’s write a function to read the time and date


void get_time(void) 
 RTC_DateTypeDef gDate; 
 RTC_TimeTypeDef gTime; 
/* Get the RTC current Time */ 
 HAL_RTC_GetTime(&hrtc, &gTime, RTC_FORMAT_BIN); 
/* Get the RTC current Date */ 
 HAL_RTC_GetDate(&hrtc, &gDate, RTC_FORMAT_BIN); 
/* Display time Format: hh:mm:ss */ 
 sprintf((char*)time,"%02d:%02d:%02d",gTime.Hours, gTime.Minutes, gTime.Seconds); 
/* Display date Format: dd-mm-yy */ 
 sprintf((char*)date,"%02d-%02d-%2d",gDate.Date, gDate.Month, 2000 + gDate.Year); 

The above function is self explanatory. sprintf is used to convert the values into the char so that we can display it on the LCD

display_time on LCD

void display_time (void) 
 lcd_send_cmd (0x80); // send cursor to 0,0 
 lcd_send_string (time); 
 lcd_send_cmd (0xc0); // send cursor to 1,0 
 lcd_send_string (date); 


The following function is used to set ALARM and we will also write another function to be called when this alarm get triggered.

void set_alarm (void) 
  sAlarm.AlarmTime.Hours = 0x10; // hours 
  sAlarm.AlarmTime.Minutes = 0x31; // min 
  sAlarm.AlarmTime.Seconds = 0x00; //seconds 
  sAlarm.AlarmTime.SubSeconds = 0x0; 
  sAlarm.AlarmTime.DayLightSaving = RTC_DAYLIGHTSAVING_NONE; 
  sAlarm.AlarmTime.StoreOperation = RTC_STOREOPERATION_RESET; 
  sAlarm.AlarmMask = RTC_ALARMMASK_NONE; 
  sAlarm.AlarmSubSecondMask = RTC_ALARMSUBSECONDMASK_ALL; 
  sAlarm.AlarmDateWeekDay = 0x9; // DATE 
  sAlarm.Alarm = RTC_ALARM_A; 
  if (HAL_RTC_SetAlarm_IT(&hrtc, &sAlarm, RTC_FORMAT_BCD) != HAL_OK) 
    _Error_Handler(__FILE__, __LINE__); 

ALARM Event Callback Function

void HAL_RTC_AlarmAEventCallback(RTC_HandleTypeDef *hrtc) 
  HAL_GPIO_WritePin (GPIOA, GPIO_PIN_5, 1); // turn on the LED 

This function is called when the alarm gets triggered and it will turn ON the LED at PA5.



Check out the VIDEO Below

. . . . . DOWNLOAD . . . . .

You can buy me a coffee Sensor by clicking DONATE OR Just click DOWNLOAD to download the code

Notify of
Oldest Most Voted
Inline Feedbacks
View all comments