我的键盘扫描程序
时间:10-02
整理:3721RD
点击:
我的硬件连接是P1的P1^4到P1^7是按键控制,然后我就是想用P1^4来控制P0上的一个灯,按一下就亮,再按一下就灭,我用的是状态转移法,写完程序后,P0上的等一直是灭的,求大侠们看看问题出在哪里。
#include<reg52.h>
sbit ZSD=P0^0;
bit KEY_TIME=0;
#define KEY_VALUE_1 0xe0 //11110000
#define KEY_VALUE_2 0xd0 //11010000
#define KEY_VALUE_3 0xb0 //10110000
#define KEY_VALUE_4 0x70 //01110000
#define KEY_NULL 0xf0 //11110000
//定义状态返回值(按下 长按 连发 释放)
#define KEY_DOWN 0x08
#define KEY_LONG 0x04
#define KEY_CONTINUE 0x02
#define KEY_UP 0x01
//定义状态值
#define KEY_STATE_INIT 0
#define KEY_STATE_WOBBLE 1
#define KEY_STATE_PRESS 2
#define KEY_STATE_LONG 3
#define KEY_STATE_CONTINUE 4
#define KEY_STATE_RELEASE 5
//定义长按键的TICK数和连发的TICK数
#define KEY_LONG_PERIOD 1000
#define KEY_CONTINUE_PERIOD 25
sbit io_key_1=P1^4;
sbit io_key_2=P1^5;
sbit io_key_3=P1^6;
sbit io_key_4=P1^7;
//键值的返回函数
static unsigned char KeyScan(void)
{
if(io_key_1==0)return KEY_VALUE_1;
if(io_key_2==0)return KEY_VALUE_2;
if(io_key_3==0)return KEY_VALUE_3;
if(io_key_4==0)return KEY_VALUE_4;
return KEY_NULL;
}
//按键扫描函数
unsigned char GetKey()
{
static unsigned char s_u8KeyState=KEY_STATE_INIT;
static unsigned char s_u8KeyTimeCount=0;
static unsigned char s_u8LastKey=KEY_NULL;
unsigned char KeyTemp=KEY_NULL;
KeyTemp=KeyScan();
switch(s_u8KeyState)
{
case KEY_STATE_INIT:
{
if(KEY_NULL!=(KeyTemp))
{
s_u8KeyState=KEY_STATE_WOBBLE;
}
}
break;
case KEY_STATE_WOBBLE:
{
s_u8KeyState=KEY_STATE_PRESS;
}
break;
case KEY_STATE_PRESS:
{
if(KEY_NULL!=(KeyTemp))
{
s_u8LastKey=KeyTemp;
KeyTemp|=KEY_DOWN;
s_u8KeyState=KEY_STATE_LONG;
}
else
{
s_u8KeyState=KEY_STATE_INIT;
}
}
break;
case KEY_STATE_LONG:
{
if(KEY_NULL!=(KeyTemp))
{
if(++s_u8KeyTimeCount>KEY_LONG_PERIOD)
{
s_u8KeyTimeCount=0;
s_u8LastKey=KeyTemp;
KeyTemp|=KEY_LONG;
s_u8KeyState=KEY_STATE_CONTINUE;
}
}
else
{
s_u8KeyState=KEY_STATE_RELEASE;
}
}
break;
case KEY_STATE_CONTINUE:
{
if(KEY_NULL!=(KeyTemp))
{
if(++s_u8KeyTimeCount>KEY_CONTINUE_PERIOD)
{
s_u8KeyTimeCount=0;
s_u8LastKey=KeyTemp;
KeyTemp|=KEY_CONTINUE;
s_u8KeyState=KEY_STATE_CONTINUE;
}
}
else
{
s_u8KeyState=KEY_STATE_RELEASE;
}
}
break;
case KEY_STATE_RELEASE:
{
s_u8LastKey|=KEY_UP;
KeyTemp=s_u8LastKey;
s_u8KeyState=KEY_STATE_INIT;
}
break;
default:break;
}
return KeyTemp;
}
void main(void)
{
unsigned char KEY_VALUE=KEY_NULL;
io_key_1=1;
io_key_2=1;
io_key_3=1;
io_key_4=1;
ZSD=1;
TMOD|=0x01;
TH0=(65536-1000)/256;
TL0=(65535-1000)%256;
TR0=0;
ET0=0;
while(1)
{
if(KEY_TIME)
{
KEY_TIME=0;
KEY_VALUE=GetKey();
if(KEY_VALUE==0xe8)ZSD=~ZSD;
}
}
}
void Time0() interrupt 1
{
TH0=(65536-1000)/256;
TL0=(65535-1000)%256;
KEY_TIME=1;
}
#include<reg52.h>
sbit ZSD=P0^0;
bit KEY_TIME=0;
#define KEY_VALUE_1 0xe0 //11110000
#define KEY_VALUE_2 0xd0 //11010000
#define KEY_VALUE_3 0xb0 //10110000
#define KEY_VALUE_4 0x70 //01110000
#define KEY_NULL 0xf0 //11110000
//定义状态返回值(按下 长按 连发 释放)
#define KEY_DOWN 0x08
#define KEY_LONG 0x04
#define KEY_CONTINUE 0x02
#define KEY_UP 0x01
//定义状态值
#define KEY_STATE_INIT 0
#define KEY_STATE_WOBBLE 1
#define KEY_STATE_PRESS 2
#define KEY_STATE_LONG 3
#define KEY_STATE_CONTINUE 4
#define KEY_STATE_RELEASE 5
//定义长按键的TICK数和连发的TICK数
#define KEY_LONG_PERIOD 1000
#define KEY_CONTINUE_PERIOD 25
sbit io_key_1=P1^4;
sbit io_key_2=P1^5;
sbit io_key_3=P1^6;
sbit io_key_4=P1^7;
//键值的返回函数
static unsigned char KeyScan(void)
{
if(io_key_1==0)return KEY_VALUE_1;
if(io_key_2==0)return KEY_VALUE_2;
if(io_key_3==0)return KEY_VALUE_3;
if(io_key_4==0)return KEY_VALUE_4;
return KEY_NULL;
}
//按键扫描函数
unsigned char GetKey()
{
static unsigned char s_u8KeyState=KEY_STATE_INIT;
static unsigned char s_u8KeyTimeCount=0;
static unsigned char s_u8LastKey=KEY_NULL;
unsigned char KeyTemp=KEY_NULL;
KeyTemp=KeyScan();
switch(s_u8KeyState)
{
case KEY_STATE_INIT:
{
if(KEY_NULL!=(KeyTemp))
{
s_u8KeyState=KEY_STATE_WOBBLE;
}
}
break;
case KEY_STATE_WOBBLE:
{
s_u8KeyState=KEY_STATE_PRESS;
}
break;
case KEY_STATE_PRESS:
{
if(KEY_NULL!=(KeyTemp))
{
s_u8LastKey=KeyTemp;
KeyTemp|=KEY_DOWN;
s_u8KeyState=KEY_STATE_LONG;
}
else
{
s_u8KeyState=KEY_STATE_INIT;
}
}
break;
case KEY_STATE_LONG:
{
if(KEY_NULL!=(KeyTemp))
{
if(++s_u8KeyTimeCount>KEY_LONG_PERIOD)
{
s_u8KeyTimeCount=0;
s_u8LastKey=KeyTemp;
KeyTemp|=KEY_LONG;
s_u8KeyState=KEY_STATE_CONTINUE;
}
}
else
{
s_u8KeyState=KEY_STATE_RELEASE;
}
}
break;
case KEY_STATE_CONTINUE:
{
if(KEY_NULL!=(KeyTemp))
{
if(++s_u8KeyTimeCount>KEY_CONTINUE_PERIOD)
{
s_u8KeyTimeCount=0;
s_u8LastKey=KeyTemp;
KeyTemp|=KEY_CONTINUE;
s_u8KeyState=KEY_STATE_CONTINUE;
}
}
else
{
s_u8KeyState=KEY_STATE_RELEASE;
}
}
break;
case KEY_STATE_RELEASE:
{
s_u8LastKey|=KEY_UP;
KeyTemp=s_u8LastKey;
s_u8KeyState=KEY_STATE_INIT;
}
break;
default:break;
}
return KeyTemp;
}
void main(void)
{
unsigned char KEY_VALUE=KEY_NULL;
io_key_1=1;
io_key_2=1;
io_key_3=1;
io_key_4=1;
ZSD=1;
TMOD|=0x01;
TH0=(65536-1000)/256;
TL0=(65535-1000)%256;
TR0=0;
ET0=0;
while(1)
{
if(KEY_TIME)
{
KEY_TIME=0;
KEY_VALUE=GetKey();
if(KEY_VALUE==0xe8)ZSD=~ZSD;
}
}
}
void Time0() interrupt 1
{
TH0=(65536-1000)/256;
TL0=(65535-1000)%256;
KEY_TIME=1;
}
学习学习。
#define KEY_STATE_INIT 0
#define KEY_STATE_WOBBLE 1
#define KEY_STATE_PRESS 2
#define KEY_STATE_LONG 3
#define KEY_STATE_CONTINUE 4
#define KEY_STATE_RELEASE 5这个定义用枚举就行了
这个跟枚举每什么关系吧,问题应该不是出在这里啊!
按键是否需要消抖啊?
EA没打开。MAIN()中的if语句不成立吧