#include"reg51.h"
#include"lcd1602.h"
#include<intrins.h>
#define uchar unsigned char
#define uint unsigned int
#define BUTTON_PORT P1
#define passwordBit 6
sbit LED=p3^7;
sbit BUZZER=p2^3;
uchar code table[]={0x30,0x31,0x32,0x33,0x34,0x35,0x36,0x37,0x38,0x39,'A','B','C','D','E'.'F'};
uchar Original_password[passwordBit]={0x30,0x31,0x32,0x33,0x34,0x35};
uchar code Set_password[]={0x30,0x31,0x32,0x33,0x34,0x35};
uchar code dis[]={"Please selects menu ! Button1 selects normal mode. Button2 selects modify system password!!"};
uchar code test1[]={"Password error"};
uchar code test2[]={"Password correct"};
uchar code test3[]={"Please input"};
uchar code test4[]={"Check password."};
uchar code test5[]={"Input again!!!"};
uchar code test6[]={"System open"};
uchar code conceal='*';
uchar code menuString1[]={"Operation "};
uchar code menuString2[]={"Modify Password"};
uchar save[16]={0};
uchar operationFlag=0;
uchar flag=1;
uchar cnt=0;
uchar systemFlag=1;
uchar k1=0;
uchar k2=0;
uchar modify=0;
uchar exitFlag=1;
/********************************************************函数声明区******************************************/
void delay_ms(uint i);
uchar Scan_Button(void);
void Check-Password(void);
void Menu(void);
void modify_Password(void);
void renew_Password(void);
void input_Password(void);
void entry(void);
void displayMenu(void);
/*
* 函数名称:延时函数
* 参数: 延时数
* 返回值: 无
*/
void delay_ms(uint i)
{
uint j=0;
uint n=0;
for(j=0;j<i;j++)
for(n=0;n<=118;n++);}
void entry(void)
{ uchar i=0;
LCD_Clear();
LCD_Write_String(0,0,"Entry SYSTEM");
LCD_Write_String(0,1,"KEY1 Exit");
BUZZER=1;
LED=1;
while(exitFlag)
{ if(i==100)
{ i=0;
LED!=LED;
}
BUZZER=!BUZZER;
i++;
delay_ms(10);
}
}
void modify_Password(void)
{ LCD_Clear();
LCD_Write_String(0,0,test3);
LCD_Write_Com(0XC0);
while(modify==0)
scan_Button();
}
uchar Scan_Button(void)
{ uchar i=0,count=0,temp=0,LINE[4]={0xef,0xdf,oxbf,ox7f};
BUTTON_PORT=0xff;
for(i=0;i<4;i++)
{ BUTTON_PORT = LINE[i];
delay_ms(20);
temp = 0x01;
for(count=0;count<4;count++)
{ if(!(BUTTON_PORT&temp))
{ if(systemFlag==1)
{ LCD_Write_Data(conceal);
save[k1++]=table[(count+i*4)];
if(k1==passwordBit)
{ k1=0;
check_Password();
}
}
else if(systemFlag==2)
{
Original_password[k2++]=table[(count+i*4)];
LCD_Write_Data((count+i*4)+'0');
if(k2==passwordBit)
{ k2=0;
renew_Password();
}
}
else if(0==systemFlag)
{ operationFlag=(count+i*4);
}
while(!(BUTTON_PORT&temp));
return (count+i*4);
}
temp<<=1;
}
}
return 0xff;
}
void renew_Password(void)
{
LCD_Clear();
LCD_Write_String(0,0,menuString2);
LCD_write_String(0,1,"Finished!!!");
systemFlag=0;
modify=1;
delay_ms(2000);
}
void Check_Password(void)
{
uchar i=0;
for(i=0;i<passwordBit;i++)
{
if(save[i]!=Original_password[i])
break;
}
if(i==passwordBit)
{ LCD_Write_String(0,1,test2);
cnt=0;
flag=0;
systemFlag=0;
}
else
{ LCD-Write_String(0,1,test1);
cnt++;
flag=1;delay_ms(2000);
LCD_Write_String(0,0,test5);
systemFlag=1;
}
delay)ms(2000);
LCD_Clear();
LCD_Write_Com(0x80);
}
void displayMenu(void)
{
uchar i=0;
LCD_Clear();
LCD_Write_Com(0x80);
for(i=0;i<91;i++)
{
if(i==16)
LCD_Write_Com(0xc0);
else if(i==32)
{ LCD_Clear();
LCD_Write_Com(0x80);
}
else if(i==48)
LCD_Write_Com(0xc0);
else if(i==64){
LCD_Clear();
LCD_Write_Com(0x80);
}
else if(i==80)
LCD_Write_Com(0xc0);
else;
LCD_Write_Data(dis[i]);
delay_ms(300);
}
}
void Menu(void)
{
LED=1;
modify=0;
displayMenu();
systemFlag=0;
operationFlag=0;
while((operationFlag!=1)&&(operationFlag!=2))
{
Scan_Button();
}
if(operationFlag==1)
{
exitFlag=1;
entry();
LED=1;
delay_ms(2000);
}
else if(operationFlag==2)
{ systemFlag=2;
modify_Password();
LCD_Clear();
LCD_Write_String(0,0,"Please input");
LCD_Write_String(0,1,"Password...");
delay_ms(2000);
LCD_Clear();
systemFlag=1;
flag=1;
LCD_Write_Com(0x80);
while(flag==1)
{
Scan_Button();
}
displayMenu();
delay_ms(2000);
}
else
{
systemFlag=0;
}
}
int main(void)
{ LCD_Init();
IT0=1;EX0=1;EA=1;
LCD_Clear();
LCD_Write_String(0,0,"Please input");
LCD_Write_String(0,1,"password...");
delay_ms(2000);
LCD_Clear();
LCD_Write_Com(0x80);
while(flag==1)
{
Scan_Button();
}
delay_ms(2000);
LCD_Clear();
LCD_Write_String(0,0,test6);
delay_ms(2000);
while(1)
{ Menu();
}
return 0;
}
void int0_Handler(void)interrupt 0
{ exitFlag=0;
}
|