最新回复
#include
#include
#include
extern viod start_timer2(void);
unsigned char scankey;
bit timerout=0;
bit keypress;
#define kr0 p1_0
#define kr1 p1_1
#define kr2 p1_2
#define kr3 p1_3
#define kc0 p1_4
#define kc1 p1_5
#define kc2 p1_6
#define kc3 p1_7
#define kbd_port p1
#define kbd_val_vaild 0x80
#define fsm_wait_press 0x01
#define fsm_wait_relese 0x04
#define fsm_verfy1 0x02
#define fsm_verfy2 0x03
#define fsm_init 0x00
main()
{
unsigned char fsm_key=0;
unsigned char kbd_col;
unsigned char kbd_row;
unsigned char key_val;
ie=0x40;
start_timer2();
ea=1;
while(1)
{
if(timerout)
{
timerout=0;
switch(fsm_key)
{
case fsm_init: kr0=1;kr1=1;kr2=1;kr3=1;fsm_key=fsm_wait_press;break;//行全部置1
case fsm_wait_press:
if(keypress)//如果有键按下keypress=1,否则为0
{
key_val=0;返回值清0
if(kc0) {kc0=1;kbd_col=1;}
else if(kc1) {kc1=1;kbd_col=2;}
else if(kc2) {kc2=1;kbd_col=3;}
else if(kc3) {kc3=1;kbd_col=4;}//行全部置1,检测列,如果该列读到1,证明该列有键被按下
else {fsm_key=fsm_init;}
kbd_row=kbd_port;
kbd_row>>=4;
if(kbd_row)
{
if(kr0) {kbd_row=0;fsm_key=fsm_verfy1;}//有键按下的列置1,检测行,如果该行读到1,证明该行有键被按下
else if(kr1) {kbd_row=4;fsm_key=fsm_verfy1;}
else if(kr2) {kbd_row=8;fsm_key=fsm_verfy1;}
else if(kr3) {kbd_row=12;fsm_key=fsm_verfy1;}
else {fsm_key=fsm_init;}
} break; case fsm_verfy1: switch(kbd_row)
{
case 0: if(kr0) fsm_key=fsm_verfy2;
else fsm_key=fsm_init; break;
case 4: if(kr1) fsm_key=fsm_verfy2;
else fsm_key=fsm_init; break;
case 8: if(kr0) fsm_key=fsm_verfy2;
else fsm_key=fsm_init; break;
case 12: if(kr1) fsm_key=fsm_verfy2;
else fsm_key=fsm_init; break;
default: fsm_key=fsm_init; break;
} break;
case fsm_verfy2: switch(kbd_row)
{
case 0: if(kr0) key_val=(kbd_row+kbd_col)|kbd_val_vaild; break;
case 4: if(kr1) key_val=(kbd_row+kbd_col)|kbd_val_vaild; break;
case 8: if(kr0) key_val=(kbd_row+kbd_col)|kbd_val_vaild; break;
case 12: if(kr1) key_val=(kbd_row+kbd_col)|kbd_val_vaild; break;
default: break;
} kr0=1;kr1=1;kr2=1;kr3=1;fsm_key=fsm_wait_relese; break; case fsm_wait_relese: if(!keypress){fsm_key=fsm_init;}break;
default: fsm_key=fsm_init; break;
}
}
}
}
void timer2() interrupt 5
{
TF2=0;
Timerout=1;
scankey=kbd_port;
if(scankey&0x0f){ keypress=1;}
else{ keypress=0;}
}
详情
回复
发表于 2010-3-16 17:29
| |
|
|
| |
|
|
此帖出自单片机论坛
| ||
个人签名要把目标订的实现起来辛苦一点!
|
||
曾经的版主且威望大于2000,或对EEWORLD论坛有突出贡献的坛友
EEWorld Datasheet 技术支持