stm32—按键控制LED灯亮灭

LED灯模块代码请看此文
stm32GPIO-LED闪烁解读-CSDN博客文章浏览阅读562次,点赞13次,收藏3次。此篇代码我主要沿用的是UP主的模式此外,在此篇文章中我只是陈述代码,对基本理论不再做过多陈述,如果细节可以看CSDN。https://blog.csdn.net/weixin_54210362/article/details/140807079?spm=1001.2014.3001.5501


一、Key.h

#ifndef __KEY_H
#define __KEY_H

///
//移植修改区
//按照实际端口修改
//较前版本更加容易移植(注意,端口仅仅适用于非JTAG/SWD引脚,如果是JTAG引脚,需要打开AFIO时钟,并失能JTAG)

#define RCC_ALL_KEY 	( KEY0_GPIO_CLK  | KEY1_GPIO_CLK )
#define RCC_ALL_GPIO_PIN  (KEY0_GPIO_PIN | KEY1_GPIO_PIN)

#define KEY0_GPIO_PIN		GPIO_Pin_1				//KEY0引脚号
#define KEY0_PIN_ID			1						//KEY0引脚序号
#define KEY0_GPIO_PORT		GPIOB					//KEY0端口号
#define KEY0_GPIO_CLK		RCC_APB2Periph_GPIOB	//KEY0时钟
#define KEY0_FUN_OUT		PBout					//KEY0输出端口配置函数
//#define KEY0_FUN_IN		PBin					//KEY0输入端口配置函数

#define KEY1_GPIO_PIN		GPIO_Pin_11				//KEY1引脚号
#define KEY1_PIN_ID			11						//KEY1引脚序号
#define KEY1_GPIO_PORT		GPIOB					//KEY1端口号
#define KEY1_GPIO_CLK		RCC_APB2Periph_GPIOB	//KEY1时钟
#define KEY1_FUN_OUT		PBout					//KEY1输出端口配置函数
//#define KEY1_FUN_IN		Pin					//KEY1输入端口配置函数

void Key_Init();
uint8_t Key_Scan(void);

#endif

二、key.c

注意:使用按键的时候要注意电平抖动

           硬件抖动可以通过简单的延时函数避免,在Key_scan函数中有涉及到,可以自己尝试一下

#include "stm32f10x.h"
#include "Key.h"
#include "Delay.h"
/*
*********************************************************************************************************
*	函 数 名: Key_Init
*	功能说明: 配置Key按键相关的GPIO.
*	形    参:  无
*	返 回 值: 无
*********************************************************************************************************
*/
void Key_Init()
{
	//定义结构体
	GPIO_InitTypeDef GPIO_InitStructure;
	//打开时钟
	RCC_APB2PeriphClockCmd(RCC_ALL_KEY,ENABLE);
	//结构体赋值
	GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IPU;
	GPIO_InitStructure.GPIO_Pin = KEY0_GPIO_PIN|KEY1_GPIO_PIN ;
	GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
	//调用GPIO_Init()函数进行初始化
	GPIO_Init(KEY0_GPIO_PORT,&GPIO_InitStructure);
}
/*
*********************************************************************************************************
*	函 数 名: Key_scan函数
*	功能说明: 通过读取端口上的电平,确定哪个按键被按下了
*	形    参:无
*	返 回 值: KeyNum
*********************************************************************************************************
*/
uint8_t Key_Scan(void)
{
	uint8_t KeyNum = 0;
	if(GPIO_ReadInputDataBit( KEY0_GPIO_PORT, KEY0_GPIO_PIN)==0)
	{
		Delay_ms(20);
		while (GPIO_ReadInputDataBit( KEY0_GPIO_PORT, KEY0_GPIO_PIN)==0);	//等待按键松手
		Delay_ms(20);											//延时消抖
		KeyNum = 1;												//置键码为1
		
	}
	else if(GPIO_ReadInputDataBit( KEY1_GPIO_PORT, KEY1_GPIO_PIN) ==0        )
	{
		Delay_ms(20);											//延时消抖
		while (GPIO_ReadInputDataBit(KEY1_GPIO_PORT, KEY1_GPIO_PIN) == 0);	//等待按键松手
		Delay_ms(20);											//延时消抖
		KeyNum = 2;												//置键码为2
	}
	return KeyNum;

}

三、main.c文件

#include "stm32f10x.h"                  // Device header
#include <stdio.h>
#include "LED.h"
#include "Delay.h"
#include "Key.h"

uint8_t KEY_Num;

int main(void)
{
	
	LED_Init();
	Key_Init();
	while(1)
	{	
		KEY_Num = Key_Scan();        //获取键值,用来判断是哪一个按键按下

		if (KEY_Num  == 1)
		{
			Led_Toggle(1);
		}
		if (KEY_Num  == 2)
		{
			Led_Toggle(2);
		}
		
	}                                       
}

作者:打地基的小白_CH

物联沃分享整理
物联沃-IOTWORD物联网 » stm32—按键控制LED灯亮灭

发表回复