问题一,时间读取错误
原因,读写时间必须Time在前,Date在后
HAL_RTC_GetTime(&hrtc, &time, RTC_FORMAT_BCD);
HAL_RTC_GetDate(&hrtc, &date, RTC_FORMAT_BCD);
HAL_RTC_SetTime(&hrtc, &time, RTC_FORMAT_BCD);
HAL_RTC_SetDate(&hrtc, &date, RTC_FORMAT_BCD);
问题二,rtc时间超过24小时且日期不变
原因,time一开始声明时没有初始化={0};并且没有给DayLightSaving和StoreOperation赋值,下面的函数是修改后的读写函数
void PCF8563_Read(void)
{
?? ?RTC_DateTypeDef date;
?? ?RTC_TimeTypeDef time;
?? ?HAL_RTC_GetTime(&hrtc, &time, RTC_FORMAT_BCD);
?? ?HAL_RTC_GetDate(&hrtc, &date, RTC_FORMAT_BCD);
?? ?systemclock.year = date.Year;
?? ?systemclock.month = date.Month;
?? ?systemclock.day = date.Date;
?? ?systemclock.week = date.WeekDay;
?? ?systemclock.hour = time.Hours;
?? ?systemclock.min = time.Minutes;
?? ?systemclock.sec = time.Seconds;
}
void ZPCF8563_Write()
{
?? ?RTC_DateTypeDef date={0};
?? ?RTC_TimeTypeDef time={0};
?? ?date.Year=systemclock.year;
?? ?date.Month=systemclock.month;
?? ?date.Date=systemclock.day;
?? ?date.WeekDay=systemclock.week;
?? ?
?? ?time.Hours=systemclock.hour;
?? ?time.Minutes=systemclock.min;
?? ?time.Seconds=systemclock.sec;
?? ?time.DayLightSaving = RTC_DAYLIGHTSAVING_NONE;
? ? time.StoreOperation = RTC_STOREOPERATION_RESET;
?? ?HAL_RTC_SetTime(&hrtc, &time, RTC_FORMAT_BCD);
?? ?HAL_RTC_SetDate(&hrtc, &date, RTC_FORMAT_BCD);
}