在STM32中中断的使用非常的频繁,其中EXTI 线 0~15:对应外部 IO 口的输入中断。
STM32F4 的 IO 口外部中断函数只有 7 个,分别为:
EXPORT EXTI0_IRQHandler
EXPORT EXTI1_IRQHandler
EXPORT EXTI2_IRQHandler
EXPORT EXTI3_IRQHandler
EXPORT EXTI4_IRQHandler
EXPORT EXTI9_5_IRQHandler
EXPORT EXTI15_10_IRQHandler
中断线 0-4 每个中断线对应一个中断函数,中断线 5-9 共用中断函数 EXTI9_5_IRQHandler,中
断线 10-15 共用中断函数 EXTI15_10_IRQHandler。
配置中断分组nvic.c,也就是设置好中断线与GPIO口的映射关系。
#include #include "stm32f4xx.h" void EXTI_nvic_init(void) { NVIC_InitTypeDef NVIC_InitStructure; NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 0x01;//抢占优先级0 NVIC_InitStructure.NVIC_IRQChannelSubPriority = 0x02;//子优先级2 NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;//使能外部中断通道 NVIC_InitStructure.NVIC_IRQChannel = EXTI15_10_IRQn;//外部中断10-15 NVIC_Init(&NVIC_InitStructure);//配置 NVIC_InitStructure.NVIC_IRQChannel = EXTI4_IRQn;//外部中断4 NVIC_Init(&NVIC_InitStructure);//配置 NVIC_InitStructure.NVIC_IRQChannel = EXTI9_5_IRQn;//外部中断5-9 NVIC_Init(&NVIC_InitStructure);//配置 } static void (*_call_4[5])(void); void EXTI4_Register_Call(unsigned int line,void (*fun)(void)) { if (line & EXTI_Line0) _call_4[0] = fun; if (line & EXTI_Line1) _call_4[1] = fun; if (line & EXTI_Line2) _call_4[2] = fun; if (line & EXTI_Line3) _call_4[3] = fun; if (line & EXTI_Line4) _call_4[4] = fun; } void EXTI4_IRQHandler(void) { if (EXTI_GetITStatus(EXTI_Line0)) { if (_call_4[0] != NULL) _call_4[0] (); EXTI_ClearITPendingBit(EXTI_Line0); } if (EXTI_GetITStatus(EXTI_Line1)) { if (_call_4[1] != NULL)_call_4[1] (); EXTI_ClearITPendingBit(EXTI_Line1); } if (EXTI_GetITStatus(EXTI_Line2)) { if (_call_4[2] != NULL)_call_4[2] (); EXTI_ClearITPendingBit(EXTI_Line2); } if (EXTI_GetITStatus(EXTI_Line3)) { if (_call_4[3] != NULL)_call_4[3] (); EXTI_ClearITPendingBit(EXTI_Line3); } if (EXTI_GetITStatus(EXTI_Line4)) { if (_call_4[4] != NULL)_call_4[4] (); EXTI_ClearITPendingBit(EXTI_Line4); } } static void (*_call_5_9[5])(void); void EXTI9_5_Register_Call(unsigned int line, void (*fun)(void)) { if (line & EXTI_Line5) _call_5_9[0] = fun; if (line & EXTI_Line6) _call_5_9[1] = fun; if (line & EXTI_Line7) _call_5_9[2] = fun; if (line & EXTI_Line8) _call_5_9[3] = fun; if (line & EXTI_Line9) _call_5_9[4] = fun; } void EXTI9_5_IRQHandler(void) { if (EXTI_GetITStatus(EXTI_Line5)) { if (_call_5_9[0] != NULL) _call_5_9[0] (); EXTI_ClearITPendingBit(EXTI_Line5); } if (EXTI_GetITStatus(EXTI_Line6)) { if (_call_5_9[1] != NULL) _call_5_9[1] (); EXTI_ClearITPendingBit(EXTI_Line6); } if (EXTI_GetITStatus(EXTI_Line7)) { if (_call_5_9[2] != NULL) _call_5_9[2] (); EXTI_ClearITPendingBit(EXTI_Line7); } if (EXTI_GetITStatus(EXTI_Line8)) { if (_call_5_9[3] != NULL) _call_5_9[3] (); EXTI_ClearITPendingBit(EXTI_Line8); } if (EXTI_GetITStatus(EXTI_Line9)) { if (_call_5_9[4] != NULL) _call_5_9[4] (); EXTI_ClearITPendingBit(EXTI_Line9); } } static void (*_call[6])(void); void EXTI15_10_Register_Call(unsigned int line, void (*fun)(void)) { if (line & EXTI_Line10) _call[0] = fun; if (line & EXTI_Line11) _call[1] = fun; if (line & EXTI_Line12) _call[2] = fun; if (line & EXTI_Line13) _call[3] = fun; if (line & EXTI_Line14) _call[4] = fun; if (line & EXTI_Line15) _call[5] = fun; } void EXTI15_10_IRQHandler(void) { if (EXTI_GetITStatus(EXTI_Line10)) { if (_call[0] != NULL) _call[0] (); EXTI_ClearITPendingBit(EXTI_Line10); } if (EXTI_GetITStatus(EXTI_Line11)) { if (_call[1] != NULL) _call[1] (); EXTI_ClearITPendingBit(EXTI_Line11); } if (EXTI_GetITStatus(EXTI_Line12)) { if (_call[2] != NULL) _call[2] (); EXTI_ClearITPendingBit(EXTI_Line12); } if (EXTI_GetITStatus(EXTI_Line13)) { if (_call[3] != NULL) _call[3] (); EXTI_ClearITPendingBit(EXTI_Line13); } if (EXTI_GetITStatus(EXTI_Line14)) { if (_call[4] != NULL) _call[4] (); EXTI_ClearITPendingBit(EXTI_Line14); } if (EXTI_GetITStatus(EXTI_Line15)) { if (_call[5] != NULL) _call[5] (); EXTI_ClearITPendingBit(EXTI_Line15); } } 在这里需要说明一下,固件库还提供了两个函数用来判断外部中断状态以及清除外部状态标志位的函数 EXTI_GetFlagStatus 和 EXTI_ClearFlag, 他们的作用和前面两个函数的作用类似。只是在 EXTI_GetITStatus 函数中会先判断这种中断是否使能,使能了才去判断中断标志位,而EXTI_GetFlagStatus 直接用来判断状态标志位。 下面举个栗子。 void ft6x36_init(void) { GPIO_InitTypeDef GPIO_InitStructure; RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOC|RCC_AHB1Periph_GPIOE, ENABLE);//使能GPIOB时钟 GPIO_InitStructure.GPIO_Pin = GPIO_Pin_1 ; GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN;//普通输出模式 GPIO_InitStructure.GPIO_OType = GPIO_OType_PP;//推挽输出 GPIO_InitStructure.GPIO_Speed = GPIO_Speed_2MHz;//100MHz GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_UP;//上拉 GPIO_Init(GPIOE, &GPIO_InitStructure);//初始化 EXTI_InitTypeDef EXTI_InitStructure; RCC_APB2PeriphClockCmd(RCC_APB2Periph_SYSCFG, ENABLE);//使能SYSCFG时钟 SYSCFG_EXTILineConfig(EXTI_PortSourceGPIOE, EXTI_PinSource1);//PE0 连接到中断线0 /* 配置EXTI_Line1 */ EXTI_InitStructure.EXTI_Line = EXTI_Line1;// EXTI_InitStructure.EXTI_Mode = EXTI_Mode_Interrupt;//中断事件 EXTI_InitStructure.EXTI_Trigger = EXTI_Trigger_Falling; //下降沿触发 EXTI_InitStructure.EXTI_LineCmd = ENABLE;//使能LINE0 EXTI_Init(&EXTI_InitStructure);//配置 EXTI4_Register_Call(EXTI_Line1, _touch_irq); } void _touch_irq(void) { 。。。 }
上一篇:stm32中断服务函数c语言,STM32中断使用总结——不使用固件库
下一篇:STM32–中断程序总结(库函数)
推荐阅读最新更新时间:2024-11-09 21:02
设计资源 培训 开发板 精华推荐
- [DCDC]RT7258核心,8A可调降压模块
- 用于汽车的 8/10/12 位、1CH DAC
- 基于KP1071非隔离LED恒流驱动
- 用于背光手持计算机显示器的 LED 的高效低成本替代品
- 使用 LatticeECP3 视频协议板和 HDMI 夹层卡的 HDMI/DVI 视频接口参考设计
- LTC1450 并行输入、12 位轨至轨微功率 DAC 的典型应用
- ESP32开发板
- DC2197A-A,使用 LTC2645 四通道、12 位 PWM 至 Vout DAC 的演示板
- 使用 Analog Devices 的 LT4276CHUFD 的参考设计
- MPC86XADS、XPC8xx 参考板,基于 XPC8860DE MPU PowerQUICC MPC8xx 处理器