嵌入式蓝桥杯学习3 外部中断实现按键

Cubemx配置

前面的配置依旧一样。

原文链接:https://blog.csdn.net/m0_74246768/article/details/144227188

1.打开cubemx,将PB0到PB1配置为GPIO_EXTI模式。

2.在System-Core中点击GPIO,选择PB0到PB2,

GPIO_Mode(触发模式):External Interrupt Mode with Falling edge trigger detection

GPIO Pull up/Pull down(上下拉):Pull up

3.在System-Core中点击NVIC,勾选EXTI LINE interrupt三条线的使能中断。

4.将三条线的(Preemption Priority)抢占优先级调整低一些,都设置为5(让更高级的优先级处理更紧急的事件)。

5.将Time base: System tick timer的(Preemption Priority)抢占优先级设置为4.

点击GENERATE CODE.

代码编写

中断回调函数:
void HAL_GPIO_EXTI_Callback(uint16_t GPIO_Pin)

my_main.c

复制代码
#include "my_main.h"
uint8_t led_sta=0x01;
void LED_Disp(uint8_t dsLED)
{
	HAL_GPIO_WritePin(GPIOC, GPIO_PIN_All, GPIO_PIN_SET);
	HAL_GPIO_WritePin(GPIOC, dsLED<<8, GPIO_PIN_RESET);
	HAL_GPIO_WritePin(GPIOD, GPIO_PIN_2, GPIO_PIN_SET);
	HAL_GPIO_WritePin(GPIOC, GPIO_PIN_2, GPIO_PIN_RESET);
}
uint8_t LED_chg(uint8_t num,uint8_t sta)
{
	uint8_t pos=0x01<<(num-1);
	led_sta=(led_sta&(~pos))|(pos*sta);
	LED_Disp(led_sta);
}
	
	
void setup()
{
	LED_Disp(0x00);
}
void loop()
{

}
void HAL_GPIO_EXTI_Callback(uint16_t GPIO_Pin)
{
	if(GPIO_Pin==GPIO_PIN_0)
	{
		HAL_Delay(10);
		if(HAL_GPIO_ReadPin(GPIOB,GPIO_PIN_0)==0)
		{
			led_sta=(led_sta&0xfd)|0x02;
			led_sta=(led_sta&0xfe)|0x00;
			LED_Disp(led_sta);
			HAL_Delay(100);
		}
	}
}
相关推荐
2501_918126914 小时前
stm32做一个次声波检测器,需要哪些元件
stm32·单片机·嵌入式硬件·学习·个人开发
GocNeverGiveUp5 小时前
大模型学习2-Agent
学习
Bal炎魔5 小时前
AI 学习专题一,AI 实现的原理
人工智能·学习
Ro Jace6 小时前
分岔机制学习
人工智能·学习·机器学习
反向跟单策略7 小时前
期货反向跟单-2025年回顾及2026年展望
大数据·人工智能·学习·数据分析·区块链
yunhuibin7 小时前
GoogLeNet学习
人工智能·python·深度学习·神经网络·学习
xcLeigh8 小时前
Python入门:Python3 正则表达式全面学习教程
python·学习·正则表达式·教程·python3
天真小巫8 小时前
2026.2.24总结(像经营企业一样经营自己)
职场和发展
知识分享小能手8 小时前
PostgreSQL 入门学习教程,从入门到精通,PostgreSQL 16 语法知识点与案例详解(1)
数据库·学习·postgresql
代码游侠8 小时前
Linux驱动复习——驱动
linux·运维·arm开发·笔记·学习