按键控制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)。

使用 PB1PB11 作为两个独立按键输入。

按键检测函数: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.cKey.cDelay.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灯亮一次(非连发)
相关推荐
炸膛坦客14 小时前
单片机/C/C++八股:(二十六)IIC 专题(I²C)---- 上集
c语言·c++·单片机
华清远见IT开放实验室19 小时前
实验室建设案例 | 石家庄科技信息职业学院嵌入式实验室——从底层硬件到系统应用,一所应用型高校的嵌入式人才培养这样落地
linux·arm开发·stm32·嵌入式硬件·高校·实验室建设
AI的探索之旅21 小时前
AI辅助原理图评审:电源去耦、BOOT引脚、VCAP——19项逐一核查,遗漏?不存在的
人工智能·vscode·嵌入式硬件
茯苓gao21 小时前
嵌入式开发笔记:EtherCAT协议从硬件到软件完整配置指南——从零搭建一套EtherCAT通信系统
笔记·嵌入式硬件·学习
GeekArch1 天前
第24讲:Vibe模式代码风格控制——适配Keil/STM32工程规范
人工智能·stm32·单片机·嵌入式硬件·mcu·决策树·ai编程
Freedom_my1 天前
STM32项目3
stm32·单片机·嵌入式硬件
国科安芯1 天前
星间光链路:AS32S601型抗辐射MCU在空间激光通信终端控制中的技术实现
服务器·网络·单片机·嵌入式硬件·物联网·安全·信息与通信
小李不困还能学1 天前
基于 51 单片机的8 路抢答器设计教程
单片机·嵌入式硬件·mongodb·抢答器
CedarQR1 天前
万字长文:从零在 RK3588 上部署 PaddleSpeech 中文 TTS 全流程(FastSpeech2 + HiFiGAN)
开发语言·c++·嵌入式硬件·ubuntu·json
萌动的小火苗1 天前
嵌入式开发中的栈与队列:任务调度为什么依赖数据结构
数据结构·c++·单片机·嵌入式硬件