微波EDA网,见证研发工程师的成长!
首页 > 硬件设计 > 嵌入式设计 > AT89C52单片机+AT24C02+1602构成的密码锁

AT89C52单片机+AT24C02+1602构成的密码锁

时间:11-25 来源:互联网 点击:
这是一款AT89C52单片机+AT24C02+1602构成的密码锁C语言源程序原理图,键盘由16只按键开关组成,6位密码,密码可以实现断电保存,可以随时进行修改。电路简单,容易制作,是学习单片机比较好的实践项目。

#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--); //延时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) //确认是否按下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) //松手检测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;

}


Copyright © 2017-2020 微波EDA网 版权所有

网站地图

Top