如图设置,并设置年月日,时分秒
void RTC_CalendarRead(void)
{
RTC_DateTypeDef sdatestructureget;
RTC_TimeTypeDef stimestructureget;
HAL_RTC_GetTime(&hrtc,&stimestructureget,RTC_FORMAT_BIN);
HAL_RTC_GetDate(&hrtc,&sdatestructureget,RTC_FORMAT_BIN);
RTC_Year=sdatestructureget.Year;
RTC_Month=sdatestructureget.Month;
RTC_Day=sdatestructureget.Date;
RTC_Hour=stimestructureget.Hours;
RTC_Min=stimestructureget.Minutes;
RTC_Sec=stimestructureget.Seconds;
}
void RTC_SetTime(uint8_t HH,uint8_t MM,uint8_t SS)
{
RTC_TimeTypeDef sTime = {0};
sTime.Hours = HH;
sTime.Minutes = MM;
sTime.Seconds = SS;
if(HAL_RTC_SetTime(&hrtc,&sTime,RTC_FORMAT_BIN)!=HAL_OK)
{
Error_Handler();
}
}
void RTC_SetDate(uint8_t YY,uint8_t MM,uint8_t DD)
{
RTC_DateTypeDef sDate = {0};
sDate.Month = MM;
sDate.Date = DD;
sDate.Year = YY;
if(HAL_RTC_SetDate(&hrtc,&sDate,RTC_FORMAT_BIN)!=HAL_OK)
{
Error_Handler();
}
}
在主程序中使用上述三个函数
1.设置日期及时间
/* RTC Set */
RTC_SetDate(24,3,18);
RTC_SetTime(23,59,30);
2.读取时间
if(RTC_ref==1)
{
RTC_ref=0;
RTC_CalendarRead();
}
!!!注意!!!
RTC中相关时间及日期设置在RTC.c中有相关提示,在编写过程中可以复制参考其中代码,但是要关注相关变量引用是否正确的问题,相关结构体的编写问题也要注意