[STM32F2] STM32CubeMX-内部RTC功能验证实录

[复制链接]
481|5
 楼主| 漫天星yl 发表于 2022-8-31 15:38 | 显示全部楼层 |阅读模式

cubeMx基础配置


59977630f0fc9c54cf.png

参数配置    24小时格式,默认分频系数,不采用BCD编码方式
31394630f0fe070068.png

 楼主| 漫天星yl 发表于 2022-8-31 15:40 | 显示全部楼层
时钟配置   外部时钟 32.768
91437630f107159323.png
 楼主| 漫天星yl 发表于 2022-8-31 15:42 | 显示全部楼层
生成代码
95309630f10c8f420f.png
 楼主| 漫天星yl 发表于 2022-8-31 15:46 | 显示全部楼层
系统默认初始化添加代码,实现系统重启设备,时间不被初始化,同时初始化首次上电填充时间
99551630f10d914737.png
 楼主| 漫天星yl 发表于 2022-8-31 15:47 | 显示全部楼层
日期读写API


  1. //读年月日时分秒,十进制
  2. void get_date(unsigned int *yy,unsigned int *mm,unsigned int *dd,unsigned int  *hh,unsigned int *nn,unsigned int *ss)
  3. {
  4.         RTC_DateTypeDef sdatestructureget;
  5.   RTC_TimeTypeDef stimestructureget;
  6.         if(HAL_OK!=HAL_RTC_GetTime(&hrtc, &stimestructureget, RTC_FORMAT_BIN))
  7.         {
  8.                 printf("HAL_RTC_GetTime ERR\r\n");
  9.         }
  10.         if(HAL_OK!=HAL_RTC_GetDate(&hrtc, &sdatestructureget, RTC_FORMAT_BIN))
  11.         {
  12.                 printf("HAL_RTC_GetDate ERR\r\n");
  13.         }
  14. *yy=sdatestructureget.Year;   //年
  15. *mm=sdatestructureget.Month;  //月
  16. *dd=sdatestructureget.Date;   //日

  17. *hh=stimestructureget.Hours;    //时
  18. *nn=stimestructureget.Minutes;  //分
  19. *ss=stimestructureget.Seconds;  //秒       
  20. *yy=*yy+2000;
  21. }

  22. //写年月日时分秒,十进制
  23. void set_date(unsigned int  yy,unsigned int  mm,unsigned int  dd,unsigned int   hh,unsigned int  nn,unsigned int  ss)
  24. {
  25.         RTC_TimeTypeDef sTime = {0};
  26.   RTC_DateTypeDef sDate = {0};
  27.        
  28.   sDate.Month   = mm;
  29.   sDate.Date    = dd;
  30.   sDate.Year    = yy;
  31.         sTime.Hours   = hh;
  32.   sTime.Minutes = nn;
  33.   sTime.Seconds = ss;
  34.   sTime.DayLightSaving = RTC_DAYLIGHTSAVING_NONE;
  35.   sTime.StoreOperation = RTC_STOREOPERATION_RESET;
  36.        
  37.   if (HAL_RTC_SetTime(&hrtc, &sTime, RTC_FORMAT_BIN) != HAL_OK)
  38.   {
  39.                 printf("sTime ERR\r\n");
  40.     Error_Handler();
  41.   }
  42.         if (HAL_RTC_SetDate(&hrtc, &sDate, RTC_FORMAT_BIN) != HAL_OK)
  43.   {
  44.                 printf("sDate ERR\r\n");
  45.     Error_Handler();
  46.   }       
  47.        
  48. }
 楼主| 漫天星yl 发表于 2022-8-31 15:48 | 显示全部楼层
一秒输出一次日期,效果如下
38295630f1221e4f59.png

2873630f12278887f.png
您需要登录后才可以回帖 登录 | 注册

本版积分规则

34

主题

350

帖子

0

粉丝
快速回复 在线客服 返回列表 返回顶部