#include <REGX51.H>#include "LCD1602.H"#include "KeyBoard4x4.h"#include "EEPROM.h"#include "Timer0.h"sbit RELAY = P3^7; //--- 定义的继电器引脚 ---struct STRUCT_TIMER LockKeyBoardTimer,RelayTimer;//========================================================================================================//--- 定义与密码相关结构体变量 ---struct STRUCT_PASSWORD{ unsigned char Status; unsigned char ErrCnt; unsigned char Pointer; unsigned char Buffer[6]; unsigned char OldBuffer[6]; unsigned char NewBuffer[6];};struct STRUCT_PASSWORD Password;//========================================================================================================//--- main()主程序区 ---void main(void){ unsigned char i,KeyValue; LCD_Init(); // --- 初始化1602液晶模块 --- LCD_PutChar(0,0,'A'); LCD_PutString(0,0," Input Password "); T0_Init(); EA = 1; for(i=0;i<sizeof(Password.OldBuffer);i++) Password.OldBuffer[i] = IAPReadByte(i); while(1) { if(0 == RELAY) { if(0 == RelayTimer.Enable)RELAY = 1; //--- 继电器动作时间到,则恢复原位 --- } if(Password.ErrCnt > 2) //--- 密码输错三次,锁键盘 --- { if(0 == LockKeyBoardTimer.Enable)Password.ErrCnt = 0; //--- 锁键盘时间到,解锁 --- } else { KeyValue = KeyBoard4x4_Scan(); if(KeyValue < 16) //--- 识别出有效的按键 --- { if(KeyValue < 10) //--- 当前按键是数字“0~9”键 --- { if(Password.Pointer < sizeof(Password.Buffer)) //--- 若输入的密码少于指定的长度 --- { LCD_PutChar(1,Password.Pointer + 5,'*'); //--- 显示“*” --- if(2 == Password.Status) Password.NewBuffer[Password.Pointer] = KeyValue; Password.Buffer[Password.Pointer ++] = KeyValue; //--- 装入密码输入缓冲区 --- } } else if(11 == KeyValue) //--- 当前按键是“B”键,用于修改密码 --- { LCD_PutString(0,0," Old Password "); //--- 显示输入旧密码 --- LCD_PutString(1,0," "); //--- 清显示区域 --- Password.Pointer = 0; Password.Status = 1; } else if(14 == KeyValue) //--- 当前按键是“*”键 --- { LCD_PutString(1,0," "); //--- 清显示区域 --- Password.Pointer = 0; } //--- 清密码输入计数变量 --- else if(15 == KeyValue) //--- 当前按键是“#”键 --- { switch(Password.Status) { case 0: //--- 密码输入状态 --- case 1: if(Password.Pointer < sizeof(Password.Buffer)) //--- 输入的密码长度不够 --- { LCD_PutString(1,0," Password Error "); //--- 显示密码错误信息 --- if(++ Password.ErrCnt > 2) //--- 若超过3次 --- { LockKeyBoardTimer.Interval = 60000; //--- 置锁键盘时间为1分钟 --- LockKeyBoardTimer.Enable = 1; } } else { for(i=0;i<sizeof(Password.Buffer);i++) //--- 密码比较 --- { if(Password.Buffer[i] != Password.OldBuffer[i]) //--- 若不正确,则退出下面的比较 --- { i = sizeof(Password.Buffer) + 2; } } if(i > sizeof(Password.Buffer)) { LCD_PutString(1,0," Password Error "); //--- 显示密码错误信息 --- if(++ Password.ErrCnt > 2) //--- 若超过3次 --- { LockKeyBoardTimer.Interval = 60000; //--- 置锁键盘时间为1分钟 --- LockKeyBoardTimer.Enable = 1; } } else { LCD_PutString(1,0," Password Right "); //--- 显示密码输入正确信息 --- Password.ErrCnt = 0; if(0 == Password.Status)RELAY = 0; //--- 发出打开动作 --- else if(1 == Password.Status) { LCD_PutString(0,0," New Password "); //--- 显示输入旧密码 --- LCD_PutString(1,0," "); //--- 清显示区域 --- Password.Pointer = 0; Password.Status = 2; } if(0 == Password.Status) { RelayTimer.Interval = 60000; RelayTimer.Enable = 1; } } } break; case 2: //--- 输入新密码状态 --- LCD_PutString(0,0," Input Again "); //--- 显示输入旧密码 --- LCD_PutString(1,0," "); //--- 清显示区域 --- Password.Pointer = 0; Password.Status = 3; break; case 3: //--- 比较两次输入的密码正确性 --- for(i=0;i<sizeof(Password.Buffer);i++) //--- 密码比较 --- { if(Password.Buffer[i] != Password.NewBuffer[i]) //--- 若不正确,则退出下面的比较 --- { i = sizeof(Password.Buffer) + 2; } } if(i > sizeof(Password.Buffer)) { LCD_PutString(1,0," Compare Error "); //--- 显示密码比较错误信息 --- Password.Status = 0; //--- 回到原始状态 --- } else { LCD_PutString(1,0," Compare Right "); //--- 显示密码比较正确信息 --- for(i=0;i<sizeof(Password.NewBuffer);i++) //--- 将新密码写入EEPROM --- IAPProgramByte(i,Password.NewBuffer[i]); for(i=0;i<sizeof(Password.OldBuffer);i++) Password.OldBuffer[i] = IAPReadByte(i); DelaymS(250); LCD_PutString(0,0," Input Password "); LCD_PutString(1,0," "); //--- 清显示区域 --- Password.Pointer = 0; Password.Status = 0; //--- 回到原始状态 --- } break; } } } } }} |