|
#include
#include
#include
#include
#define uchar unsigned char
#define uint unsigned int
uchar ledbuf[7]={0xfc,0xfa,0xf6,0xee,0xde,0xbe,0xff};
uchar led[11]={0x28,0x7E,0xA2,0x62,0x74,0x61,0x21,0x7A,0x20,0x60,0xff};
uchar wordbuf[6]; //字型码缓冲区
uchar tlcount;
uint count; //密码位计数
uchar pw[6]; //初始密码存储区
uchar pwbuf[6]; //输入密码存储区
uchar key;
uchar d,d1,d2,d3,d4,d5,d6;
bit pwflag; //密码正确与否标志
bit enterflag; //确认键按下与否标志
bit showflag; //数码管显示与否标志
sbit green=P3^0;
sbit red=P3^1;
void display(uchar w1,w2,w3,w4,w5,w6,d1,d2,d3,d4,d5,d6); // 显示函数
/*去键盘抖动函数*/
void delay(void)
{
uchar i,j;
for(i=0;i<100;i++)
for(j=0;j<6;j++)
;
}
/*键盘扫描函数 P1口的分布方式为 7,6,5,4,(列,高位分别为1,2,3,4列 )3,2,1,0(行,低位,分别为 1,2,3,4 行)*/
uchar keyscan(void)
{
uchar scancode,tmpcode;
P1=0xf0;
if((P1&0xf0)!=0xf0) //将行全部置为0,再判断列是否全为1,若不是,则说明该列有键按下
{
delay(); //延时去抖动
if((P1&0xf0)!=0xf0) //延时后再判断一次,去除抖动影响
{
scancode=0xfe; //将列全部置为1,逐行扫描第一行为0其余三行为1,若此时列上有不为1的值.
while((scancode&0x10)!=0) //则表示该行上有键按下,对应前面的行和列值得到键盘值
{
P1=scancode; //输出扫描码
if((P1&0xf0)!=0xf0) //本行有键按下
{
tmpcode=(P1&0xf0)|0x0f;
return((~scancode)+(~tmpcode));
}
else scancode=(scancode<<1)|0x01; //左移一位,与上0x01为保证只有一位为0
}
}
}
return(0); //没有键被按下
}
/*显示函数*/
void display(uchar w1,w2,w3,w4,w5,w6,d1,d2,d3,d4,d5,d6)
{
P2=ledbuf[w1]; //字位
P0=led[d1]; //送字型码
delay();
P2=ledbuf[w2]; //字位
P0=led[d2]; //送字型码
delay();
P2=ledbuf[w3]; //字位
P0=led[d3]; //送字型码
delay();
P2=ledbuf[w4]; //字位
P0=led[d4]; //送字型码
delay();
P2=ledbuf[w5]; //字位
P0=led[d5]; //送字型码
delay();
P2=ledbuf[w6]; //字位
P0=led[d6]; //送字型码
delay();
}
/*密码对比*/
bit pwcmp(void)
{
bit flag;
uchar i;
for(i=0;i<6;i++)
{
if(pw==pwbuf)
flag=1;
else
{
flag=0;
break;
}
}
return(flag); //返回对比标志
}
void getkey(void)
{
uchar j;
key=keyscan(); //调用键盘扫描函数
switch(key)
{
case 0x88: //1行1列 1
if(count<6)
{
wordbuf[count]=1; //对应密码位上显示1
pwbuf[count]=1; //输入的为1
d=pwbuf[count];
count++;
}
break;
case 0x48: //1行2列 2
if(count<6)
{
wordbuf[count]=2; //对应密码位上显示F
pwbuf[count]=2;
d=pwbuf[count];
count++;
}
break;
case 0x28: //1行3列 3
if(count<6)
{
wordbuf[count]=3; //对应密码位上显示F
pwbuf[count]=3;
d=pwbuf[count];
count++;
}
break;
/*case 0x18: //1行4列 A
if(count<6)
{
wordbuf[count]=0x30; //对应密码位上显示A,输入的当作为3
pwbuf[count]=3;
count++;
}
break;*/
case 0x84: //2行1列 4
if(count<6)
{
wordbuf[count]=4; //对应密码位上显示4
pwbuf[count]=4;
d=pwbuf[count];
count++;
}
break;
case 0x44: //2行2列 5
if(count<6)
{
wordbuf[count]=5; //对应密码位上显示F
pwbuf[count]=5;
d=pwbuf[count];
count++;
}
break;
case 0x24: //2行3列 6
if(count<6)
{
wordbuf[count]=6; //对应密码位上显示F
pwbuf[count]=6;
d=pwbuf[count];
count++;
}
break;
/* case 0x14: //2行4列 B
if(count<6)
{
wordbuf[count]=8; //显示8
pwbuf[count]=8;
count++;
}
break;*/
case 0x82: //3行1列 7
if(count<6)
{
wordbuf[count]=7; //对应密码位上显示F
pwbuf[count]=7;
d=pwbuf[count];
count++;
}
break;
case 0x42: //3行2列 8
if(count<6)
{
wordbuf[count]=8;
pwbuf[count]=8;
d=pwbuf[count];
count++;
}
break;
case 0x22: //3行3列 9
if(count<6)
{
wordbuf[count]=9;
pwbuf[count]=9;
d=pwbuf[count];
count++;
}
break;
/* case 0x12: //3行4列 显赫为c
if(count<6)
{
wordbuf[count]=12;
pwbuf[count]=12;
count++;
}
break;*/
case 0x41: //4行2列 0
if(count<6)
{
wordbuf[count]=0;
pwbuf[count]=0;
d=pwbuf[count];
count++;
}
break;
/*case 0x11: //4行4列 D
if(count<6)
{
wordbuf[count]=0;
pwbuf[count]=0;
count++;
}
break;*/
case 0x21: //4行3列 取消(#)
count=0;
for(j=0;j<6;j++)
{
wordbuf[j]=8; //数码管显示888888
pwbuf[j]=0x0f; //用FFFFFF清除已经输入的密码
}
break;
case 0x81: //*号作为确认键
enterflag=1;
if(count==6)
pwflag=pwcmp();
else
pwflag=0;
break;
default: break;
}//switch
}
/*主程序*/
void main()
{
uchar j;
count=0; //初始没有输入密码.计数器设为0
enterflag=0; //没有按下确认键
pwflag=0; //密码正确与否标志为0
green=1;
red=1; //红绿灯不亮
/*假定内定密码为851219*/
pw[0]=8;
pw[1]=5;
pw[2]=1;
pw[3]=2;
pw[4]=1;
pw[5]=9;
showflag=1; //打开数码管显示
//刚加电时显示888888
while(1)
{
getkey();
if(count==0)
display(0,1,2,3,4,5,8,8,8,8,8,8);
while(count!=0)
{
while(count==1)
{
display(0,6,6,6,6,6,d,d,d,d,d,d);
d1=d;
getkey();
}
while(count==2)
{
display(0,1,6,6,6,6,d1,d,d,d,d,d);
d2=d;
getkey();
}
while(count==3)
{
display(0,1,2,6,6,6,d1,d2,d,d,d,d);
d3=d;
getkey();
}
while(count==4)
{
display(0,1,2,3,6,6,d1,d2,d3,d,d,d);
d4=d;
getkey();
}
while(count==5)
{
display(0,1,2,3,4,6,d1,d2,d3,d4,d,d);
d5=d;
getkey();
}
while(count==6)
{
display(0,1,2,3,4,5,d1,d2,d3,d4,d5,d);
d6=d;
getkey();
}
}
if(enterflag==1) //如果按下确认键
{
enterflag=0; //清除标志
count=0; //清除密码计数位
/*用FFFFFF清除已经输入的密码*/
for(j=0;j<6;j++)
pwbuf[j]=0x0f;
showflag=0; //关闭数码管显示
TR1=1; //开始计数
tlcount=0; //定时器1由50ms累积到1s所用的计数器
if(pwflag==1)
green=0; //密码正确,亮绿灯
else
red=0;
}
} //while
} //main
|
|