设备里面有段代码,关于STM32 RTC做时钟的,计算年月日时分秒RTC初始化如下:
void RTC_Configuration(void)
{
/* Enable PWR and BKP clocks */
RCC_APB1PeriphClockCmd(RCC_APB1Periph_PWR | RCC_APB1Periph_BKP, ENABLE);
/* Allow access to BKP Domain */
PWR_BackupAccessCmd(ENABLE);
if (BKP_ReadBackupRegister(BKP_DR1)!=0xA5A5)
{
/* Reset Backup Domain */
BKP_DeInit();
BKP_WriteBackupRegister(BKP_DR2,2008);
BKP_WriteBackupRegister(BKP_DR3,1);
BKP_WriteBackupRegister(BKP_DR4,1);
BKP_WriteBackupRegister(BKP_DR1, 0xA5A5);
RTC_SetCounter(0x0);
/* 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();
/* Wait until last write operation on RTC registers has finished */
RTC_WaitForLastTask();
RTC_WaitForSynchro();
RTC_WaitForLastTask();
/* Enable the RTC Second */
RTC_ITConfig(RTC_IT_SEC, ENABLE);
/* Wait until last write operation on RTC registers has finished */
RTC_WaitForLastTask();
/* Set RTC prescaler: set RTC period to 1sec */
RTC_SetPrescaler(32767); /* RTC period = RTCCLK/RTC_PR = (32.768 KHz)/(32767+1) */
// RTC_SetPrescaler(16383); /* RTC period = RTCCLK/RTC_PR = (32.768 KHz)/(32767+1) */
/* Wait until last write operation on RTC registers has finished */
RTC_WaitForLastTask();
}
else
{
RTC_WaitForLastTask();
RTC_ITConfig(RTC_IT_SEC, ENABLE);
RTC_WaitForLastTask();
}
CheckForDaysElapsed();
}
有时候会出现上电后,RTC初始化不成功,在else{}里面出不来了,看门狗在RTC后面配置的,因此会表现出来程序跑死。这种情况去掉纽扣电池,上电程序就可以正常。不知道RTC初始话else{}中哪里有问题,哪位大侠给看看。不胜感激 |