STM32CubeMX 实时时钟(RTC)

1.RTC简介
 
        实时时钟 (RTC) 是一个独立的 BCD 定时器/计数器。 RTC 提供具有可编程闹钟中断功能的日历时钟 /日历。RTC 还包含具有中断功能的周期性可编程唤醒标志。
        两个 32 位寄存器包含二进码十进数格式 (BCD) 的秒、分钟、小时( 12 或 24 小时制)、星期几、日期、月份和年份。此外,还可提供二进制格式的亚秒值。系统可以自动将月份的天数补偿为 28、29(闰年)、30 和 31 天。
        只要芯片的备用电源一直供电,RTC上的时间会一直走。
 
2.新建工程
        本章程序在串口printf工程的基础上修改,复制串口printf的工程,修改文件夹名。点击STM32F746I.ioc打开STM32cubeMX的工程文件重新配置。RTC选择内部唤醒开启RTC。为晶振管脚。
 
 
  
 
 
开启外部低速晶振,PC14,PC15配置
 

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

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

生成报告以及代码,编译程序。
 
 
3.添加应用程序
 
在rtc.c文件中可以看到ADC初始化函数。在stm32f7xx_hal_rtc.h头文件中可以看到rtc时间和日期读写操作函数。
 

从操作函数中可以看到,时间和日期是以结构体的形式读写的。所以在main.c文件前面申明两个结构体变量存储读取的时间和日期数据。
 
1 /* USER CODE BEGIN PV */
2 /* Private variables ---------------------------------------------------------*/
3 RTC_DateTypeDef sdatestructure;
4 RTC_TimeTypeDef stimestructure;
5 /* USER CODE END PV */
在stm32f7xx_hal_rtc.h头文件中,可以找到RTC_TimeTypeDef,RTC_DateTypeDef这两个结构体的成员变量。
 
01 /**
02   * @brief  RTC Time structure definition 
03   */
04 typedef struct
05 {
06   uint8_t Hours;            /*!< Specifies the RTC Time Hour.
07                           This parameter must be a number between Min_Data = 0 and Max_Data = 12                                                                                   
08                            if the RTC_HourFormat_12 is selected.
09                           This parameter must be a number between Min_Data = 0 and Max_Data = 23
10                           if the RTC_HourFormat_24 is selected  */
11   
12   uint8_t Minutes;     /*!< Specifies the RTC Time Minutes.
13                        This parameter must be a number between Min_Data = 0 and Max_Data = 59 */
14     
15   uint8_t Seconds;     /*!< Specifies the RTC Time Seconds.
16                        This parameter must be a number between Min_Data = 0 and Max_Data = 59 */
17     
18   uint32_t SubSeconds;  /*!< Specifies the RTC Time SubSeconds.
19                        This parameter must be a number between Min_Data = 0 and Max_Data = 59 */
20   
21   uint8_t TimeFormat;       /*!< Specifies the RTC AM/PM Time.
22                                  This parameter can be a value of @ref RTC_AM_PM_Definitions */
23     
24   uint32_t DayLightSaving;  /*!< Specifies RTC_DayLightSaveOperation:
25                          the value of hour adjustment.
26                          This parameter can be a value of @ref RTC_DayLightSaving_Definitions */
27     
28   uint32_t StoreOperation;  /*!< Specifies RTC_StoreOperation value to be written in the BCK bit
29                                  in CR register to store the operation.
30                           This parameter can be a value of @ref RTC_StoreOperation_Definitions*/
31 }RTC_TimeTypeDef;
32     
33 /**
34   * @brief  RTC Date structure definition 
35   */
36 typedef struct
37 {
38   uint8_t WeekDay;  /*!< Specifies the RTC Date WeekDay.
39                          This parameter can be a value of @ref RTC_WeekDay_Definitions */
40     
41   uint8_t Month;    /*!< Specifies the RTC Date Month (in BCD format).
42                          This parameter can be a value of @ref RTC_Month_Date_Definitions */
43   
44   uint8_t Date;     /*!< Specifies the RTC Date.
45                         This parameter must be a number between Min_Data = 1 and Max_Data = 31*/
46     
47   uint8_t Year;     /*!< Specifies the RTC Date Year.
48                         This parameter must be a number between Min_Data = 0 and Max_Data = 99*/
49                           
50 }RTC_DateTypeDef;
 
在while循环中添加应用程序,读取当前的时间和日期,并通过串口发送到电脑上显示。
 
01 /* USER CODE BEGIN WHILE */
02 while (1)
03 {
04 /* USER CODE END WHILE */
05  
06 /* USER CODE BEGIN 3 */
07       /* Get the RTC current Time ,must get time first*/
08       HAL_RTC_GetTime(&hrtc, &stimestructure, RTC_FORMAT_BIN);
09       /* Get the RTC current Date */
10       HAL_RTC_GetDate(&hrtc, &sdatestructure, RTC_FORMAT_BIN);
11  
12       /* Display date Format : yy/mm/dd */
13       printf("%02d/%02d/%02d ",2000 + sdatestructure.Year, sdatestructure.Month, sdatestructure.Date);
14       /* Display time Format : hh:mm:ss */
15       printf("%02d:%02d:%02d ",stimestructure.Hours, stimestructure.Minutes, stimestructure.Seconds);
16  
17       printf(" ");
18       HAL_Delay(1000);
19 }
20 /* USER CODE END 3 */
 
        程序中使用HAL_RTC_GetTime(),HAL_RTC_GetDate()读取时间和日期,并保存到结构体变量中,然后通过串口输出读取的时间和日期。注意:要先读取时间再读取日期,如果先读取日期在读取时间会导致读取的时间不准确,一直都是原来设置的时间。
        编译程序并下载到开发板。打开串口调试助手。设置波特率为115200。串口助手上会显示RTC的时间日期。
 

 
 
 
Talk is cheap, show me the code
原文地址:https://www.cnblogs.com/birdBull/p/15351793.html