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