单片机电子密码锁 (可以修改设置密码)——基于51单片机与AT24C02与LCD1602显示
其电路图连接如下:本人已经用硬件实验,程序可用。正确~~ 本程序特点:装载后读者可以自改密码,然后需要再次载入程序时: 可以把主程序aa=Sendstring(0xa0,1,table2,6);这一句去掉。然后程序的电子锁密码就是你个人设置的密码。
程序代码为:
#include #define uchar unsigned char #define uint unsigned int #define LCDIO P2 #define delay4us() _nop_();_nop_();_nop_();_nop_(); uchar buffer[6]={0}; sbit sda=P3^7; sbit scl=P3^6; sbit beep=P3^5; bit flag=0,aa; //用户蹲渊义定时溢出标志位 uchar DSY_BUFFER[16]=\" \"; uchar DSY_BUFFER1[16]=\" \"; uchar Userpassword[6]={0}; sbit rs=P0^4; sbit rd=P0^3; sbit lcden=P0^2; sbit led=P3^0; uchar code table2[]=\"123456\"; uchar code table[]=\"Your Password...\"; void delayms(uint z) { uint x,y; for(x=z;x>0;x--) for(y=110;y>0;y--); } void delay() //短延时,两个机器周期,做总线的延时用 {;;} void write_com(uchar com) { rs=0; rd=0; lcden=0; P2=com; delayms(3); lcden=1; delayms(3); lcden=0; } void write_date(uchar date) { rs=1; rd=0; lcden=0; P2=date; delayms(3); lcden=1; delayms(3); lcden=0; } void Display_String(uchar *p,uchar com) { uchar i; write_com(com); for(i=0;i<16;i++) { write_date(p[i]); } } void init_lcd() { lcden=0; write_com(0x38); write_com(0x0c); write_com(0x06); write_com(0x01); write_com(0x80); Display_String(table,0x80); Display_String(\"Lock OK! } void start() { sda=1; scl=1; delay4us(); sda=0; delay4us(); scl=0; } void stop() { sda=0; scl=1; delay4us(); sda=1; delay4us(); scl=0; } void init() { sda=1; delay(); scl=1; delay(); } void ack() { \//初始化 sda=0; scl=1; delay4us(); scl=0; sda=1; } void noack() { sda=1; scl=1; delay4us(); scl=0; sda=0; } uchar recbyte() { uchar i,rd; rd=0x00; sda=1; for(i=0;i<8;i++) { scl=1; rd<<=1; rd|=sda; delay4us(); scl=0; delay4us(); } scl=0; delay4us(); return rd; } uchar sendbyte(uchar wd) { uchar i; bit ack0; for(i=0;i<8;i++) { sda=(bit)(wd&0x80); _nop_(); _nop_(); scl=1; delay4us(); scl=0; wd<<=1; } delay4us(); sda=1; scl=1; delay4us(); ack0=!sda; scl=0; delay4us(); return ack0; } uchar Recstring(uchar slave,uchar subaddr,uchar *buffer,uchar n) { uchar i; start(); if(!sendbyte(slave)) return 0; if(!sendbyte(subaddr)) return 0; start(); if(!sendbyte(slave+1)) return 0; for(i=0;i buffer[n-1]=recbyte(); noack(); stop(); return 1; } uchar Sendstring(uchar slave,uchar subaddr,uchar *buffer,uchar n) { uchar i; start(); if(!sendbyte(slave)) return 0; if(!sendbyte(subaddr)) return 0; for(i=0;i { uchar i; for(i=0;i<6;i++) { Userpassword[i]=' '; } for(i=0;i<16;i++) { DSY_BUFFER[i]=' '; } } uchar Keys_Scan() { uchar temp,keynum; P1=0x0F; delayms(5); temp=P1^0x0F; switch(temp) { case 1:keynum=0;break; case 2:keynum=1;break; case 4:keynum=2;break; case 8:keynum=3;break; break; } P1=0xF0; delayms(5); temp=P1>>4^0x0F; switch(temp) { case 1:keynum+=0;break; case 2:keynum+=4;break; case 4:keynum+=8;break; case 8:keynum+=12;break; break; } delayms(600); return keynum; } void main() { uchar temp,i=0,j=0,k=0,n; uchar IS_valid_user; beep=1; init(); init_lcd(); delayms(5); aa=Sendstring(0xa0,1,table2,6); delayms(5); aa=Recstring(0xa0,1,buffer,6); delayms(10); P1=0x0f; while(1) { if(P1!=0x0f) { temp=Keys_Scan(); switch(temp) { case 0: case 1: case 2: case 3: case 4: case 5: case 6: case 7: case 8: case 9: if (i<=5) //密码限制在6位以内 { Userpassword[i]=temp; DSY_BUFFER[i]='*'; Display_String(DSY_BUFFER,0xc0); i++; } break; case 10: //按A键开锁 for(k=0;k<6;k++) { if(buffer[k]==(Userpassword[k]+48)) flag=1; else flag=0; } if (flag==1) { flag=0; i=0; led=0; //点亮LED clear_password(); Display_String(\"OPEN OK! \ IS_valid_user = 1; j=0; } else { j++; led=1; //关闭LED clear_password(); Display_String(\"ERROR!Have try \ write_com(0xcf); write_date(0x30+j); IS_valid_user=0; } i=0; break; case 11: //按B键上锁 led=1; clear_password(); Display_String(table,0x80); Display_String(\"Lock OK! \ i=0; IS_valid_user=0; break; case 12: //按C键设置新密码 //如果是合法用户则提示输入新密码 if ( !IS_valid_user) { i=0; Display_String(\"No rights ! \ delayms(1000); Display_String(\"Your Password...\ Display_String(\"Lock OK! \ } else { i=0; Display_String(\"New Password: \ Display_String(\" \ } break; case 13: //按D键保存新密码 if ( !IS_valid_user) { i=0; Display_String(\"No rights ! \ delayms(1000); Display_String(\"Your Password...\ Display_String(\"Lock OK! \ } else {i = 0; init(); delayms(5); for(k=0;k<6;k++) { Userpassword[k]=Userpassword[k]+48; } aa=Sendstring(0xa0,1,Userpassword,6); delayms(5); aa=Recstring(0xa0,1,buffer,6); delayms(5); clear_password(); Display_String(table,0x00); Display_String(\"Password Saved! \ delayms(1000); Display_String(\"Do lock agian ? \ } break; case 14: //按E键消除所有输入 i=0; clear_password(); Display_String(\" \ break; case 15: //清除一位 if(i!=0)i--; for(n=0;n} } } beep=0; 因篇幅问题不能全部显示,请点此查看更多更全内容