#include<at89c51.h>
#define uint unsigned int
#define uchar unsigned char
uchar key,key_num,sbuf_out,led_sta;
uchar countm,buf[2];
sbit keep=P3^7;
void delay(uchar x)
{
uchar y,z;
for(y=x;y>0;y--)
for(z=100;z>0;z--);
}
uchar keyscan()
{
uchar code_h;
uchar code_l;
P1=0XF0;
if((P1&0xF0)!=0XF0)
{
delay(5);
if((P1&0xF0)!=0XF0)
{
code_h=0xfe;
while((code_h&0x10)!=0x00)
{
P1=code_h;
if((P1&0xF0)!=0XF0)
{
code_l=(P1&0xF0|0x0F);
return((~code_h)+(~code_l));
}
else
code_h=(code_h<<1)|0x01;
}
}
}
return(0);
}
void keynum()
{
uchar i,j;
uchar code tab[4][4]={{1,2,3,4},{5,6,7,8},{9,10,11,12},{13,14,15,16}};
key=0;
key=keyscan();
if((key&0x01)!=0) i=0;
if((key&0x02)!=0) i=1;
if((key&0x04)!=0) i=2;
if((key&0x08)!=0) i=3;
if((key&0x10)!=0) j=0;
if((key&0x20)!=0) j=1;
if((key&0x40)!=0) j=2;
if((key&0x80)!=0) j=3;
if(key!=0) key_num=tab[i][j];
}
void TAKE_SBUF(uchar dat)
{
ES=0;
SBUF=dat;
while(~TI);
TI=0;
SBUF=dat;
while(~TI);
TI=0;
ES=1;
}
void keyplay()
{
bit take_key;
keynum();
if(key_num==13)
{
take_key=1;
sbuf_out=0x00;
led_sta=0xff;
P2=0xff;
P0=0xbf;
}
if(key_num==14)
{
take_key=1;
sbuf_out=0x00;
led_sta=0x00;
P2=0xff;
P0=0xbf;
}
while(take_key)
{
keynum();
while(key!=0)
keynum();
if(key_num<9)
{
sbuf_out=sbuf_out|(0x80>>(key_num-1));
key_num=17;
P2=~sbuf_out;
}
if(key_num==15)
{
TAKE_SBUF(sbuf_out);
key_num=17;
take_key=0;
delay(250);
TAKE_SBUF(led_sta);
}
}
}
void main()
{
SCON=0XD0;
TMOD=0X20;
TH1=0XFD;
TL1=0XFD;
TR1=1;
EA=1;
ES=1;
P2=0xff;
while(1)
{
keyplay();
}
}
void setled() interrupt 4
{
buf[countm]=SBUF;
countm++;
if(countm>1)
{
countm=0;
if(buf[0]==sbuf_out)
{
if(buf[1]==led_sta)
{
if(buf[1]==0xff)
P0=0xf9;
if(buf[1]==0x00)
P0=0xc0;
keep=0;
delay(250);
keep=1;
}
}
}
RI=0;
}
|