STM32CubeMX系列教程13:实时时钟(RTC)

来自丢石头百科
Admin讨论 | 贡献2019年11月18日 (一) 16:27的版本 (摘要:本章介绍实时时钟(RTC)。1.RTC简介 实时时钟(RTC)是一个独立的BCD定时器/计数器。RTC提供具有可编程闹钟中断功能的日历时钟/日历。RTC还包含具有中断功能的周期性可编程唤醒标志。 两个32位寄存器包含二 ...)
(差异) ←上一版本 | 最后版本 (差异) | 下一版本→ (差异)


1.RTC简介         实时时钟 (RTC) 是一个独立的 BCD 定时器/计数器。 RTC 提供具有可编程闹钟中断功能的日历时钟 /日历。RTC 还包含具有中断功能的周期性可编程唤醒标志。         两个 32 位寄存器包含二进码十进数格式 (BCD) 的秒、分钟、小时( 12 或 24 小时制)、星期几、日期、月份和年份。此外,还可提供二进制格式的亚秒值。系统可以自动将月份的天数补偿为 28、29(闰年)、30 和 31 天。         只要芯片的备用电源一直供电,RTC上的时间会一直走。


2.新建工程         本章程序在串口printf工程的基础上修改,复制串口printf的工程,修改文件夹名。点击STM32F746I.ioc打开STM32cubeMX的工程文件重新配置。RTC选择内部唤醒开启RTC。为晶振管脚。



   162032tiuk3v2u2gguizvt.png



开启外部低速晶振,PC14,PC15配置



162032iuslzbj01ijieeil.png

162033x1sepsll8nlwz8li.png


RTC时钟选择为外部低速晶振(LSE),频率为32.768。



162033fyr6jb1zb0rr16dy.png


在RTC配置中,设置时间和日期,其他为默认设置。(此处设置时间为2016/04/16 16:25:49)



162033ml9wzg5xeoh5mlde.png


生成报告以及代码,编译程序。



3.添加应用程序 在rtc.c文件中可以看到ADC初始化函数。在stm32f7xx_hal_rtc.h头文件中可以看到rtc时间和日期读写操作函数。




162033r8bxt88igulcqy0q.png


从操作函数中可以看到,时间和日期是以结构体的形式读写的。所以在main.c文件前面申明两个结构体变量存储读取的时间和日期数据。



/* USER CODE BEGIN PV */
/* Private variables ---------------------------------------------------------*/
RTC_DateTypeDef sdatestructure;
RTC_TimeTypeDef stimestructure;
/* USER CODE END PV */

在stm32f7xx_hal_rtc.h头文件中,可以找到RTC_TimeTypeDef,RTC_DateTypeDef这两个结构体的成员变量。



/** 
  * @brief  RTC Time structure definition  
  */
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 */
   
  uint32_t SubSeconds;  /*!< Specifies the RTC Time SubSeconds.
                       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 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 BCK bit 
                                 in CR register to store the operation.
                          This parameter can be a value of @ref RTC_StoreOperation_Definitions*/
}RTC_TimeTypeDef; 
   
/** 
  * @brief  RTC Date structure definition  
  */
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;


在while循环中添加应用程序,读取当前的时间和日期,并通过串口发送到电脑上显示。



  /* USER CODE BEGIN WHILE */
  while (1)
  {
  /* USER CODE END WHILE */
 
  /* USER CODE BEGIN 3 */
        /* Get the RTC current Time ,must get time first*/
        HAL_RTC_GetTime(&amp;hrtc, &amp;stimestructure, RTC_FORMAT_BIN);
        /* Get the RTC current Date */
        HAL_RTC_GetDate(&amp;hrtc, &amp;sdatestructure, RTC_FORMAT_BIN);
 
        /* Display date Format : yy/mm/dd */
        printf("%02d/%02d/%02d\r\n",2000 + sdatestructure.Year, sdatestructure.Month, sdatestructure.Date); 
        /* Display time Format : hh:mm:ss */
        printf("%02d:%02d:%02d\r\n",stimestructure.Hours, stimestructure.Minutes, stimestructure.Seconds);
 
        printf("\r\n");
        HAL_Delay(1000);
  }
  /* USER CODE END 3 */


        程序中使用HAL_RTC_GetTime(),HAL_RTC_GetDate()读取时间和日期,并保存到结构体变量中,然后通过串口输出读取的时间和日期。注意:要先读取时间再读取日期,如果先读取日期在读取时间会导致读取的时间不准确,一直都是原来设置的时间。         编译程序并下载到开发板。打开串口调试助手。设置波特率为115200。串口助手上会显示RTC的时间日期。



162033z7nyz5gwhw5yhcg7.png