金牌会员
- 积分
- 1547
- 金钱
- 1547
- 注册时间
- 2014-7-11
- 在线时间
- 256 小时
|
楼主 |
发表于 2024-1-25 16:09:10
|
显示全部楼层
uint8_t rtc_init(void)
{
uint16_t ssr;
uint16_t bkpflag = 0;
uint16_t retry = 200;
uint32_t tempreg = 0;
RCC->APB1ENR|=1<<28; /* ê1ÄüμçÔ′½ó¿úê±Öó */
PWR->CR|=1<<8; /* oó±¸Çøóò·ÃÎêê1Äü(RTC+SRAM) */
bkpflag = rtc_read_bkr(0); /* ¶áè¡BKP0μÄÖμ */
if (bkpflag != 0X5050) /* ֮ǰê1óÃμÄ2»êÇLSE */
{
RCC->CSR |= 1 << 0; /* LSI×üêÇê1Äü */
while (!(RCC->CSR & 0x02)); /* μè′yLSI¾íD÷ */
RCC->BDCR |= 1 << 0; /* 3¢êÔ¿aÆôLSE */
while (retry && ((RCC->BDCR & 0X02) == 0)) /* μè′yLSE×¼±¸oà */
{
retry--;
delay_ms(5);
}
tempreg = RCC->BDCR; /* ¶áè¡BDCRμÄÖμ */
tempreg &= ~(3 << 8); /* Çåáã8/9λ */
if (retry == 0)tempreg |= 1 << 9; /* LSE¿aÆô꧰ü,Æô¶ˉLSI. */
else tempreg |= 1 << 8; /* Ñ¡ÔñLSE,×÷ÎaRTCê±Öó */
tempreg |= 1 << 15; /* ê1ÄüRTCê±Öó */
RCC->BDCR = tempreg; /* ÖØDÂéèÖÃBDCR¼Ä′æÆ÷ */
/* 1رÕRTC¼Ä′æÆ÷D′±£»¤ */
RTC->WPR = 0xCA;
RTC->WPR = 0x53;
RTC->CR = 0;
if (rtc_init_mode())
{
RCC->BDCR = 1 << 16;/* ¸′λBDCR */
delay_ms(10);
RCC->BDCR = 0; /* ½áêø¸′λ */
return 2; /* ½øèëRTC3õê¼»ˉÄ£ê½ê§°ü */
}
RTC->PRER = 0XFF; /* RTCí¬2½·ÖÆμÏμêy(0~7FFF),±ØDëÏèéèÖÃí¬2½·ÖÆμ,ÔùéèÖÃòì2½·ÖÆμ,Frtc=Fclks/((Sprec+1)*(Asprec+1)) */
RTC->PRER |= 0X7F << 16;/* RTCòì2½·ÖÆμÏμêy(1~0X7F) */
RTC->CR &= ~(1 << 6); /* RTCéèÖÃÎa,24D¡ê±¸ñê½ */
RTC->ISR &= ~(1 << 7); /* íË3öRTC3õê¼»ˉÄ£ê½ */
RTC->WPR = 0xFF; /* ê1ÄüRTC¼Ä′æÆ÷D′±£»¤ */
if (bkpflag != 0X5051) /* BKP0μÄÄúèY¼è2»êÇ0X5050,ò22»êÇ0X5051,ËμÃ÷êÇμúò»′ÎÅäÖÃ,DèòaéèÖÃê±¼äèÕÆú. */
{
rtc_set_time(23, 59, 56, 0);/* éèÖÃê±¼ä */
rtc_set_date(20, 1, 13, 7); /* éèÖÃèÕÆú */
//rtc_set_alarma(7,0,0,10); /* éèÖÃÄÖÖóê±¼ä */
}
if (retry == 0)
{
rtc_write_bkr(0, 0X5051); /* ±ê¼ÇòѾ-3õê¼»ˉ1yáË,ê1óÃLSI */
}
else
{
rtc_write_bkr(0, 0X5050); /* ±ê¼ÇòѾ-3õê¼»ˉ1yáË,ê1óÃLSE */
}
}
else
{
retry = 10; /* á¬Dø10′ÎSSRμÄÖμ¶¼Ã»±ä»ˉ,ÔòLSEËàáË. */
ssr = RTC->SSR; /* ¶áè¡3õê¼Öμ */
while (retry) /* ¼ì2assr¼Ä′æÆ÷μĶˉì¬,à′ÅD¶ÏLSEêÇ·ñÕy3£ */
{
delay_ms(10);
if (ssr == RTC->SSR) /* ¶Ô±è */
{
retry--;
}
else
{
break;
}
}
if (retry == 0) /* LSE1òáË,Çå3yÅäÖÃμè′yÏÂ′νøèëÖØDÂéèÖà */
{
rtc_write_bkr(0, 0XFFFF); /* ±ê¼Ç′íÎóμÄÖμ */
RCC->BDCR = 1 << 16; /* ¸′λBDCR */
delay_ms(10);
RCC->BDCR = 0; /* ½áêø¸′λ */
}
}
//rtc_set_wakeup(4,0); /* ÅäÖÃWAKE UPÖD¶Ï,1ÃëÖóÖD¶Ïò»′Î */
return 0;
}
|
|