void EXTIX_Init(void)
{
EXTI_InitTypeDef EXTI_InitStructure;
NVIC_InitTypeDef NVIC_InitStructure;
RCC_APB2PeriphClockCmd(RCC_APB2Periph_AFIO,ENABLE); //外部中断,需要使能AFIO时钟
KEY_Init(); //初始化按键对应的IO模式 (必不可少)
//GPIOC.5 中断线以及中断初始化配置
GPIO_EXTILineConfig(GPIO_PortSourceGPIOC,GPIO_PinSource5);
EXTI_InitStructure.EXTI_Line=EXTI_Line5;
EXTI_InitStructure.EXTI_Mode=EXTI_Mode_Interrupt;
EXTI_InitStructure.EXTI_Trigger=EXTI_Trigger_Falling;
EXTI_InitStructure.EXTI_LineCmd=ENABLE;
EXTI_Init(&EXTI_InitStructure); //根据EXTI_InitStruct中指定的参数初始化外设EXTI的寄存器
//GPIOC.13 中断线以及中断初始化配置
GPIO_EXTILineConfig(GPIO_PortSourceGPIOC,GPIO_PinSource13);
EXTI_InitStructure.EXTI_Line=EXTI_Line13;
EXTI_InitStructure.EXTI_Mode=EXTI_Mode_Interrupt;
EXTI_InitStructure.EXTI_Trigger=EXTI_Trigger_Falling;
EXTI_InitStructure.EXTI_LineCmd=ENABLE;
EXTI_Init(&EXTI_InitStructure); //根据EXTI_InitStruct中指定的参数初始化外设EXTI的寄存器
//GPIOC.4 中断线以及中断初始化配置
GPIO_EXTILineConfig(GPIO_PortSourceGPIOC,GPIO_PinSource4);
EXTI_InitStructure.EXTI_Line=EXTI_Line4;
EXTI_InitStructure.EXTI_Mode=EXTI_Mode_Interrupt;
EXTI_InitStructure.EXTI_Trigger=EXTI_Trigger_Falling;
EXTI_InitStructure.EXTI_LineCmd=ENABLE;
EXTI_Init(&EXTI_InitStructure); //根据EXTI_InitStruct中指定的参数初始化外设EXTI的寄存器
//GPIOA.0 中断线以及中断初始化配置
GPIO_EXTILineConfig(GPIO_PortSourceGPIOA,GPIO_PinSource0);
EXTI_InitStructure.EXTI_Line=EXTI_Line0;
EXTI_InitStructure.EXTI_Mode=EXTI_Mode_Interrupt;
EXTI_InitStructure.EXTI_Trigger=EXTI_Trigger_Falling;
EXTI_InitStructure.EXTI_LineCmd=ENABLE;
EXTI_Init(&EXTI_InitStructure); //根据EXTI_InitStruct中指定的参数初始化外设EXTI的寄存器
NVIC_InitStructure.NVIC_IRQChannel=EXTI0_IRQn; //使能按键PA0所在的外部中断通道
NVIC_InitStructure.NVIC_IRQChannelSubPriority=0x00; //子优先级0
NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority=0x02; //抢占优先级2
NVIC_InitStructure.NVIC_IRQChannelCmd=ENABLE; //使能中断通道
NVIC_Init(&NVIC_InitStructure); //根据NVIC_InitStruct中指定的参数初始化外设NVIC寄存器
NVIC_InitStructure.NVIC_IRQChannel=EXTI4_IRQn; //使能按键PA4所在的外部中断通道
NVIC_InitStructure.NVIC_IRQChannelSubPriority=0x02; //子优先级0
NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority=0x02; //抢占优先级2
NVIC_InitStructure.NVIC_IRQChannelCmd=ENABLE; //使能外部中断通道
NVIC_Init(&NVIC_InitStructure); //根据NVIC_InitStruct中指定的参数初始化外设NVIC寄存器
NVIC_InitStructure.NVIC_IRQChannel=EXTI9_5_IRQn; //使能按键PC5所在的外部中断通道
NVIC_InitStructure.NVIC_IRQChannelSubPriority=0x03; //子优先级1
NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority=0x02; //抢占优先级2
NVIC_InitStructure.NVIC_IRQChannelCmd=ENABLE; //使能外部中断通道
NVIC_Init(&NVIC_InitStructure); //根据NVIC_InitStruct中指定的参数初始化外设NVIC寄存器
NVIC_InitStructure.NVIC_IRQChannel=EXTI15_10_IRQn; //使能按键PC13所在的外部中断通道
NVIC_InitStructure.NVIC_IRQChannelSubPriority=0x01; //子优先级2
NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority=0x02; //抢占优先级2
NVIC_InitStructure.NVIC_IRQChannelCmd=ENABLE; //使能外部中断通道
NVIC_Init(&NVIC_InitStructure); //根据NVIC_InitStruct中指定的参数初始化外设NVIC寄存器
}
void EXTI0_IRQHandler(void)
{
if(EXTI_GetITStatus(EXTI_Line0)!=RESET) // 检测是否发生了0号线中断
{
delay_ms(10); //消抖
if(WK_UP==0)
{
LED0=!LED0;
LED1=!LED1;
LED2=!LED2;
LED3=!LED3;
delay_ms(10);
}
EXTI_ClearITPendingBit(EXTI_Line0); //清除EXTI0上的中断标志位
}
}
void EXTI4_IRQHandler(void)
{
if(EXTI_GetITStatus(EXTI_Line4)!=RESET) //检测是否发生了4号线中断
{
delay_ms(10); //消抖
if(KEY1==0)
{
LED1=!LED1;
delay_ms(10);
}
EXTI_ClearITPendingBit(EXTI_Line4); //清除EXTI4上的中断标志位
}
}
void EXTI9_5_IRQHandler(void)
{
if(EXTI_GetITStatus(EXTI_Line5)!=RESET) //很重要,不要遗忘
{
delay_ms(10); //消抖
if(KEY2==0)
{
LED0=!LED0;
delay_ms(10);
}
EXTI_ClearITPendingBit(EXTI_Line5); //清除EXTI5线路挂起位
}
}
void EXTI15_10_IRQHandler(void) //误写成这个了EXTI15_10_IRQnHandler()
{
if(EXTI_GetITStatus(EXTI_Line13)!=RESET)
{
delay_ms(10); //消抖
if(KEY0==0)
{
LED2=!LED2;
delay_ms(10);
}
EXTI_ClearITPendingBit(EXTI_Line13); //除EXTI13上的中断标志位
}
}
友情提示: 此问题已得到解决,问题已经关闭,关闭后问题禁止继续编辑,回答。
你理解的很正确
一周热门 更多>