嵌入式蓝桥杯学习2 按键

Cubemx 配置

前面的配置和上一篇文章是一样的。

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

打开cubemx。

(按键引脚PB0 PB1 PB2 PA0)

1.把四个按键引脚配置为GPIO_Input。

2.点击system Core中的GPIO,选择四个引脚进行配置

GPIO mode(模式):Input mode

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

点击GENERATE CODE.

代码编写

HAL_GPIO_ReadPin(GPIO_TypeDef *GPIOx, uint16_t GPIO_Pin);

参数说明:

GPIOx:GPIO名称,取值是GPIOA~GPIOG

GPIO_Pin:GPIO引脚,取值是GPIO_PIN_0 ~ GPIO_PIN_15

返回值:GPIO引脚状态(GPIO_PIN_RESET 或 GPIO_PIN_SET)

my_main.h

复制代码
#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()
{
	if(HAL_GPIO_ReadPin(GPIOA,GPIO_PIN_0)==0)
	{
		HAL_Delay(10);
		if(HAL_GPIO_ReadPin(GPIOA,GPIO_PIN_0)==0)
		{
			led_sta=(led_sta&0xfe)|0x01;
			led_sta=(led_sta&0xfd)|0x00;
			LED_Disp(led_sta);
			HAL_Delay(100);
		}
	}
	if(HAL_GPIO_ReadPin(GPIOB,GPIO_PIN_0)==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);
		}
	}
}
相关推荐
西岸行者9 天前
学习笔记:SKILLS 能帮助更好的vibe coding
笔记·学习
悠哉悠哉愿意9 天前
【单片机学习笔记】串口、超声波、NE555的同时使用
笔记·单片机·学习
qq_459234429 天前
【题库】| 商用密码应用安全性评估从业人员考核题库(四十)
职场和发展·密码学·学习方法·考核·商用密码·商用密码应用安全性评估·密评
敲敲了个代码9 天前
[特殊字符] 空数组的迷惑行为:为什么 every 为真,some 为假?
前端·javascript·react.js·面试·职场和发展
别催小唐敲代码9 天前
嵌入式学习路线
学习
毛小茛9 天前
计算机系统概论——校验码
学习
babe小鑫9 天前
大专经济信息管理专业学习数据分析的必要性
学习·数据挖掘·数据分析
winfreedoms9 天前
ROS2知识大白话
笔记·学习·ros2
在这habit之下9 天前
Linux Virtual Server(LVS)学习总结
linux·学习·lvs
我想我不够好。9 天前
2026.2.25监控学习
学习