一:触发方式
STM32 的外部中断是通过边沿来触发的,不支持电平触发;
二:外部中断分组
STM32 的每一个GPIO都能配置成一个外部中断触发源,STM32 通过根据引脚的序号不同将众多中断触发源分成不同的组,比如:PA0,PB0,PC0,PD0,PE0,PF0,PG0为第一组,那么依此类推,我们能得出一共有16 组,STM32 规定,每一组中同时只能有一个中断触发源工作,那么,最多工作的也就是16个外部中断。
STM32 分组和对应中断处理函数分配:
管脚 |
中断标志 |
中断处理函数分配 |
PA0~PG0 |
EXTI0 |
EXTI0_IRQHandler |
PA1~PG1 |
EXTI1 |
EXTI1_IRQHandler |
PA2~PG2 |
EXTI2 |
EXTI2_IRQHandler |
PA3~PG3 |
EXTI3 |
EXTI3_IRQHandler |
PA4~PG4 |
EXTI4 |
EXTI4_IRQHandler |
PA5~PG5 |
EXTI5 |
EXTI9_5_IRQHandler |
PA6~PG6 |
EXTI6 |
|
PA7~PG7 |
EXTI7 |
|
PA8~PG8 |
EXTI8 |
|
PA9~PG9 |
EXTI9 |
|
PA10~PG10 |
EXTI10 |
EXTI15_10_IRQHandler |
PA11~PG11 |
EXTI11 |
|
PA12~PG12 |
EXTI12 |
|
PA13~PG13 |
EXTI13 |
|
PA14~PG14 |
EXTI14 |
|
PA15~PG15 |
EXTI15 |
三:外部中断的配置过程
l 配置触发源 -GPIO
触发源为通过GPIO端口输入,所以,要配置GPIO的模式,输入方式,输入方式有以下几种:
1.GPIO_Mode_AIN ,模拟输入(ADC模拟输入,或者低功耗下省电)
2.GPIO_Mode_IN_FLOATING ,浮空输入
3.GPIO_Mode_IPD = 0x28,带下拉输入
4.GPIO_Mode_IPU = 0x48,带上拉输
-----------------------具体配置例子-----------------------
参考STM32的库函数版自带例程 PB5对应的EXTI9_5_IRQHandler,及5号中断线,这个不能乱设,否则不工作
void EXTIX_Init(void)
{
{
GPIO_InitTypeDef GPIO_InitStructure;
EXTI_InitTypeDef EXTI_InitStructure;
NVIC_InitTypeDef NVIC_InitStructure;
EXTI_InitTypeDef EXTI_InitStructure;
NVIC_InitTypeDef NVIC_InitStructure;
GPIO_PinRemapConfig(GPIO_Remap_SWJ_JTAGDisable , ENABLE); //关闭jtag
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOB|RCC_APB2Periph_AFIO,ENABLE);
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_5;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IPU;
GPIO_Init(GPIOB, &GPIO_InitStructure);
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IPU;
GPIO_Init(GPIOB, &GPIO_InitStructure);
GPIO_EXTILineConfig(GPIO_PortSourceGPIOB,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_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);
NVIC_InitStructure.NVIC_IRQChannel = EXTI9_5_IRQn; //使能按键所在的外部中断通道
NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 0x02; //先占优先级4位,共16级
NVIC_InitStructure.NVIC_IRQChannelSubPriority = 0x01; //先占优先级0位,从优先级4位
NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE; //使能外部中断通道
NVIC_Init(&NVIC_InitStructure);
NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 0x02; //先占优先级4位,共16级
NVIC_InitStructure.NVIC_IRQChannelSubPriority = 0x01; //先占优先级0位,从优先级4位
NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE; //使能外部中断通道
NVIC_Init(&NVIC_InitStructure);
}
处理函数,要清除中断标志位,不然一秒M次的中断都可能
void EXTI9_5_IRQHandler(void)
{
delay_ms(10); //消抖,是按键的话这里就需要,否则就不用延迟
printf("delay_ms ");
if (EXTI_GetITStatus(EXTI_Line5) != RESET)
{
{
delay_ms(10); //消抖,是按键的话这里就需要,否则就不用延迟
printf("delay_ms ");
if (EXTI_GetITStatus(EXTI_Line5) != RESET)
{
//自己要实现的动作
printf("EXTI0_IRQHandler ");
}
EXTI_ClearITPendingBit(EXTI_Line5); //清除EXTI13线路挂起位
}
printf("EXTI0_IRQHandler ");
}
EXTI_ClearITPendingBit(EXTI_Line5); //清除EXTI13线路挂起位
}