|
我想在里面加入一個(gè)在第四次錯(cuò)誤是不可以輸入了,但是為什么在我加入定時(shí)器之后不可以使用呢
#include <reg52.h> //包含52頭文件
#define uint unsigned int //宏定義uint代替unsigned int
#define uchar unsigned char //宏定義uchar代替unsigned char
#define out P0
sbit lcdrs=P2^4;
sbit lcdrw=P2^5;
sbit lcden=P2^6;
sbit buzzer=P2^7;//蜂鳴器
sbit led=P3^1;
void check_busy(void);
void write_cmd(char cmd);
void write_data(uchar dat);
void write_str(uchar *str);
void lcd_init();
void keyscan();
void verify();
void beep();
void T1_init();
uchar key,num=100,flag,warn,time1,time2;
unsigned char code password[6]={1,4,0,2,0,6};//密碼
uchar dat[]="0123456789abcdf";
uchar DispBuff[6];
uchar code xianshi1[]=" first error!";
uchar code xianshi2[]=" second error!";
uchar code xianshi3[]=" third error!";
uchar code xianshi4[]=" alter fail!!";
uchar code xianshi5[]=" you are thief!";
void delay (uint xms) //毫秒函數(shù)定義
{
uint i,j;
for(i=0;i<xms;i++)
for(j=0;j<120;j++);
}
void main() //主函數(shù)
{ T1_init();
lcd_init();
num=0;
while(1)
{
keyscan();
if(key!=0xff)
{ uchar i;
DispBuff[num]=key;
num++;
key=0xff;
write_cmd(0x01);//清除屏幕
delay(1);
write_cmd(0x80+0x40);
for(i=0;i<num;i++)
{ write_data(dat[DispBuff[i]]); }
verify();
}
if(time2==4) //門鎖打開(kāi)后4秒關(guān)閉
{ time2=0;
TR1=0; //關(guān)閉定時(shí)器1
}
}
}
/**********************檢查忙標(biāo)志函數(shù)************************/
void check_busy(void)
{
uchar dt;
out=0xff;
do
{
lcden=0;
lcdrs=0;
lcdrw=1;
lcden=1;
dt=out;
}while(dt&0x80);
lcden=0;
}
/**************************1602顯示****************************/
void write_cmd(char cmd) //寫(xiě)指令函數(shù)
{
check_busy();
lcden=0;//再把EN拉低
lcdrs=0;
lcdrw=0;//先將RW拉低
out=cmd;
lcden=1;
lcden=0;
}
void write_data(uchar dat) //寫(xiě)數(shù)據(jù)函數(shù)
{
check_busy();
lcden=0;//再把EN拉低
lcdrs=1;
lcdrw=0;//先將RW拉低
out=dat;
lcden=1;
lcden=0;
}
void write_str(uchar *str)//寫(xiě)字符串函數(shù)
{
while(*str!='\0')
{
write_data(*str++);
delay(5);
}
}
/***************************初始化******************************/
void lcd_init()//初始化1602
{
write_cmd(0x38);//顯示模式設(shè)置
write_cmd(0x0c);//顯示開(kāi)關(guān),光標(biāo)沒(méi)有閃爍
write_cmd(0x06);//顯示光標(biāo)移動(dòng)設(shè)置
write_cmd(0x01);//清除屏幕
delay(1);
}
void keyscan()
{
uchar temp,num;
P1=0xff; //先向P1寫(xiě)1
P1=0xfe; //選中按鍵第一行
temp=P1; //把P1給中間變量
temp=temp&0xf0;//低四位清零
while(temp!=0xf0)
{
delay(5);
temp=P1;
temp=temp&0xf0;
while(temp!=0xf0)
{
temp=P1;
switch(temp)
{
case 0xee:key=0; break;
case 0xde:key=1; break;
case 0xbe:key=2; break;
case 0x7e:key=3; break;
}
while(temp!=0xf0)
{
temp=P1;
temp=temp&0xf0;
}
}
}
P1=0xfd;//選中按鍵第二行
temp=P1; //把P1給中間變量
temp=temp&0xf0;//低四位清零
while(temp!=0xf0)
{
delay(5);
temp=P1;
temp=temp&0xf0;
while(temp!=0xf0)
{
temp=P1;
switch(temp)
{
case 0xed:key=4; break;
case 0xdd:key=5; break;
case 0xbd:key=6; break;
case 0x7d:key=7; break;
}
while(temp!=0xf0)
{
temp=P1;
temp=temp&0xf0;
}
}
}
P1=0xfb;//選中按鍵第三行
temp=P1; //把P1給中間變量
temp=temp&0xf0;//低四位清零
while(temp!=0xf0)
{
delay(5);
temp=P1;
temp=temp&0xf0;
while(temp!=0xf0)
{
temp=P1;
switch(temp)
{
case 0xeb:key=8; break;
case 0xdb:key=9; break;
case 0xbb:key=10; break;
case 0x7b:key=11; break;
}
while(temp!=0xf0)
{
temp=P1;
temp=temp&0xf0;
}
}
}
P1=0xf7;//選中按鍵第四行
temp=P1; //把P1給中間變量
temp=temp&0xf0;//低四位清零
while(temp!=0xf0)
{
delay(5);
temp=P1;
temp=temp&0xf0;
while(temp!=0xf0)
{
temp=P1;
switch(temp)
{
case 0xe7:key=12; break;
case 0xd7:key=13; break;
case 0xb7:key=14; break;
case 0x77:key=15; break;
}
while(temp!=0xf0)
{
temp=P1;
temp=temp&0xf0;
}
}
}
while((num<50)&&(temp!=0xf0))
{delay(10);
num++;
}
num=0;
}
void verify()//對(duì)輸入密碼進(jìn)行判斷
{ uchar j,Flag,a;
if(num==6)
{ num=0;
Flag=1;
for(j=0;j<6;j++)
Flag=(DispBuff[j]==password[j]);
if(Flag)
{
write_cmd(0x80+0x04);
write_str("Right! ");
}
else
{
warn++;
if(warn==1)
{ write_cmd(0x01); //清屏
write_cmd(0x80);
for(a=0;a<14;a++) //顯示first error
{ write_data(xianshi1[a]);
}}
if(warn==2)
{ write_cmd(0x01); //清屏
write_cmd(0x80);
for(a=0;a<14;a++) //顯示first error
{ write_data(xianshi2[a]);
}}
if(warn==3)
{ write_cmd(0x01); //清屏
write_cmd(0x80);
for(a=0;a<14;a++) //顯示first error
{ write_data(xianshi3[a]);
}}
if(warn==4)
{ write_cmd(0x01); //清屏
write_cmd(0x80);
for(a=0;a<14;a++) //顯示first error
{ write_data(xianshi4[a]);
}
write_cmd(0x01); //清屏
write_cmd(0xc0);
for(a=0;a<14;a++) //顯示first error
{ write_data(xianshi5[a]);
}
TR0=1; //定時(shí)器0啟動(dòng)
beep();
led=~led;
warn=0;
}
}
}
}
void beep()
{ buzzer=1;
delay(200);
buzzer=0;
delay(200);
}
void T1_init()
{
TMOD=TMOD|0x20; //設(shè)置T1為方式2
TH1=256-200; //置T1高8位,備用初值
TL1=256-200; //置T1低8位,初值
ET1=1; //開(kāi)T1中斷
EA=1; //開(kāi)總中斷
}
/**********T1中斷服務(wù)程序*********/
void T1_int() interrupt 3 //200us進(jìn)入中斷
{ TH1=(65536-50000)/256;
TL1=(65536-50000)%256;
time1++;
if(time1==20)//一秒
{
time2++;
time1=0;
}
}
|
|