当前位置:首页 > 技术 > Cortex-M3 > 正文内容

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

Watrt8年前 (2017-12-16)Cortex-M336780
1.RTC简介

        实时时钟 (RTC) 是一个独立的 BCD 定时器/计数器。 RTC 提供具有可编程闹钟中断功能的日历时钟 /日历。RTC 还包含具有中断功能的周期性可编程唤醒标志。
        两个 32 位寄存器包含二进码十进数格式 (BCD) 的秒、分钟、小时( 12 或 24 小时制)、星期几、日期、月份和年份。此外,还可提供二进制格式的亚秒值。系统可以自动将月份的天数补偿为 2829(闰年)、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文件前面申明两个结构体变量存储读取的时间和日期数据。

/* 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(&hrtc, &stimestructure, RTC_FORMAT_BIN);
      /* Get the RTC current Date */
      HAL_RTC_GetDate(&hrtc, &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的时间日期。



打赏 支付宝打赏 微信打赏
分享给朋友:

相关文章

STM32CubeMX系列教程10:串行外设接口SPI

STM32CubeMX系列教程10:串行外设接口SPI

摘要:本章介绍SPI总线。以及介绍W25Qxx芯片。1.SPI简介SPI(Serial Peripheral Interface),串行外围设备接口,一种高速的,全双工,同步的通信总线。芯片的管脚上只占用四根线。MISO: 主器件数据输出,从器件数据输入。MOSI:主器件数据输入,从器件数据输出。SCK: 时钟信号,由主设备控制发出。NSS(CS): 从设备选择信号,由主设备控制。当NSS为低电平则选中从器件。    &nbs...

STM32CubeMX系列教程17:SDMMC

STM32CubeMX系列教程17:SDMMC

一、SDMMC简介    MMC:MMC就是MultiMediaCard的缩写,即多媒体卡    SD:SD卡为Secure Digital Memory Card, 即安全数码卡    SDIO:SD Input Output 带有输入输出接口,SDIO是在SD标准上定义了一种外设接口SD种类    SD卡:<=2GB    SDHC卡(SD High Capacity...

STM32CubeMX系列教程20:Nand Flash

STM32CubeMX系列教程20:Nand Flash

一、Nand Flash 简介        Flash 中文名字叫闪存,是一种长寿命的非易失性(断电数据不丢失)的存储器。可以对称为块的存储器单元块进行擦写和再编程,在进行写入操作之前必须先执行擦除。功能性分为两种:NOR Flash:允许随机存取存储器上的任何区域,以编码应用为主,其功能多与运算相关Nand Flash:主要功能是存储资料,适合储存卡之类的大量数据的存储。本章以K9F1G08U0E芯片为例讲解Nand Flash。如下为此芯片的数据手册...

发表评论

访客

看不清,换一张

◎欢迎参与讨论,请在这里发表您的看法和观点。