[Overview]
- Each of IO STM32 can be used as external interrupt inputs.
- STM32F1 interrupt controller supports 19 external interrupt / event request:
Line 0 to 15: corresponding to the external IO port input interruption.
Line 16: PVD is connected to the output.
Line 17: Connect to the RTC alarm event.
Line 18: Connect to the USB wake-up event. - Independently each external interrupt trigger arrangement (rising edge, falling edge or double edge triggered), the trigger / block, dedicated status bits.
[External interrupt Universal IO mapping broken]
GPIOx.0 mapped to EXTI0
GPIOx.1 mapped to EXTI1
...
GPIOx.15 mapped to EXTI15
[Interrupt service function]
EXTI0_IRQHandler () EXT line 0
EXTI1_IRQHandler () the EXTI line. 1
EXTI2_IRQHandler () the EXTI line 2
EXTI3_IRQHandler () the EXTI line. 3
EXTI4_IRQHandler () the EXTI line. 4
EXTI9_5_IRQHandler () the EXTI line 5-EXTI line. 9
EXTI15_10_IRQHandler () the EXTI line 10-EXTI line 15
[External interrupt configuration steps]
- IO port initialization input.
GPIO_Init (); - Open IO port multiplexing clock.
RCC_APB2PeriphClockCmd (RCC_APB2Periph_AFIO, ENABLE); - The mapping between IO port and interrupt lines.
void GPIO_EXTILineConfig (); - Initialization line break, set the trigger conditions.
EXTI_Init (); - Configure interrupt packet (NVIC), and interrupts.
NVIC_Init (); - Write interrupt service function.
EXTIx_IRQHandler (); - Clear the interrupt flag
EXTI_ClearITPendingBit ();
[External interrupt example code]
exti.c file
void EXTIX_Init(void)
{
EXTI_InitTypeDef MyEXTIstruct;
NVIC_InitTypeDef MyNVICstruct;
KEY_Init();
RCC_APB2PeriphClockCmd(RCC_APB2Periph_AFIO,ENABLE); //使能复用功能
GPIO_EXTILineConfig(GPIO_PortSourceGPIOE,GPIO_PinSource4); //外部中断引脚选择
MyEXTIstruct.EXTI_Line=EXTI_Line4; //配置中断线
MyEXTIstruct.EXTI_LineCmd=ENABLE; //使能
MyEXTIstruct.EXTI_Mode=EXTI_Mode_Interrupt; //中断
MyEXTIstruct.EXTI_Trigger=EXTI_Trigger_Falling; //下降沿触发
EXTI_Init(&MyEXTIstruct);
MyNVICstruct.NVIC_IRQChannel=EXTI4_IRQn; //选择是哪个中断
MyNVICstruct.NVIC_IRQChannelCmd=ENABLE; //使能
MyNVICstruct.NVIC_IRQChannelPreemptionPriority=2; //配置抢占优先级
MyNVICstruct.NVIC_IRQChannelSubPriority=2; //配置响应优先级
NVIC_Init(&MyNVICstruct);
}
void EXTI4_IRQHandler(void)
{
delay_ms(10); //由于是按键,消抖
if(KEY0==0)
{
LED0=!LED0;
LED1=!LED1;
}
EXTI_ClearITPendingBit(EXTI_Line4); //清空中断标志位
}
main.c file
int main(void)
{
delay_init(); //延时函数初始化
NVIC_PriorityGroupConfig(NVIC_PriorityGroup_2); //设置NVIC中断分组2:2位抢占优先级,2位响应优先级
uart_init(115200); //串口初始化为115200
LED_Init(); //初始化与LED连接的硬件接口
BEEP_Init(); //初始化蜂鸣器端口
KEY_Init(); //初始化与按键连接的硬件接口
EXTIX_Init(); //外部中断初始化
LED0=0; //点亮LED0
while(1)
{
printf("OK\r\n");
delay_ms(1000);
}
}