外部中断配置的目标是:PA15、PA13、PA0为按键,PA8,PD0为LED,按键进入中断,相应的LED亮灭。
void EXTI_Configuration()
{
EXTI_InitTypeDef EXTI_InitStructure;
NVIC_InitTypeDef NVIC_InitStructure;
//清空中断标志
EXTI_ClearITPendingBit(EXTI_Line13);
EXTI_ClearITPendingBit(EXTI_Line15);
EXTI_ClearITPendingBit(EXTI_Line0);
//(1)管脚配置
LED_Init();
KEY_Init();
//(2)外部中断线配置,选择中断线管脚PA13、PA15、PA0
GPIO_EXTILineConfig(GPIO_PortSourceGPIOA, GPIO_PinSource13);
GPIO_EXTILineConfig(GPIO_PortSourceGPIOA, GPIO_PinSource15);
GPIO_EXTILineConfig(GPIO_PortSourceGPIOA, GPIO_PinSource0);
//(3)对中断线进行中断设置
EXTI_InitStructure.EXTI_Line = EXTI_Line13 | EXTI_Line15 ; //选择中断线路PA13/15
EXTI_InitStructure.EXTI_Mode = EXTI_Mode_Interrupt; //设置为中断请求,非事件请求
EXTI_InitStructure.EXTI_Trigger = EXTI_Trigger_Falling ; //设置中断触发方式为上下降沿触发
EXTI_InitStructure.EXTI_LineCmd = ENABLE; //外部中断使能
EXTI_Init(&EXTI_InitStructure); //初始化中断
EXTI_InitStructure.EXTI_Line = EXTI_Line0; //选择中断线路PA0
EXTI_InitStructure.EXTI_Mode = EXTI_Mode_Interrupt; //设置为中断请求,非事件请求
EXTI_InitStructure.EXTI_Trigger = EXTI_Trigger_Rising ; //设置中断触发方式为上下降沿触发
EXTI_InitStructure.EXTI_LineCmd = ENABLE; //外部中断线使能
EXTI_Init(&EXTI_InitStructure);
//EXTI_GenerateSWInterrupt(EXTI_Line0| EXTI_Line13 | EXTI_Line15 ); //中断线使能、中断结构体初始化、以及设置软中断综合起来才启用了中断
//(4)NVIC配置
NVIC_PriorityGroupConfig(NVIC_PriorityGroup_0); //选择中断分组2
NVIC_InitStructure.NVIC_IRQChannel = EXTI15_10_IRQChannel; //选择中断通道2
NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 0; //抢占式中断优先级设置为0
NVIC_InitStructure.NVIC_IRQChannelSubPriority = 0; //响应式中断优先级设置为0
NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE; //使能中断
NVIC_Init(&NVIC_InitStructure);
NVIC_InitStructure.NVIC_IRQChannel = EXTI0_IRQChannel; //选择中断通道2
NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 0; //抢占式中断优先级设置为0
NVIC_InitStructure.NVIC_IRQChannelSubPriority = 1; //响应式中断优先级设置为0
NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE; //使能中断
NVIC_Init(&NVIC_InitStructure);
}
void LED_Init( )
{
GPIO_InitTypeDef GPIO_InitStructure;
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_8;//选择PA.8
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz; //管脚频率为50MHZ
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP; //模式为推挽输出
GPIO_Init(GPIOA, &GPIO_InitStructure); //初始化GPIOA寄存器
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_2;//选择PX.2
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;//管脚频率为50MHZ
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP;//设置推挽输出
GPIO_Init(GPIOD , &GPIO_InitStructure);//初始化GPIOD寄存器
GPIO_WriteBit(GPIOA,GPIO_Pin_8,Bit_SET);
GPIO_WriteBit(GPIOD,GPIO_Pin_2,Bit_SET);
}
void KEY_Init()
{
/*PA13、PA15管口引脚的配置**下拉输入******************************/
GPIO_InitTypeDef GPIO_InitStructure;
GPIO_InitStructure.GPIO_Pin=GPIO_Pin_13|GPIO_Pin_15;
GPIO_InitStructure.GPIO_Mode=GPIO_Mode_IPU;
GPIO_InitStructure.GPIO_Speed=GPIO_Speed_50MHz;
GPIO_Init(GPIOA, &GPIO_InitStructure);
/*PA0管口引脚的配置**上拉输入************************************/
GPIO_InitStructure.GPIO_Pin=GPIO_Pin_0;
GPIO_InitStructure.GPIO_Mode=GPIO_Mode_IPD;
GPIO_InitStructure.GPIO_Speed=GPIO_Speed_50MHz;
GPIO_Init(GPIOA, &GPIO_InitStructure);
}
void EXTI15_10_IRQHandler(void)
{
delay_ms(10);
if(EXTI_GetITStatus(EXTI_Line15)==RESET)
{
if((GPIO_ReadOutputDataBit(GPIOD,GPIO_Pin_2)==0) )
LED1_Off();
else
LED1_On();
}
else if(EXTI_GetITStatus(EXTI_Line13)==RESET)
{
if((GPIO_ReadOutputDataBit(GPIOA,GPIO_Pin_8)==0) )
LED0_Off();
else
LED0_On();
}
EXTI_ClearITPendingBit(EXTI_Line13);
EXTI_ClearITPendingBit(EXTI_Line15);
}
void EXTI0_IRQHandler(void)
{
delay_ms(10);
if(EXTI_GetITStatus(EXTI_Line0)!=RESET)
{
if((GPIO_ReadOutputDataBit(GPIOA,GPIO_Pin_8)==0) )
LED0_Off();
else
LED0_On();
if((GPIO_ReadOutputDataBit(GPIOD,GPIO_Pin_2)==0) )
LED1_Off();
else
LED1_On();
}
EXTI_ClearITPendingBit(EXTI_Line0);
}
最后中断了,在中断里
文章评论(0条评论)
登录后参与讨论