//=========================================================================
//void RTC_Configuration(void)
//=========================================================================
void STM32_RTC_Configuration(void)
{
/* Enable PWR and BKP clocks */
//RCC_APB1PeriphClockCmd(RCC_APB1Periph_PWR | RCC_APB1Periph_BKP, ENABLE);
STM32_Rcc_Regs->apb1enr.all |=(RCC_PWREN| RCC_BKPEN);
/* Allow access to BKP Domain */
//PWR_BackupAccessCmd(ENABLE);
STM32_Pwr_Regs->cr.bit.DBP=1; //DBP:取消后备区域的写保护
/* Reset Backup Domain */
//BKP_DeInit();
STM32_Rcc_Regs->bdcr.bit.BDRST=1;//1:复位整个备份域
STM32_Rcc_Regs->bdcr.bit.BDRST=1;//1:复位整个备份域
STM32_Rcc_Regs->bdcr.bit.BDRST=0;
STM32_Rcc_Regs->bdcr.bit.BDRST=0;
/* Enable LSE */
//RCC_LSEConfig(RCC_LSE_ON);
STM32_Rcc_Regs->bdcr.bit.LSEON=0; // Reset LSEON bit 0:外部32kHz振荡器关闭
STM32_Rcc_Regs->bdcr.bit.LSEBYP=0; // Reset LSEBYP bit 0:LSE时钟未被旁路
STM32_Rcc_Regs->bdcr.bit.LSERDY=0; // Reset LSERDY bit 0:外部32kHz振荡器未就绪
STM32_Rcc_Regs->bdcr.bit.LSEON=1; // 外部低速振荡器使能
/* Wait till LSE is ready */
//while (RCC_GetFlagStatus(RCC_FLAG_LSERDY) == RESET){}
while( !(STM32_Rcc_Regs->bdcr.bit.LSERDY ) );//由硬件置1来指示外部时钟已经稳定。
/* Select LSE as RTC Clock Source */
//RCC_RTCCLKConfig(RCC_RTCCLKSource_LSE);
STM32_Rcc_Regs->bdcr.bit.RTCSEL=1; // RCC_RTCCLK_LSE
/* Enable RTC Clock */
//RCC_RTCCLKCmd(ENABLE);
STM32_Rcc_Regs->bdcr.bit.RTCEN=1; // RTC时钟使能
/* Wait for RTC registers synchronization */
//RTC_WaitForSynchro();
STM32_Rtc_Regs->crl.bit.RSF=0;// 待RTC_CRL 寄存器中的RSF 位(寄存器同步标志)被硬件置1。
while( !(STM32_Rtc_Regs->crl.bit.RSF ) );
/* Wait until last write operation on RTC registers has finished */
//RTC_WaitForLastTask();
// 软件来查询在前一次写操作结束来判断RTC 寄存器是否处于更新中。仅当RTOFF 状态位是“1"时,RTC 寄存器可以写入新的值。
while( !(TM32_Rtc_Regs->crl.bit.RTOFF ) );
/* Enable the RTC Second */
//RTC_ITConfig(RTC_IT_SEC, ENABLE);
STM32_Rtc_Regs->crh.bit.SECIE=1; //允许秒中断
/* Wait until last write operation on RTC registers has finished */
//RTC_WaitForLastTask();
while( !(STM32_Rtc_Regs->crl.bit.RTOFF ) );//等待写操作结束
/* Set RTC prescaler: set RTC period to 1sec */
//RTC_SetPrescaler(32767); /* RTC period = RTCCLK/RTC_PR = (32.768 KHz)/(32767+1) */
STM32_Rtc_Regs->crl.bit.CNF=1;// RTC进入配置模式
STM32_Rtc_Regs->prlh.all=((u32)32767 & ((u32)0x000F0000))>>16; // 32767 的bit16-19
STM32_Rtc_Regs->prll.all=(u32)32767 & ((u32)0x0000FFFF); // 32767 的bit0-15
STM32_Rtc_Regs->crl.bit.CNF=0;// RTC退出配置模式
/* Wait until last write operation on RTC registers has finished */
//RTC_WaitForLastTask();
while( !(STM32_Rtc_Regs->crl.bit.RTOFF ) );//等待写操作结束
}
文章评论(0条评论)
登录后参与讨论