《数显电子密码锁.doc》由会员分享,可在线阅读,更多相关《数显电子密码锁.doc(8页珍藏版)》请在金锄头文库上搜索。
1、#include#include#define uchar unsigned char#define uint unsigned intuchar a1=14,b1=14,c1=14,d1=14,e1=14,f1=14,mx1,mx2,mx3,mx4,mx5,mx6;uchar wei,key,queding,esc,change,flag,flag1,flag2,T;uchar x,m1=0,m2=0,m3=0,m4=0,m5=0,m6=0;uchar n1=16,n2=16,n3=16,n4=16,n5=16,n6=16;sbit led=P33;sbit beep=P37;uchar c
2、ode table=0xc0,0xf9,0xa4,0xb0,0x99,0x92,0x82,0xf8,0x80,0x90, 0x89,0x86,0xc7,0xc8,0xbf,0xc1,0xf6;/H,E,L,N,-,Uuchar code table1=0xfe,0xbf,0xfb,0xf7,0xef,0xdf;#define WaitTime 0x01/定义CPU等待时间sfr ISP_DATA=0xe2;/ISP/IAP数据寄存器声明sfr ISP_ADDRH=0xe3;/ISP/IAP高字节地址寄存器sfr ISP_ADDRL=0xe4;/ISP/IAP低字节地址寄存器sfr ISP_CM
3、D=0xe5;/ISP/IAP命令模式寄存器sfr ISP_TRIG=0xe6;/ISP/IAP命令触发寄存器sfr ISP_CONTR=0xe7;/ISP/IAP控制寄存器#define RdCommand0x01#define PrgCommand0x02#define EraseCommand 0x03#define WaitTime0x01/* = 打开 ISP,IAP 功能 = */void ISP_IAP_enable()EA=0;/* 关中断 */ISP_CONTR=ISP_CONTR&0x18;/* 0001,1000*/ISP_CONTR=ISP_CONTR|WaitTime
4、;/* 写入硬件延时*/ISP_CONTR=ISP_CONTR|0x80;/* ISPEN=1*/* = 关闭 ISP,IAP 功能 = */void ISP_IAP_disable()ISP_CONTR=ISP_CONTR & 0x7f;/* ISPEN = 0 */ISP_TRIG =0x00;EA =1;/* 开中断 */* = 公用的触发代码 = */void ISPgoon()ISP_IAP_enable();/* 打开 ISP,IAP 功能*/ISP_TRIG=0x46;/* 触发ISP_IAP命令字节1*/ISP_TRIG=0xb9;/* 触发ISP_IAP命令字节2*/_nop
5、_();/* = 字节读 = */uchar byte_read(uint byte_addr)ISP_ADDRH=(uchar)(byte_addr8);/* 地址赋值*/ISP_ADDRL=(uchar)(byte_addr&0x00ff);ISP_CMD =ISP_CMD&0xf8;/* 清除低3位 */ISP_CMD =ISP_CMD|RdCommand;/* 写入读命令*/ISPgoon();/* 触发执行*/ISP_IAP_disable();/* 关闭ISP,IAP功能*/return (ISP_DATA);/* 返回读到的数据*/* = 扇区擦除 = */void sector
6、erase(uint sector_addr)uint iSectorAddr;iSectorAddr=sector_addr & 0xfe00; /* 取扇区地址 */ISP_ADDRH=(uchar)(iSectorAddr 8);ISP_ADDRL=0x00;ISP_CMD =ISP_CMD&0xf8;/* 清空低3位*/ISP_CMD =ISP_CMD|EraseCommand;/* 擦除命令3*/ISPgoon();/* 触发执行*/ISP_IAP_disable();/* 关闭ISP,IAP功能*/* = 字节写 = */void byte_write(uint byte_addr
7、, uchar original_data)ISP_ADDRH=(uchar)(byte_addr 8);/* 取地址*/ISP_ADDRL=(uchar)(byte_addr & 0x00ff);ISP_CMD =ISP_CMD&0xf8;/* 清低3位*/ISP_CMD =ISP_CMD|PrgCommand;/* 写命令2*/ISP_DATA =original_data;/* 写入数据准备*/ISPgoon(); /* 触发执行*/ISP_IAP_disable();/* 关闭IAP功能*/void delay(uint z)uchar i,j;while(z-)for(i=10;i0
8、;i-)/1msfor(j=44;j0;j-);void dis_play(uchar k,uchar information)P2=tableinformation;P0=table1k;delay(3);P0=0xff;void display(uchar a,uchar b,uchar c,uchar d,uchar e,uchar f)dis_play(0,a);dis_play(1,b);dis_play(2,c);dis_play(3,d);dis_play(4,e);dis_play(5,f);void matrixkeys()uchar temp,h;for(h=0;h4;h+
9、)P1=(0x01h);temp=P1;if(temp&0xf0)!=0xf0)delay(5);temp=P1;if(temp&0xf0)!=0xf0)temp=P1;beep=0;switch(temp)case 0xe7:key=7;wei+;break;case 0xd7:key=8;wei+;break;case 0xb7:key=9;wei+;break;/case 0x77:key=7;wei+;break;case 0xed:key=1;wei+;break;case 0xdd:key=2;wei+;break;case 0xbd:key=3;wei+;break;case 0
10、xeb:key=4;wei+;break;case 0xdb:key=5;wei+;break;case 0xbb:key=6;wei+;break;/case 0x7b:key=7;wei+;break;/*密码更改键*/ case 0xee:if(flag1=1)change=1;break;/*清除键*/case 0xde:if(wei)esc=1;break;/*确定键*/case 0xbe:if(wei=6|flag1=1)queding=1;break;case 0x7e:key=0;wei+;break;while(temp!=0xf0)temp=P1;temp=temp&0xf
11、0;beep=1;void entering()switch(wei)case 1:mx1=key;a1=10;break;case 2:mx2=key;b1=10;break;case 3:mx3=key;c1=10;break;case 4:mx4=key;d1=10;break;case 5:mx5=key;e1=10;break;case 6:mx6=key;f1=10;break;case 7:wei=6;break;void init()TMOD=0x01;TH0=0x4c;TL0=0x00;EA=1;ET0=1;void time0()interrupt 1/密码错误-报警5分钟
12、uchar t,t1,t2;TH0=0x4c;TL0=0x00;t+;if(t=4)t=0;t1+;if(t1=100)t1=0;t2+;if(t2=15)t2=0;TR0=0;flag2=0;T=1;beep=beep;void qinchu()/清除while(esc=1|flag=1)flag=0;flag1=0;wei=0;esc=0;mx1=0;mx2=0;mx3=0;mx4=0;mx5=0;mx6=0;/输入密码清除a1=14;b1=14;c1=14;d1=14;e1=14;f1=14;/清屏n1=16;n2=16;n3=16;n4=16;n5=16;n6=16;/清屏m1=0;m2=0;m3=0;m4=0;m5=0;m6=0;/重置密码清除void main()/*sectorerase(0x2000);/当EEPROM该区被其他数据byte_write(0x2000,0);/-占用时,可将此段写入byte_write(0x2002,0);/-重置初始密码,然后将byte_write(0x2004,0);/-此段屏蔽byte_write(0x2006,0);byte_