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
相关推荐
工匠Sola1 小时前
STM32G473VET6 在 Keil MDK 下手动移植 FreeRTOS 指南
stm32·单片机·嵌入式硬件
道一231 小时前
STM32 微控制器库RCC_ClkInitTypeDef结构参数介绍
stm32·单片机·嵌入式硬件
道一231 小时前
STM32 微控制器库RCC_OscInitTypeDef结构参数介绍
stm32·单片机·嵌入式硬件
厂太_STAB_丝针2 小时前
【自学嵌入式(11)闪存文件系统的应用】
c语言·单片机·嵌入式硬件
电子小子洋酱2 小时前
ESP32移植Openharmony外设篇(9)NB-IOT
单片机·物联网·华为·harmonyos·鸿蒙
RFID舜识物联网2 小时前
RFID测温技术:为生产安全与稳定保驾护航
大数据·人工智能·嵌入式硬件·物联网
北京迅为2 小时前
【北京迅为】itop-3568 开发板openharmony鸿蒙烧写及测试-第1章 体验OpenHarmony—烧写镜像
人工智能·单片机·嵌入式硬件·harmonyos·鸿蒙
影阴2 小时前
HAL库之是stm32cubemx安装
stm32·单片机·嵌入式硬件
宋隽颢3 小时前
STM32学习【4】ARM汇编(够用)
arm开发·stm32·学习
谏君之3 小时前
编写第一个 C++ 程序 – Hello World 示例
开发语言·c++·单片机·学习