最近想利用开发板做一个密码锁!框架是通过4X4键盘输入四位密码!与预先设的密码比较!对的话灯亮,可是不是预期结果,请大虾们解释解释!!
基本程序:
#include<REG51.H>
#define uchar unsigned char
#define uint unsigned int
uchar code act[4]={0xfe,0xfd,0xfb,0xf7};
uchar shuru[4];
uchar code pass[4]={1,2,3,4}; //密码
//========================
void delay(uint k) //延时
{
uint data i,j;
for(i=0;i<k;i++){
for(j=0;j<121;j++)
{;}}
}
//===========================
char scan_key(void) //按键扫描
{
uchar i,j,in,ini,inj;
bit find=0;
for(i=0;i<4;i++)
{
P1=act[i];
delay(10);
in=P1;
in=in>>4;
in=in|0xf0;
for(j=0;j<4;j++)
{
if(act[j]==in)
{find=1;
inj=j;ini=i;
}
}
}
if(find==0)return -1;
return (ini*4+inj);
}
//============================
void main(void)
{
int i;
char c;
for(i=0;i<=3;i++)
{
c=scan_key();
if(c==-1)
{for(j=0; ;j++)}//等待
else {shuru[i]=c;c=-1;}}
if(shuru[0]==pass[0]&shuru[1]==pass[1]&shuru[2]==pass
[2]&shuru[3]==pass[3])
{P0=0xfe;
delay(250);}
}
} |