嵌入式蓝桥杯学习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);
		}
	}
}
相关推荐
好好学习天天向上~~12 分钟前
6_Linux学习总结_自动化构建
linux·学习·自动化
非凡ghost1 小时前
PowerDirector安卓版(威力导演安卓版)
android·windows·学习·软件需求
代码游侠2 小时前
C语言核心概念复习——C语言基础阶段
linux·开发语言·c++·学习
dingdingfish2 小时前
Bash学习 - 第3章:Basic Shell Features,第5节:Shell Expansions
开发语言·学习·bash
firewood20242 小时前
共射三极管放大电路相关情况分析
笔记·学习
zl0_00_02 小时前
美亚2023
学习
AI_56782 小时前
SQL性能优化全景指南:从量子执行计划到自适应索引的终极实践
数据库·人工智能·学习·adb
zl0_00_02 小时前
pctf wp
学习
Hello_Embed2 小时前
libmodbus STM32 主机实验(USB 串口版)
笔记·stm32·学习·嵌入式·freertos·modbus
学编程的闹钟2 小时前
98【html的php化】
学习