|
1、感觉选择LSE的时候,程序卡在了
while(RCC_GetFlagStatus(RCC_FLAG_LSERDY) == RESET)
{
}
2、选择LSI的时候,程序到是可以运行,但重起的时候(还没断电),时间就丢了。有没有人用过?
代码如下:
void RTC_Config(void)
{
uint16_t delay;
RTC_DateTypeDef RTC_DateStructure;
/* Enable the PWR clock */
RCC_APB1PeriphClockCmd(RCC_APB1Periph_PWR, ENABLE);
/* Allow access to RTC */
PWR_BackupAccessCmd(ENABLE);
#if defined (RTC_CLOCK_SOURCE_LSI) /* LSI used as RTC source clock*/
/* The RTC Clock may varies due to LSI frequency dispersion */
/* 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);
/* ck_spre(1Hz) = RTCCLK(LSI) /(uwAsynchPrediv + 1)*(uwSynchPrediv + 1)*/
uwSynchPrediv = 0xFF;
uwAsynchPrediv = 0x7F;
#elif defined (RTC_CLOCK_SOURCE_LSE) /* LSE used as RTC source clock */
/* Enable the LSE OSC */
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);
/* ck_spre(1Hz) = RTCCLK(LSE) /(uwAsynchPrediv + 1)*(uwSynchPrediv + 1)*/
uwSynchPrediv = 0xFF;
uwAsynchPrediv = 0x7F;
#else
#error Please select the RTC Clock source inside the main.c file
#endif /* RTC_CLOCK_SOURCE_LSI */
/* Enable the RTC Clock */
RCC_RTCCLKCmd(ENABLE);
/* Wait for RTC APB registers synchronisation */
RTC_WaitForSynchro();
/* Configure the RTC data register and RTC prescaler */
RTC_InitStructure.RTC_AsynchPrediv = uwAsynchPrediv;
RTC_InitStructure.RTC_SynchPrediv = uwSynchPrediv;
RTC_InitStructure.RTC_HourFormat = RTC_HourFormat_24;
RTC_Init(&RTC_InitStructure);
// /* Enable RTC Alarm A Interrupt */
RTC_ITConfig(RTC_IT_ALRA, ENABLE);
//
/* Set the date: Friday January 11th 2013 */
RTC_DateStructure.RTC_Year = 0x17;
RTC_DateStructure.RTC_Month = RTC_Month_September;
RTC_DateStructure.RTC_Date = 0x01;
RTC_DateStructure.RTC_WeekDay = RTC_Weekday_Friday;
RTC_SetDate(RTC_Format_BCD, &RTC_DateStructure);
/* Set the time to 05h 20mn 00s AM */
RTC_TimeStructure.RTC_Hours = 0x12;
RTC_TimeStructure.RTC_Minutes = 0x20;
RTC_TimeStructure.RTC_Seconds = 0x00;
RTC_SetTime(RTC_Format_BCD, &RTC_TimeStructure);
/* Indicator for the RTC configuration */
RTC_WriteBackupRegister(RTC_BKP_DR0, 0x32F2);
}
void RTC_CalendarConfig(void)
{
RTC_GetTime(RTC_Format_BIN, &RTC_TimeStructure);
if (RTC_ReadBackupRegister(RTC_BKP_DR0) != 0x32F2)
{
/* RTC configuration */
RTC_Config();
/* Display the RTC Time and Alarm */
RTC_TimeShow();
}
else
{
RTC_TimeShow();
/* Check if the Power On Reset flag is set */
if (RCC_GetFlagStatus(RCC_FLAG_PORRST) != RESET)
{
/* Power On Reset occurred */
}
/* Check if the Pin Reset flag is set */
else if (RCC_GetFlagStatus(RCC_FLAG_PINRST) != RESET)
{
/* External Reset occurred */
}
/* Enable the PWR clock */
RCC_APB1PeriphClockCmd(RCC_APB1Periph_PWR, ENABLE);
/* Allow access to RTC */
PWR_BackupAccessCmd(ENABLE);
/* Wait for RTC APB registers synchronisation */
RTC_WaitForSynchro();
/* Clear the RTC Alarm Flag */
RTC_ClearFlag(RTC_FLAG_ALRAF);
/* Display the RTC Time and Alarm */
RTC_TimeShow();
}
}
void RTC_TimeShow(void)
{
/* Get the current Time */
RTC_GetTime(RTC_Format_BIN, &RTC_TimeStructure);
} |
|