#include<reg52.h>
#define uchar unsigned char
#define uint unsigned int
void delay(uint);
void change_password(); //重设密码,检测原密码
void pleaseresetpassword(); //重新设置密码,输入新的密码
void showinputpassword(); //显示输入密码提示符
void inputpasstest(); //密码输入检测
void inputtestnum(); //输入密码的位数检测
void keysetlock (); //键盘锁定函数
void locktest(); //键盘锁定时,按键检测函数
sbit DULA=P2^6;
sbit WELA=P2^7;
sbit LCDRS=P3^5;
sbit LCDEN=P3^4;
sbit s1=P3^0;
sbit s2=P3^1;
sbit s3=P3^2;
sbit s4=P3^3;
sbit P3_6=P3^6;
//sbit s5=P3^7;
sbit P2_3=P2^3;
uchar string3[]="keyset lock";
uchar string[]="input password",string1[]="pass",string2[]="error",show[]={48,49};
uchar password[5] ,resetpassword[5],unreset[5];//={0,0,0,0,0}; //密码储存数组 , 重设置密码时储存的数组
uchar string4[]="input new pass"; //重设新密码提示符
uchar string5[]="succeed";
uchar string6[]="input old pass";
uchar string7[]="confirm Y/N",string8[]="fail";
uchar strsmark=1,locksmark=0; //strsmart为显示输入密码提示标识, locksmart为正在锁键中
uchar resetpasswordsmark=0; //重设密码时的标识符
uchar testoldpasswordsmark=0;
uchar inputnew=0;
uchar resetsure=0;
uchar m=0;
uchar i,j=0,k=0; //K为输入密码错误的次数 J为输入密码位数
write_command(char com)
{
LCDRS=0;
P0=com;
delay(5);
LCDEN=1;
delay(5);
LCDEN=0;
}
write_data(char date)
{
LCDRS=1;
P0=date;
delay(5);
LCDEN=1;
delay(5);
LCDEN=0;
}
void Init()
{
DULA=0;
WELA=0;
LCDEN=0;
write_command(0x38);
write_command(0x0e);
write_command(0x06);
write_command(0x01);
write_command(0x80);
}
////////主函////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////////////
void main()
{
EA=1;
ET0=1;
TMOD=0X01;
TH0=(65536-50000)/256;
TL0=(65536-50000)%256;
TR0=0;
Init();
P3_6=0;
while(1)
{
change_password();
// pleaseresetpassword(); //重新设置密码
if(resetpasswordsmark==0) //重设密码标识符,0为不设置
{
showinputpassword(); //输入密码提示符
inputpasstest(); //密码输入检测
inputtestnum(); //输入密码位数检测
if(k==3)
keysetlock(); //调用键盘锁定函数
locktest(); //调用键盘锁定时检测按键函数
}
}
}
///////重设密码//////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////////
void change_password() //重设密码,检验旧密码
{
if(s3==0)
{
delay(10);
if(s3==0)
{
write_command(0x01);
write_command(0x80);
for(i=0;i<14;i++) //提示输入旧密码
{
write_data(string6[i]);
}
write_command(0xc0);
while(s3==0);
resetpasswordsmark=1; //重设密码标识,1为打开
testoldpasswordsmark=1; //检验旧密码,1为开户
j=0;
}
}
/////以上是检测是否要重设密码///////////////
///////以下是检验旧密码的过程//////////////////////////////
if(testoldpasswordsmark==1)
{
if(s1==0)
{
delay(10);
if(s1==0)
{
write_data(42);
password[j]=0;
j++;
while(s1==0);
}
}
if(s2==0)
{
delay(10);
if(s2==0)
{
write_data(42);
password[j]=1;
j++;
while(s2==0);
}
}
}
if((j==5)&&resetpasswordsmark==1) //判断按键是否输入了五次
{
testoldpasswordsmark=0; //检验密码 输入标识,0为禁止
////////以下判断,是否输入正确
if(password[0]==resetpassword[0]&&password[1]==resetpassword[1]&&password[2]==resetpassword[2]&&password[3]==resetpassword[3]&&password[4]==resetpassword[4])// 决断密码是否正确
{
write_command(0x01); //清屏
write_command(0xc4); //光标定位
write_command(0x0c); //消除光标显示
for(i=0;i<7;i++) //提示验证密码成功
{
write_data(string5[i]);
}
delay(1000);
write_command(0x01);
write_command(0x80);
write_command(0x0e); // 显示光标
for(i=0;i<14;i++) //提示输入新密码
{
write_data(string4[i]);
}
write_command(0xc0); //重置新密码输入的位置
inputnew=1; //输入新密码标识
testoldpasswordsmark=0; //检验旧密码,0为关闭检验
}
else
{
write_command(0x01);
write_command(0xc4);
write_command(0x0c);
for(i=0;i<5;i++) //提示输入错误
{
write_data(string2[i]);
}
delay(500);
write_command(0x01);
write_command(0x0e);
write_command(0x80);
resetpasswordsmark=0; //设置密码标识,0为禁止,
testoldpasswordsmark=0; //检验密码标识 ,0为禁止
strsmark=1;
}
j=0;
// if(inputnewpasswordsmark==1)
// pleaseresetpassword(); //输入新的密码
}
if(inputnew==1)
{
pleaseresetpassword(); //输入新的密码
}
}
////设置新密码,输入新的密码 并提示修改成功
void pleaseresetpassword()
{
if((s1==0)&&resetsure==0)
{
delay(10);
if(s1==0)
{
write_data(42);
unreset[j]=0;
j++;
while(s1==0);
}
}
if((s2==0)&&resetsure==0)
{
delay(10);
if(s2==0)
{
write_data(42);
unreset[j]=1;
j++;
while(s2==0);
}
}
if(j==5)
{
write_command(0x01);
write_command(0x0c);
write_command(0x81);
for(i=0;i<11;i++) //确认更改提示,
{
write_data(string7[i]);
}
j=0;
resetsure=1;
}
if((s1==0)&&resetsure==1) //按S8 表示确定更改密码,
{
delay(10);
if((s1==0)&&resetsure==1)
{
write_command(0x01);
write_command(0xc4);
write_command(0x0c);
for(i=0;i<7;i++) //提示重设密码成功
{
write_data(string5[i]);
}
delay(500);
write_command(0x01);
write_command(0x0e);
// write_command(0xc0);
resetpasswordsmark=0;
strsmark=1;
inputnew=0;
resetsure=0;
for(j=0;j<5;j++)
{
resetpassword[j]=unreset[j];
}
j=0;
}
}
if((s2==0)&&resetsure==1) //按S12表示取消重设密码,
{
delay(10);
if((s2==0)&&resetsure==1)
{
write_command(0x01);
write_command(0x0c);
write_command(0xc4);
for(i=0;i<4;i++) //重新设置密码失败提示
{
write_data(string8[i]);
}
delay(500);
write_command(0x01);
write_command(0xce);
write_command(0xc);
resetpasswordsmark=0;
strsmark=1;
inputnew=0;
resetsure=0;
}
}
}
/////////////显示输入密码提示 ///////////////////////////////////////////////
//////////////////////////////////////////////////////////////////////////////////////////
void showinputpassword()
{
if(strsmark==1) //strsmart为显示输入标识,1为允许
{
write_command(0x80);
for(i=0;i<14;i++)
{
write_data(string[i]);
}
write_command(0xc0);
strsmark=0;
}
}
void inputpasstest() //密码输入检测函数
{
if((s1==0)&&locksmark==0)
{
delay(10);
if((s1==0)&&locksmark==0)
{
write_data(42);
password[j]=0;
j++;
while(s1==0);
}
}
if((s2==0)&&locksmark==0)
{
delay(10);
if((s2==0)&&locksmark==0)
{
write_data(42);
password[j]=1;
j++;
while(s2==0);
}
}
}
////////输入密码位数检测,并检测输入是否正确 /////////////////////////////////////////////
void inputtestnum()
{
if(j==5) //判断按键是否输入了五次
{
if(password[0]==resetpassword[0]&&password[1]==resetpassword[1]&&password[2]==resetpassword[2]&&password[3]==resetpassword[3]&&password[4]==resetpassword[4])// 决断密码是否正确
{
write_command(0x01); //清屏
P1=0XFE;
write_command(0xc4); //光标定位
write_command(0x0c); //消除光�