//RTC初始化 //返回值:0,初始化成功; // 1,LSE开启失败; // 2,进入初始化模式失败; u8 RTC_Init(void) { u16 retry=0X1FFF; RCC->APB1ENR|=1<<28; //使能电源接口时钟 PWR->CR|=1<<8; //后备区域访问使能(RTC+SRAM) if(RTC_Read_BKR(0)!=0X5050) //是否第一次配置? { RCC->BDCR|=1<<0; //LSE 开启 while(retry&&((RCC->BDCR&0X02)==0))//等待LSE准备好 { retry--; delay_ms(5); } if(retry==0)return 1; //LSE 开启失败. RCC->BDCR|=1<<8; //选择LSE,作为RTC的时钟 RCC->BDCR|=1<<15; //使能RTC时钟 //关闭RTC寄存器写保护 RTC->WPR=0xCA; RTC->WPR=0x53; if(RTC_Init_Mode())return 2;//进入RTC初始化模式 RTC->PRER=0XFF; //RTC同步分频系数(0~7FFF),必须先设置同步分频,再设置异步分频,Frtc=Fclks/((Sprec+1)*(Asprec+1)) RTC->PRER|=0X7F<<16; //RTC异步分频系数(1~0X7F) RTC->CR&=~(1<<6); //RTC设置为,24小时格式 RTC->ISR&=~(1<<7); //退出RTC初始化模式 RTC->WPR=0xFF; //使能RTC寄存器写保护 RTC_Set_Time(23,59,56,0); //设置时间 RTC_Set_Date(14,5,5,1); //设置日期 //RTC_Set_AlarmA(7,0,0,10); //设置闹钟时间 RTC_Write_BKR(0,0X5050); //标记已经初始化过了 } //RTC_Set_WakeUp(4,0); //配置WAKE UP中断,1秒钟中断一次 return 0; }
/*********************************************************************** module : [机器设置操作] function : [设置时间] return : [0:成功 其它:失败] comment : [全局普通函数] machine : [EH-0818] language : [CHN] keyword : [MAC_SET] date : [11/07/21] author : [chen-zhengkai] ************************************************************************/ int setDate() { typ_DATE tdate; const char returnStr[] = "按任意键返回"; const char cueStr[] = "请输入日期:"; char dBuf[40] = {0}; int ret = 0; int year = 0; int month = 0; int day = 0; int day_max = 0; char str1[20] = {0}; char str2[20] = {0}; char str3[20] = {0}; RTC_Get_Date(&tdate); year = tdate.year; month = tdate.month; day = tdate.day; strcpy(str1,"年:"); strcpy(str2,"月:"); strcpy(str3,"日:"); while (1) { // sprintf(dBuf,"%04d/%02d/%02d",year,month,day); // DispStr_CE(0,0,dBuf,DISP_CENTER|DISP_CLRSCR); DispStr_CE(0,0,cueStr,DISP_CENTER|DISP_CLRSCR); DispStr_CE(0,4,str1,DISP_POSITION); DispStr_CE(0,8,str2,DISP_POSITION); DispStr_CE(0,12,str3,DISP_POSITION); ret = input_date_time(4,4,4,1980,2100,str1,&year); if (ret == -1) { return ret; } ret = input_date_time(8,2,1,1,12,str2,&month); if (ret == -1) { continue; } //判断每个月的天数 switch (month) { case 1: case 3: case 5: case 7: case 8: case 10: case 12: day_max = 31; break; case 4: case 6: case 9: case 11: day_max = 30; break; case 2: if ( ( ((year%4)==0)&&(year%100!=0) ) ||( (year%400)==0 ) ) { day_max = 29; } else { day_max = 28; } break; default: return -1; } ret = input_date_time(12,2,1,1,day_max,str3,&day); if (ret != -1) { tdate.year = year; tdate.month = month; tdate.day = day; tdate.week = RTC_get_week(year,month,day); RTC_Set_Date(&tdate); sprintf(dBuf,"%04d/%02d/%02d",year,month,day); DispStr_CE(0,6,dBuf,DISP_CENTER|DISP_CLRSCR); DispStr_CE(0,8,returnStr,DISP_CENTER); delay_and_wait_key( 3, EXIT_KEY_ALL, 0 ); return 0; } } }