嵌入式蓝桥杯学习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);
		}
	}
}
相关推荐
potender3 分钟前
前端基础学习html+css+js
前端·css·学习·html·js
靡樊1 小时前
Socket编程UDP\TCP
网络·c++·学习·tcp/ip·udp
余渔鱼11231 小时前
ajax学习手册
学习·ajax·okhttp
东京老树根1 小时前
SAP学习笔记 - 开发24 - 前端Fiori开发 Filtering(过滤器),Sorting and Grouping(排序和分组)
笔记·学习
·云扬·1 小时前
【PmHub面试篇】Gateway全局过滤器统计接口调用耗时面试要点解析
面试·职场和发展·gateway
viperrrrrrrrrr73 小时前
大数据学习(130)-zookeeper
大数据·学习·zookeeper
落羽的落羽3 小时前
【C++】二叉搜索树
开发语言·数据结构·c++·学习
FINE!(正在努力!)3 小时前
PyTest框架学习
学习·pytest
fengye2071614 小时前
板凳-------Mysql cookbook学习 (十)
学习·mysql·adb