初级会员

- 积分
- 115
- 金钱
- 115
- 注册时间
- 2019-3-12
- 在线时间
- 37 小时
|
5金钱
下面是RTC代码配置,选择的是用内部LSI时钟
void RtcWakeUpConfig(void)
{
NVIC_InitTypeDef NVIC_InitStructure;
EXTI_InitTypeDef EXTI_InitStructure;
RTC_WakeUpCmd(DISABLE);
RTC_ClearFlag(RTC_FLAG_WUTF);
RTC_WakeUpClockConfig(RTC_WakeUpClock_CK_SPRE_16bits); //???????32.768KHz 8??
RTC_SetWakeUpCounter(10); //??500ms
RTC_ClearITPendingBit(RTC_IT_WUT);
EXTI_ClearITPendingBit(EXTI_Line20);//清除LINE22上的中断标志位
RTC_ITConfig(RTC_IT_WUT,ENABLE); //??????,???????
RTC_WakeUpCmd(ENABLE);
/* EXTI configuration *******************************************************/
EXTI_ClearITPendingBit(EXTI_Line20);
EXTI_InitStructure.EXTI_Line = EXTI_Line20;
EXTI_InitStructure.EXTI_Mode = EXTI_Mode_Interrupt;
EXTI_InitStructure.EXTI_Trigger = EXTI_Trigger_Rising;
EXTI_InitStructure.EXTI_LineCmd = ENABLE;
EXTI_Init(&EXTI_InitStructure);
/* Enable the RTC Wakeup Interrupt */
NVIC_InitStructure.NVIC_IRQChannel = RTC_WKUP_IRQn;
NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 0;
NVIC_InitStructure.NVIC_IRQChannelSubPriority = 0;
NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
NVIC_Init(&NVIC_InitStructure);
}
u8 RtcInit(void)
{
//????????????
u8 flag = 0;
if (RTC_ReadBackupRegister(RTC_BKP_DR0) != 0x5050) //??????????????:??????????????
{
/* RTC configuration */
flag = RtcConfig();
if(flag == 0)
{
RTC_WriteBackupRegister(RTC_BKP_DR0, 0x5050);
}
else
return flag;
}
else
{
/* Enable the PWR clock */
RCC_APB1PeriphClockCmd(RCC_APB1Periph_PWR, ENABLE);
/* Allow access to RTC */
PWR_RTCAccessCmd(ENABLE);
/* Wait for RTC APB registers synchronisation */
RTC_WaitForSynchro();
}
return 0; //ok
}
/**
* @brief Configure the RTC peripheral by selecting the clock source.
* @param None
* @retval None
*/
u8 RtcConfig(void)
{
/* Enable the PWR clock */
RCC_APB1PeriphClockCmd(RCC_APB1Periph_PWR, ENABLE);
/* Allow access to RTC */
PWR_RTCAccessCmd(ENABLE);
/* Enable the LSE OSC */
// RCC_LSEConfig(RCC_LSE_ON);
RCC_LSICmd(ENABLE);//开启内部低速晶振LSI
/* Wait till LSE is ready */
u32 temp = 0;
while(RCC_GetFlagStatus(RCC_FLAG_LSIRDY) == RESET)
{
temp++;
delay_ms(10);
if(temp >= 250) return 1;//???????,?????,32768????????1-5S
}
/* Select the RTC Clock Source */
// RCC_RTCCLKConfig(RCC_RTCCLKSource_LSE);
RCC_RTCCLKConfig(RCC_RTCCLKSource_LSI);
/* Enable the RTC Clock */
RCC_RTCCLKCmd(ENABLE);
/* Wait for RTC APB registers synchronisation */
RTC_WaitForSynchro();
/* Configure the RTC data register and RTC prescaler */
RTC_InitTypeDef RTC_InitStructure;
RTC_InitStructure.RTC_AsynchPrediv = 0x63;
RTC_InitStructure.RTC_SynchPrediv = 0x18F;
RTC_InitStructure.RTC_HourFormat = RTC_HourFormat_24;
RTC_Init(&RTC_InitStructure);
return 0;
}
void RTC_WKUP_IRQHandler(void)
{
if(RTC_GetITStatus(RTC_IT_WUT) != RESET)
{
EXTI_ClearITPendingBit(EXTI_Line20);
RTC_ClearITPendingBit(RTC_IT_WUT);
}
}
|
|