嵌入式蓝桥杯学习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);
		}
	}
}
相关推荐
Pniubi8 小时前
力扣2 . 两数相加
算法·leetcode·职场和发展
Z21-X16 小时前
云原生运维学习笔记:Kubernetes Volume 与 Secret 实验
运维·笔记·学习·云原生·kubernetes
console.log('npc')20 小时前
Python 学习教程 · 第4课:数据结构
数据结构·python·学习
星落梦生1 天前
Docker学习(11)
学习·docker·容器
Pniubi2 天前
力扣21合并两个有序链表
算法·leetcode·职场和发展
Pniubi2 天前
力扣239.滑动窗口最大值(优先队列)
算法·leetcode·职场和发展
重生之我是萧炎2 天前
PR安装教程,Adobe Premiere Pro安装教程(含安装包),仅供学习使用
学习·adobe
蒸蒸yyyyzwd2 天前
深搜广搜学习笔记
笔记·学习
叼烟扛炮2 天前
[LangChain] 学习笔记(13):并发不是越大越好——batch / gather / 信号量 / 配额治理
笔记·学习·langchain