新手入门
- 积分
- 7
- 金钱
- 7
- 注册时间
- 2016-8-8
- 在线时间
- 15 小时
|

楼主 |
发表于 2016-8-10 18:55:07
|
显示全部楼层
#include "stm32f10x.h"
#include "key.h"
#include "delay.h"
void Key_Init(void)
{
GPIO_InitTypeDef GPIO_InitStructure; //定义IO口模式结构体
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA,ENABLE); //使能PA端口
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOE,ENABLE); //使能PE端口
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IPD; //设置为下拉输入
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_0; //设置位0口
GPIO_Init(GPIOA,&GPIO_InitStructure); //将PA0口设置为以上模式
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IPU; //设置为上拉模式
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_2 | GPIO_Pin_3 | GPIO_Pin_4; //设置位2,3,4口
GPIO_Init(GPIOE,&GPIO_InitStructure); //设置PE2,3,4口为以上模式
}
u8 Key_Mode (void) //按键模式选择
{
static u8 Key_No = 1; //是否允许键盘长按连续输入变量
static u8 modeChoose = 1; //键盘模式1
if (Key_Up && Key_No) //键盘按下
{
delay_ms(15); //延时消抖
if (Key_Up) //确认键盘确实被按下
{
modeChoose = ~modeChoose; //变化模式
Key_No = 0; //长按时,下次下次扫描键盘时将不会进入“if”语句,防止模式连续转变
}
}
else if (Key_Up == 0) //确认键盘已松手
Key_No = 1; //允许进入“if”语句,改变mode
return (modeChoose);
}
u8 Key_Scan (u8 mode) //按键扫面,由主函数赋值mode
{
static u8 Key_Yes = 1; //允许长按键盘
if (mode) Key_Yes = 1; //当键盘模式为1时,长按键盘可以一直进入“if”语句中,一直有返回值
if (Key_Yes && (Key0==0 || Key1==0 || Key2==0)) //按下按键
{
delay_ms(10); //延时消抖
if (Key0 == 0) return (1);
else if (Key1 == 0) return (2);
else if (Key2 == 0) return (3); //判断哪个按键被按下,并返回对应的值
Key_Yes = 0;
}
else if (Key0==1 && Key1==1 && Key2==1) //检测松手
{
Key_Yes = 1; //允许下次按下键盘
}
return(0);
}
|
|