课程设计
基于 51 单片机的 8 位电子密码锁
/****************************************************
8 位电子密码锁程序
****************************************************/
#include
#define uint unsigned int
#define uchar unsigned char
sbit fmq=P3^0;
sbit led1=P3^1;
sbit led2=P3^2;
sbit scl=P3^4;
sbit sda=P3^5;
uchar code wela[]={
0xf7,0xfb,0xfd,0xfe,
//存储源密码
//存储开锁密码
//存储 HHHHHHHH
//存储修改密码
//存储换密码时输入的密码
0xef,0xdf,0xbf,0x7f
};
uchar code dula[]={
0x24,0xbd,0xe0,0xa8,
0x39,0x2a,0x22,0xbc,
0x20,0x28,0x24,0xfb,
0xee,0x31,0Xef};
uchar temp;
uchar *play;
uchar screa_ok=0;
uchar at24c02[8]={0,0,0,0,0,0,0,0};
uchar atdata[8]={11,11,11,11,11,11,11,11};
uchar atdata1[8]={12,12,12,12,12,12,12,12};
uchar atdata2[8]={11,11,11,11,11,11,11,11};
uchar atdata3[8]={14,14,14,14,14,14,14,14};
void at24c02_init();
void start();
void stop();
void respons0();
void respons1() ;
void write_add(uchar address,uchar date);
uchar read_add(uchar address);
void read_data(void);
void write_data(void);
//应答
uchar keyscan();
void display();
void keyprocess();
void delay()
{ ;;;;;;}
void delayms(uint z)
{
uint x,y;
for(x=z;x>0;x--)
for(y=110;y>0;y--);
}
void main()
{
//
at24c02_init();
play=atdata2;
read_data();
//write_data();
while(1)
{
display();
keyprocess();
}
}
void display()
{
uchar j;
for(j=0;j<=7;j++)
{
P1=0Xff;
P0=dula[play[j]];
P1=wela[j];
P1=0Xff;
}
}
uchar keyscan()
{
uchar num=0xff;
P2=0xfe;
temp=P2;
temp=temp&0xf0;
while(temp!=0xf0)
{
delayms(5);
temp=P2;
temp=temp&0xf0;
while(temp!=0xf0)
{
temp=P2;
switch(temp)
{
case 0xee:num=1;
break;
case 0xde:num=2;
break;
case 0xbe:num=3;
break;
case 0x7e:num=4;
break;
}
while(temp!=0xf0)
{
}
temp=P2;
temp=temp&0xf0;
}
}
P2=0xfd;
temp=P2;
temp=temp&0xf0;
while(temp!=0xf0)
{
delayms(5);
temp=P2;
temp=temp&0xf0;
while(temp!=0xf0)
{
temp=P2;
switch(temp)
{
}
case 0xed:num=5;
break;
case 0xdd:num=6;
break;
case 0xbd:num=7;
break;
case 0x7d:num=8;
break;
while(temp!=0xf0)
temp=P2;
temp=temp&0xf0;
{
}
}
}
P2=0xfb;
temp=P2;
temp=temp&0xf0;
while(temp!=0xf0)
{
delayms(5);
temp=P2;
temp=temp&0xf0;
while(temp!=0xf0)
{
temp=P2;
switch(temp)
{
}
case 0xeb:num=9;
break;
case 0xdb:num=0;
break;
case 0xbb:num=11;
break;
case 0x7b:num=12;
break;
while(temp!=0xf0)
temp=P2;
temp=temp&0xf0;
{
}
}
}
P2=0xf7;
temp=P2;
temp=temp&0xf0;
while(temp!=0xf0)
{
delayms(5);
temp=P2;
temp=temp&0xf0;
while(temp!=0xf0)
{
temp=P2;
switch(temp)
{
}
case 0xe7:num=13;
break;
case 0xd7:num=14;
break;
case 0xb7:num=15;
break;
case 0x77:num=16;
break;
while(temp!=0xf0)
temp=P2;
temp=temp&0xf0;
{
}
}
}
return num;
}
void keyprocess()
{
uchar keydata=0;
uchar i,error=0;
static unsigned char flag=0;
static unsigned char num=0;
keydata=keyscan();
if(keydata==0xff)
return;
switch(keydata)
{
case 1:
case 2:
case 3:
case 4:
case 5:
case 6:
case 7:
case 8:
case 9:
case 0:
if(flag==1)
{
if(num<8)
{
}
atdata3[num]=13;
atdata[num]=keydata;
num++;
}
else
if(flag==2)
{
if(num<8)
atdata1[num]=keydata;
num++;
{
}
}
else
{
if(num<8)
{
atdata2[num]=13;
atdata[num]=keydata;
num++;
}
}
break;
case 11:
if(flag==0&&num==8)
{
{
num=0;
for(i=0;i<8;i++)
if(atdata[i]==at24c02[i])
{
;
}
else
{
}
error=1;
}
{
if(error==0)
led1=0;
delayms(1000);
led1=1;
}
else
{
led2=0;
fmq=0;
delayms(500);
led2=1;
fmq=1;
delayms(500);
led2=0;
fmq=0;
delayms(500);
led2=1;
fmq=1;
delayms(500);
led2=0;
fmq=0;
delayms(500);
led2=1;
fmq=1;
}
error=0;
for(i=0;i<8;i++)
{
atdata[i]=11;
atdata2[i]=11;
}
}
else
if(flag==1&&num==8)
num=0;
for(i=0;i<8;i++)
{
{