作者:阿拉丁神丢
转自:http://blog.sina.com.cn/s/blog_6035432c0100v8b3.html
一.配置中断
void NVIC_Configuration(void)
{
NVIC_InitTypeDef NVIC_InitStructure;
/* Set the Vector Table base location at 0x08004000
NVIC_SetVectorTable(NVIC_VectTab_FLASH, 0x4000); // 1.分配中断向量表
NVIC_PriorityGroupConfig(NVIC_PriorityGroup_2); //设置中断优先级
/* Enable the EXTI1 Interrupt
NVIC_InitStructure.NVIC_IRQChannel = EXTI1_IRQChannel; // 中断通道
NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 0; // 指定抢占式优先级别1
NVIC_InitStructure.NVIC_IRQChannelSubPriority = 0; //次优先级为0
NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE; //通道中断使能
NVIC_Init(&NVIC_InitStructure); //初始化中断
}
注意:
如果外部中断针脚是PA1,PB1,PC1,PD1 那么中断就要用EXTI1。
如果我们配置的外部针脚为PA4,或PB4,或PC4,PD4等,那么采用的外部中断也必须是EXTI4;
二.配置GPIO针脚
void GPIO_Configuration(void)
{
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_1; //选择IO针脚
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING; //配置中断脚,配置为浮空输入
GPIO_Init(GPIOA, &GPIO_InitStructure); //初始化针脚
}
注意:如果的针脚是端口的1号针脚,配置的中断一定是EXTI1.
三.配置EXIT线,使中断线和IO管脚连接在一起
void EXIT1_Configuration(void)
{
EXTI_InitTypeDef EXTI_InitStructure;
//Connect EXTI Line1 to PB.01
GPIO_EXTILineConfig(GPIO_PortSourceGPIOA, GPIO_PinSource1); //将EXTI线1连接到端口GPIOD的第1个针脚上
// Configure Key Button EXTI Line to generate an interrupt on both rising and falling edge
EXTI_InitStructure.EXTI_Line = EXTI_Line1; //注意:如果配置的1号针脚,那么必须是EXTI_Line1
EXTI_InitStructure.EXTI_Mode = EXTI_Mode_Interrupt;
EXTI_InitStructure.EXTI_Trigger = EXTI_Trigger_Rising_Falling; //下降沿和上升沿都触发
EXTI_InitStructure.EXTI_LineCmd = ENABLE;
EXTI_Init(&EXTI_InitStructure); //初始化中断
}
到此中断配置完成,可以写中断处理函数。
void EXTI1_IRQHandler(void)
{
if(EXTI_GetITStatus(EXTI_Line1) != RESET)
{
// Clear the EXTI line 9 pending bit
EXTI_ClearITPendingBit(EXTI_Line1);
_485_Baud_Check() ; //调用处理函数
}
}