摘要
本文分步讲解如何在STM32CubeMX2中为GPIO配置EXTI(外部中断)功能。本教程以NUCLEOC562RE开发板为例。
简介
单片机的EXTI模块可以检测异步事件,当外部输入线上电平发生跳变时产生中断。可配置为上升沿、下降沿或者双边沿触发,能够精准检测按键按下、传感器输出等外部事件。该能力对嵌入式系统的实时响应至关重要。
本文演示示例:按下开发板用户按键触发中断,板载LED闪烁一次。
前置准备
安装以下工具:
- STM32CubeMX2
- 最新版STM32C5 HAL2驱动
- STM32CubeIDE for Visual Studio Codeextension
本教程使用NUCLEO-C562RE开发板。
1. 工程搭建
按照下述步骤创建NUCLEOC562RE应用工程。本实操案例实现简单EXTI功能:按下开发板按键翻转LED状态。

1.1 创建工程
打开STM32CubeMX2,在主页点击[MCU]新建工程。

在MCU名称搜索框输入STM32C562RE,选中该MCU,点击[Continue]。

填写工程名称与存储路径,点击[Automatically Download, Install & Create Project]完成工程创建。

选择[Launch Project]打开工程。

1.2 GPIO与EXTI配置
1.在Pinout视图,点击[PA5]引脚,鼠标悬停菜单中[Assign Signal]展开更多选项。在GPIO模式栏勾选[GPIO],选择[Configured]。

2.右键点击[PC13]引脚,重复上面操作。

3.进入GPIO设置界面,打开引脚下拉菜单,对选中引脚分别配置主要参数。

选中[PA5],在“Main features”标签页,模式设置为[Output]。

选中[PC13],在“Main features”标签页,模式设置为[Input];进入"EXTI"→"Exti line"→"Main features","Trigger Detection"设置为[Falling Edge];切换至“Interruption”标签页,确认开启[Enabled interruption]与[IRQ Handler Generation](CubeMX2 v1.1.1版本已默认打开)。

4.点击Project settings图标,在HAL通用定义区域,找到HAL EXTI配置,确认勾选开启[Use Register Callback]和 [User Data](CubeMX2 v1.1.1版本已默认打开)。开启这些配置才可以生成EXTI相关代码文件。


1.3 代码生成
生成代码操作:
点击[Project settings]图标,选择目标IDE。
点击[Generate]按钮生成代码。

2. 在Visual Studio Code中配置工程
打开VS Code,打开工程文件夹。
如果弹出配置选择窗口,选择对应配置;若无弹窗,按下Ctrl+Shift+P,输入CMake: Select Configure Preset,选中调试配置项。

编译工程确认环境正常,之后进行代码开发。

2.1 代码编辑
VS Code打开生成好的工程后,修改main.c文件,参考如下:
/** ****************************************************************************** * file : main.c * brief : Main program body * Calls target system initialization then loop in main. ****************************************************************************** * * Copyright (c) 2025 STMicroelectronics. * All rights reserved. * * This software is licensed under terms that can be found in the LICENSE file * in the root directory of this software component. * If no LICENSE file comes with this software, it is provided AS-IS. * ****************************************************************************** *//* Includes ------------------------------------------------------------------*/#include "main.h"
/* Private typedef -----------------------------------------------------------*//* Private define ------------------------------------------------------------*//* Private macro -------------------------------------------------------------*//* Private variables ---------------------------------------------------------*/hal_exti_handle_t *pEXTI13; /* Pointer to the EXTI handle from the generated code */volatile uint8_t ExtiDetected = 0;/* Private functions prototype -----------------------------------------------*/
/** * brief: The application entry point. * retval: none but we specify int to comply with C99 standard */int main(void){ /** System Init: this code placed in targets folder initializes your system. * It calls the initialization (and sets the initial configuration) of the peripherals. * You can use STM32CubeMX to generate and call this code or not in this project. * It also contains the HAL initialization and the initial clock configuration. */ if (mx_system_init() != SYSTEM_OK) { return (-1); } else { /* * You can start your application code here */ mx_cortex_nvic_init(); // Initialize the NVIC pEXTI13 = mx_gpio_default_exti13_gethandle(); // Get the EXTI13 handle HAL_EXTI_Enable(pEXTI13, HAL_EXTI_MODE_INTERRUPT); // Enable the EXTI13 interrupt while (1) { if (ExtiDetected == 1) { HAL_GPIO_TogglePin(HAL_GPIOA, HAL_GPIO_PIN_5); HAL_Delay(100); ExtiDetected = 0; } } }}void HAL_EXTI_TriggerCallback(hal_exti_handle_t *hexti, hal_exti_trigger_t trigger){ /* Prevent unused argument compilation warning */ STM32_UNUSED(trigger); if (hexti == pEXTI13) { ExtiDetected = 1; }}/* end main */
2.2 功能验证
编译完成后,点击[Run and Debug]图标;调试会话选择[STM32Cube: STLINK GDB Server]。

程序启动时用户LED处于熄灭状态;按下用户按键,LED状态翻转。
在HAL_EXTI_TriggerCallback回调函数内部设置断点,验证只有按下按键时才会触发中断。

总结
本文演示了如何借助STM32CubeMX2使用EXTI实现异步事件检测。按键触发板载LED翻转这个示例展示了EXTI在嵌入式系统的实际用法,体现出EXTI对硬件交互实时性与执行效率的重要作用。