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

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

Watrt9年前 (2017-12-16)Cortex-M341050
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系列教程2:外部中断(EXIT)

STM32CubeMX系列教程2:外部中断(EXIT)

      这一章我们在前一章GPIO的工程修改。复制GPIO的工程,修改文件夹名。点击STM32F746I.ioc打开STM32cubeMX的工程文件重新配置。PA0管脚重新配置为GPIO_EXIT0模式。 WAKEUP按键已经外部下拉,按下是PA0为高电平。在GPIO配置中配置PA0为上升沿触发。内部既不上拉也不下拉,添加用户标签WAKEUP。在NVIC(嵌套向量中断控制器)中,勾选EXIT Line0 interrupt使能PA0中断。右边两个选项设置抢占优...

STM32CubeMX系列教程5:串行通信(USART)

STM32CubeMX系列教程5:串行通信(USART)

本章以串口为例讲解,HAL 库轮询,中断,DMA 三种编程模型。1.前情回顾       在串行通信中,一个字符一个字符地传输,每个字符一位一位地传输,并且传输一个字符时,总是以“起始位”开始,以“停止位”结束。在进行传输之前,双方一定要使用同一个波特率设置。波特率就是每秒钟传输的数据位数。       常用的两种基本串行通信方式包括同步通信和异步通信。我们通常使用的是异步通信.异步通信规定传输的数据格式由起始位(...

STM32CubeMX系列教程24:STemWim移植

STM32CubeMX系列教程24:STemWim移植

摘要:本章教程带领大家移植StmemWin 5.22到STM32的LDTC接口控制的RGB接口屏幕。(注:本章只针对STM32芯片F7,F4系列带LDTC接口控制的RGB屏幕,对F1系列通过FMC控制的带控制器的屏幕不适用)一、STemWin 简介        emWin是segger公司出一种高效的而图形用户界面,是我们能够摆脱处理器和显示控制器而更专注于GUI的设计。uCGUI是segger公司授权Micrum公司推出的,uCOS操作系统也是这个公司...

STM32CubeMX系列教程25:USB Device

STM32CubeMX系列教程25:USB Device

本章不打算详细讲解USB的协议,本章只是介绍如何通过STM32CubeMX软件生成应用程序。在看本教程之前建议先看ST官方关于USB的培训视频,示例http://www.stmcu.com.cn/videos.html一、USB简介      stm32F746系列芯片有USB_OTG_FS和USB_OTG_HS两种接口,FS为全速,速度12M Bit/s,HS为高速,最高速度为480M Bit/s,此时需要外接USB HS PHY,例如USB3300。H...