我是按照STM32F4官方库的例程改的,当时钟源为LSI的时候可以正常的初始化,正常的读时间,但是一旦用了LSE始终之后初始化就会失败,RTC_Init(&RTC_InitStructure)返回值是ERROR,请问一下为什么,哦,对了,配置HSE也是不好使。下面贴源码:
void RTC_Config(void)
{
RTC_TimeTypeDef RTC_TimeStructure;
RTC_InitTypeDef RTC_InitStructure;
RTC_AlarmTypeDef RTC_AlarmStructure;
__IO uint32_t uwAsynchPrediv = 0xFF;
__IO uint32_t uwSynchPrediv = 0x7F;
RTC_DateTypeDef RTC_DateStructure;
/* Enable the PWR clock */
RCC_APB1PeriphClockCmd(RCC_APB1Periph_PWR, ENABLE);
/* Allow access to RTC */
PWR_BackupAccessCmd(ENABLE);
// RCC_LSICmd(ENABLE);
RCC_LSEConfig(RCC_LSE_ON);
/* Wait till LSI is ready */
// while(RCC_GetFlagStatus(RCC_FLAG_LSIRDY) == RESET)
// {
// }
while(RCC_GetFlagStatus(RCC_FLAG_LSERDY) == RESET)
{
}
/* Select the RTC Clock Source */
RCC_RTCCLKConfig(RCC_RTCCLKSource_LSE);
RCC_RTCCLKCmd(ENABLE);
//????????
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);
if(!RTC_Init(&RTC_InitStructure))
{
printf("init failed ");
}
// /* Set the alarm 05h:20min:30s */
// RTC_AlarmStructure.RTC_AlarmTime.RTC_H12 = RTC_H12_AM;
// RTC_AlarmStructure.RTC_AlarmTime.RTC_Hours = 0x00;
// RTC_AlarmStructure.RTC_AlarmTime.RTC_Minutes = 0x00;
// RTC_AlarmStructure.RTC_AlarmTime.RTC_Seconds = 0x00;
// RTC_AlarmStructure.RTC_AlarmDateWeekDay = 0x24;
// RTC_AlarmStructure.RTC_AlarmDateWeekDaySel = RTC_AlarmDateWeekDaySel_Date;
// RTC_AlarmStructure.RTC_AlarmMask = RTC_AlarmMask_DateWeekDay;
//
// /* Configure the RTC Alarm A register */
// RTC_SetAlarm(RTC_Format_BCD, RTC_Alarm_A, &RTC_AlarmStructure);
//
// /* Enable RTC Alarm A Interrupt */
// RTC_ITConfig(RTC_IT_ALRA, ENABLE);
//
// //????????A
// RTC_AlarmCmd(RTC_Alarm_A, ENABLE);
//
// //????????A±ê????
// RTC_ClearFlag(RTC_FLAG_ALRAF);
/* Set the date: Friday January 11th 2013 */
RTC_DateStructure.RTC_Year = 0x15;
RTC_DateStructure.RTC_Month = RTC_Month_September;
RTC_DateStructure.RTC_Date = 0x24;
RTC_DateStructure.RTC_WeekDay = RTC_Weekday_Thursday;
// RTC_SetDate(RTC_Format_BCD, &RTC_DateStructure);
if(!RTC_SetDate(RTC_Format_BCD, &RTC_DateStructure))
{
printf("SetDate error");
}
/* Set the time to 05h 20mn 00s AM */
RTC_TimeStructure.RTC_H12 = RTC_H12_AM;
RTC_TimeStructure.RTC_Hours = 0x23;
RTC_TimeStructure.RTC_Minutes = 0x40;
RTC_TimeStructure.RTC_Seconds = 0x00;
// RTC_SetTime(RTC_Format_BCD, &RTC_TimeStructure);
if(!RTC_SetTime(RTC_Format_BCD, &RTC_TimeStructure))
{
printf("SetTime error");
}
/* Indicator for the RTC configuration */
RTC_WriteBackupRegister(RTC_BKP_DR0, 0x15f3);
}
int main(void)
{
RTC_DateTypeDef RTC_DateTypeInstructure;
RTC_TimeTypeDef RTC_TimeTypeInstructure;
systickinit();
// usart3config();
//
// usart2config();
//
// uart4config();
usart1config();
if(RTC_ReadBackupRegister(RTC_BKP_DR0) != 0x12f3)
{
RTC_Config();
printf("????RTC");
}
else
{
while(1)
{
delay(1000);
RTC_GetDate(RTC_Format_BCD,&RTC_DateTypeInstructure);
RTC_GetTime(RTC_Format_BCD,&RTC_TimeTypeInstructure);
printf("20%-2.2X?ê %X?? %X?? ????%X %X???± %X·? %X??
",RTC_DateTypeInstructure.RTC_Year,
RTC_DateTypeInstructure.RTC_Month,RTC_DateTypeInstructure.RTC_Date,RTC_DateTypeInstructure.RTC_WeekDay,
RTC_TimeTypeInstructure.RTC_Hours,RTC_TimeTypeInstructure.RTC_Minutes,RTC_TimeTypeInstructure.RTC_Seconds);
}
}
while(1)
{
delay(1000);
RTC_GetDate(RTC_Format_BCD,&RTC_DateTypeInstructure);
RTC_GetTime(RTC_Format_BCD,&RTC_TimeTypeInstructure);
printf("20%-2.2X?ê %X?? %X?? ????%X %X???± %X·? %X??
",RTC_DateTypeInstructure.RTC_Year,
RTC_DateTypeInstructure.RTC_Month,RTC_DateTypeInstructure.RTC_Date,RTC_DateTypeInstructure.RTC_WeekDay,
RTC_TimeTypeInstructure.RTC_Hours,RTC_TimeTypeInstructure.RTC_Minutes,RTC_TimeTypeInstructure.RTC_Seconds);
}
}
友情提示: 此问题已得到解决,问题已经关闭,关闭后问题禁止继续编辑,回答。
难道你用f4也存在这个现象吗?
一周热门 更多>