程序是自己写的,早期用在STM32L152上面,很好:
//****************************************************************************************
//时钟初始化
void RTC_Config(void){
RTC_InitTypeDef RTC_InitStructure;
RTC_TIMER t;
/* Enable the PWR clock */
RCC_APB1PeriphClockCmd(RCC_APB1Periph_PWR, ENABLE);
/* Allow access to RTC */
PWR_BackupAccessCmd(ENABLE);
/* Reset RTC Domain */
// RCC_BackupResetCmd(ENABLE);
//RCC_BackupResetCmd(DISABLE);
/* Enable the LSE OSC */
/* Allow access to RTC */
//PWR_RTCAccessCmd(ENABLE);
/* Reset RTC Domain */
// RCC_RTCResetCmd(ENABLE);
// RCC_RTCResetCmd(DISABLE);
/* Wait for RTC APB registers synchronisation */
RTC_WaitForSynchro();
if(RTC_ID ==RTC_ReadBackupRegister(RTC_BKP_DR0)) return ;///RTC_WaitForSynchro();
// RTC_AlarmConfig();
//**************************************************************
//RCC_RTCResetCmd(ENABLE);
//RTC_WriteProtectionCmd(DISABLE);
//RTC_EnterInitMode();
//RTC_DeInit();
/* LSI used as RTC source clock */
/* The RTC Clock may varies due to LSI frequency dispersion. */
if (LSI_EN){
/* Enable the LSI OSC */
RCC_LSICmd(ENABLE);
/* Wait till LSI is ready */
while(RCC_GetFlagStatus(RCC_FLAG_LSIRDY) == RESET);
/* Select the RTC Clock Source */
RCC_RTCCLKConfig(RCC_RTCCLKSource_LSI);
}else{
/* Enable the LSE OSC */
//RTC_AlarmConfig();
RCC_LSEConfig(RCC_LSE_ON);
/* Wait till LSE is ready */
while(RCC_GetFlagStatus(RCC_FLAG_LSERDY) == RESET);
/* Select the RTC Clock Source */
RCC_RTCCLKConfig(RCC_RTCCLKSource_LSE);//RCC_RTCCLKSource_HSE_Div16
}
/* Wait for RTC APB registers synchronisation */
RTC_WaitForSynchro();
//RCC_RTCCLKConfig(RCC_RTCCLKSource_HSE_Div16);
/* Enable the RTC Clock */
RCC_RTCCLKCmd(ENABLE);
/* 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 the time to 00h 00mn 00s AM */
t.Hour = 8;
t.Min = 0;
t.Sec = 1;
//******************************************************
//日期设定!
t.Mon =5;
t.Day =1;
t.Year =14;
t.Week =GetWeek (t.Year ,t.Mon,t.Day );
//*******************************************************
RTC_WrTimer(t);
//*******************************************************
RTC_WriteBackupRegister(RTC_BKP_DR0, RTC_ID);//放入标志位!
/* Enable the RTC Clock */
//RCC_RTCCLKCmd(ENABLE);
/* Wait for RTC APB registers synchronisation */
RTC_WaitForSynchro();
//**********************************************************
//RTC_ExitInitMode();
//RTC_WriteProtectionCmd(ENABLE);
}
|