STM32 RTC_Calendar(G0 Series)

RTC introduce

Real-Time Clock (RTC) is an independent BCD timer/counter that provides a calendar clock/calendar with programmable alarm interrupt function. The RTC also includes a periodic programmable wake-up flag with interrupt function. Two 32-bit registers contain the seconds, minutes, hours (12 or 24-hour format), day of the week, date, month, and year in Binary-Coded Decimal (BCD) format. In addition, a binary format sub-second value can be provided. The system can automatically compensate for the number of days in a month to be 28, 29 (leap year), 30, or 31 days. As long as the backup power supply of the chip is continuously powered, the time on the RTC will continue to advance.

STM32CubeMX Setting

To ensure accuracy, the Clock source has been changed from LSI to LSE.


“To ensure accuracy, the Clock source has been changed from LSI to LSE.

Here we introduce two important features:

  • Activate Clock Source
  • Activate calendar
  1. RTC calibration clock output: The 1Hz output of the RTC can serve as the clock source for a seconds counter, flashing once per second, like the colon on a digital clock. The 512Hz output can be used as the clock source for a stopwatch, which has a precision of one-hundredth of a second, achieving a quarter of that precision with 512Hz.
  2. RTC reference clock detection: External clock correction can be used, but the clock source must be more accurate than the LSE clock.”

STM32 code

RTC_HAL Lib

/*設置系統時間*/
HAL_StatusTypeDef HAL_RTC_SetTime(RTC_HandleTypeDef *hrtc, RTC_TimeTypeDef *sTime, uint32_t Format) 
/*讀取系統時間*/
HAL_StatusTypeDef HAL_RTC_GetTime(RTC_HandleTypeDef *hrtc, RTC_TimeTypeDef *sTime, uint32_t Format)
/*設置系統日期*/
HAL_StatusTypeDef HAL_RTC_SetDate(RTC_HandleTypeDef *hrtc, RTC_DateTypeDef *sDate, uint32_t Format)
/*讀取系統日期*/
HAL_StatusTypeDef HAL_RTC_GetDate(RTC_HandleTypeDef *hrtc, RTC_DateTypeDef *sDate, uint32_t Format)
/*啟動報警功能*/
HAL_StatusTypeDef HAL_RTC_SetAlarm(RTC_HandleTypeDef *hrtc, RTC_AlarmTypeDef *sAlarm, uint32_t Format)
/*設置報警中斷*/
HAL_StatusTypeDef HAL_RTC_SetAlarm_IT(RTC_HandleTypeDef *hrtc, RTC_AlarmTypeDef *sAlarm, uint32_t Format)
/*報警時間回調函數*/
__weak void HAL_RTC_AlarmAEventCallback(RTC_HandleTypeDef *hrtc)
/*寫入後備儲存器*/
void HAL_RTCEx_BKUPWrite(RTC_HandleTypeDef *hrtc, uint32_t BackupRegister, uint32_t Data)
/*讀取後備儲存器*/
uint32_t HAL_RTCEx_BKUPRead(RTC_HandleTypeDef *hrtc, uint32_t BackupRegister

這邊主要取前面4個來使用讀取出系統時間

  • Set system time:HAL_RTC_SetTime();
  • Get system time: HAL_RTC_GetTime();
  • Set system date: HAL_RTC_SetDate();
  • Get system date: HAL_RTC_GetDate();
static void RTC_CalendarShow(uint8_t *showtime, uint8_t *showdate)
{
  RTC_DateTypeDef sdatestructureget;
  RTC_TimeTypeDef stimestructureget;

  /* Get the RTC current Time */
  HAL_RTC_GetTime(&hrtc, &stimestructureget, RTC_FORMAT_BIN);
  /* Get the RTC current Date */
  HAL_RTC_GetDate(&hrtc, &sdatestructureget, RTC_FORMAT_BIN);
  /* Display time Format : hh:mm:ss */
  sprintf((char *)showtime, "%2d:%2d:%2d", stimestructureget.Hours, stimestructureget.Minutes, stimestructureget.Seconds);
  /* Display date Format : mm-dd-yy */
  sprintf((char *)showdate, "%2d-%2d-%2d", sdatestructureget.Month, sdatestructureget.Date, 2000 + sdatestructureget.Year);
}

Leave a Comment

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

Shopping Cart