STM32CubeMX-内部RTC功能验证实录

    技术2022-07-17  80

    1.cubeMx基础配置

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

     

    3. 时钟配置   外部时钟 32.768

    4.生成代码

    5.系统默认初始化添加代码,实现系统重启设备,时间不被初始化,同时初始化首次上电填充时间

    6.日期读写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(); } }

    7.一秒输出一次日期,效果如下

     

     

    Processed: 0.014, SQL: 9