当前位置:文档之家› 51单片机密码锁程序

51单片机密码锁程序

自己做的基于单片机的密码锁设计(c语言设计)时间:2011-08-16 21:08:01 来源:作者:自己做的基于单片机的密码锁设计(c语言设计)系统说明该系统如图所示使用了80C51单片机、普通键盘、排阻、1602液晶。

该系统的功能:①该系统输入正确的密码LED灯会亮(其实就是很多操作都可以,用LED只是代表作用,主要是操作简单,效果明显。

)②系统可以修改密码;(系统断电后重启后必须用初始密码(初始密码是:000000)重新改密,当然修改的密码可以与掉电前的一样。

)具体操作:①系统开机时显示请输入密码的提示,直接输入密码#键确认②系统会自动提醒密码错误,数秒后系统会再次提醒重新输密码。

③在系统提醒输入密码的界面可以按*键修改密码,系统会提醒输入两次密码,并且会检查两次密码是否相同。

同则修改成功。

否则在数秒后可以重新按*键修改。

④在改密前(就是按了*键后系统)会提醒身份识别,请输入旧密码,系统判断旧密码正确方可更改密码。

源程序#include<reg52.h>#define uchar unsigned char#define uint unsigned intsbit lcdrs=P3^0;sbit lcdrw=P3^1;sbit lcden=P3^2;sbit h1=P1^0;sbit h2=P1^1;sbit h3=P1^2;sbit sa=P1^3;sbit sb=P1^4;sbit sc=P1^5;sbit sd=P1^6;sbit kd=P3^7;uchar code table[]=" PLEASE INPUT ";uchar code table1[]="CODE: ";uchar code table2[]=" *****WIN*****";uchar code table3[]=" PLEASE AFFIRM";uchar code table4[]=" *****FAIL*****";uchar code table5[]=" MODIFICATION";uchar code table6[]=" IDENTITY LIMITS ";uchar unm,M1,M2,M3,M4,M5,M6,sex,k1,k2,k3,k4,k5,k6,flge,flge1;uchar q1,q2,q3,q4,q5,q6,w1,w2,w3,w4,w5,w6,g;void delay(int z){int x,y;for(x=z;x>0;x--)for(y=110;y>0;y--);}void delay1(int z){int x,y;for(x=z;x>0;x--)for(y=20000;y>0;y--);}void writen_com(uchar com) //1602写命令{lcdrs=0;P0=com;delay(1);lcden=1;delay(1);lcden=0;}void writen_dat(uchar dat) //1602写数据{lcdrs=1;P0=dat;delay(1);lcden=1;delay(1);lcden=0;}void main();void cheak_mima() //检查第二次确认密码是否与第一次相同{if(q1==w1){if(q2==w2){if(q3==w3){if(q4==w4){if(q5==w5){if(q6==w6){writen_com(0x01);writen_com(0x0f);writen_com(0x06);writen_com(0x80);for(unm=0;unm<14;unm++){writen_dat(table5[unm]);delay(5);}writen_com(0x80+0x40+1);for(unm=0;unm<14;unm++){writen_dat(table2[unm]);delay(5);}M1=w1;M2=w2;M3=w3;M4=w4;M5=w5;M6=w6;delay1(6);main();}/*-----------------------------------*/ //密码确认错误返回writen_com(0x01);writen_com(0x80);for(unm=0;unm<14;unm++){writen_dat(table4[unm]);delay(5);}delay1(6);main();}writen_com(0x01);writen_com(0x80);for(unm=0;unm<14;unm++){writen_dat(table4[unm]);delay(5);}delay1(6);main();}writen_com(0x01);writen_com(0x80);for(unm=0;unm<14;unm++){writen_dat(table4[unm]);delay(5);}delay1(6);main();}writen_com(0x01);writen_com(0x80);for(unm=0;unm<14;unm++){writen_dat(table4[unm]);delay(5);}delay1(6);main();}writen_com(0x01);writen_com(0x80);for(unm=0;unm<14;unm++){writen_dat(table4[unm]);delay(5);}delay1(6);main();}writen_com(0x01);writen_com(0x80);for(unm=0;unm<14;unm++){writen_dat(table4[unm]);delay(5);}delay1(6);main();}/*------------------------------------*/ //第二次确认密码void affirm_code(){P1=0x07;if(P1!=0x07)delay(1);if(P1!=0x07){P1=0x77;if(h1==0){delay(1);if(h1==0){while(!h1);writen_com(0x80+0x45+sex);writen_dat('*');switch(sex){case 0:w1=1;break;case 1:w2=1;break;case 2:w3=1;break;case 3:w4=1;break;case 4:w5=1;break;case 5:w6=1;break;}sex++;}}if(h2==0){delay(1);if(h2==0){while(!h2);writen_com(0x80+0x45+sex);writen_dat('*');{case 0:w1=2;break;case 1:w2=2;break;case 2:w3=2;break;case 3:w4=2;break;case 4:w5=2;break;case 5:w6=2;break;}sex++;}}if(h3==0){delay(1);if(h3==0){while(!h3);writen_com(0x80+0x45+sex); writen_dat('*');switch(sex){case 0:w1=3;break;case 1:w2=3;break;case 2:w3=3;break;case 3:w4=3;break;case 4:w5=3;break;case 5:w6=3;break;}sex++;}}/*-------------------------------*/ P1=0x6f;if(h1==0){delay(1);if(h1==0){while(!h1);writen_com(0x80+0x45+sex); writen_dat('*');{case 0:w1=4;break;case 1:w2=4;break;case 2:w3=4;break;case 3:w4=4;break;case 4:w5=4;break;case 5:w6=4;break;}sex++;}}if(h2==0){delay(1);if(h2==0){while(!h2);writen_com(0x80+0x45+sex); writen_dat('*');switch(sex){case 0:w1=5;break;case 1:w2=5;break;case 2:w3=5;break;case 3:w4=5;break;case 4:w5=5;break;case 5:w6=5;break;}sex++;}}if(h3==0){delay(1);if(h3==0){while(!h3);writen_com(0x80+0x45+sex); writen_dat('*');switch(sex){case 1:w2=6;break;case 2:w3=6;break;case 3:w4=6;break;case 4:w5=6;break;case 5:w6=6;break;}sex++;}}/*--------------------------------*/ P1=0x5f;if(h1==0){delay(1);if(h1==0){while(!h1);writen_com(0x80+0x45+sex); writen_dat('*');switch(sex){case 0:w1=7;break;case 1:w2=7;break;case 2:w3=7;break;case 3:w4=7;break;case 4:w5=7;break;case 5:w6=7;break;}sex++;}}if(h2==0){delay(1);if(h2==0){while(!h2);writen_com(0x80+0x45+sex); writen_dat('*');switch(sex){case 1:w2=8;break;case 2:w3=8;break;case 3:w4=8;break;case 4:w5=8;break;case 5:w6=8;break;}sex++;}}if(h3==0){delay(1);if(h3==0){while(!h3);writen_com(0x80+0x45+sex); writen_dat('*');switch(sex){case 0:w1=9;break;case 1:w2=9;break;case 2:w3=9;break;case 3:w4=9;break;case 4:w5=9;break;case 5:w6=9;break;}sex++;}}/*-----------------------------------*/ P1=0x3f;if(h2==0) //0键扫描{delay(1);if(h2==0){while(!h2);writen_com(0x80+0x45+sex); writen_dat('*');switch(sex){case 1:w2=0;break;case 2:w3=0;break;case 3:w4=0;break;case 4:w5=0;break;case 5:w6=0;break;}sex++;}}if(h3==0) //#键扫描{delay(1);if(h3==0){while(!h3);while(!flge1)cheak_mima();}}}}/*--------------------------------------*/ //修改密码自己做的基于单片机的密码锁设计(c语言设计)时间:2011-08-16 21:08:01 来源:作者:void set_mima(){P1=0x07;if(P1!=0x07)delay(1);if(P1!=0x07){P1=0x77;if(h1==0){delay(1);if(h1==0){while(!h1);writen_com(0x80+0x45+sex);writen_dat('*');switch(sex){case 0:q1=1;break;case 1:q2=1;break;case 2:q3=1;break;case 3:q4=1;break;case 4:q5=1;break;case 5:q6=1;break;}sex++;}}if(h2==0){delay(1);if(h2==0){while(!h2);writen_com(0x80+0x45+sex); writen_dat('*');switch(sex){case 0:q1=2;break;case 1:q2=2;break;case 2:q3=2;break;case 3:q4=2;break;case 4:q5=2;break;case 5:q6=2;break;}sex++;}}if(h3==0){delay(1);if(h3==0){while(!h3);writen_com(0x80+0x45+sex); writen_dat('*');switch(sex){case 1:q2=3;break;case 2:q3=3;break;case 3:q4=3;break;case 4:q5=3;break;case 5:q6=3;break;}sex++;}}P1=0x6f;if(h1==0){delay(1);if(h1==0){while(!h1);writen_com(0x80+0x45+sex); writen_dat('*');switch(sex){case 0:q1=4;break;case 1:q2=4;break;case 2:q3=4;break;case 3:q4=4;break;case 4:q5=4;break;case 5:q6=4;break;}sex++;}}if(h2==0){delay(1);if(h2==0){while(!h2);writen_com(0x80+0x45+sex); writen_dat('*');switch(sex){case 0:q1=5;break;case 2:q3=5;break;case 3:q4=5;break;case 4:q5=5;break;case 5:q6=5;break;}sex++;}}if(h3==0){delay(1);if(h3==0){while(!h3);writen_com(0x80+0x45+sex); writen_dat('*');switch(sex){case 0:q1=6;break;case 1:q2=6;break;case 2:q3=6;break;case 3:q4=6;break;case 4:q5=6;break;case 5:q6=6;break;}sex++;}}P1=0x5f;if(h1==0){delay(1);if(h1==0){while(!h1);writen_com(0x80+0x45+sex); writen_dat('*');switch(sex){case 0:q1=7;break;case 1:q2=7;break;case 3:q4=7;break;case 4:q5=7;break;case 5:q6=7;break;}sex++;}}if(h2==0){delay(1);if(h2==0){while(!h2);writen_com(0x80+0x45+sex); writen_dat('*');switch(sex){case 0:q1=8;break;case 1:q2=8;break;case 2:q3=8;break;case 3:q4=8;break;case 4:q5=8;break;case 5:q6=8;break;}sex++;}}if(h3==0){delay(1);if(h3==0){while(!h3);writen_com(0x80+0x45+sex); writen_dat('*');switch(sex){case 0:q1=9;break;case 1:q2=9;break;case 2:q3=9;break;case 3:q4=9;break;case 5:q6=9;break;}sex++;}}P1=0x3f;if(h2==0) //0键扫描{delay(1);if(h2==0){while(!h2);writen_com(0x80+0x45+sex); writen_dat('*');switch(sex){case 0:q1=0;break;case 1:q2=0;break;case 2:q3=0;break;case 3:q4=0;break;case 4:q5=0;break;case 5:q6=0;break;}sex++;}}if(h3==0) //#键扫描{delay(1);if(h3==0){while(!h3);writen_com(0x01);writen_com(0x0f);writen_com(0x06);writen_com(0x80);for(unm=0;unm<14;unm++){writen_dat(table3[unm]);delay(5);}writen_com(0x80+0x40);for(unm=0;unm<5;unm++) {writen_dat(table1[unm]);delay(5);}sex=0;while(!flge1)affirm_code();}}}}void init();void cheak_identity2(){if(k1==M1){if(k2==M2){if(k3==M3){if(k4==M4){if(k5==M5){if(k6==M6){sex=0;init();while(!flge1)set_mima();}writen_com(0x01);writen_com(0x0c);writen_com(0x06);writen_com(0x80);for(unm=0;unm<14;unm++){writen_dat(table4[unm]);delay(5);}delay1(6);main();}writen_com(0x01);writen_com(0x0c);writen_com(0x06);writen_com(0x80);for(unm=0;unm<14;unm++) {writen_dat(table4[unm]); delay(5);}delay1(6);main();}writen_com(0x01);writen_com(0x0c);writen_com(0x06);writen_com(0x80);for(unm=0;unm<14;unm++) {writen_dat(table4[unm]); delay(5);}delay1(6);main();}writen_com(0x01);writen_com(0x0c);writen_com(0x06);writen_com(0x80);for(unm=0;unm<14;unm++) {writen_dat(table4[unm]); delay(5);}delay1(6);main();}writen_com(0x01);writen_com(0x0c);writen_com(0x06);writen_com(0x80);for(unm=0;unm<14;unm++){writen_dat(table4[unm]); delay(5);}delay1(6);main();}void cheak_identity(){P1=0x07;if(P1!=0x07)delay(1);if(P1!=0x07){P1=0x77;if(h1==0){delay(1);if(h1==0){while(!h1);writen_com(0x80+0x45+sex); writen_dat('*');switch(sex){case 0:k1=1;break;case 1:k2=1;break;case 2:k3=1;break;case 3:k4=1;break;case 4:k5=1;break;case 5:k6=1;break;}sex++;}}if(h2==0)delay(1);if(h2==0){while(!h2);writen_com(0x80+0x45+sex);writen_dat('*');switch(sex){case 0:k1=2;break;case 1:k2=2;break;case 2:k3=2;break;case 3:k4=2;break;case 4:k5=2;break;case 5:k6=2;break;}sex++;}}if(h3==0){delay(1);if(h3==0){while(!h3);writen_com(0x80+0x45+sex);writen_dat('*');switch(sex){case 0:k1=3;break;case 1:k2=3;break;case 2:k3=3;break;case 3:k4=3;break;case 4:k5=3;break;case 5:k6=3;break;}sex++;}}/*-------------------------------*/P1=0x6f;if(h1==0){delay(1);if(h1==0){while(!h1);writen_com(0x80+0x45+sex); writen_dat('*');switch(sex){case 0:k1=4;break;case 1:k2=4;break;case 2:k3=4;break;case 3:k4=4;break;case 4:k5=4;break;case 5:k6=4;break;}sex++;}}if(h2==0){delay(1);if(h2==0){while(!h2);writen_com(0x80+0x45+sex); writen_dat('*');switch(sex){case 0:k1=5;break;case 1:k2=5;break;case 2:k3=5;break;case 3:k4=5;break;case 4:k5=5;break;case 5:k6=5;break;}sex++;}}if(h3==0){delay(1);if(h3==0){while(!h3);writen_com(0x80+0x45+sex); writen_dat('*');switch(sex){case 0:k1=6;break;case 1:k2=6;break;case 2:k3=6;break;case 3:k4=6;break;case 4:k5=6;break;case 5:k6=6;break;}sex++;}}/*--------------------------------*/ P1=0x5f;if(h1==0){delay(1);if(h1==0){while(!h1);writen_com(0x80+0x45+sex); writen_dat('*');switch(sex){case 0:k1=7;break;case 1:k2=7;break;case 2:k3=7;break;case 3:k4=7;break;case 4:k5=7;break;case 5:k6=7;break;}sex++;}}if(h2==0){delay(1);if(h2==0){while(!h2);writen_com(0x80+0x45+sex);writen_dat('*');switch(sex){case 0:k1=8;break;case 1:k2=8;break;case 2:k3=8;break;case 3:k4=8;break;case 4:k5=8;break;case 5:k6=8;break;}sex++;}}if(h3==0){delay(1);if(h3==0){while(!h3);writen_com(0x80+0x45+sex); writen_dat('*');switch(sex){case 0:k1=9;break;case 1:k2=9;break;case 2:k3=9;break;case 3:k4=9;break;case 4:k5=9;break;case 5:k6=9;break;}sex++;}}/*-----------------------------------*/ P1=0x3f;if(h2==0) //0键扫描{delay(1);if(h2==0){while(!h2);writen_com(0x80+0x45+sex); writen_dat('*');switch(sex){case 0:k1=0;break;case 1:k2=0;break;case 2:k3=0;break;case 3:k4=0;break;case 4:k5=0;break;case 5:k6=0;break;}sex++;}}if(h3==0) //#键扫描{delay(1);if(h3==0){while(!h3);while(!flge1)cheak_identity2();}}}}/*---------------------------------------*/ //键盘扫描void keyscan(){P1=0x07;if(P1!=0x07)delay(1);if(P1!=0x07){P1=0x77;if(h1==0){delay(1);if(h1==0){while(!h1);writen_com(0x80+0x45+sex);writen_dat('*');switch(sex){case 0:k1=1;break;case 1:k2=1;break;case 2:k3=1;break;case 3:k4=1;break;case 4:k5=1;break;case 5:k6=1;break;}sex++;}}if(h2==0){delay(1);if(h2==0){while(!h2);writen_com(0x80+0x45+sex); writen_dat('*');switch(sex){case 0:k1=2;break;case 1:k2=2;break;case 2:k3=2;break;case 3:k4=2;break;case 4:k5=2;break;case 5:k6=2;break;}sex++;}}if(h3==0){delay(1);if(h3==0){while(!h3);writen_com(0x80+0x45+sex); writen_dat('*');switch(sex){case 0:k1=3;break;case 1:k2=3;break;case 2:k3=3;break;case 3:k4=3;break;case 4:k5=3;break;case 5:k6=3;break;}sex++;}}P1=0x6f;if(h1==0){delay(1);if(h1==0){while(!h1);writen_com(0x80+0x45+sex); writen_dat('*');switch(sex){case 0:k1=4;break;case 1:k2=4;break;case 2:k3=4;break;case 3:k4=4;break;case 4:k5=4;break;case 5:k6=4;break;}sex++;}}if(h2==0){delay(1);if(h2==0){while(!h2);writen_com(0x80+0x45+sex); writen_dat('*');switch(sex){case 0:k1=5;break;case 1:k2=5;break;case 2:k3=5;break;case 3:k4=5;break;case 4:k5=5;break;case 5:k6=5;break;}}}if(h3==0){delay(1);if(h3==0){while(!h3);writen_com(0x80+0x45+sex); writen_dat('*');switch(sex){case 0:k1=6;break;case 1:k2=6;break;case 2:k3=6;break;case 3:k4=6;break;case 4:k5=6;break;case 5:k6=6;break;}sex++;}}/*--------------------------------*/ P1=0x5f;if(h1==0){delay(1);if(h1==0){while(!h1);writen_com(0x80+0x45+sex); writen_dat('*');switch(sex){case 0:k1=7;break;case 1:k2=7;break;case 2:k3=7;break;case 3:k4=7;break;case 4:k5=7;break;case 5:k6=7;break;}}}if(h2==0){delay(1);if(h2==0){while(!h2);writen_com(0x80+0x45+sex); writen_dat('*');switch(sex){case 0:k1=8;break;case 1:k2=8;break;case 2:k3=8;break;case 3:k4=8;break;case 4:k5=8;break;case 5:k6=8;break;}sex++;}}if(h3==0){delay(1);if(h3==0){while(!h3);writen_com(0x80+0x45+sex); writen_dat('*');switch(sex){case 0:k1=9;break;case 1:k2=9;break;case 2:k3=9;break;case 3:k4=9;break;case 4:k5=9;break;case 5:k6=9;break;}sex++;}/*-----------------------------------*/ P1=0x3f;if(h1==0) //*键扫描{delay(1);if(h1==0){while(!h1);writen_com(0x0f);writen_com(0x06);writen_com(0x80);for(unm=0;unm<16;unm++){writen_dat(table6[unm]);delay(5);}while(!flge1)cheak_identity();}}if(h2==0) //0键扫描{delay(1);if(h2==0){while(!h2);writen_com(0x80+0x45+sex); writen_dat('*');switch(sex){case 0:k1=0;break;case 1:k2=0;break;case 2:k3=0;break;case 3:k4=0;break;case 4:k5=0;break;case 5:k6=0;break;}sex++;}}if(h3==0) //#键扫描delay(1);if(h3==0){while(!h3);flge=1;}}}}/*-------------------------------------*/ //密码检查void init();void check_code(){if(k1==M1){if(k2==M2){if(k3==M3){if(k4==M4){if(k5==M5){if(k6==M6){writen_com(0x01);kd=0;writen_com(0x0c);writen_com(0x06);writen_com(0x80);for(unm=0;unm<14;unm++){writen_dat(table2[unm]);delay(5);}delay1(6);kd=1;main();writen_com(0x0c);writen_com(0x06);writen_com(0x80);for(unm=0;unm<14;unm++) {writen_dat(table4[unm]); delay(5);}delay1(6);main();}writen_com(0x0c);writen_com(0x06);writen_com(0x80);for(unm=0;unm<14;unm++) {writen_dat(table4[unm]); delay(5);}delay1(6);main();}writen_com(0x0c);writen_com(0x06);writen_com(0x80);for(unm=0;unm<14;unm++) {writen_dat(table4[unm]); delay(5);}delay1(6);main();}writen_com(0x0c);writen_com(0x06);writen_com(0x80);for(unm=0;unm<14;unm++) {writen_dat(table4[unm]); delay(5);}delay1(6);main();}writen_com(0x0c);writen_com(0x06);writen_com(0x80);for(unm=0;unm<14;unm++) {writen_dat(table4[unm]); delay(5);}delay1(6);main();}}writen_com(0x0c);writen_com(0x06);writen_com(0x80);for(unm=0;unm<14;unm++) {writen_dat(table4[unm]); delay(5);}delay1(6);main();}void now_mima(){M1=0;M2=0;M3=0;M4=0;M5=0;M6=0;g=0;}void init(){if(g==0)now_mima();g=1;flge1=0;flge=0;sex=0;lcdrw=0;lcden=0;writen_com(0x01);writen_com(0x38);writen_com(0x0f);writen_com(0x06);writen_com(0x80);for(unm=0;unm<14;unm++){writen_dat(table[unm]); delay(5);}writen_com(0x80+0x40);for(unm=0;unm<5;unm++) {writen_dat(table1[unm]); delay(5);}}void main(){while(1){init();set_mima();while(!flge)keyscan();check_code();}}。

相关主题