u8 RTC_Init(void) { //检查是不是第一次配置时钟 u8 temp=0; RTC_NVIC_Config(); //if(BKP->DR1!=0X5050)//第一次配置 if (BKP_ReadBackupRegister(BKP_DR1) != 0x5050) //从指定的后备寄存器中读出数据:读出了与写入的指定数据不相乎 { /* Enable PWR and BKP clocks */ RCC_APB1PeriphClockCmd(RCC_APB1Periph_PWR | RCC_APB1Periph_BKP, ENABLE); //使能PWR和BKP外设时钟 /* Allow access to BKP Domain */ PWR_BackupAccessCmd(ENABLE); //使能RTC和后备寄存器访问 /* Reset Backup Domain */ BKP_DeInit(); //将外设BKP的全部寄存器重设为缺省值 /* Enable LSE */ RCC_LSEConfig(RCC_LSE_ON); //设置外部低速晶振(LSE),使用外设低速晶振 /* Wait till LSE is ready */ while (RCC_GetFlagStatus(RCC_FLAG_LSERDY) == RESET) //检查指定的RCC标志位设置与否,等待低速晶振就绪 { temp++; delay_ms(10); } if(temp>=250)return 1;//初始化时钟失败,晶振有问题 /* Select LSE as RTC Clock Source */ RCC_RTCCLKConfig(RCC_RTCCLKSource_LSE); //设置RTC时钟(RTCCLK),选择LSE作为RTC时钟 /* Enable RTC Clock */ RCC_RTCCLKCmd(ENABLE); //使能RTC时钟 /* Wait for RTC registers synchronization */ RTC_WaitForSynchro(); //等待最近一次对RTC寄存器的写操作完成 /* Wait until last write operation on RTC registers has finished */ RTC_WaitForLastTask(); //等待最近一次对RTC寄存器的写操作完成 /* Enable the RTC Second */ RTC_ITConfig(RTC_IT_SEC, ENABLE); //使能RTC秒中断 /* Wait until last write operation on RTC registers has finished */ RTC_WaitForLastTask(); //等待最近一次对RTC寄存器的写操作完成 /* Set RTC prescaler: set RTC period to 1sec */ /* RTC period = RTCCLK/RTC_PR = (32.768 KHz)/(32767+1) */ RTC_SetPrescaler(32767); //设置RTC预分频的值 /* Wait until last write operation on RTC registers has finished */ RTC_WaitForLastTask(); //等待最近一次对RTC寄存器的写操作完成 RTC_Set(2009,12,2,10,0,55); //设置时间 BKP_WriteBackupRegister(BKP_DR1, 0X5050); //向指定的后备寄存器中写入用户程序数据 } else//系统继续计时 { /* Check if the Power On Reset flag is set */ if (RCC_GetFlagStatus(RCC_FLAG_PORRST) != RESET) //检查指定的RCC标志位设置与否:POR/PDR复位 { //printf("\rPower On Reset occurred...."); } /* Check if the Pin Reset flag is set */ else if (RCC_GetFlagStatus(RCC_FLAG_PINRST) != RESET) //检查指定的RCC标志位设置与否:管脚复位 { //printf("\rExternal Reset occurred...."); } //printf("\rNo need to configure RTC...."); /* Wait for RTC registers synchronization */ RTC_WaitForSynchro(); //等待最近一次对RTC寄存器的写操作完成 /* Enable the RTC Second */ RTC_ITConfig(RTC_IT_SEC, ENABLE); //使能RTC秒中断 /* Wait until last write operation on RTC registers has finished */ RTC_WaitForLastTask(); //等待最近一次对RTC寄存器的写操作完成 } RTC_Get();//更新时间 /* Clear reset flags */ RCC_ClearFlag(); //清除RCC的复位标志位 return 0; //ok }
void init_set_chk_interval(){ cursor_x=M4_VAL_LCD_POS; cursor_y=1; chk_int = (u8)BKP_ReadBackupRegister(CHECK_INTERVAL_BKP); }