按键控制LED灯亮灭

cpp 复制代码
#include "stm32f10x.h"                  // Device header

void LED_Init(void)
{
	RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA,ENABLE);
	
	GPIO_InitTypeDef GPIO_InitStructure;
	GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP;
	GPIO_InitStructure.GPIO_Pin = GPIO_Pin_1 | GPIO_Pin_2;
	GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
	GPIO_Init(GPIOA,&GPIO_InitStructure);
	
	GPIO_SetBits(GPIOA,GPIO_Pin_1 | GPIO_Pin_2);
}


void LED1_ON(void)
{
   GPIO_ResetBits(GPIOA,GPIO_Pin_1);
}

void LED1_OFF(void)
{
   GPIO_SetBits(GPIOA,GPIO_Pin_1);
}

void LED1_Turn(void)
{
	if(GPIO_ReadOutputDataBit(GPIOA,GPIO_Pin_1)==0)
	{
		GPIO_SetBits(GPIOA,GPIO_Pin_1);
	}
	else
	{
		GPIO_ResetBits(GPIOA,GPIO_Pin_1);
	}
}

void LED2_ON(void)
{
   GPIO_ResetBits(GPIOA,GPIO_Pin_2);
}

void LED2_OFF(void)
{
   GPIO_SetBits(GPIOA,GPIO_Pin_2);
}

void LED2_Turn(void)
{
	if(GPIO_ReadOutputDataBit(GPIOA,GPIO_Pin_2)==0)
	{
		GPIO_SetBits(GPIOA,GPIO_Pin_2);
	}
	else
	{
		GPIO_ResetBits(GPIOA,GPIO_Pin_2);
	}
}

一、头文件包含

#include "stm32f10x.h" // Device header

包含了 STM32F10x 系列芯片的寄存器定义和相关宏,这是使用标准外设库开发的基础。

二、LED 初始化函数:LED_Init()

void LED_Init(void)

{

RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA,ENABLE);

启用 APB2 总线上的 GPIOA 时钟,这是操作 GPIOA 的前提。

GPIO_InitTypeDef GPIO_InitStructure;

GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP; //推挽输出

GPIO_InitStructure.GPIO_Pin = GPIO_Pin_1 | GPIO_Pin_2;//PA1,PA2

GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;//输出最大速度50MHz

GPIO_Init(GPIOA,&GPIO_InitStructure);

GPIO_SetBits(GPIOA,GPIO_Pin_1 | GPIO_Pin_2);//初始设置为高电平

LED 低电平点亮(即共阳极接法,LED 阴极接 MCU 引脚)

}

三、LED1 控制函数

复制代码
void LED1_ON(void)
{
    GPIO_ResetBits(GPIOA, GPIO_Pin_1); // PA1 输出低电平 → LED1 亮
}

void LED1_OFF(void)
{
    GPIO_SetBits(GPIOA, GPIO_Pin_1); // PA1 输出高电平 → LED1 灭
}

void LED1_Turn(void)
{
    if (GPIO_ReadOutputDataBit(GPIOA, GPIO_Pin_1) == 0)
    {
        GPIO_SetBits(GPIOA, GPIO_Pin_1);     // 当前为低 → 变高(灭)
    }
    else
    {
        GPIO_ResetBits(GPIOA, GPIO_Pin_1);   // 当前为高 → 变低(亮)
    }
}

读取当前 PA1 的输出状态,然后取反,实现 LED1 的翻转(Toggle)。


四、LED2 控制函数(与 LED1 类似)

LED2_ON():PA2 输出低 → LED2 亮

LED2_OFF():PA2 输出高 → LED2 灭

LED2_Turn():读取 PA2 当前输出状态并翻转

void LED2_ON(void)

{

GPIO_ResetBits(GPIOA,GPIO_Pin_2);

}

void LED2_OFF(void)

{

GPIO_SetBits(GPIOA,GPIO_Pin_2);

}

void LED2_Turn(void)

{

if(GPIO_ReadOutputDataBit(GPIOA,GPIO_Pin_2)==0)

{

GPIO_SetBits(GPIOA,GPIO_Pin_2);

}

else

{

GPIO_ResetBits(GPIOA,GPIO_Pin_2);

}

}

cpp 复制代码
#include "stm32f10x.h"                  // Device header
#include "Delay.h"     //自定义延时函数用于消抖

void Key_Init(void)
{
	RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOB,ENABLE);
	
	GPIO_InitTypeDef GPIO_InitStructure;
	GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IPU;//内部上拉电阻使能
	GPIO_InitStructure.GPIO_Pin = GPIO_Pin_1 |GPIO_Pin_11;
	GPIO_InitStructure.GPIO_Speed =GPIO_Speed_50MHz;
	GPIO_Init(GPIOB,&GPIO_InitStructure);
}

uint8_t Key_GetNum(void)
{
   uint8_t KeyNum = 0;
	if(GPIO_ReadInputDataBit(GPIOB,GPIO_Pin_1)==0)
	{
	    Delay_ms(20);
		while(GPIO_ReadInputDataBit(GPIOB,GPIO_Pin_1)==0);
		Delay_ms(20);
		KeyNum = 1;
	}
		if(GPIO_ReadInputDataBit(GPIOB,GPIO_Pin_11)==0)
	{
	    Delay_ms(20);
		while(GPIO_ReadInputDataBit(GPIOB,GPIO_Pin_11)==0);
		Delay_ms(20);
		KeyNum = 2;
	}
	return KeyNum;
}
	

GPIO_Mode_IPU 表示 Input Pull-Up(内部上拉电阻使能)

当按键未按下时,引脚被内部上拉至高电平(逻辑 1);

按键按下时,引脚接地 → 读取为低电平(逻辑 0)。

使用 PB1 和 PB11 作为两个独立按键输入。

按键检测函数:Key_GetNum()

按键检测逻辑(低电平有效)

因为使用了 上拉输入,按键未按时引脚为高(1),按下后接地为低(0)。

所以判断条件是 == 0。

软件消抖处理

第一次 Delay_ms(20):确认是否真的按下(滤除机械抖动)。

while(... == 0);:等待用户松开按键(实现"按下一次只响应一次")。

第二次 Delay_ms(20):消除释放时的抖动,提高稳定性。

cpp 复制代码
#include "stm32f10x.h"                  // Device header
#include "Delay.h"
#include "LED.h"
#include "Key.h"

uint8_t KeyNum;  //用于存储当前检测到的按键编号(0 = 无按键,1 = 按键1,2 = 按键2)。

int main(void)
{
	
	LED_Init();
	Key_Init();
	
	while(1)
	{
       KeyNum = Key_GetNum();
		if(KeyNum ==1)
		{
		    LED1_Turn();
		}
		if(KeyNum ==2)
		{
		    LED2_Turn();
		}
	}
}

在主程序调用头文件实际工程中,LED.c、Key.c、Delay.c 应分别实现对应功能。

LED_Init() :初始化 GPIOA 的 Pin1 和 Pin2 为推挽输出,并默认熄灭 LED(高电平)。Key_Init():初始化 GPIOB 的 Pin1 和 Pin11 为上拉输入,用于检测两个独立按键。

功能 说明
硬件连接 - LED1 接 PA1,LED2 接 PA2(低电平点亮) - 按键1 接 PB1,按键2 接 PB11(按下接地,内部上拉)
用户操作 按一下按键1 → LED1 切换状态;按一下按键2 → LED2 切换状态
防抖处理 软件延时 20ms + 等待释放,有效消除机械抖动
响应方式 按一次,LED灯亮一次(非连发)
相关推荐
myy-learn3 小时前
ARM-I2C 驱动优化与 LM75 温度传感器读取实践
arm开发·单片机·嵌入式硬件
元器件BOM超人4 小时前
R7FA6M3AH3CFC#AA0 瑞萨RA6M3系列MCU解析
单片机
..Dauntless..5 小时前
【stm32】I2C(二)——Master和Slave之间的协议
stm32·单片机·嵌入式硬件
硅基手札5 小时前
【串口技术系列文档 04】UART协议深度解析
驱动开发·stm32·单片机·嵌入式硬件·mcu·计算机外设
sramdram5 小时前
预付费电能表BLE蓝牙MCU方案
单片机·嵌入式硬件·ble蓝牙·蓝牙mcu·蓝牙mcu方案
weixin_690654745 小时前
龙迅#LT7911EXS 高性价比IC,功能适用于HDMI/DP/TPYE-C转单PORT MIPICSI/DSI ,分辨率高达4K60HZ。
嵌入式硬件·音视频·信号处理
Zhang.Yi6 小时前
STM32-BKP和RTC
stm32·嵌入式硬件·实时音视频
Yiran_G6 小时前
LoRa模块休眠后如何快速恢复通信?低功耗终端唤醒与数据交互实践
嵌入式硬件·物联网
意法半导体STM327 小时前
【官方原创】LAT1724 STM32CubeIDE for VSCode 修改链接文件中的起始地址无效?
vscode·stm32·单片机·嵌入式硬件·mcu·stm32cube
BSD_HY7 小时前
嵌入式薄膜开关中断去抖:中断优先级分层实战策略
单片机·嵌入式硬件·薄膜开关·源头工厂·薄膜按键