void setTime() { RTC_DateTypeDef sDate; RTC_TimeTypeDef sTime; sDate.WeekDay = 4; sDate.Month = 6; sDate.Date = 8; sDate.Year = 23; HAL_RTC_SetDate(&hrtc, &sDate, RTC_FORMAT_BIN); sTime.Hours = 14; sTime.Minutes = 25; sTime.Seconds = 5; HAL_RTC_SetTime(&hrtc, &sTime, RTC_FORMAT_BIN); HAL_RTCEx_BKUPWrite(&hrtc, RTC_BKP_DR1, RTC_BKP_DATA); } void showTime() { uint16_t Rtctime = 0; char showtime[100]; RTC_DateTypeDef sDateStructure; RTC_TimeTypeDef sTimeStructure; while(1) { HAL_RTC_GetTime(&hrtc, &sTimeStructure, RTC_FORMAT_BIN); HAL_RTC_GetDate(&hrtc, &sDateStructure, RTC_FORMAT_BIN); if(Rtctime!= sTimeStructure.Seconds) { printf("Date = : year:%0.2d - month:%0.2d - day:%0.2d - weekday:%0.2d",sDateStructure.Year, sDateStructure.Month, sDateStructure.Date, sDateStructure.WeekDay); sprintf(showtime,"the Date = : year:20%0.2d - month:%0.2d - day:%0.2d - weekday:%0.2d",sDateStructure.Year, sDateStructure.Month, sDateStructure.Date, sDateStructure.WeekDay); printf("Arr Date = : year:%0.2d - month:%0.2d - day:%0.2d - weekday:%0.2d",showtime[0],showtime[1],showtime[2],showtime[3],showtime[4]); printf("The Time : %0.2d:%0.2d:%0.2d \r\n\r\n", sTimeStructure.Hours, sTimeStructure.Minutes, sTimeStructure.Seconds); sprintf(showtime,"The Time : %0.2d:%0.2d:%0.2d ", sTimeStructure.Hours, sTimeStructure.Minutes, sTimeStructure.Seconds); printf("Arr The Time : %0.2d:%0.2d:%0.2d \r\n\r\n",showtime[0],showtime[1],showtime[2],showtime[3],showtime[4]); } Rtctime = sTimeStructure.Seconds; } } void RTC_INT() { if(HAL_RTCEx_BKUPRead(&hrtc, RTC_BKP_DR1)!=RTC_BKP_DATA ) { setTime(); } else { //�????????????????????????查电源是否复�???????????????????????? if(__HAL_RCC_GET_FLAG(RCC_FLAG_PORRST)!=RESET) { printf("\r\n-----------电源发生复位------------\r\n"); } else if(__HAL_RCC_GET_FLAG(RCC_FLAG_PINRST)!= RESET) { printf("\r\n---------------发生外部复位-------------\r\n"); } printf("\r\n------正常不需要重新配�????????????????????????--------\r\n"); //使能pwr时钟 __HAL_RCC_RTC_ENABLE(); //PWR_cr:DBF�????????????????????????1使能rtc,rtc备份寄存器和备份SRAM的访�???????????????????????? HAL_PWR_EnableBkUpAccess(); //等待RTC APB 同步 HAL_RTC_WaitForSynchro(&hrtc); } }
int main()
{
RTC_INT()
showTime();
}
标签:HAL,RTC,showtime,sTimeStructure,0.2,sDateStructure,STM32CUBEIDE From: https://www.cnblogs.com/mokongking/p/17483475.html