STM32 - 按键控制LED灯

功能:按键控制LED的亮灭。

两个按键:PE3和PE2

两个LED:PE5和PB5

按键PE3控制LED2 - PE5;按键PE2控制LED3 - PB5

main.c:

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

uint8_t KeyNum;

int main(void)
{
	LED_Init();
	Key_Init();
	
	
	while(1)
	{
       KeyNum = Key_GetNum();
		   if(KeyNum == 1)   //按键2按下
			 {
				 LED2_Turn();
			 }
			 if(KeyNum == 2)   //按键3按下
			 {
				 LED3_Turn();
			 }
	}
	
}
 

下面是LED的函数功能实现:

LED.c:

cs 复制代码
//用来存放驱动程序的主体代码

#include "stm32f10x.h"                  // Device header

void LED_Init(void)
{
	RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOE, ENABLE);
	RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOB, ENABLE);
	
	GPIO_InitTypeDef GPIO_InitStructure;
	GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP;
	GPIO_InitStructure.GPIO_Pin =  GPIO_Pin_5  ;
	GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
	GPIO_Init(GPIOB, &GPIO_InitStructure);
	GPIO_Init(GPIOE, &GPIO_InitStructure);
	
	GPIO_SetBits(GPIOE, GPIO_Pin_5);
	GPIO_SetBits(GPIOB, GPIO_Pin_5);
	
}

void LED2_ON(void)    //打开和关闭LED2
{
	GPIO_ResetBits(GPIOE, GPIO_Pin_5);
}


void LED2_OFF(void)
{
	GPIO_SetBits(GPIOE, GPIO_Pin_5);
}

void LED2_Turn(void)	
{ 
	//如果当前输出为0,就置1;否则,就置0.这样可以实现端口的电平翻转
	if  (GPIO_ReadOutputDataBit(GPIOE, GPIO_Pin_5) == 0) 
	{
		GPIO_SetBits(GPIOE, GPIO_Pin_5);     //把PE5置1
	}
	else
	{
		GPIO_ResetBits(GPIOE, GPIO_Pin_5);   //把PE5置0
	}
}
void LED3_ON(void)    //打开和关闭LED3
{
	GPIO_ResetBits(GPIOB, GPIO_Pin_5);
}

void LED3_OFF(void)
{
	GPIO_SetBits(GPIOB, GPIO_Pin_5);
}

void LED3_Turn(void)	
{ 
	//如果当前输出为0,就置1;否则,就置0.这样可以实现端口的电平翻转
	if  (GPIO_ReadOutputDataBit(GPIOB, GPIO_Pin_5) == 0) 
	{
		GPIO_SetBits(GPIOB, GPIO_Pin_5);     //把PE5置1
	}
	else
	{
		GPIO_ResetBits(GPIOB, GPIO_Pin_5);   //把PE5置0
	}
}

LED.h:

cs 复制代码
//用来存放这个驱动程序可有对外提供的函数或变量的声明

#ifndef __LED_H
#define __LED_H

void LED_Init(void);

void LED2_ON(void);
void LED2_OFF(void);
void LED2_Turn(void);

void LED3_ON(void);
void LED3_OFF(void);
void LED3_Turn(void);



#endif

下面是按键函数的实现:

Key.c:

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

void Key_Init(void)
{
	RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOE, ENABLE);
	
	GPIO_InitTypeDef GPIO_InitStructure;
	GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IPU;    //上拉输入
	GPIO_InitStructure.GPIO_Pin =  GPIO_Pin_3 | GPIO_Pin_2;
	GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
	GPIO_Init(GPIOE, &GPIO_InitStructure);
}

uint8_t Key_GetNum(void)   //调用这个函数,就可以返回按下按键的键码
{
	 uint8_t KeyNum = 0;
	 if (GPIO_ReadInputDataBit(GPIOE,GPIO_Pin_3) == 0)
	 {
		 Delay_ms(20);
		 while(GPIO_ReadInputDataBit(GPIOE,GPIO_Pin_3) == 0);
		 Delay_ms(20);
		 
		 KeyNum = 1;
	 }
	 if (GPIO_ReadInputDataBit(GPIOE,GPIO_Pin_2) == 0)
	 {
		 Delay_ms(20);
		 while(GPIO_ReadInputDataBit(GPIOE,GPIO_Pin_2) == 0);
		 Delay_ms(20);
		 
		 KeyNum = 2;
	 }
	
	 return KeyNum;     //将这个变量作为返回值
}

Key.h:

cs 复制代码
#ifndef __KEY_H
#define __KEY_H

void Key_Init(void);

uint8_t Key_GetNum(void);

#endif
相关推荐
wuyk5556 小时前
98.C语言易混难点:字符数组与字符串指针的底层差异
c语言·开发语言·c++·stm32·嵌入式硬件·算法
恒锐丰-罗生7 小时前
国产 H 桥驱动 SS6289|18V/4A 有刷电机驱动芯片性与应用分析 内置采样 + 多保护,门锁 / 机器人优选器件
驱动开发·嵌入式硬件·硬件工程
zcmodeltech11 小时前
工程机械与矿山机械沙盘模型多系统协同控制系统设计:基于STM32与Modbus RTU的露天开采-井下掘进-智慧矿山全场景联动方案
数据库·stm32·单片机·嵌入式硬件·制造·多分类
云泽80812 小时前
深入浅出 STM32(十五):多通道GPIO驱动的软件抽象与实现
stm32·单片机·嵌入式硬件
嵌入式阿蔡12 小时前
OTA 实战五(收官):量产落地 Checklist —— 版本号 / 防回滚 / 灰度 / 断点续传
网络·stm32·单片机·嵌入式硬件·嵌入式实时数据库
碧海银沙音频科技研究院14 小时前
基于杰理AC7016C的GTCRN门控卷积神经网络语音增强方法
人工智能·嵌入式硬件·语音识别
ACP广源盛1392462567314 小时前
Qwen3.8‑2.4T 开源落地@ACP#国产 MoE 大模型私有化部署中 DP 重定时器 GSV6155 的硬件链路价值与应用场景
大数据·数据库·人工智能·嵌入式硬件·矩阵·开源
遇雪长安15 小时前
STM32_BOOT未引出_串口升级系统Bootloader方案
stm32·单片机·嵌入式硬件·bootloader
dcdannycheung15 小时前
【无标题】
嵌入式硬件
自小吃多15 小时前
Capture软件原理图添加元器件笔记
笔记·嵌入式硬件