最近在用STM32F407的芯片画的板子,RTC这块功能一直不能实现,问题主要表现:1、有主供电的时候,能够正常计时。断电后只有电池供电,隔一段时间再上电读取RTC时间仍是我程序中初始化设置中的时间。但加断点观察并没有运行初始化设置时间的函数。不知道什么原因?有没有人碰到过啊!
贴程序:
if (RTC_ReadBackupRegister(RTC_BKP_DR0) != 0x32F2) //读RTC时间备份寄存器DR0的值判断是否是第一次配置,以防上电重新配置。如果=0x32F2 表示前面配置过,无需再配置。
{
flag_rtc=RTC_ReadBackupRegister(RTC_BKP_DR0);//wu add 5 4
/* RTC configuration */
RTC_Config();
/* Configure the RTC data register and RTC prescaler */
RTC_InitStructure.RTC_AsynchPrediv = AsynchPrediv;
RTC_InitStructure.RTC_SynchPrediv = SynchPrediv;
RTC_InitStructure.RTC_HourFormat = RTC_HourFormat_24; //时间格式为24H
/* Check on RTC init */
if (RTC_Init(&RTC_InitStructure) == ERROR)
{
// printf("\n\r /!\\***** RTC Prescaler Config failed ********/!\\ \n\r");
}
/* Configure the time register */
RTC_TimeRegulate(); //给寄存器设定初始时间值
/* Backup SRAM ********wu add 5 14*******************************************************/
/* Enable BKPRAM Clock */
RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_BKPSRAM, ENABLE);
/* Write to Backup SRAM with 32-Bit Data */
for (i = 0x0; i < 0x1000; i += 4)
{
*(__IO uint32_t *) (BKPSRAM_BASE + i) = i;
}
/* Check the written Data */
for (i = 0x0; i < 0x1000; i += 4)
{
if ((*(__IO uint32_t *) (BKPSRAM_BASE + i)) != i)
{
errorindex++;
}
}
/* Enable the Backup SRAM low power Regulator to retain it's content in VBAT mode */
PWR_BackupRegulatorCmd(ENABLE);
/* Wait until the Backup SRAM low power Regulator is ready */
while(PWR_GetFlagStatus(PWR_FLAG_BRR) == RESET)
{
}
/*********************************************************/
}
else //不是第一次配置。
{
flag_rtc=RTC_ReadBackupRegister(RTC_BKP_DR0);//wu add 5 4
/* Check if the Power On Reset flag is set */
if (RCC_GetFlagStatus(RCC_FLAG_PORRST) != RESET)
{
}
/* Check if the Pin Reset flag is set */
else if (RCC_GetFlagStatus(RCC_FLAG_PINRST) != RESET)
{
}
/* Enable the PWR clock */
// RCC_APB1PeriphClockCmd(RCC_APB1Periph_PWR, ENABLE);
/* Backup SRAM ***************************************************************/
/* Enable BKPSRAM Clock */
RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_BKPSRAM, ENABLE);
/* Check the written Data */
for (i = 0x0; i < 0x1000; i += 4)
{
if ((*(__IO uint32_t *) (BKPSRAM_BASE + i)) != i)
{
errorindex++;
}
}
/* Allow access to RTC 后备域解锁*/
PWR_BackupAccessCmd(ENABLE);
/* Wait for RTC APB registers synchronisation等待RTC时间寄存器的值与APB时钟同步 */
RTC_WaitForSynchro();
/* Clear the RTC Alarm Flag */
RTC_ClearFlag(RTC_FLAG_ALRAF);
/* Clear the EXTI Line 17 Pending bit (Connected internally to RTC Alarm) */
// EXTI_ClearITPendingBit(EXTI_Line17);
} |