c51单片机在下面程序里面,在输入键盘数字时,已经输入了4位数字后,本来应该是停下来没有继续显示数字,但是LCD1602却还是显示最后输入的数字。并且一直重复。有没有什么解决的方案。
#include <reg51.h>
#include <string.h>
#define PASSWORD_LENGTH 6
char input_password[PASSWORD_LENGTH];
#define AT24C02_ADDRESS 0xA0
#define LCD_COLS 16
#define PASSWORD_SIZE 4
#define LCD_LINE_LENGTH 16
sbit lcd_RS = P2^0;
sbit lcd_RW = P2^1;
sbit lcd_E = P2^2;
sbit SDA = P3^3;
sbit SCL = P3^2;
sbit jidianqi = P2^7;
char key[] = {0, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37, 0x38, 0x39, 0x30};
char table[] = "password";
char table1[] = "input:";
char table2[] ="newpassword";
int current_input_index = 0;
char passwordInput[5];
char key2[] = "Correct";
char key3[] = "Error";
char newPassword;
void delay(unsigned int x) {
unsigned int i,j;
for(i = 0; i < x; i++)
for(j = 0; j < 12000; j++);
}
void write_com(unsigned char com){
lcd_RS = 0;
lcd_RW = 0;
delay(2);
P0 = com;
delay(1);
lcd_E = 1;
delay(1);
lcd_E = 0;
}
// 写数据到LCD函数
void write_dat(unsigned char dat){
lcd_RS = 1;
lcd_RW = 0;
P0 = dat;
delay(1);
lcd_E = 1;
delay(1);
lcd_E = 0;
}
void init() {
write_com(0x01);
write_com(0x38);
write_com(0x0c);
write_com(0x06);
}
unsigned char juxingjianpan() {
unsigned char KeyNumber, x, y,debounce_count;
for (debounce_count = 0; debounce_count < 5; debounce_count++) { // 假设循环5次作为消抖延时
P1 = 0x0f;
x = P1;
P1 = 0xf0;
y = P1;
// 检查按键是否稳定(连续读取几次都相同)
if ((x + y) == (P1 & 0x0f) + ((P1 & 0xf0) >> 4)) {
break;
}
delay(10); // 添加一个简短的延时,例如20ms
}
switch(x + y) {
case 0xee: KeyNumber = 1; break;
case 0xde: KeyNumber = 2; break;
case 0xbe: KeyNumber = 3; break;
case 0x7e: KeyNumber = 4; break;
case 0xed: KeyNumber = 5; break;
case 0xdd: KeyNumber = 6; break;
case 0xbd: KeyNumber = 7; break;
case 0x7d: KeyNumber = 8; break;
case 0xeb: KeyNumber = 9; break;
case 0xdb: KeyNumber = 10; break;
case 0xbb: KeyNumber = 11; break;
case 0x7b: KeyNumber = 12; break;
case 0xe7: KeyNumber = 13; break;
case 0xd7: KeyNumber = 14; break;
case 0xb7: KeyNumber = 15; break;
}
return KeyNumber;
}
void write_password_char() {
write_dat(42);
}
void i2c_init() {
SDA = 1;
SCL = 1;
}
void i2c_Start(void) {
SDA = 1;
SCL = 1;
SDA = 0;
SCL = 0;
}
void i2c_Stop(void) {
SDA = 0;
SCL = 1;
SDA = 1;
}
void i2c_sendByte(unsigned char Byte) //发送一个字节
{ unsigned char i;
for(i=0;i<8;i++)
{ SDA = Byte&(0X80>>i);
SCL = 1;
SCL = 0;
}
}
unsigned char i2c_ReceiverByte(void) //接收一个字节
{ unsigned char i,Byte=0x00;
SDA = 1;
for(i=0;i<8;i++)
{ SCL = 1;
if(SDA){Byte|=(0x80>>i);}
SCL = 0;
}
return Byte;
}
void i2c_sendAck(unsigned char AckBit)
{ SDA =AckBit;
SCL = 1;
SCL = 0;
}
unsigned char i2c_ReceiveAck(void)
{ unsigned char AckBit=0;
SDA =1;
SCL =1;
AckBit=SDA;
SCL =0;
return AckBit;
}
void AT24C02_WriteByte( unsigned char wordAddress,Data) //写字节
{
i2c_Start();
i2c_sendByte(AT24C02_ADDRESS);
i2c_ReceiveAck();
i2c_sendByte(wordAddress);
i2c_ReceiveAck();
i2c_sendByte(Data);
i2c_ReceiveAck();
i2c_Stop();
}
unsigned char AT24C02_ReadByte(unsigned char wordAddress)
{ unsigned char Data;
i2c_Start();
i2c_sendByte(AT24C02_ADDRESS);
i2c_ReceiveAck();
i2c_sendByte(wordAddress);
i2c_ReceiveAck();
i2c_Start();
i2c_sendByte(AT24C02_ADDRESS|0x01);
i2c_ReceiveAck();
Data=i2c_ReceiverByte();
i2c_sendAck(1);
i2c_Stop();
return Data;
}
void main() {
static unsigned char position = 0, x = 0, y;
unsigned int i, KeyNum, passwordLen = 0;
unsigned char storedPassword[PASSWORD_SIZE];
unsigned char addressToWrite = 0;
unsigned char addressToRead = 0;
init();
i2c_init();
// 在LCD上显示"password"和"input:"
write_com(0x80);
for(i = 0; i < strlen(table); i++) {
write_dat(table[i]);
}
write_com(0xc0);
for(i = 0; i < strlen(table1); i++) {
write_dat(table1[i]);
}
while(1) {
write_com(0xc6 + position); // 设置初始显示位置
KeyNum = juxingjianpan();
if(KeyNum) {
if(KeyNum <= 10 && x <= 3) {
write_dat(key[KeyNum]); // 显示输入的字符
passwordInput[passwordLen++] = key[KeyNum];
position++;
}
if(KeyNum == 11) { // 确认键
if(strcmp(passwordInput, "1234") == 0) {
write_com(0x01);
delay(20);
write_com(0x86);
jidianqi = 0;
for(y = 0; y < 7; y++) {
write_dat(key2[y]);
delay(5);
}
} else {
write_com(0x01);
delay(20);
write_com(0x86);
for(y = 0; y < 5; y++) {
write_dat(key3[y]);
delay(5);
}
}
memset(passwordInput, 0, sizeof(passwordInput));
passwordLen = 0;
x = 0;
position = 0;
}
if(KeyNum == 12) { // 清除键
memset(passwordInput, 0, sizeof(passwordInput));
passwordLen = 0;
x = 0;
position = 0;
jidianqi = 1;
write_com(0x01);
write_com(0x80);
for(i = 0; i < 8; i++) {
write_dat(table[i]);
}
write_com(0xc0);
for(i = 0; i < 6; i++) {
write_dat(table1[i]);
}
}
if (KeyNum == 13) { //显示'nwepassword'键
write_com(0x01);
delay(20);
write_com(0x80);
for(i = 0; i < strlen(table2); i++) {
write_dat(table2[i]);
}
}
// K14键用于结束输入并写入EEPROM
if (KeyNum == 14) {
if (passwordLen > 0 && passwordLen <= PASSWORD_SIZE) {
passwordInput[passwordLen] = '\0'; // 添加终止符便于使用strcmp()
for(i = 0; i < passwordLen; i++) {
AT24C02_WriteByte(addressToWrite++, passwordInput[i]);
}
x++;
}
}
// K15键用于验证密码
if (KeyNum == 15) {
if (passwordLen > 0 && passwordLen <= PASSWORD_SIZE) {
passwordInput[passwordLen] = '\0'; // 添加终止符便于使用strcmp()
for(i = 0; i < PASSWORD_SIZE; ++i) {
storedPassword[i] = AT24C02_ReadByte(addressToRead++);
}
storedPassword[PASSWORD_SIZE] = '\0'; // 添加终止符便于使用strcmp()
if (strcmp(passwordInput, storedPassword) == 0) {
// 密码正确,执行相应操作
write_com(0x01);
for(y = 0; y < 7; y++) {
write_dat(key2[y]);
delay(5);
}
} else {
// 密码错误,执行相应操作
write_com(0x01);
for(y = 0; y < 5; y++) {
write_dat(key3[y]);
delay(5);
}
}
// 清空输入状态,准备下次输入
passwordLen = 0;
memset(passwordInput, 0, sizeof(passwordInput));
addressToRead = 0;
}
}
}
}
}