51单片机,芯片STC89C52
密码修改部分,我的思路是这样的:
1.首先输入原密码,若输错就判断为输入错误,直到输入正确才能进入下一个环节;
2.原密码输入正确后进入修改密码环节;
3.第一次输入新密码;
4.第二次输入新密码,若输对就成功修改密码,若输错就只能再次输入直到输入正确。
目前问题出现在第4部分:
1.新密码只能是666666等几位数字都一样的密码,若输入123456等密码就显示错误;
2.密码修改成功后返回到密码输入环节(这部分代码我没发出来,但感觉没问题),重新输入密码,此时无论是输入旧密码还是新密码都会显示错误;
3.若第二次密码输入错误,不能成功进入“再次输入直到输入正确”这一环节。
请问我的代码问题出现在哪里?请各位朋友指正!
下面是部分代码:
#include <REGX52.H>
#include <intrins.H>
#include "LCD1602.h"
#include "juzhen.h"
#include "delay.h"
#include "Buzzer.h"
#include "Timer0.h"
unsigned char KeyNum,i=0,ErrorNum;
unsigned char Password,cout;
char pw[6];
static char realpw[6]={0,0,0,0,0,0};
void judge(unsigned char press_flag,unsigned int count);
unsigned char press_flag;
unsigned int count;
void PasswordChange();
//密码修改模块
void PasswordChange()
{
char i=0,A=6,KeyNum2;
cout=0;//计算当前输入的密码位数
while(1)
{
KeyNum2=juzhen(); //juzhen矩阵键盘
if(KeyNum2)
{
if(KeyNum2<=10)
{
if(KeyNum2==10){KeyNum2=0;}
if(cout<6)
{
pw[i]=KeyNum2;
LCD_wfh10(2,cout+1,pw[i],1);
i++;
cout++;
Buzzer_Time(100);
}
}
}
if(KeyNum2==11)
{
Buzzer_Time(100);
for(i=0;i<6;i++)
{
if(pw[i]!=realpw[i]){A=0;break;}
else {A=1;}
}
if(cout<6){A=0;}
}
if(A==0)//原密码输入错误
{
Buzzer_Time2(100);
LCD_zfc(1,14,"ERR");
delay(500);
LCD_zfc(1,14," ");
cout=0;
i=0;
LCD_zfc(2,1," ");
pw[6]=0;
A=6;
}
if(A==1)//正式修改密码前的初始化
{
LCD_zfc(1,14,"OK ");
delay(500);
LCD_zfc(1,14," ");
LCD_zfc(1,1,"input new: ");
cout=0;
i=0;
LCD_zfc(2,1," ");
pw[6]=0;realpw[6]=0;
A=2;
}
if(A==2)//修改密码值
{
i=0;
KeyNum2=juzhen();
if(KeyNum2)
{
if(KeyNum2<=10)
{
if(KeyNum2==10){KeyNum2=0;}
if(cout<6)
{
realpw[i]=KeyNum2;
LCD_wfh10(2,cout+1,realpw[i],1);
i++;
cout++;
Buzzer_Time(100);
}
}
}
if(cout==6)
{
LCD_zfc(1,1,"input again:");
LCD_zfc(2,1," ");
Buzzer_Time(100);
cout=0;
A=3;
}
}
if(A==3)
{
KeyNum2=juzhen();
if(KeyNum2)
{
if(KeyNum2<=10)
{
if(KeyNum2==10){KeyNum2=0;}
if(cout<6)
{
pw[i]=KeyNum2;
LCD_wfh10(2,cout+1,pw[i],1);
i++;
cout++;
Buzzer_Time(100);
}
}
}
if(cout==6)
{
for(i=0;i<6;i++)
{
if(pw[i]!=realpw[i]){A=4;break;}
else{Buzzer_Time(100);LCD_zfc(1,1,"Have changed!");delay(1000);LCD_zfc(1,1," ");A=6;main();}
}
}
}//A==3
if(A==4)
{
while(A==4)
{
Buzzer_Time2(100);
A=6;
LCD_zfc(1,1," ");
LCD_zfc(1,1,"ERROR! ");
LCD_zfc(2,1," ");
delay(50);
pw[6]=0;
i=0;
cout=0;
KeyNum2=juzhen();
if(KeyNum2)
{
if(KeyNum2<=10)
{
if(KeyNum2==10){KeyNum2=0;}
if(cout<6)
{
pw[i]=KeyNum2;
LCD_wfh10(2,cout+1,pw[i],1);
i++;
cout++;
Buzzer_Time(100);
}
}
}
if(cout==6)
{
for(i=0;i<6;i++)
{
if(pw[i]!=realpw[i]){A=4;break;}
else {LCD_zfc(1,1,"Have changed!");delay(1000);LCD_zfc(1,1," ");A=6;}
}
}
}
}//A=4
if(KeyNum2==12)//密码修改模块的取消键,不能删
{
Buzzer_Time(100);
LCD_zfc(2,cout," ");
cout--;
i--;
pw[cout]=0;
}
}//while
}