蓝桥杯嵌入式RTC实时时钟

772 阅读3分钟

@TOC


前言

本篇文章将给大家介绍RTC实时时钟。

一、RTC是什么

STM32的实时时钟RTC是一个独立的定时器,RTC时钟内部依靠BCD码计数。RTC实时时钟提高时钟、闹钟、日历功能。RTC功耗较低,可以使用在低功耗设备上。 在这里插入图片描述 具体我们了解到RTC时钟有什么功能即可,蓝桥杯比赛中一般只需要提供时钟功能就行了。

二、cubemx的配置

在这里插入图片描述 在这里插入图片描述

三、函数的使用

时间的设置函数:

RTC_TimeTypeDef setTime;//定义一个时间结构体

setTime.Hours = 20;
setTime.Minutes = 20;
setTime.Seconds = 20;
HAL_RTC_SetTime(&hrtc, &setTime, RTC_FORMAT_BIN);

RTC_TimeTypeDef结构体如下: 这个结构体里面存储了时间Hours,Minutes,Seconds,我们在这里只需要设置这几个参数即可,设置完成使用HAL_RTC_SetTime这个函数以二进制的方式存储进去。

typedef struct
{
  uint8_t Hours;            /*!< Specifies the RTC Time Hour.
                                 This parameter must be a number between Min_Data = 0 and Max_Data = 12 if the RTC_HourFormat_12 is selected.
                                 This parameter must be a number between Min_Data = 0 and Max_Data = 23 if the RTC_HourFormat_24 is selected */

  uint8_t Minutes;          /*!< Specifies the RTC Time Minutes.
                                 This parameter must be a number between Min_Data = 0 and Max_Data = 59 */

  uint8_t Seconds;          /*!< Specifies the RTC Time Seconds.
                                 This parameter must be a number between Min_Data = 0 and Max_Data = 59 */

  uint8_t TimeFormat;       /*!< Specifies the RTC AM/PM Time.
                                 This parameter can be a value of @ref RTC_AM_PM_Definitions */

  uint32_t SubSeconds;     /*!< Specifies the RTC_SSR RTC Sub Second register content.
                                 This parameter corresponds to a time unit range between [0-1] Second
                                 with [1 Sec / SecondFraction +1] granularity */

  uint32_t SecondFraction;  /*!< Specifies the range or granularity of Sub Second register content
                                 corresponding to Synchronous pre-scaler factor value (PREDIV_S)
                                 This parameter corresponds to a time unit range between [0-1] Second
                                 with [1 Sec / SecondFraction +1] granularity.
                                 This field will be used only by HAL_RTC_GetTime function */

  uint32_t DayLightSaving;  /*!< Specifies RTC_DayLightSaveOperation: the value of hour adjustment.
                                 This parameter can be a value of @ref RTC_DayLightSaving_Definitions */

  uint32_t StoreOperation;  /*!< Specifies RTC_StoreOperation value to be written in the BKP bit
                                 in CR register to store the operation.
                                 This parameter can be a value of @ref RTC_StoreOperation_Definitions */
} RTC_TimeTypeDef;

读取时间函数:

RTC_TimeTypeDef Time;//用于保存读取到的时间
HAL_RTC_GetTime(&hrtc, &Time, RTC_FORMAT_BIN);

日历设置函数:

RTC_DateTypeDef GetDate;//定义一个日历结构体
setCalendar.Year = 21;
setCalendar.Month = 4;
setCalendar.Date = 15;
HAL_RTC_SetDate(&hrtc, &setCalendar, RTC_FORMAT_BIN);

RTC_DateTypeDef中保存了WeekDay,Month,Date,Year等参数,这里根据需要设置对应参数即可。

typedef struct
{
  uint8_t WeekDay;  /*!< Specifies the RTC Date WeekDay.
                         This parameter can be a value of @ref RTC_WeekDay_Definitions */

  uint8_t Month;    /*!< Specifies the RTC Date Month (in BCD format).
                         This parameter can be a value of @ref RTC_Month_Date_Definitions */

  uint8_t Date;     /*!< Specifies the RTC Date.
                         This parameter must be a number between Min_Data = 1 and Max_Data = 31 */

  uint8_t Year;     /*!< Specifies the RTC Date Year.
                         This parameter must be a number between Min_Data = 0 and Max_Data = 99 */
} RTC_DateTypeDef;

日历读取函数:

RTC_DateTypeDef GetDate;//保存读取到的日历参数
HAL_RTC_GetTime(&hrtc, &Time, RTC_FORMAT_BIN);

最后我们将这些读取出来的时间和日历显示到LCD显示屏上:

RTC_TimeTypeDef setTime;
RTC_DateTypeDef setCalendar;

RTC_TimeTypeDef Time;
RTC_DateTypeDef GetDate;
u8 Clock_buf[30];
u8 Calendar_buf[30];

	LCD_Clear(Blue);
	
	setTime.Hours = 20;
	setTime.Minutes = 20;
	setTime.Seconds = 20;
	HAL_RTC_SetTime(&hrtc, &setTime, RTC_FORMAT_BIN);
	
	setCalendar.Year = 21;
	setCalendar.Month = 4;
	setCalendar.Date = 15;
	HAL_RTC_SetDate(&hrtc, &setCalendar, RTC_FORMAT_BIN);
	
  while (1)
  {
    /* USER CODE END WHILE */

    /* USER CODE BEGIN 3 */
		
		HAL_RTC_GetDate(&hrtc, &GetDate, RTC_FORMAT_BIN);
		HAL_RTC_GetTime(&hrtc, &Time, RTC_FORMAT_BIN);
		
		sprintf((char*)Clock_buf, "      %02d-%02d-%02d", Time.Hours, Time.Minutes, Time.Seconds);
		sprintf((char*)Calendar_buf, "      %02d-%02d-%02d", GetDate.Year, GetDate.Month, GetDate.Date);
		LCD_DisplayStringLine(Line5, Clock_buf);
		LCD_DisplayStringLine(Line6, Calendar_buf);
  }

这样就能将时间和日历都显示出来了。

总结

RTC时钟的介绍就到这里了,RTC时钟在比赛中也算是一个经常考的点但是他却不会很难,只是几个函数的基本使用而已,大家多去练习就可以掌握的。