新聞中心

EEPW首頁 > 嵌入式系統(tǒng) > 設(shè)計(jì)應(yīng)用 > AT89C52單片機(jī)+AT24C02+1602構(gòu)成的密碼鎖

AT89C52單片機(jī)+AT24C02+1602構(gòu)成的密碼鎖

作者: 時(shí)間:2016-11-25 來源:網(wǎng)絡(luò) 收藏
這是一款AT89C52單片機(jī)+AT24C02+1602構(gòu)成的密碼鎖C語言源程序原理圖,鍵盤由16只按鍵開關(guān)組成,6位密碼,密碼可以實(shí)現(xiàn)斷電保存,可以隨時(shí)進(jìn)行修改。電路簡(jiǎn)單,容易制作,是學(xué)習(xí)單片機(jī)比較好的實(shí)踐項(xiàng)目。
#include
#include
#include
#define uchar unsigned char
#define uint unsigned int
uchar code table[]="Input Password: ";
uchar code table1[]=" Input Error! ";
uchar code table2[]="You are welcome!";
uint ucSendBuffer[6]={0,0,0,0,0,0};
uint mima[6];
sbit lcdrs=P2^0;
sbit lcdrw=P2^1;
sbit lcden=P2^2;
sbit led=P2^5;
sbit fmq=P2^4;
sbit jdq=P2^3;
sbit key=P2^6;
uint x;
uint num,temp;
uint keyscan();
void delay_50us(uint t)
{
uint y;
for(;t>0;t--)
for(y=19;y>0;y--);
}
void delay_1ms(uint z)
{
uint x,y;
for(x=z;x>0;x--)
for(y=110;y>0;y--);
}
void delay(void)
{
uint i;
for(i=100;i>0;i--)
_nop_();
}
void delay1ms()
{
uchar i;
for(i=124;i>0;i--); //延時(shí)124*8+10=1002ushttp://www.duankudp.com/
}
void write_com(uchar com)
{
lcdrs=0;
lcdrw=0;
lcden=0;
P0=com;
delay_50us(10);
lcden=1;
delay_50us(20);
lcden=0;
}
void write_data(uchar date)
{
lcden=0;
lcdrs=1;
lcdrw=0;
P0=date;
delay_50us(10);
lcden=1;
delay_50us(20);
lcden=0;
}
void init()
{
write_com(0x38);
delay_50us(100);
write_com(0x01);
delay_50us(100);
write_com(0x06);
delay_50us(100);
write_com(0x0d);
delay_50us(100);
}
void main()
{
uint a=0,q=0,d=0,jiesu;
uint b=0;
uint kai;
delay_50us(300);
init();
fmq=1;
write_com(0x80);
for(x=0;x<16;x++)
{
write_data(table[x]);
delay_50us(100);
}
write_com(0x80+0x44);
while(1)
{
if(keyscan()!=0)
{
kai=keyscan();
mima[a++]=kai;
write_com(0x80+0x44+b);
write_data(keyscan());
num=0;
b++;
if(b>7&&kai!=0x7f)
{
write_com(0x80);
for(x=0;x<16;x++)
{
write_data(table1[x]);
delay_50us(100);
}
delay_1ms(700);
break;
}
else if(kai==0x7f&&b<=7)
{
if(b<7)
{
write_com(0x80);
for(x=0;x<16;x++)
{
write_data(table1[x]);
delay_50us(100);
}
delay_1ms(700);
break;
}
else
{
while(q<=5)
{
if(mima[q]==ucSendBuffer[q]+0x30)
{
d++;
}
q++;
}
if(d==6)
{
fmq=0;
write_com(0x80);
for(x=0;x<16;x++)
{
write_data(table2[x]);
delay_50us(100);
}
jdq=0;
delay_1ms(500);
fmq=1;
led=0;
break;
}
else
{
write_com(0x80);
for(x=0;x<16;x++)
{
write_data(table1[x]);
delay_50us(100);
}
delay_1ms(700);
break;
}
}
}
else if(kai==0x7e)
{
break;
}
}
}
}
uint keyscan()
{
P1=0xef;
temp=P1;
temp=temp&0x0f;
while(temp!=0x0f) //確認(rèn)是否按下http://www.duankudp.com/
{
delay_1ms(5);
temp=P1;
temp=temp&0x0f;
while(temp!=0x0f)
{
temp=P1;
switch(temp)
{
case 0xee:num=0x31;
break;
case 0xed:num=0x34;
break;
case 0xeb:num=0x37;
break;
case 0xe7:num=0x7f;
break;
default:num=0;
break;
}
while(temp!=0x0f) //松手檢測(cè)http://www.5imcu.net/
{
temp=P1;
temp=temp&0x0f;
}
}
}
P1=0xdf;
temp=P1;
temp=temp&0x0f;
while(temp!=0x0f)
{
delay_1ms(5);
temp=P1;
temp=temp&0x0f;
while(temp!=0x0f)
{
temp=P1;
switch(temp)
{
case 0xde:num=0x32;
break;
case 0xdd:num=0x35;
break;
case 0xdb:num=0x38;
break;
case 0xd7:num=0x30;
break;
default:num=0;
break;
}
while(temp!=0x0f)
{
temp=P1;
temp=temp&0x0f;
}
}
}
P1=0xbf;
temp=P1;
temp=temp&0x0f;
while(temp!=0x0f)
{
delay_1ms(5);
temp=P1;
temp=temp&0x0f;
while(temp!=0x0f)
{
temp=P1;
switch(temp)
{
case 0xbe:num=0x33;
break;
case 0xbd:num=0x36;
break;
case 0xbb:num=0x39;
break;
case 0xb7:num=0x7e;
break;
default:num=0;
break;
}
while(temp!=0x0f)
{
temp=P1;
temp=temp&0x0f;//http://www.5imcu.net/
}
}
}
P1=0x7f;
temp=P1;
temp=temp&0x0f;
while(temp!=0x0f)
{
delay_1ms(5);
temp=P1;
temp=temp&0x0f;
while(temp!=0x0f)
{
temp=P1;
switch(temp)
{
case 0x7e:num=0x41;
break;
case 0x7d:num=0x42;
break;
case 0x7b:num=0x43;
break;
case 0x77:num=0x44;
break;
default:num=0;
break;
}
while(temp!=0x0f)
{
temp=P1;
temp=temp&0x0f;
}
}
}
return num;
}



評(píng)論


技術(shù)專區(qū)

關(guān)閉