stm32f030c8t6 按键中断例程
时间: 2025-01-03 13:35:00 浏览: 7
### STM32F030C8T6 按键中断示例代码
对于STM32F030C8T6微控制器而言,配置按键中断涉及初始化GPIO端口、设置EXTI线以及编写相应的中断服务程序。下面提供一段基于HAL库的示例代码用于实现按键触发外部中断并控制LED灯的功能。
#### 初始化部分
```c
#include "stm32f0xx_hal.h"
// 定义使用的IO引脚
#define BUTTON_PIN GPIO_PIN_13
#define LED_PIN GPIO_PIN_5
#define BUTTON_PORT GPIOA
#define LED_PORT GPIOA
void SystemClock_Config(void);
static void MX_GPIO_Init(void);
int main(void){
HAL_Init();
SystemClock_Config();
// 初始化GPIO
MX_GPIO_Init();
while (1){
__WFI(); // 进入低功耗模式等待中断唤醒
}
}
/**
* @brief 配置系统时钟
*/
void SystemClock_Config(void){
RCC_OscInitTypeDef RCC_OscInitStruct = {0};
RCC_ClkInitTypeDef RCC_ClkInitStruct = {0};
/* Configure the main internal regulator output voltage */
__HAL_PWR_VOLTAGESCALING_CONFIG(PWR_REGULATOR_VOLTAGE_SCALE1);
/* Initializes the CPU, AHB and APB busses clocks */
RCC_OscInitStruct.OscillatorType = RCC_OSCILLATORTYPE_HSI;
RCC_OscInitStruct.HSIState = RCC_HSI_ON;
RCC_OscInitStruct.HSICalibrationValue = RCC_HSICALIBRATION_DEFAULT;
RCC_OscInitStruct.PLL.PLLState = RCC_PLL_NONE;
if (HAL_RCC_OscConfig(&RCC_OscInitStruct) != HAL_OK){
Error_Handler();
}
/* Initializes the CPU, AHB and APB busses clocks */
RCC_ClkInitStruct.ClockType = RCC_CLOCKTYPE_HCLK|RCC_CLOCKTYPE_SYSCLK |RCC_CLOCKTYPE_PCLK1;
RCC_ClkInitStruct.SYSCLKSource = RCC_SYSCLKSOURCE_HSI;
RCC_ClkInitStruct.AHBCLKDivider = RCC_SYSCLK_DIV1;
RCC_ClkInitStruct.APB1CLKDivider = RCC_HCLK_DIV1;
if (HAL_RCC_ClockConfig(&RCC_ClkInitStruct, FLASH_LATENCY_0) != HAL_OK){
Error_Handler();
}
}
/**
* @brief GPIO Initialization Function
*/
static void MX_GPIO_Init(void){
GPIO_InitTypeDef GPIO_InitStruct = {0};
/* GPIO Ports Clock Enable */
__HAL_RCC_GPIOA_CLK_ENABLE()
/*Configure GPIO pin : PA5 as output to control an LED*/
GPIO_InitStruct.Pin = LED_PIN;
GPIO_InitStruct.Mode = GPIO_MODE_OUTPUT_PP;
GPIO_InitStruct.Pull = GPIO_NOPULL;
GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_LOW;
HAL_GPIO_Init(LED_PORT, &GPIO_InitStruct);
/*Configure GPIO pin : PA13 as input with external interrupt capability for button press detection.*/
GPIO_InitStruct.Pin = BUTTON_PIN;
GPIO_InitStruct.Mode = GPIO_MODE_IT_FALLING; // 下降沿触发
GPIO_InitStruct.Pull = GPIO_PULLUP; // 上拉电阻
HAL_GPIO_Init(BUTTON_PORT, &GPIO_InitStruct);
/* EXTI interrupt init*/
HAL_NVIC_SetPriority(EXTI15_10_IRQn, 0, 0);
HAL_NVIC_EnableIRQ(EXTI15_10_IRQn);
}
```
#### 中断服务程序
```c
/* External Interrupt Service Routine */
void EXTI15_10_IRQHandler(void){
HAL_GPIO_EXTI_IRQHandler(BUTTON_PIN); // 调用HAL库中的通用处理函数[^2]
}
/* Callback function called by HAL library when a specific line triggers an interrupt event */
void HAL_GPIO_EXTI_Callback(uint16_t GPIO_Pin){
if(GPIO_Pin == BUTTON_PIN){ // 如果是目标按钮产生的中断
static uint8_t led_state = 0;
led_state ^= 1; // 切换状态
if(led_state){
HAL_GPIO_WritePin(LED_PORT, LED_PIN, GPIO_PIN_SET); // 打开LED
}else{
HAL_GPIO_WritePin(LED_PORT, LED_PIN, GPIO_PIN_RESET); // 关闭LED
}
}
}
```
这段代码展示了如何利用STM32F0系列MCU上的硬件资源来创建简单的按键检测机制,并通过改变连接到PA5管脚上LED的状态作为反馈。当按下连接至PA13的开关时,会触发一次下降沿中断请求,在`EXTI15_10_IRQHandler()`中调用了`HAL_GPIO_EXTI_IRQHandler()`完成实际的工作,包括清除中断标志位等操作[^4]。
阅读全文