|
发表于 2008-8-14 21:06:01
|
显示全部楼层
if(BKP_ReadBackupRegister(BKP_DR1) != 0xA5A5)
{
}
else
{
是否允许访问BKP?
}
这是我的配置,没问题
//判断是否RTC是第一次写入
if(BKP_ReadBackupRegister(BKP_DR1)!=0x5120)
{
//RTC 配置 包括BKP的配置
RTC_Configuration();
//写入固定值
BKP_WriteBackupRegister(BKP_DR1, 0x5120);
BKP_WriteBackupRegister(BKP_DR2,7);
BKP_WriteBackupRegister(BKP_DR3,7);
BKP_WriteBackupRegister(BKP_DR4,2008);
}
//已经有时钟在运行
else
{
// 开启 PWR 和 BKP 时钟
RCC_APB1PeriphClockCmd(RCC_APB1Periph_PWR | RCC_APB1Periph_BKP, ENABLE);
//允许访问后备寄存器
PWR_BackupAccessCmd(ENABLE);
//同步RTC
RTC_WaitForSynchro();
/* Wait until last write operation on RTC registers has finished
This function must be called before any write to RTC registers.*/
RTC_WaitForLastTask();
//开启RTC秒中断
RTC_ITConfig(RTC_IT_SEC, ENABLE);
/* Wait until last write operation on RTC registers has finished */
RTC_WaitForLastTask();
//读出最后一次关机时的日期
Day=BKP_ReadBackupRegister(BKP_DR2);
Month=BKP_ReadBackupRegister(BKP_DR3);
Year=BKP_ReadBackupRegister(BKP_DR4);
}
void RTC_Configuration(void)
{
/* Enable PWR and BKP clocks */
RCC_APB1PeriphClockCmd(RCC_APB1Periph_PWR | RCC_APB1Periph_BKP, ENABLE);
/* 允许访问后备寄存器 */
PWR_BackupAccessCmd(ENABLE);
/* 重置后备寄存器 */
BKP_DeInit();
/* 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);
/* 等待RTC同步 */
RTC_WaitForSynchro();
/* Wait until last write operation on RTC registers has finished
This function must be called before any write to RTC registers.*/
RTC_WaitForLastTask();
/* 允许RTC秒中断 */
RTC_ITConfig(RTC_IT_SEC, ENABLE);
/* Wait until last write operation on RTC registers has finished
This function must be called before any write to RTC registers.*/
RTC_WaitForLastTask();
/* Set RTC prescaler: set RTC period to 1sec */
RTC_SetPrescaler(32767); /* RTC period = RTCCLK/RTC_PR = (32.768 KHz)/(32767+1) */
/* Wait until last write operation on RTC registers has finished */
RTC_WaitForLastTask();
} |
|