打印
[STM32F2]

STM32CubeMX-内部RTC功能验证实录

[复制链接]
160|5
手机看帖
扫描二维码
随时随地手机跟帖
跳转到指定楼层
楼主

cubeMx基础配置




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


使用特权

评论回复
沙发
漫天星yl|  楼主 | 2022-8-31 15:40 | 只看该作者
时钟配置   外部时钟 32.768

使用特权

评论回复
板凳
漫天星yl|  楼主 | 2022-8-31 15:42 | 只看该作者
生成代码

使用特权

评论回复
地板
漫天星yl|  楼主 | 2022-8-31 15:46 | 只看该作者
系统默认初始化添加代码,实现系统重启设备,时间不被初始化,同时初始化首次上电填充时间

使用特权

评论回复
5
漫天星yl|  楼主 | 2022-8-31 15:47 | 只看该作者
日期读写API
 

//读年月日时分秒,十进制
void get_date(unsigned int *yy,unsigned int *mm,unsigned int *dd,unsigned int  *hh,unsigned int *nn,unsigned int *ss)
{
        RTC_DateTypeDef sdatestructureget;
  RTC_TimeTypeDef stimestructureget;
        if(HAL_OK!=HAL_RTC_GetTime(&hrtc, &stimestructureget, RTC_FORMAT_BIN))
        {
                printf("HAL_RTC_GetTime ERR\r\n");
        }
        if(HAL_OK!=HAL_RTC_GetDate(&hrtc, &sdatestructureget, RTC_FORMAT_BIN))
        {
                printf("HAL_RTC_GetDate ERR\r\n");
        }
*yy=sdatestructureget.Year;   //年
*mm=sdatestructureget.Month;  //月
*dd=sdatestructureget.Date;   //日

*hh=stimestructureget.Hours;    //时
*nn=stimestructureget.Minutes;  //分
*ss=stimestructureget.Seconds;  //秒       
*yy=*yy+2000;
}

//写年月日时分秒,十进制
void set_date(unsigned int  yy,unsigned int  mm,unsigned int  dd,unsigned int   hh,unsigned int  nn,unsigned int  ss)
{
        RTC_TimeTypeDef sTime = {0};
  RTC_DateTypeDef sDate = {0};
       
  sDate.Month   = mm;
  sDate.Date    = dd;
  sDate.Year    = yy;
        sTime.Hours   = hh;
  sTime.Minutes = nn;
  sTime.Seconds = ss;
  sTime.DayLightSaving = RTC_DAYLIGHTSAVING_NONE;
  sTime.StoreOperation = RTC_STOREOPERATION_RESET;
       
  if (HAL_RTC_SetTime(&hrtc, &sTime, RTC_FORMAT_BIN) != HAL_OK)
  {
                printf("sTime ERR\r\n");
    Error_Handler();
  }
        if (HAL_RTC_SetDate(&hrtc, &sDate, RTC_FORMAT_BIN) != HAL_OK)
  {
                printf("sDate ERR\r\n");
    Error_Handler();
  }       
       
}

使用特权

评论回复
6
漫天星yl|  楼主 | 2022-8-31 15:48 | 只看该作者
一秒输出一次日期,效果如下



使用特权

评论回复
发新帖 我要提问
您需要登录后才可以回帖 登录 | 注册

本版积分规则

26

主题

311

帖子

0

粉丝