一文解析GPIO外部中断输入检测

本篇主要讲述gpio外部中断输入检测,实际物理输入方式是按键,但是实际上更适合外部设备信号的输入检测,物理按键输入因为有抖动,还需要硬件或者软件去抖才比较可靠。这个例子不考虑防抖的情况。
软件版本:
stm32cubemx v4.25.0
system workbench v2.4
硬件:onenet 麒麟座v2.3
在stm32cubemx中新建项目,选择正确的mcu型号
设置rcc和sys,然后根据板子实际情况设置时钟(麒麟座外部晶振是12m,stm32f103x的最高主频是72m)
根据板子的具体连接设置4个gpio_output (连接到led)和4个gpio_exit*(外部中断模式,连接到物理按键)。
gpio output引脚设置
麒麟座按键有外部上拉,所以gpio_exit*不需要配置内部上拉,检测下降沿(falling edge),gpio_exit*设置如下
led名称标号我用了1/2/3/4,sw名称标号我用了2/3/4/5,实例中标号也未必相同,但是要做好一一对应关系。
在nvic(嵌套向量中断控制器)中,勾选exit line2 interrupt 和 exit line[15:12] interrupt 使能中断。右边两个选项设置抢占优先级和响应优先级。此处我们选择默认的,不修改。
同样修改project - setting ,toolchain/ide选择 sw4stm32
勾选这里。
生成代码后点击open project在eclipse中打开项目,然后在在stm32f7xx_it.c中断服务函数文件中,我们可以找到exti2 和exti15_10中断的服务函数。
/******************************************************************************//* stm32f1xx peripheral interrupt handlers *//* add here the interrupt handlers for the used peripherals. *//* for the available peripheral interrupt handler names, *//* please refer to the startup file (startup_stm32f1xx.s). *//******************************************************************************//***@brief this function handles exti line2 interrupt.*/void exti2_irqhandler(void){/* user code begin exti2_irqn 0 *//* user code end exti2_irqn 0 */hal_gpio_exti_irqhandler(gpio_pin_2);/* user code begin exti2_irqn 1 *//* user code end exti2_irqn 1 */}/***@brief this function handles exti line[15:10] interrupts.*/void exti15_10_irqhandler(void){/* user code begin exti15_10_irqn 0 *//* user code end exti15_10_irqn 0 */hal_gpio_exti_irqhandler(gpio_pin_11);hal_gpio_exti_irqhandler(gpio_pin_12);hal_gpio_exti_irqhandler(gpio_pin_13);/* user code begin exti15_10_irqn 1 *//* user code end exti15_10_irqn 1 */}
右键点击hal_gpio_exti_irqhandler 选择open declaration会跳转到如下代码:
/***@briefthis function handles exti interrupt request.*@paramgpio_pin: specifies the pins connected exti line*@retvalnone*/voidhal_gpio_exti_irqhandler(uint16_t gpio_pin){/* exti line interrupt detected */if(__hal_gpio_exti_get_it(gpio_pin) != reset){__hal_gpio_exti_clear_it(gpio_pin);hal_gpio_exti_callback(gpio_pin);}}/***@briefexti line detection callbacks.*@paramgpio_pin: specifies the pins connected exti line*@retvalnone*/__weakvoidhal_gpio_exti_callback(uint16_t gpio_pin){/* prevent unused argument(s) compilation warning */unused(gpio_pin);/*note:this function should not be modified, when the callback is needed,the hal_gpio_exti_callback could be implemented in the user file*/}
上述代码中可以看到gpio外部中断处理函数首先清除中断标识位,然后调用中断回调函数hal_gpio_exti_callback()。往下看这个回调函数定义的时候使用了__weak修饰符(关于__weak修饰符, 可以参看此博文),里面没有任何有作用的代码,我们需要重新定义这个函数。编辑gpio.c(或者main.c也可以),进行如下修改:
/* user code begin 2 */voidhal_gpio_exti_callback(uint16_tgpio_pin){if(gpio_pin == sw2_pin){hal_gpio_togglepin(led1_gpio_port,led1_pin);}if(gpio_pin == sw3_pin){hal_gpio_togglepin(led2_gpio_port,led2_pin);}if(gpio_pin == sw4_pin){hal_gpio_togglepin(led3_gpio_port,led3_pin);}if(gpio_pin == sw5_pin){hal_gpio_togglepin(led4_gpio_port,led4_pin);}}/* user code end 2 */
然后右键点击项目,选择properties, run-debug settings, 点击右侧的new,在弹出对话框中选择ac6 stm32 debugging。
然后任务栏上点击run图,当然会报错的,原因请查看另一篇我的博客,所以需要右键点击 项目名run.cfg ,给它改个名字,
然后右键点击项目树里面的项目名称,选择“propeties”,然后在run/debug settings-选择项目名-edit-main-c/c++application那里点击“search project”,然后选择出现的默认的elf文件:
然后在debugger-user defined-browse 那里选择你自己改名的配置文件:
然后右键点击那个新的cfg文件,选择open with - text editor, 进行如下更改:
source [findinterface/stlink.cfg]//更改为 source [find interface/stlink-v2.cfg]reset_config srst_only srst_nogate connect_assert_srst//这一行改为 reset_config none
run一下,就可以了。然后再run一下,就实现四个按键分别控制led的开关切换了。但是这里没有防抖处理,你会发现有时候led会闪一下,或者没反应,其实是很快地点亮然后熄灭一次或者若干次,只是由于速度太快电流很小导致你看不到。下一篇我们会通过使用systick中断来实现按键去抖扫描。在实际项目中,最好加上硬件防抖,因为抖动产生的反向电压可能会冲击gpio导致损坏。

致敬三星?双侧曲面屏幕iPhone真的要来了!
匠心独运,以人为本,三星智能门锁人性化改进
2020年将是我国央行数字货币全面发展的一年
可编程高速直流电子负载仪概述
2.4GHz与5GHz Wi-Fi:哪个更快?
一文解析GPIO外部中断输入检测
对于绝缘子串,它的电压分布规律是什么
解密ZigBee IP规范:智能能源传感网络更可靠
机器视觉中相机参数对高质量图像的作用分析
晶华微SD8114蓝牙营养秤的详细说明
小米mix4参数配置详细
iQOO的夏天狂暑季,一大波暑期大促来袭
工业控制系统五个层次 工业控制系统有哪些
狂砸25亿元 广州挹注“高标准”城市智能化视频系统
ARM打破Intel垄断局面,一个新的时代即将来临
北京大学与中科院联合制备出超强抗辐照能力的集成电路
合约量化系统开发详解流程
SiC MOSFET和SiC IGBT的区别
看行业大咖畅谈无人驾驶
电流探头在电机调试中的具体测试方法和操作步骤