|
#include <reg51.h>
/**********端口定义*************/
sbit RS = P1^0;
sbit RW = P1^1;
sbit E = P1^2;
sbit KEY_IN1 = P2^4;
sbit KEY_IN2 = P2^5;
sbit KEY_IN3 = P2^6;
sbit KEY_IN4 = P2^7;
sbit KEY_OUT1 = P2^3;
sbit KEY_OUT2 = P2^2;
sbit KEY_OUT3 = P2^1;
sbit KEY_OUT4 = P2^0;
unsigned char Key_Buff[4][4]={{1,1,1,1},{1,1,1,1},{1,1,1,1},{1,1,1,1}};
#define Date P0 //定义Date口为数据口P0
void Delays_Lcd(unsigned int x) //延时子函数
{ unsigned int i,j;
for(i=45;i>0;i--)
for(j=x;j>0;j--);
}
void T0_Init()
{
EA = 1;
TMOD = 0x01;
TH0 = 0xFC;
TL0 = 0x67;
ET0 = 1;
TR0 = 1;
}
void Write_Instruction(unsigned char Parameter) //写指令(Write/Instruction)。。形参(Parameter)
{
RS=0;
RW=0;
E=0;
Date=Parameter;
E=1;
Delays_Lcd(0);
E=0;
}
void Write_Date(unsigned char Parameter) //写数据(Write/Date)。。形参(Parameter)
{
RS=1;
RW=0;
E=0;
Date=Parameter;
E=1;
Delays_Lcd(0);
E=0;
}
void init_Lcd() //液晶初始化(init/lcd)
{
Write_Instruction(0x01);
Write_Instruction(0x0c);
Write_Instruction(0x30);
}
void Position_Lcd(unsigned char x,unsigned char y) //进入点设定,,液晶显示位置(Lcd/Position)
{
unsigned char Position;
if(x==1)
{x=0x80;}
else if(x==2)
{x=0x90;}
else if(x==3)
{x=0x88;}
else if(x==4)
{x=0x98;}
Position=x+y;
Write_Instruction(Position); //显示地址
}
void Staret()
{
unsigned char code TV1[]={"输入密码"};
unsigned char code TV2[]={":"};
unsigned char Num1;
RS=0;
RW=0;
E=0;
init_Lcd();
Position_Lcd(1,2);
Num1=0;
while(TV1[Num1]!='\0') //教材P176页讲述了TV1[Num1]!='\0'的意思特别是'\0'的意思("空"在储存器中的存储形式是"\0")
{
Write_Date(TV1[Num1]);
Num1++;
}
Position_Lcd(2,0);
Num1=0;
while(TV2[Num1]!='\0') //教材P176页讲述了TV1[Num1]!='\0'的意思特别是'\0'的意思("空"在储存器中的存储形式是"\0")
{
Write_Date(TV2[Num1]);
Num1++;
}
Position_Lcd(2,1);
}
void main()
{
unsigned char Key_Loosen[4][4]={{1,1,1,1},{1,1,1,1},{1,1,1,1},{1,1,1,1}};
Staret();
P2=0xff;
T0_Init();
while(1)
{
static unsigned char Num_Password2=0;
unsigned char Num2,Num4;
static unsigned char Num_Password1=0;
static bit Num_Bit1=0;
static bit Num_Bit2=0;
unsigned char Num1;
static unsigned char Num_Pa1=0;
static unsigned char Num_Pa2=0;
static unsigned char Num_Pa3=0;
static unsigned char Num_Pa4=0;
static unsigned char Num_Pa5=0;
static unsigned char Num_Pa6=0;
static unsigned char Num_Pa7=0;
static unsigned char Num_Pa8=0;
static unsigned char Num_Pa9=0;
static unsigned char Num_Pa0=0;
if(Num_Bit2==1)
{
switch(Num_Password2)
{
case 1:if(Num_Password1==1){Num_Pa1++;}break;
case 2:if(Num_Password1==3){Num_Pa3++;}break;
case 3:if(Num_Password1==1){Num_Pa1++;}break;
case 4:if(Num_Password1==4){Num_Pa4++;}break;
case 5:if(Num_Password1==5){Num_Pa5++;}break;
case 6:if(Num_Password1==2){Num_Pa2++;}break;
case 7:if(Num_Password1==0){Num_Pa0++;}break;
default : break;
}
Num_Bit2=0;
}
while(Num_Pa1==2&Num_Pa2==1&Num_Pa3==1&Num_Pa4==1&Num_Pa5==1&Num_Pa0==1)
{
unsigned char TV2[]={" 欢迎使用"};
init_Lcd();
Num1=0;
Position_Lcd(2,0);
while(TV2[Num1]!='\0') //教材P176页讲述了TV1[Num1]!='\0'的意思特别是'\0'的意思("空"在储存器中的存储形式是"\0")
{
Write_Date(TV2[Num1]);
Num1++;
}
while(1);
}
if(Num_Password2==7)
{
unsigned char TV1[]={" "};
unsigned char TV2[]={"密码错误 ^-^ "};
unsigned long ss;
Num1=0;
Position_Lcd(2,1);
while(TV2[Num1]!='\0') //教材P176页讲述了TV1[Num1]!='\0'的意思特别是'\0'的意思("空"在储存器中的存储形式是"\0")
{
Write_Date(TV2[Num1]);
Num1++;
}
Write_Instruction(0x0c);
for(ss=50000;ss>0;ss--);
Num1=0;
Position_Lcd(2,1);
while(TV1[Num1]!='\0') //教材P176页讲述了TV1[Num1]!='\0'的意思特别是'\0'的意思("空"在储存器中的存储形式是"\0")
{
Write_Date(TV1[Num1]);
Num1++;
}
Num_Pa1=0;
Num_Pa2=0;
Num_Pa3=0;
Num_Pa4=0;
Num_Pa5=0;
Num_Pa6=0;
Num_Pa7=0;
Num_Pa8=0;
Num_Pa9=0;
Num_Pa0=0;
Num_Bit1=0;
Num_Password2=0;
}
if(Num_Bit1==1)
{
unsigned char TV1[]={"*"};
Write_Instruction(0x0f);
Num1=0;
Position_Lcd(2,Num_Password2);
while(TV1[Num1]!='\0') //教材P176页讲述了TV1[Num1]!='\0'的意思特别是'\0'的意思("空"在储存器中的存储形式是"\0")
{
Write_Date(TV1[Num1]);
Num1++;
}
}
/*************************************************************/
for(Num4=0;Num4<4;Num4++)
{
for(Num2=0;Num2<4;Num2++)
{
if(Key_Loosen[Num4][Num2]!=Key_Buff[Num4][Num2])
{
if(Key_Loosen[Num4][Num2]==0)
{
unsigned char Fg;
Fg=Num4*4+Num2;
switch(Fg)
{
case 0:P0 = Num_Password1=14;break; //*
case 1:P0 = Num_Password1=0;break; //0
case 2:P0 = Num_Password1=15;break; //#
case 3:P0 = Num_Password1=13;break; //D
case 4:P0 = Num_Password1=7;break;
case 5:P0 = Num_Password1=8;break;
case 6:P0 = Num_Password1=9;break;
case 7:P0 = Num_Password1=12;break; //C
case 8:P0 = Num_Password1=4;break;
case 9:P0 = Num_Password1=5;break;
case 10:P0 = Num_Password1=6;break; //6
case 11:P0 = Num_Password1=11;break; //B
case 12:P0 = Num_Password1=1;break;
case 13:P0 = Num_Password1=2;break;
case 14:P0 = Num_Password1=3;break;
case 15:P0 = Num_Password1=10;break; //A
}
Num_Password2++;
Num_Bit1=1;
Num_Bit2=1;
}
Key_Loosen[Num4][Num2]=Key_Buff[Num4][Num2];
}
}
}
}
}
void InterruptTimer0() interrupt 1
{
unsigned char Num1;
static unsigned char Keys=0;
TH0 = 0xFC;
TL0 = 0x67;
Key_Buff[Keys][0]=(Key_Buff[Keys][0]<<1)|KEY_IN1;
Key_Buff[Keys][1]=(Key_Buff[Keys][1]<<1)|KEY_IN2;
Key_Buff[Keys][2]=(Key_Buff[Keys][2]<<1)|KEY_IN3;
Key_Buff[Keys][3]=(Key_Buff[Keys][3]<<1)|KEY_IN4;
for(Num1=0;Num1<4;Num1++)
{
if((Key_Buff[Keys][Num1]&0x0f)==0x00)
{
Key_Buff[Keys][Num1]=0;
}
}
Keys++;
Keys=Keys&0x03;
switch(Keys)
{
case 0:KEY_OUT4=1;KEY_OUT1=0;break;
case 1:KEY_OUT1=1;KEY_OUT2=0;break;
case 2:KEY_OUT2=1;KEY_OUT3=0;break;
case 3:KEY_OUT3=1;KEY_OUT4=0;break;
}
}
|
|