void RTC_Configuration(void) { //EXTI_InitTypeDef EXTI_InitStructure; //NVIC_InitTypeDef NVIC_InitStructure; RTC_InitTypeDef RTC_InitStructure; / Enable PWR and BKP clocks / RCC_APB1PeriphClockCmd(RCC_APB1Periph_PWR, ENABLE); / Reset RTC Backup Domain / RCC_RTCResetCmd(ENABLE); RCC_RTCResetCmd(DISABLE); / Allow access to rtc / PWR_RTCAccessCmd(ENABLE); / Enable LSE / RCC_LSEConfig(RCC_LSE_ON); / Wait till LSE is ready / while (RCC_GetFlagStatus(RCC_FLAG_LSERDY) == RESET) {} / Select LSE as RTC Clock Source / RCC_RTCCLKConfig(RCC_RTCCLKSource_LSE); / Enable RTC Clock / RCC_RTCCLKCmd(ENABLE); / Wait for RTC registers synchronization / RTC_WaitForSynchro(); / Configure the RTC data register and RTC prescaler / RTC_InitStructure.RTC_AsynchPrediv = 0x7F; RTC_InitStructure.RTC_SynchPrediv = 0xFF; RTC_InitStructure.RTC_HourFormat = RTC_HourFormat_24; RTC_Init(&RTC_InitStructure); Set_DateTime(); } u8 RtcInit(void) { //检查是不是第一次配置时钟 u8 flag = 0; if (RTC_ReadBackupRegister(RTC_BKP_DR0) != 0xA55A) //从指定的后备寄存器中读出数据:读出了与写入的指定数据不相乎 { / RTC configuration / RTC_Configuration(); if(flag == 0){ RTC_WriteBackupRegister(RTC_BKP_DR0, 0xA55A);}} else { / Enable the PWR clock / RCC_APB1PeriphClockCmd(RCC_APB1Periph_PWR, ENABLE); / Allow access to RTC / PWR_RTCAccessCmd(ENABLE); / Wait for RTC APB registers synchronisation / RTC_WaitForSynchro(); } RTC_IRQConfig();return 0; //ok }
|