STM32按键LED灯蜂鸣器
按键、LED灯、蜂鸣器库初始化
按键库
#ifndef __KEY_H
#define __KEY_H //使库没有被多次定义
#include "sys.h"
#define KEY0 GPIO_ReadInputDataBit(GPIOE,GPIO_Pin_4)
#define KEY1 GPIO_ReadInputDataBit(GPIOE,GPIO_Pin_3)
#define KEY2 GPIO_ReadInputDataBit(GPIOE,GPIO_Pin_2)
#define WK_UP GPIO_ReadInputDataBit(GPIOA,GPIO_Pin_0)
//对每个按键进行宏定义
#define KEY0_PRES 1
#define KEY1_PRES 2
#define KEY2_PRES 3
#define WKUP_PRES 4 //将每个按键按下的状态转化为数字
void KEY_Init(void);
u8 KEY_Scan(u8);
#endif
//多一行回车不会报warning
按键库主代码
#include "key.h"
#include "delay.h"
void KEY_Init(void)
{
GPIO_InitTypeDef GPIO_InitStructure;
RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOA|RCC_AHB1Periph_GPIOE, ENABLE);
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_2|GPIO_Pin_3|GPIO_Pin_4;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_100MHz;
GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_UP;
GPIO_Init(GPIOE, &GPIO_InitStructure);
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_0;
GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_DOWN ;
GPIO_Init(GPIOA, &GPIO_InitStructure);
}
//将按键所对应的引脚进行库函数的状态初始化,设置输出方式,输出速度
u8 KEY_Scan(u8 mode)
{
static u8 key_up=1;
if(mode)key_up=1;
if(key_up&&(KEY0==0||KEY1==0||KEY2==0||WK_UP==1))
{
delay_ms(10);
key_up=0;
if(KEY0==0)return 1;
else if(KEY1==0)return 2;
else if(KEY2==0)return 3;
else if(WK_UP==1)return 4;
}else if(KEY0==1&&KEY1==1&&KEY2==1&&WK_UP==0)key_up=1;
//写入连续按键和不连续按键的库函数
return 0;
}
LED灯库和蜂鸣器库
#ifndef __LED_H
#define __LED_H
#include "sys.h"
#define LED0 PFout(9)
#define LED1 PFout(10) //将对应端口定义为变量,方便改变
void LED_Init(void);
#endif
#include "led.h"
void LED_Init(void)
{
GPIO_InitTypeDef GPIO_InitStructure;
RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOF, ENABLE);
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_9 | GPIO_Pin_10;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_OUT;
GPIO_InitStructure.GPIO_OType = GPIO_OType_PP;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_100MHz;
GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_UP;
GPIO_Init(GPIOF, &GPIO_InitStructure);
GPIO_SetBits(GPIOF,GPIO_Pin_9 | GPIO_Pin_10);
}
#ifndef __BEEP_H
#define __BEEP_H
#include "sys.h"
#define BEEP PFout(8)
void BEEP_Init(void);
#endif
#include "beep.h"
void BEEP_Init(void)
{
GPIO_InitTypeDef GPIO_InitStructure;
RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOF, ENABLE);
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_8;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_OUT;
GPIO_InitStructure.GPIO_OType = GPIO_OType_PP;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_100MHz;
GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_DOWN;
GPIO_Init(GPIOF, &GPIO_InitStructure);
GPIO_ResetBits(GPIOF,GPIO_Pin_8);
}
主函数
#include "sys.h"
#include "delay.h"
#include "usart.h"
#include "led.h"
#include "beep.h"
#include "key.h" //调用所需要的函数库
int main(void)
{
u8 key;
delay_init(168);
LED_Init();
BEEP_Init();
KEY_Init(); //按键、LED灯、蜂鸣器初始化
LED0=1;
LED1=1; //没按键时灯不亮
while(1)
{
key=KEY_Scan(0);
if(key)
{
switch(key)
{
case 4:
BEEP=!BEEP;
LED0=!LED0;
LED1=!LED1;
break;
case 3:
BEEP=!BEEP;
LED0=!LED0;
LED1=!LED1;
break;
case 2:
BEEP=!BEEP;
LED0=!LED0;
LED1=!LED1;
break;
case 1:
BEEP=!BEEP;
LED0=!LED0;
LED1=!LED1;
break;
}
}else delay_ms(10);
}
}
博客围绕STM32展开,介绍了按键、LED灯、蜂鸣器库的初始化,包含按键库及其主代码、LED灯库和蜂鸣器库,还提及主函数,最后有相关视频展示。
4143

被折叠的 条评论
为什么被折叠?



