05_按键控制LED

本文介绍了如何在STM32F10x平台中,通过GPIO控制LED,包括创建硬件文件夹、编写驱动程序(LED.c和Key.c)以及主函数,展示了如何处理按键输入并控制LED灯的状态。
摘要由CSDN通过智能技术生成

按键控制LED

流程

新建文件夹Hardware用于存放文件驱动
在这里插入图片描述

在这里插入图片描述

在这里插入图片描述
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述

LED.c用于存放驱动程序的主体代码
LED.h用于存放这个驱动程序可以对外提供的函数或变量声明
在这里插入图片描述
以上是防止头文件被重复包含的代码

#ifndef __LED_H //如果没有定义LED这个字符串
#define __LED_H //那么就定义这个字符串

#endif // 与#ifndef __LED_H为一对
//文件一定要以空行结尾

ctrl+alt+空格进行代码提示
ctrl+空格也可以,记得会和输入法冲突,取消输入法快捷键

主函数

#include "stm32f10x.h"                  // Device header
#include "Delay.h"
#include "LED.h"
#include "Key.h"
#include "stdint.h"

uint8_t KeyNum;//用于读取返回的键码值

int main(void)
{
	LED_Init();
	Key_Init();
	
	while(1)
	{
		KeyNum=Key_GetNum();
		if(KeyNum==1)//如果按键1按下了
		{
			LED1_Turn();
		}
		if(KeyNum==2)//如果按键2按下了
		{
			LED2_Turn();
		}
	}
	
}

LED.c

#include "stm32f10x.h"                  // Device header

void LED_Init(void)
{
	//ctrl+alt+空格进行代码提示
	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); //1,2口置高电平,即LED熄灭
}


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

void LED1_OFF(void)
{
	GPIO_SetBits(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 LED1_Turn(void) // LED1状态取反
{
	if(GPIO_ReadOutputDataBit(GPIOA,GPIO_Pin_1)==0)//读取输出寄存器的一位,读取端口输出状态
	{
		GPIO_SetBits(GPIOA,GPIO_Pin_1);//把Pin1置1
	}
	else
	{
		GPIO_ResetBits(GPIOA,GPIO_Pin_1);//把Pin1置0
	}
}

void LED2_Turn(void) // LED1状态取反
{
	if(GPIO_ReadOutputDataBit(GPIOA,GPIO_Pin_2)==0)//读取输出寄存器的一位,读取端口输出状态
	{
		GPIO_SetBits(GPIOA,GPIO_Pin_2);//把Pin2置1
	}
	else
	{
		GPIO_ResetBits(GPIOA,GPIO_Pin_2);//把Pin2置0
	}
}

LED.h

#ifndef __LED_H
#define __LED_H

void LED_Init(void);
void LED1_ON(void);
void LED1_OFF(void);		
void LED2_OFF(void);
void LED2_ON(void);
void LED1_Turn(void);
void LED2_Turn(void);

#endif

Key.c

#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等于unsigned char
{
	uint8_t KeyNum = 0;
	if(GPIO_ReadInputDataBit(GPIOB,GPIO_Pin_1)==0)//读取pin1口值,如果为0,表示按键按下
	{
		Delay_ms(20);//按键消抖
		while(GPIO_ReadInputDataBit(GPIOB,GPIO_Pin_1)==0)//检测按键是否松手,松手后pin1为高电平,跳出while循环,否则一直在while里面循环
		Delay_ms(20);//按键松手消抖
		KeyNum=1;
		
	}
	if(GPIO_ReadInputDataBit(GPIOB,GPIO_Pin_11)==0)//读取pin1口值,如果为0,表示按键按下
	{
		Delay_ms(20);//按键消抖
		while(GPIO_ReadInputDataBit(GPIOB,GPIO_Pin_11)==0)//检测按键是否松手,松手后pin1为高电平,跳出while循环,否则一直在while里面循环
		Delay_ms(20);//按键松手消抖
		KeyNum=2;
		
	}
	return KeyNum;
}

Key.h

#ifndef __KEY_H
#define __KEY_H
#include "stdint.h"
void Key_Init(void);
uint8_t Key_GetNum(void);
#endif

  • 6
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值