密码锁-无储存(掉电密码丢失,可根据自己需求加存储如24c02)
单片机源程序如下:
- #include<reg52.h>
- #include"LCD.H"
- #define uchar unsigned char
- #define uint unsigned int
- uchar count=1,ci=0;//修改密码时,只清屏一次,给wei=-1赋初值
- uchar mima[4];
- bit flag,flag1,flag2;//flag为按键14,15是否有效,flag1为修改密码是否有效,flag2为初始化密码是否有效
- sbit beep=P1^5;
- uchar table[4];//初始密码
- uchar code table1[]="pass word!";
- uchar code table2[]="welcome!";
- uchar code table3[]="error!";
- uchar code table4[]="alarm!";
- uchar code table5[]="new mima!";
- void feng() //蜂鸣器函数
- {
- uchar i;
- for(i=0;i<10;i++)
- {
- beep=0;
- delayms(200);
- beep=1;
- delayms(200);
- }
- }
- void chushi() //初始函数
- {
- uchar i;
- write_byte(0,0x82);
- for(i=0;i<10;i++)
- {
- write_byte(1,table1[i]);
- }
- write_byte(1,'[');
- write_byte(1,0x30+count); //count是输入密码次数,当3次时,蜂鸣器报错
- write_byte(1,']');
- }
- void bijiao() //比较函数
- {
- uchar i;
- keyscan(); //键盘扫描函数
- if(flag1==0) //当修改密码没有按下时,正常输入密码
- {
- if((keynum>=0)&&(keynum<=9))
- {
- write_byte(0,0xc0+wei);
- if((wei>=0)&&(wei<=3))//密码为4位,wei位密码位数
- {
- write_byte(1,'*');
- mima[wei]=keynum; //将密码存入到mima[]数组中,用于比较
- }
- }
- }
- if(flag1==1)//当密码修改按下时
- {
- if(ci==0) //清屏,wei=-1,只执行一次
- {
- write_byte(0,0x01);
- delayms(20);
- wei=-1;
- ci++; //
- }
- else
- {
- write_byte(0,0x84); //修改密码
- for(i=0;i<8;i++)
- {
- write_byte(1,table5[i]);
- }
- if((keynum>=0)&&(keynum<=9)) //修改密码位数为4
- {
- write_byte(0,0xc0+wei);
- if((wei>=0)&&(wei<=3))
- {
- write_byte(1,0x30+keynum);
- table[wei]=keynum;
- }
- }
- flag=1; //开按键14,关按键15
- flag2=1; //将修改后的密码存入table[]中
- count=1; //输入密码次数为1
- }
- }
- if(flag==0) //按键15有效
- {
- if(flag2==0) //初始密码有效
- {
- table[0]=1;table[1]=2;table[2]=3;table[3]=4;
- }
- if(keynum==15)
- {
- if((mima[0]==table[0])&&(mima[1]==table[1])&&
- (mima[2]==table[2])&&(mima[3]==table[3])) //密码比较 ,正确的话显示welcome
- {
- write_byte(0,0x01);
- delayms(20);
- write_byte(0,0x82);
- for(i=0;i<8;i++)
- {
- write_byte(1,table2[i]);
- }
- while(1);
- }
- else //错误的话显示error
- {
- write_byte(0,0x01);
- delayms(20);
- write_byte(0,0x84);
- for(i=0;i<6;i++)
- {
- write_byte(1,table3[i]);
- }
- count++; //输入次数,最多三次
- flag=1; //按键14打开,15关闭
- if(count>=4) //多余三次蜂鸣器响
- {
- write_byte(0,0x84);
- for(i=0;i<6;i++)
- {
- write_byte(1,table4[i]);
- }
- feng();
- while(1);
- }
- }
- }
- if(keynum==13) //按键13修改密码有效
- {
- flag1=1;
- }
- }
- if(flag==1) //按键14有效
- {
- if(keynum==14) //清屏,初始化,输入次数加一
- {
- write_byte(0,0x01);
- delayms(20);
- chushi();
- wei=-1;
- flag=0; //按键15有效
- flag1=0; //修改密码无效
- }
- }
- }
- void main()
- {
- init_lcd1602();
- chushi();
- while(1)
- {
- bijiao();
- }
- }
复制代码
所有资料51hei提供下载:
密码锁-无存储.zip
(37.42 KB, 下载次数: 10)
|