STM32CUBEIDE RTC
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();
}
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· Ollama——大语言模型本地部署的极速利器
· 使用C#创建一个MCP客户端
· 分享一个免费、快速、无限量使用的满血 DeepSeek R1 模型,支持深度思考和联网搜索!
· Windows编程----内核对象竟然如此简单?
· ollama系列1:轻松3步本地部署deepseek,普通电脑可用