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\r\n",2000 + sdatestructure.Year, sdatestructure.Month, sdatestructure.Date);
14       /* Display time Format : hh:mm:ss */
15       printf("%02d:%02d:%02d\r\n",stimestructure.Hours, stimestructure.Minutes, stimestructure.Seconds);
16  
17       printf("\r\n");
18       HAL_Delay(1000);
19 }
20 /* USER CODE END 3 */
 
        程序中使用HAL_RTC_GetTime(),HAL_RTC_GetDate()讀取時間和日期,並保存到結構體變量中,然后通過串口輸出讀取的時間和日期。注意:要先讀取時間再讀取日期,如果先讀取日期在讀取時間會導致讀取的時間不准確,一直都是原來設置的時間。
        編譯程序並下載到開發板。打開串口調試助手。設置波特率為115200。串口助手上會顯示RTC的時間日期。
 

 
 

 

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM