基于51单片机的密码锁设计程序.docx
《基于51单片机的密码锁设计程序.docx》由会员分享,可在线阅读,更多相关《基于51单片机的密码锁设计程序.docx(36页珍藏版)》请在冰豆网上搜索。
![基于51单片机的密码锁设计程序.docx](https://file1.bdocx.com/fileroot1/2022-11/30/d38c33a7-8906-4f21-84cc-5917fa6d231e/d38c33a7-8906-4f21-84cc-5917fa6d231e1.gif)
基于51单片机的密码锁设计程序
#include"1302.h"
voidwrite_ds1302_byte(uchardat)
{
uchari;
for(i=0;i<8;i++)
{
T_IO=dat&0x01;
T_CLK=1;
T_CLK=0;
dat=dat>>1;
}
}
ucharread_ds1302_byte()
{
uchari;
for(i=0;i<8;i++)
{
ACC=ACC>>1;
ACC7=T_IO;
T_CLK=1;
T_CLK=0;
}
return(ACC);
}
voidwrite_ds1302_dat(ucharaddr,uchardat)
{
T_EN=0;
T_CLK=0;
T_EN=1;
write_ds1302_byte(addr);
write_ds1302_byte(dat);
T_CLK=1;
T_EN=0;
}
ucharread_ds1302_data(ucharaddr)
{
uchardate;
T_EN=0;
T_CLK=0;
T_EN=1;
write_ds1302_byte(addr);
date=read_ds1302_byte();
T_EN=0;
T_CLK=0;
return(date);
}
voidset_real_time(uchar*pclock)
{
uchari,addr=0x80;
EA=0;
write_ds1302_dat(0x8e,0x00);
for(i=0;i<7;i++)
{
write_ds1302_dat(addr,*pclock);
pclock++;
addr+=2;
}
//write_ds1302_dat(0x8e,0x80);
EA=1;
}
voidget_real_time(ucharCurtime[])
{
ucharj;
ucharaddress=0x81;
EA=0;
for(j=0;j<7;j++)
{
Curtime[j]=read_ds1302_data(address);
address+=2;
}
EA=1;
}
#ifndef__1302_H__
#define__1302_H__
#include
#include
#defineucharunsignedchar
#defineuintunsignedint
sbitT_IO=P2^5;
sbitT_CLK=P2^4;
sbitT_EN=P1^0;
sbitACC0=ACC^0;
sbitACC7=ACC^7;
voidwrite_ds1302_byte(uchardat);
ucharread_ds1302_byte();
voidwrite_ds1302_dat(ucharaddr,uchardat);
ucharread_ds1302_data(ucharaddr);
voidset_real_time(uchar*pclock);
voidget_real_time(ucharCurtime[]);
#endif
#include"delay.h"
#include"1602.h"
voidlcd_busy_wait()/*LCD1602忙等待*/
{
lcd_rs_port=0;
lcd_rw_port=1;
lcd_en_port=1;
lcd_data_port=0xff;
while(lcd_data_port&0x80);
lcd_en_port=0;
}
voidwrite_com(ucharcommand)/*LCD1602命令字写入*/
{
lcd_busy_wait();
lcd_rs_port=0;
lcd_rw_port=0;
lcd_en_port=0;
lcd_data_port=command;
lcd_en_port=1;
lcd_en_port=0;
}
voidwrite_data(uchardat)
{
lcd_busy_wait();
lcd_rs_port=1;
lcd_rw_port=0;
lcd_en_port=0;
P0=dat;
lcd_en_port=1;
lcd_en_port=0;
}
voidwtite_lcd_1602_dat(ucharx_pos,uchary_pos,uchardat)
{
x_pos&=0x0f;
y_pos&=0x01;
if(y_pos==0)
{
x_pos=x_pos+0x80;
}
else
{
x_pos=x_pos+0x80+0x40;
}
write_com(x_pos);
lcd_busy_wait();
lcd_rs_port=1;
lcd_rw_port=0;
lcd_en_port=0;
P0=dat;
lcd_en_port=1;
lcd_en_port=0;
}
voidlcd_init()/*LCD1602初始化*/
{
lcd_delay(20);
write_com(0x38);
lcd_delay(100);
write_com(0x38);
lcd_delay(50);
write_com(0x38);
lcd_delay(10);
write_com(0x08);
write_com(0x01);
write_com(0x06);
write_com(0x0c);
lcd_data_port=0xff;/*释放数据端口*/
}
#ifndef__1602_H__
#define__1602_H__
#include
#include
#defineucharunsignedchar
#defineuintunsignedint
sbitlcd_rs_port=P3^5;
sbitlcd_rw_port=P3^6;
sbitlcd_en_port=P3^4;
#definelcd_data_portP0
voidlcd_busy_wait();
voidwrite_com(ucharcommand);
voidwrite_data(uchardat);
voidwtite_lcd_1602_dat(ucharx_pos,uchary_pos,uchardat);
voidlcd_init();
#endif
#include"2402.h"
#include"delay.h"
voidi2c_start()
{
scl=1;
delay1();
sda=1;
delay1();
sda=0;
delay1();
}
voidi2c_stop()
{
sda=0;
delay1();
scl=1;
delay1();
sda=1;
delay1();
}
voidi2c_ack()
{
uchari;
scl=1;
delay1();
while((sda==1)&&(i<250))i++;
scl=0;
delay1();
}
voidi2c_nack()
{
scl=1;
delay1();
sda=1;
scl=0;
delay1();
}
voidi2c_write_byte(uchardate)//
{//
uchari,temp;
temp=date;
for(i=0;i<8;i++)//
{//
temp=temp<<1;//
scl=0;//
delay1();;//
sda=CY;//
delay1();//
scl=1;//
}//
scl=0;//
delay1();//
sda=1;//
delay1();//
}//
uchari2c_read_byte()
{
uchari,temp=0;
scl=0;
//delay1();;
//sda=1;
delay1();
for(i=0;i<8;i++)
{
temp=temp<<1;
scl=1;
delay1();
temp=temp|sda;
scl=0;
delay1();
}
return(temp);
}
voidwrite_at2404(ucharaddr,uchardat)
{
i2c_start();
i2c_write_byte(0xa0);
i2c_ack();
i2c_write_byte(addr);
i2c_ack();
i2c_write_byte(dat);
i2c_ack();
i2c_stop();
delay3(5000);
}
ucharread_at2402(ucharaddr)
{
uchartemp;
i2c_start();
i2c_write_byte(0xa0);
i2c_ack();
i2c_write_byte(addr);
i2c_ack();
i2c_start();
i2c_write_byte(0xa1);
i2c_ack();
temp=i2c_read_byte();
i2c_nack();
i2c_stop();
return(temp);
}
#ifndef__2402_H__
#define__2402_H__
#include
#include
#defineucharunsignedchar
#defineuintunsignedint
sbitscl=P2^1;
sbitsda=P2^0;
voidi2c_start();
voidi2c_stop();
voidi2c_ack();
voidi2c_nack();
voidi2c_write_byte(uchar);
uchari2c_read_byte();//
voidwrite_at2404(ucharaddr,uchardat);
ucharread_at2402(ucharaddr);
#endif
#include"delay.h"
voiddelay(ucharx)
{
uchara,b;
for(a=x;a>0;a--)
for(b=200;b>0;b--);
}
voidlcd_delay(ucharms)
{
ucharj;
while(ms--){
for(j=0;j<250;j++)
{;}
}
}
voiddelay1()
{;;}
voiddelay3(uintq)
{
uinte;
for(e=0;e}
#ifndef__DELAY_H__
#define__DELAY_H__
#include
#include
#defineucharunsignedchar
#defineuintunsignedint
voiddelay(ucharx);
voidlcd_delay(ucharms);
voiddelay1();
voiddelay3(uintm);
#endif
#include"display.h"
voiddisplay()
{
P0=0;
dula=1;
dula=0;
P0=0x7f;
wela=1;
wela=0;
}
#ifndef__DISPLAY_H__
#define__DISPLAY_H__
#include
#include
#defineucharunsignedchar
#defineuintunsignedint
sbitdula=P2^6;
sbitwela=P2^7;
voiddisplay();
#endif
#include"delay.h"//一般放在最前面这个前后由调用先后决定着
#include"scan.h"
#include"1602.h"
//externvoidwrite_data(uchardat);
//externvoidwrite_com(ucharcommand);
ucharkey;
uchartemp,count,aa,bb,cc;/*一般将这些变量定义在C文件中
当主函数需要时用extern引用*/
bitshu_flag,modify_flag,rst;
bitfun_flag;//功能键标志位没使用
bitdown_flag,up_flag,open_flag;
voidkeyscan()
{
P1=0xfe;
temp=P1;
temp=temp&0xf0;
if(temp!
=0xf0)
{
delay(15);
if(temp!
=0xf0)
{
temp=P1;
switch(temp)
{
case0xee:
key=0;shu_flag=1;write_data('*');
count++;if(count>6)count=0;aa=0;bb=0;cc=0;break;
case0xde:
key=1;shu_flag=1;write_data('*');
count++;if(count>6)count=0;aa=0;bb=0;cc=0;break;
case0xbe:
key=2;shu_flag=1;write_data('*');
count++;if(count>6)count=0;aa=0;bb=0;cc=0;break;
case0x7e:
key=3;shu_flag=1;write_data('*');
count++;if(count>6)count=0;aa=0;bb=0;cc=0;break;
}
while(temp!
=0xf0)
{
temp=P1;
temp=temp&0xf0;
//beep=0;
delay(500);
}
//beep=1;
//write_data('*');count++;
}
while(temp!
=0xf0);
{
temp=P1;
temp=temp&0xf0;
}
}
P1=0xfd;
temp=P1;
temp=temp&0xf0;
if(temp!
=0xf0)
{
delay(15);
if(temp!
=0xf0)
{
temp=P1;
switch(temp)
{
case0xed:
shu_flag=1;key=4;write_data('*');
count++;if(count>6)count=0;aa=0;bb=0;cc=0;break;//aa,bb,cc定时器中使用
case0xdd:
shu_flag=1;key=5;write_data('*');//每次按键清零
count++;if(count>6)count=0;aa=0;bb=0;cc=0;break;//保证在按键器件不会自动返回
case0xbd:
shu_flag=1;key=6;write_data('*');//
count++;if(count>6)count=0;aa=0;bb=0;cc=0;break;
case0x7d:
shu_flag=1;key=7;write_data('*');
count++;if(count>6)count=0;aa=0;bb=0;cc=0;break;
}
while(temp!
=0xf0)
{
temp=P1;
temp=temp&0xf0;
//beep=0;
delay(500);//松手检测屏蔽蜂鸣器噪音污染
}
//beep=1;
//write_data('*');count++;
}
}
P1=0xfb;
temp=P1;
temp=temp&0xf0;
if(temp!
=0xf0)
{
delay(15);
if(temp!
=0xf0)
{
temp=P1;
switch(temp)
{//fun_flag功能键标志位
case0xeb:
key=8;shu_flag=1;write_data('*');
count++;if(count>6)count=0;aa=0;bb=0;cc=0;break;
case0xdb:
key=9;shu_flag=1;write_data('*');
count++;if(count>6)count=0;aa=0;bb=0;cc=0;break;
case0xbb:
modify_flag=1;fun_flag=1;aa=0;bb=0;cc=0;break;//修改键
case0x7b:
rst=1;fun_flag=1;aa=0;bb=0;cc=0;break;//返回键
}
while(temp!
=0xf0)
{
temp=P1;
temp=temp&0xf0;
//beep=0;
delay(500);
}
//beep=1;
}
}
P1=0xf7;
temp=P1;
temp=temp&0xf0;
if(temp!
=0xf0)
{
delay(15);
if(temp!
=0xf0)
{
temp=P1;
switch(temp)
{//清除键
case0xe7:
//key=12;shu_flag=1;break;
aa=0;bb=0;cc=0;
switch(count)
{
case1:
write_com(0x80+0x40+5);write_data('');write_com(0x80+0x40+5);break;
case2:
write_com(0x80+0x40+6);write_data('');write_com(0x80+0x40+6);break;
case3:
write_com(0x80+0x40+7);write_data('');write_com(0x80+0x40+7);break;
case4:
write_com(0x80+0x40+8);write_data('');write_com(0x80+0x40+8);break;
case5:
write_com(0x80+0x40+9);write_data('');write_com(0x80+0x40+9);break;
case6:
write_com(0x80+0x40+0x0a);write_data('');write_com(0x80+0x40+0x0a);break;
}
case0xd7:
key=13;up_flag=1;fun_flag=1;aa=0;bb=0;cc=0;break;//开门键
case0xb7:
key=14;down_flag=1;fun_flag=1;aa=0;bb=0;cc=0;break;
case0x77:
key=13;open_flag=1;fun_flag=1;aa=0;bb=0;cc=0;break;
while(temp!
=0xf0)
{
temp=P1;
temp=temp&0xf0;
//beep=0;
delay(500);
}
//beep=1;
if(count>0)count--;break;
}//清除数字
}
}
}
#ifndef__SCAN_H__
#define__SCAN_H__
#include
#include
#defineucharunsignedchar
#defineuintunsignedint
voidkeyscan();
#endif
/***********************************************************
名称:
电子密码锁
时间:
2014年8月2日16:
33:
48WRITEBY:
Kengkeng
TodayisValentine'sDay
程序功能:
1:
开机1302显示时间,按下key15:
开门键进入输入密码界面
2:
输入密码输入错误超过三次电子锁锁死倒计时60s之后重新设置蜂鸣器快速响
3:
输入正确进入菜单页面两个选择key13:
开门key10:
修改密码
4:
修改已输入密码可由key12键清除重新输入
5:
选择key13开门打开继电器显示倒计时15s开门蜂鸣器慢响提示15s内开门
否则15后关门关闭继电器并自动返回时间页面
6:
选择key10修改密码共两次两次密码一致时设置成功否则会一直提示输入密码
7:
修改的密码存入2402中上电读出
8:
修改密码无限循环直到输入正确为止可以使用key11:
返回键返回
9:
在10s内无任何按键按下时自动返回时间页面可重新进入