毕业设计密码锁电路程序.docx
- 文档编号:26899113
- 上传时间:2023-06-23
- 格式:DOCX
- 页数:18
- 大小:39.90KB
毕业设计密码锁电路程序.docx
《毕业设计密码锁电路程序.docx》由会员分享,可在线阅读,更多相关《毕业设计密码锁电路程序.docx(18页珍藏版)》请在冰豆网上搜索。
毕业设计密码锁电路程序
密码锁
本设计以STC89C52RC单片机为核心,利用AT24C02存储密码,做到掉电存储密码的电子密码锁。
电路:
程序:
#include
#include
#defineuintunsignedint
#defineucharunsignedchar
#defineN7
#defineOP_READ0xa1//器件地址以及读取操作
#defineOP_WRITE0xa0//器件地址以及写入操作
#defineMAX_ADDR0x7f//AT24C02最大地址
sbitSCL=P2^0;
sbitSDA=P2^1;
sbitCSH=P3^0;
sbite=P3^4;
sbitrs=P3^5;
sbitsp=P3^6;
sbitlock=P3^1;
sfrduan=0x80;
sfrkey=0x90;
uintci;
ucharkc,sc,sc2;
intshu1,shu2,jh,jg;
ucharcodetable[]="LOCK";
ucharcodetable2[]="WELLCOMETOUSE!
";
ucharcodetable3[]="PASSWORD:
";
ucharcodetable4[]="ERRO!
";
ucharcodetable5[]="OPEN!
";
ucharcodetable6[]="NEWPASSWORD";
ucharcodetable7[]="SUCCESSFUL!
";
ucharcodetable8[]="FAIL!
";
ucharcodecmm[6]={1,2,3,3,2,1};
ucharmm[6];
uchartablesuan[N];
voidinit();
voidwritec(uintcom);
voidwrited(uintdat);
voiddelay(uintshu);
ucharkeyscan();
voidxieshuzu(uchara[],ucharadd);
voidshuru();
voidkaiji();
voidbijiao();
voidxies(uchara,ucharadd);
voiddelayms(unsignedcharms);
voidgmm();
/***********************/
/***********************/
/***********************/
voidstart()
//开始位
{
SDA=1;
SCL=1;
_nop_();
_nop_();
SDA=0;
_nop_();
_nop_();
_nop_();
_nop_();
SCL=0;
}
voidstop()
//停止位
{
SDA=0;
_nop_();
_nop_();
SCL=1;
_nop_();
_nop_();
_nop_();
_nop_();
SDA=1;
}
unsignedcharshin()
//从AT24Cxx移入数据到MCU
{
unsignedchari,read_data;
for(i=0;i<8;i++)
{
SCL=1;
read_data<<=1;
read_data|=(unsignedchar)SDA;
SCL=0;
}
return(read_data);
}
bitshout(unsignedcharwrite_data)
//从MCU移出数据到AT24Cxx
{
unsignedchari;
bitack_bit;
for(i=0;i<8;i++)//循环移入8个位
{
SDA=(bit)(write_data&0x80);
_nop_();
SCL=1;
_nop_();
_nop_();
SCL=0;
write_data<<=1;
}
SDA=1;//读取应答
_nop_();
_nop_();
SCL=1;
_nop_();
_nop_();
_nop_();
_nop_();
ack_bit=SDA;
SCL=0;
returnack_bit;//返回AT24Cxx应答位
}
voidwrite_byte(unsignedcharaddr,unsignedcharwrite_data)
//在指定地址addr处写入数据write_data
{
start();
shout(OP_WRITE);
shout(addr);
shout(write_data);
stop();
delayms(10);//写入周期
}
unsignedcharread_current()
//在当前地址读取
{
unsignedcharread_data;
start();
shout(OP_READ);
read_data=shin();
stop();
returnread_data;
}
unsignedcharread_random(unsignedcharrandom_addr)
//在指定地址读取
{
start();
shout(OP_WRITE);
shout(random_addr);
return(read_current());
}
voiddelayms(unsignedcharms)
//延时子程序
{
unsignedchari;
while(ms--)
{
for(i=0;i<120;i++);
}
}
/***********************/
/***********************/
/***********************///主程序/
voidmain()
{init();
kaiji();
delay(1000);
writec(0x01);
xieshuzu(table3,0x80);
if(CSH==0)
{
write_byte(2,cmm[0]);//在24c02的地址2中写入数据sec
delayms
(2);
write_byte(3,cmm[1]);//在24c02的地址2中写入数据sec
delayms
(2);
write_byte(4,cmm[2]);//在24c02的地址2中写入数据sec
delayms
(2);
write_byte(5,cmm[3]);//在24c02的地址2中写入数据sec
delayms
(2);
write_byte(6,cmm[4]);//在24c02的地址2中写入数据sec
delayms
(2);
write_byte(7,cmm[5]);//在24c02的地址2中写入数据sec
delayms
(2);
}
mm[0]=read_random
(2);
delayms
(2);
mm[1]=read_random(3);
delayms
(2);
mm[2]=read_random(4);
delayms
(2);
mm[3]=read_random(5);
delayms
(2);
mm[4]=read_random(6);
delayms
(2);
mm[5]=read_random(7);
while
(1)
{
lock=0;
shuru();
bijiao();
}
}
/***********************///初始化/
voidinit()
{
lock=0;
writec(0x01);
writec(0x38);
writec(0x0c);
IT0=1;
EX0=1;
}
/***********************///写开机画面/
voidkaiji()
{
xieshuzu(table,0x80);
xieshuzu(table2,0xc0);
}
/***********************///写数组/
voidxieshuzu(uchara[],ucharadd)
{
ucharcis;
writec(add);
for(cis=0;a[cis]!
='\0';cis++)
{
writed(a[cis]);
delay
(2);
}
}
/***********************///写数字/
voidxies(uchara,ucharadd)
{
writec(add);
writed(0x30+a);
}
/***********************///写指令/
voidwritec(uintcom)
{
e=0;
rs=0;
duan=com;
delay
(2);
e=1;
delay
(2);
e=0;
}
/***********************///写数据/
voidwrited(uintdat)
{
e=0;
rs=1;
duan=dat;
delay
(2);
e=1;
delay
(2);
e=0;
}
/***********************///延时/
voiddelay(uintshu)
{
uinti,j;
for(i=shu;i>0;i--)
for(j=110;j>0;j--);
}
/***********************///现盘扫描/
ucharkeyscan()
{
uchark,skey;
key=0xfe;
skey=key;
sp=1;
if(skey!
=0xfe)
{
delay(10);
skey=key;
if(skey!
=0xfe)
{
switch(skey)
{
case(0xee):
k=7;kc++;break;
case(0xde):
k=8;kc++;break;
case(0xbe):
k=9;kc++;break;
case(0x7e):
k=0x0a;kc++;break;
default:
break;
}
while(skey!
=0xfe)
skey=key;
sp=0;
returnk;
}
}
key=0xfd;
skey=key;
if(skey!
=0xfd)
{
delay(10);
skey=key;
if(skey!
=0xfd)
{
switch(skey)
{
case(0xed):
k=4;kc++;break;
case(0xdd):
k=5;kc++;break;
case(0xbd):
k=6;kc++;break;
case(0x7d):
k=0x0b;kc++;break;
default:
break;
}
while(skey!
=0xfd)
skey=key;
sp=0;
returnk;
}
}
key=0xfb;
skey=key;
if(skey!
=0xfb)
delay(10);
skey=key;
if(skey!
=0xfb)
{
switch(skey)
{
case(0xeb):
k=1;kc++;break;
case(0xdb):
k=2;kc++;break;
case(0xbb):
k=3;kc++;break;
case(0x7b):
k=0x0c;kc++;break;
default:
break;
}
while(skey!
=0xfb)
skey=key;
sp=0;
returnk;
}
key=0xf7;
skey=key;
if(skey!
=0xf7)
{
delay(10);
skey=key;
if(skey!
=0xf7)
{
switch(skey)
{
case(0xe7):
k=0x0d;kc++;break;
case(0xd7):
k=0;kc++;break;
case(0xb7):
k=0x0e;kc++;break;
case(0x77):
k=0x0f;kc++;break;
default:
break;
}
while(skey!
=0xf7)
skey=key;
sp=0;
returnk;
}
}
}
/***********************///输入数据处理/
voidshuru()
{
uchark=0,xw=0x80;
sc=0;kc=0;
xieshuzu(table3,0x80);
do
{
while(kc==sc)
k=keyscan();
if(kc==1)
writec(0x01);
tablesuan[sc]=k;
//xies(tablesuan[sc],xw);
xies(tablesuan[sc],0xc0);
writec(xw);
writed(0x2A);
xw++;
if(sc==5)
gotoout;
}
while((tablesuan[sc++]<=9)&&(sc out: ; if(sc>N) { writec(0x01); xieshuzu(table4,0x80); } } voidbijiao() { if((tablesuan[0]==mm[0])&&(tablesuan[1]==mm[1])&&(tablesuan[2]==mm[2])&&(tablesuan[3]==mm[3])&&(tablesuan[4]==mm[4])&&(tablesuan[5]==mm[5])) { EA=1; lock=1; writec(0x01); xieshuzu(table5,0x80); delay(6500); EA=0; } else { writec(0x01); xieshuzu(table3,0x80); } } voidgmm() { uchark=0,xw=0x80; sc2=0;kc=0; xieshuzu(table6,0x80); do { while(kc==sc2) k=keyscan(); if(kc==1) writec(0x01); mm[sc2]=k; xies(mm[sc2],0xc0); writec(xw); writed(0x2A); xw++; if(sc2==5) gotoout2; } while((mm[sc2++]<=9)&&(sc2 //out: ; //if(sc>N) //{ writec(0x01); xieshuzu(table8,0x80); gotoout3; //} out2: ; writec(0x01); xieshuzu(table7,0x80); write_byte(2,mm[0]);//在24c02的地址2中写入数据sec delayms (2); write_byte(3,mm[1]);//在24c02的地址2中写入数据sec delayms (2); write_byte(4,mm[2]);//在24c02的地址2中写入数据sec delayms (2); write_byte(5,mm[3]);//在24c02的地址2中写入数据sec delayms (2); write_byte(6,mm[4]);//在24c02的地址2中写入数据sec delayms (2); write_byte(7,mm[5]);//在24c02的地址2中写入数据sec delayms (2); out3: ; } voidINTT0()interrupt0 { EA=0; gmm(); EA=1; }
- 配套讲稿:
如PPT文件的首页显示word图标,表示该PPT已包含配套word讲稿。双击word图标可打开word文档。
- 特殊限制:
部分文档作品中含有的国旗、国徽等图片,仅作为作品整体效果示例展示,禁止商用。设计者仅对作品中独创性部分享有著作权。
- 关 键 词:
- 毕业设计 密码锁 电路 程序
![提示](https://static.bdocx.com/images/bang_tan.gif)