嵌入式蓝桥杯学习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);
		}
	}
}
相关推荐
一匹电信狗5 小时前
【LeetCode_547_990】并查集的应用——省份数量 + 等式方程的可满足性
c++·算法·leetcode·职场和发展·stl
wdfk_prog7 小时前
[Linux]学习笔记系列 -- [drivers][i2c]i2c-dev
linux·笔记·学习
越努力越幸运5088 小时前
CSS3学习之网格布局grid
前端·学习·css3
chillxiaohan9 小时前
GO学习记录——多文件调用
开发语言·学习·golang
Funny_AI_LAB10 小时前
AI Agent最新重磅综述:迈向高效智能体,记忆、工具学习和规划综述
人工智能·学习·算法·语言模型·agi
代码游侠12 小时前
学习笔记——Linux内核与嵌入式开发1
linux·运维·前端·arm开发·单片机·嵌入式硬件·学习
宇钶宇夕12 小时前
CoDeSys入门实战一起学习(二十八):(LD)三台电机顺起逆停程序详解—上升、下降沿使用上
单片机·嵌入式硬件·学习
科技林总12 小时前
【系统分析师】6.5 电子商务
学习
代码游侠12 小时前
C语言核心概念复习(一)
c语言·开发语言·c++·笔记·学习
tb_first12 小时前
万字超详细苍穹外卖学习笔记1
java·jvm·spring boot·笔记·学习·tomcat·mybatis