以前幫別人做過壹個類似的
#include<reg52.h>
#include <def.h> ?
#include"lcd.c"
uchar key;
uchar num;
sbit alarm=P2^4;
sbit open=P2^1;
uchar code table[]={' ','1','2','3',
'4','5','6','7',
'8','9','0'};
void keyscan();
uchar hptable[10];
uchar password[6];
uchar wordbuf[6];
uchar fom[]="input password:";
uchar change[]="set mask:";
uchar state1[]="right";
uchar state2[]="error";
uchar sflag;
uchar dflag,cnt;
uchar setf,fset;
uchar flag;
void main ()
{
uchar hp;
alarm=0;
flag=0;
setf=0;
fset=0;
dflag=0;
lcden=0;
lcdrw=1;
lcdrs=1;
P3=0x0f;
for(hp=0;hp<6;hp++)
{
password[hp]='8';
}
write(0x38,0); ?//0為寫命令
delay(9);
write(0x08,0);
write(0x01,0);
write(0x06,0);
write(0x0c,0);
write(0x80,0);
for(hp=0;hp<15;hp++)
{
write(fom[hp],1);
}
while(1)
{
// alarm=!alarm;
keyscan();
if(setf==0)
{
if(num>=1&&num<=10)
{
write(0x80+0x40+cnt,0);
write('*',1);
wordbuf[cnt]=table[num];
cnt++;
num=0;
}
}
else
{
write(0x01,0);
write(0x80,0);
for(hp=0;hp<9;hp++)
{
write(change[hp],1);
}
fset=1;
cnt=0;
while(fset)
{
keyscan();
if(num>=1&&num<=10)
{
write(0x80+0x40+cnt,0);
write('*',1);
password[cnt]=table[num];
cnt++;
num=0;
}
if(num==11)
{
write(0x80+0x40+cnt-1,0);
write('\0',1);
cnt--;
num=0;
}
if(num==13)
{
flag=1;
num=0;
fset=0;
setf=0;
// goto chuz;
}
}
cnt=0;
}
if(flag==1)
{
write(0x01,0);
write(0x80,0);
for(hp=0;hp<15;hp++)
{
write(fom[hp],1);
}
flag=0;
}
if(num==11)
{
write(0x80+0x40+cnt-1,0);
write('\0',1);
cnt--;
num=0;
}
if(num==12)
{
if(dflag==1)
setf=1;
num=0;
}
if(num==13)
{
num=0;
write(0x80+0x40+10,0);
if(wordbuf[0]==password[0]&&wordbuf[1]==password[1]&&wordbuf[2]==password[2]&&wordbuf[3]==password[3]&&wordbuf[4]==password[4]&&wordbuf[5]==password[5])
{
for(hp=0;hp<5;hp++)
{
write(state1[hp],1);
}
sflag=0;
dflag=1;
}
else
{
for(hp=0;hp<5;hp++)
{
write(state2[hp],1);
}
sflag++;
}
}
if(sflag==3)
{
alarm=!alarm;
//delay(9);
}
if(num==14)
{
num=0;
cnt=0;
write(0x01,0);
write(0x80,0);
for(hp=0;hp<15;hp++)
{
write(fom[hp],1);
}
}
if(num==15)
{
if(dflag==1)
{
open=0;
dflag=0;
}
}
}
}
void keyscan ()
{
uchar tem,temp;
P3=0xfe;
temp=P3;
tem=temp&0xf0;
if (tem!=0xf0) //沒有鍵按下時它的值是不變的,即仍為0XF0,而有鍵接下時要變
{
delay(5); //延時消抖
temp=P3;
tem=temp&0xf0;
if (tem!=0xf0) //確認有鍵按下
{
temp=P3;
tem=temp&0xf0; ? //這裏也可以不要這壹步,但下面的case裏的值也要變
switch (tem)
{
case 0xe0: num=0; break;
case 0xd0: num=1; break;
case 0xb0: num=2; break;
case 0x70: num=3; break;
default : ?; break ;
}
while(tem!=0xf0) //等待松手
{
temp=P3;
tem=temp&0xf0;
}
}
}
P3=0xfd;
temp=P3;
tem=temp&0xf0;
if (tem!=0xf0)
{
delay(5);
temp=P3;
tem=temp&0xf0;
if (tem!=0xf0)
{
temp=P3;
tem=temp&0xf0;
switch (tem)
{
case 0xe0: num=4; break;
case 0xd0: num=5; break;
case 0xb0: num=6; break;
case 0x70: num=7; break;
default : ?; break ;
}
while(tem!=0xf0)
{
temp=P3;
tem=temp&0xf0;
}
}
}
P3=0xfb;
temp=P3;
tem=temp&0xf0;
if (tem!=0xf0)
{
delay(5);
temp=P3;
tem=temp&0xf0;
if (tem!=0xf0)
{
temp=P3;
tem=temp&0xf0;
switch (tem)
{
case 0xe0: num=8; break;
case 0xd0: num=9; break;
case 0xb0: num=10; break;
case 0x70: num=11; break;
default : ?; break ;
}
while(tem!=0xf0)
{
temp=P3;
tem=temp&0xf0;
}
}
}
P3=0xf7;
temp=P3;
tem=temp&0xf0;
if (tem!=0xf0)
{
delay(5);
temp=P3;
tem=temp&0xf0;
if (tem!=0xf0)
{
temp=P3;
tem=temp&0xf0;
switch (tem)
{
case 0xe0: num=12; break;
case 0xd0: num=13; break;
case 0xb0: num=14; break;
case 0x70: num=15; break;
default : ?; break ;
}
while(tem!=0xf0)
{
temp=P3;
tem=temp&0xf0;
}
}
}
}