#include<reg51.h>
unsigned char code dispbitcode[]={0xfe,0xfd,0xfb,0xf7,
0xef,0xdf,0xbf,0x7f};
unsigned char code dispcode[]={0x3f,0x06,0x5b,0x4f,0x66,
0x6d,0x7d,0x07,0x7f,0x6f,
0x77,0x7c,0x39,0x5e,0x79,0x71,
0x00,0x40,0x73,0xff};
unsigned char k,keycount=0,flashcount=0,errorcount=0,num_bit=6;
unsigned char get_num[6]={0};
unsigned char init_num[6]={1,2,3,4,5,6};
bit rightflag=0,alarmflag=0,errorflag=0,keyoverflag=0;
unsigned char keys;
sbit P1_6=P1^6;
sbit P1_7=P1^7;
sbit P0_7=P0^7;
void main(void)
{
unsigned char read_keys(unsigned char m);
void in_keys(unsigned char x);
void check_keys();
void del_keys();
void change_keys();
TMOD=0x01;
TH0=(65536-4000) / 256;
TL0=(65536-4000) % 256;
TR0=1;
ET0=1;
EA=1;
for ( k=4;;k++)
{
keys=read_keys(k);
if ((keys>=0) && (keys<10))
{
if(keycount<6)
in_keys(keys);
else
keyoverflag=1;
}
else if (keys==14)
check_keys();
else if (keys==12)
{
if (keycount>0)
del_keys();
else
keyoverflag=1;
}
else if (keys==13)
{
if (rightflag==1)
change_keys();
}
else if (keys==10)
{
rightflag=0;
errorflag=0;
keycount=0;
}
else if (keys==11)
{
alarmflag=0;
errorflag=0;
rightflag=0;
}
if (k==7)
k=3;
}
}
void t0(void) interrupt 1 using 0
{
TR0=0;
TH0=(65536-4000) / 256;
TL0=(65536-4000) % 256;
if (rightflag==1)
P1_7=1;
else
P1_7=0;
if (errorflag==1)
P0_7=~P0_7;
else
P0_7=0;
if (alarmflag==1)
{
P1_6=~P1_6;
}
else
P1_6=0;
if (keycount==0)
{
P1=P1 | 0x3f;
P2=dispcode[0];
P1=P1 & dispbitcode[0];
}
else
{
P1=P1 |0X3f;
P2=dispcode[8];
P1=P1 & dispbitcode[flashcount];
flashcount++;
if (flashcount>=keycount)
flashcount=0;
}
TR0=1;
}
unsigned char read_keys(unsigned char m)
{
unsigned char temp,i,j,key;
P3=dispbitcode[m];
temp=P3;
temp=temp & 0x0f;
if (temp!=0x0f)
{
for(i=50;i>0;i--)
for(j=200;j>0;j--);
temp=P3;
temp=temp & 0x0f;
if (temp!=0x0f)
{
temp=P3;
P1_6=~P1_6;
temp=temp & 0x0f;
switch(temp)
{
case 0x0e:
if (m==4)
key=7;
if (m==5)
key=4;
if (m==6)
key=1;
if (m==7)
key=15;
break;
case 0x0d:
if (m==4)
key=8;
if (m==5)
key=5;
if (m==6)
key=2;
if (m==7)
key=0;
break;
case 0x0b:
if (m==4)
key=9;
if (m==5)
key=6;
if (m==6)
key=3;
if (m==7)
key=14;
break;
case 0x07:
if (m==4)
key=10;
if (m==5)
key=11;
if (m==6)
key=12;
if (m==7)
key=13;
break;
}
while(temp!=0x0f)
{
temp=P3;
temp=temp & 0x0f;
}
}
}
else
key=16;
return key;
}
void in_keys(unsigned char x)
{
get_num[keycount]=x;
keycount++;
}
void check_keys()
{
unsigned char i=0;
while (i<keycount)
{
if (get_num[i]==init_num[i])
i++;
else
break;
}
if (i==num_bit)
{
rightflag=1;
errorflag=0;
}
else
{
errorflag=1;
rightflag=0;
errorcount++;
}
if (errorcount==3)
{
alarmflag=1;
errorcount=0;
}
keycount=0;
}
void del_keys()
{
keycount--;
}
void change_keys()
{
unsigned char num;
for ( num=0;num<keycount;num++)
init_num[num]=get_num[num];
num_bit=keycount;
keycount=0;
}