经1天折腾,终于实现RTC闹钟中断功能了,前期参考了论坛上各位大神的解决办法,和原子哥的事例,但作为初学者来说有几个最容易犯错的地方:
1.需要开启RTC_ITConfig(RTC_IT_ALR, ENABLE);
2.配置中断向量抢占优先级RTCAlarm_IRQn>RTC_IRQn
void NVIC_Config(void){ NVIC_InitTypeDef NVIC_InitStructure;
NVIC_PriorityGroupConfig(NVIC_PriorityGroup_1);
NVIC_InitStructure.NVIC_IRQChannel = RTC_IRQn; NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 2; NVIC_InitStructure.NVIC_IRQChannelSubPriority = 1; NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE; NVIC_Init(&NVIC_InitStructure);
NVIC_InitStructure.NVIC_IRQChannel = RTCAlarm_IRQn; NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 1; NVIC_InitStructure.NVIC_IRQChannelSubPriority = 2; NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE; NVIC_Init(&NVIC_InitStructure); }
3.在中断处理函数中,必须先清除闹钟中断标志位,再清除秒中断标志位,不然进不了闹钟中断
void RTC_IRQHandler(void)
{
if(RTC_GetFlagStatus(RTC_IT_SEC)!=RESET )
{
TimeDisplay=1;
if(RTC_Alr_1min==0)
{
RTC_SetAlarm(RTC_GetCounter()+5-1); //手册上描述要在比较后的下一次才触发中断,所以要-1
RTC_Alr_1min=1;
}
if(RTC_GetFlagStatus(RTC_IT_ALR)!=RESET )
{
PBout(5)=~PBout(5);
RTC_Alr_1min=0;
RTC_ClearITPendingBit(RTC_IT_ALR); //先清闹钟中断标志位
}
RTC_ClearITPendingBit(RTC_IT_SEC);//再清秒中断标志位
RTC_WaitForLastTask();
}
}
希望对初学者有帮助
一周热门 更多>