u8 RTC_Init(void)
{
u8 temp=0;
if (BKP_ReadBackupRegister(BKP_DR1) != 0x5050) //´ÓÖ¸¶¨µÄºó±¸¼Ä´æÆ÷ÖжÁ³öÊý¾Ý:¶Á³öÁËÓëдÈëµÄÖ¸¶¨Êý¾Ý²»Ïàºõ
{ //µ±Ïë´ÓÐÂÐÞ¸Äʱ¼äʱ£¬¿ÉÒÔ½«²»µÈÓÚ¸ÃΪµÈÓÚ£¬¼´¿ÉÐ޸ijɹ¦
//´ò¿ªAPB1×ÜÏßÉϵÄPWR,BKPʱÖÓ
RCC_APB1PeriphClockCmd(RCC_APB1Periph_BKP | RCC_APB1Periph_PWR,ENABLE);
////ʹÄܺ󱸼ĴæÆ÷·ÃÎÊ
PWR_BackupAccessCmd(ENABLE); //PWR_CR¼Ä´æÆ÷µÄDBP=1
BKP_DeInit(); //¸´Î»±¸·ÝÇøÓò
//¿ªÆôÍⲿʱÖÓ
RCC_LSEConfig(RCC_LSE_ON); //ÉèÖÃÍⲿµÍËÙ¾§Õñ(LSE),ʹÓÃÍâÉèµÍËÙ¾§Õñ
while (RCC_GetFlagStatus(RCC_FLAG_LSERDY) == RESET&&temp<250)//&&temp<250 //¼ì²éÖ¸¶¨µÄRCC±ê־λÉèÖÃÓë·ñ,µÈ´ýµÍËÙ¾§Õñ¾ÍÐ÷
{
temp++;
//delay_ms(10);
OSTimeDly(10);
}
if(temp>=250)return 1;//³õʼ»¯Ê±ÖÓʧ°Ü,¾§ÕñÓÐÎÊÌâ
//Ñ¡ÔñʱÖÓ
RCC_RTCCLKConfig(RCC_RTCCLKSource_LSE); //ÉèÖÃRTCʱÖÓ(RTCCLK),Ñ¡ÔñLSE×÷ΪRTCʱÖÓ
//ʹÄÜRTCʱÖÓ
RCC_RTCCLKCmd(ENABLE);
//µÈ´ýRTC¼Ä´æÆ÷ͬ²½
RTC_WaitForSynchro();
// //µÈ´ý×î½üÒ»´Î¶ÔRTC¼Ä´æÆ÷д²Ù×÷Íê³É
RTC_WaitForLastTask();
//ʹÄÜRTCÃëÖжÏ
RTC_ITConfig(RTC_IT_SEC, ENABLE); //´Ë´¦»¹¿ÉÒÔÌí¼ÓÆäËûÖжÏ
//µÈ´ý×î½üÒ»´Î¶ÔRTC¼Ä´æÆ÷µÄд²Ù×÷Íê³É
RTC_WaitForLastTask();
//ÉèÖÃRTCÔ¤·ÖƵµÄÖµ
RTC_SetPrescaler(32767); //1HZ
RTC_WaitForLastTask(); //µÈ´ý×î½üÒ»´Î¶ÔRTC¼Ä´æÆ÷µÄд²Ù×÷Íê³É
RTC_Set(2016,11,21,22,40,0); //ÉèÖÃʱ¼ä
RTC_ExitConfigMode(); //Í˳öÅäÖÃģʽ
BKP_WriteBackupRegister(BKP_DR1, 0X5050); //ÏòÖ¸¶¨µÄºó±¸¼Ä´æÆ÷ÖÐдÈëÓû§³ÌÐòÊý¾Ý
}
else
{
RTC_WaitForSynchro(); //µÈ´ý×î½üÒ»´Î¶ÔRTC¼Ä´æÆ÷µÄд²Ù×÷Íê³É
RTC_ITConfig(RTC_IT_SEC, ENABLE); //ʹÄÜRTCÃëÖжÏ
RTC_WaitForLastTask(); //µÈ´ý×î½üÒ»´Î¶ÔRTC¼Ä´æÆ÷µÄд²Ù×÷Íê³É
}
RTC_NVIC_Config();//RCTÖжϷÖ×éÉèÖÃ
RTC_Get();//¸üÐÂʱ¼ä
return 0; //ok
}
|