您好,欢迎访问三七文档
当前位置:首页 > 电子/通信 > 综合/其它 > 单片机设计方案密码锁程序和仿真图
1/10#includeat89x51.h#defineucharunsignedchar#defineuintunsignedintuintnum=10;//开始让数码管什么都不显示bitset=0;//定义设置密码的位charcount=-1;//开始让COUNT=-1,方便后面显示数码管sbitBeep=P1^2;//蜂鸣器uchartemp;ucharpws[6]={3,6,2,3,3,0};//原始密码ucharpwx[6];//按下的数字存储区bitrightflag;//密码正确标志位ucharworkbuf[6];ucharcodetabledu[]={0x3f,0x06,0x5b,0x4f,0x66,0x6d,0x7d,0x07,0x7f,0x6f,0x00,0x40};//段选码,共阴极ucharcodetablewe[]={0xfe,0xfd,0xfb,0xf7,0xef,0xdf};//位选码uintkeyscan();voiddelay(ucharz)//延时,ms级{uchary;for(;z0;z--)for(y=120;y0;y--);}voidsetpw()//设置密码函数{keyscan();}uintkeyscan()//键盘扫描函数{P3=0xfe;temp=P3;temp=temp&0xf0;if(temp!=0xf0){delay(5);//键盘去抖,最好ms以上,这里用了mstemp=P3;temp=temp&0xf0;if(temp!=0xf0){count++;//按键计数加temp=P3;2/10switch(temp){case0xee:{num=7;if(count6)//六位密码,所以COUNT6{if(set==0)//设置密码键没有按下时pwx[count]=num;//存储按下的数字elsepws[count]=num;//设置密码键按下时,设置新密码workbuf[count]=tabledu[11];//相应位的数码管显示--,不显示相应的数字,密码是保密的}}break;case0xde:{num=8;if(count6)//以下扫描键盘的原理差不多同上{if(set==0)pwx[count]=num;elsepws[count]=num;workbuf[count]=tabledu[11];}}break;case0xbe:{num=9;{if(count6){if(set==0)pwx[count]=num;elsepws[count]=num;workbuf[count]=tabledu[11];}}}break;3/10case0x7e://设置密码键按下{set=1;//设置密码标志位置P1_3=0;//设置密码指示灯亮workbuf[0]=0x00;//数码管第一位不显示workbuf[1]=0x00;//......workbuf[2]=0x00;//......workbuf[3]=0x00;workbuf[4]=0x00;workbuf[5]=0x00;//......count=-1;//按键计数复位为-1if(count6)//密码没有设置完,继续设置密码{setpw();//设置密码}}break;}while(temp!=0xf0)//按键抬起检测{temp=P3;temp=temp&0xf0;}}}P3=0xfd;temp=P3;temp=temp&0xf0;if(temp!=0xf0){delay(5);temp=P3;temp=temp&0xf0;if(temp!=0xf0){count++;temp=P3;switch(temp){case0xed:{num=4;if(count6){4/10if(set==0)pwx[count]=num;elsepws[count]=num;workbuf[count]=tabledu[11];}}break;case0xdd:{num=5;if(count6){if(set==0)pwx[count]=num;elsepws[count]=num;workbuf[count]=tabledu[11];}}break;case0xbd:{num=6;if(count6){if(set==0)pwx[count]=num;elsepws[count]=num;workbuf[count]=tabledu[11];}}break;}while(temp!=0xf0){temp=P3;temp=temp&0xf0;}}}P3=0xfb;temp=P3;5/10temp=temp&0xf0;if(temp!=0xf0){delay(5);temp=P3;temp=temp&0xf0;if(temp!=0xf0){count++;temp=P3;switch(temp){case0xeb:{num=1;if(count6){if(set==0)pwx[count]=num;elsepws[count]=num;workbuf[count]=tabledu[11];}}break;case0xdb:{num=2;if(count6){if(set==0)pwx[count]=num;elsepws[count]=num;workbuf[count]=tabledu[11];}}break;case0xbb:{num=3;if(count6){if(set==0)6/10pwx[count]=num;elsepws[count]=num;workbuf[count]=tabledu[11];}}break;}while(temp!=0xf0){temp=P3;temp=temp&0xf0;}}}P3=0xf7;temp=P3;temp=temp&0xf0;if(temp!=0xf0){delay(5);temp=P3;temp=temp&0xf0;if(temp!=0xf0){count++;temp=P3;switch(temp){case0xd7:{num=0;if(count6){if(set==0)pwx[count]=num;elsepws[count]=num;workbuf[count]=tabledu[11];}}break;case0xe7:num=20;break;//确定键按下检测case0x77://复位键或者输入密码全部一次删除7/10{P1_1=0;//锁关P1_3=1;//密码设置指示灯灭set=0;//不设置密码num=10;//num复位count=-1;//COUNT复位workbuf[0]=tabledu[10];//第一位数码管不显示workbuf[1]=tabledu[10];//第二位数码管不显示workbuf[2]=tabledu[10];workbuf[3]=tabledu[10];workbuf[4]=tabledu[10];workbuf[5]=tabledu[10];//......P1_0=1;//锁关}break;case0xb7://输入密码删除键(一位一位删除){count--;workbuf[count]=0x00;//因确定键按下时,COUNT也会加,而确定键不是密码,所以这里是COUNT,而不是COUNT+1count--;//因确定键按下时,确定键不是密码,COUNT也会加,这里COUNT再自减if(count=-1)count=-1;}break;}while(temp!=0xf0){temp=P3;temp=temp&0xf0;}}}return(num);}voidinit()//利用定时显示数码管{TMOD=0x01;TH0=(65536-500)/200;TL0=(65536-500)%200;ET0=1;EA=1;TR0=1;8/10}bitcompare()//密码比较函数{if((pwx[0]==pws[0])&(pwx[1]==pws[1])&(pwx[2]==pws[2])&(pwx[3]==pws[3])&(pwx[4]==pws[4])&(pwx[5]==pws[5]))rightflag=1;elserightflag=0;return(rightflag);}voidmain(){uinti,j;init();P0=0;P1_1=0;//锁关while(1){keyscan();if(num==20)//如果确定键按下(修改密码和输入密码共用的确定键){if(count==6){if(set==1)//修改密码确定{P1_3=1;workbuf[0]=0;workbuf[1]=0;workbuf[2]=0;workbuf[3]=0;workbuf[4]=0;workbuf[5]=0;}else//输入密码确定{set=0;compare();if(rightflag==1)//如果密码正确{P1_0=0;//锁开P1_1=1;workbuf[0]=tabledu[8];//数码管第一位显示8workbuf[1]=tabledu[8];//数码管第二位显示8workbuf[2]=tabledu[8];9/10workbuf[3]=tabledu[8];workbuf[4]=tabledu[8];workbuf[5]=tabledu[8];//......}else{P1_1=0;//锁仍然是关workbuf[0]=0X71;//数码管第一位显示Fworkbuf[1]=0X71;workbuf[2]=0X71;workbuf[3]=0X71;workbuf[4]=0X71;workbuf[5]=0X71;//......for(i=0;i1000;i++)//密码错误报警{for(j=0;j80;j++);Beep=~Beep;}break;}}}else//若输入的密码位数不为位时{P1_1=0;//锁仍然关workbuf[0]=0X71;//数码管第一位显示Fworkbuf[1]=0X71;workbuf[2]=0X71;workbuf[3]=0X71;workbuf[4]=0X71;workbuf[5]=0X71;for(i=0;i1000;i++){for(j=0;j80;j++);Beep=~Beep;}break;}}}}voidtimer0()interrupt1//显示数码管{uchari;10/10TH0=(65536-500)/200;TL0=(65536-500)%200;for(i=0;i6;i++){P0=workbuf[i];P2=tablewe[i];delay(5);P0=0;}}XTAL218XTAL119ALE30EA31PSEN29RST9P0.0/AD039P0.1/AD138P0.2/AD237P0.3/AD336P0.4/AD435P0.5/AD534P0.6/AD633P0.7/AD732P1.01P1.12P1.23P1.34P1.45P1.56P1.67P1.78P3.0/RXD10P3.1/TXD11P3.2/INT012P3.3/INT113P3.4/T014P3.7/RD17P3.6/WR16P3.5/T115P2.7/A1528P2.0/A821P2.1/A922P2.2/A1023P2.3/A1124P2.4/A1225P2.5/A1326P2.6/A1427U1AT89C51234567891RP1RESPACK-812365489=7++CON0ABCD1243D2LED-RED锁开锁关LS1SOUNDERD3LED-BLUE设置密码指示灯R11kR21kR310KR410K+12VQ3TIP127Q4TIP127Q18050Q28050Q5TIP122Q6TIP122+88.8Amps+88.8Amps+88.8Volts
本文标题:单片机设计方案密码锁程序和仿真图
链接地址:https://www.777doc.com/doc-4045166 .html