就是不理解为什么这个板子一开电源会出现0,而不是空白呢?
/**************************************************/
#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;
uint cnt=2;
unsigned char code password[6]={1,4,0,2,0,6};//密码
uchar dat[]="0123456789";
uchar DispBuff[6];
void delay (uint xms) //毫秒函数定义
{
uint i,j;
for(i=0;i<xms;i++)
for(j=0;j<120;j++);
}
void main() //主函数
{ 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) //门锁打开后4秒关闭
{ time2=0;
TR1=0; //关闭定时器1
}
}
}
/**********************检查忙标志函数************************/
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) //写指令函数
{
check_busy();
lcden=0;//再把EN拉低
lcdrs=0;
lcdrw=0;//先将RW拉低
out=cmd;
lcden=1;
lcden=0;
}
void write_data(uchar dat) //写数据函数
{
check_busy();
lcden=0;//再把EN拉低
lcdrs=1;
lcdrw=0;//先将RW拉低
out=dat;
lcden=1;
lcden=0;
}
void write_str(uchar *str)//写字符串函数
{
while(*str!='\0')
{
write_data(*str++);
delay(5);
}
}
/***************************初始化******************************/
void lcd_init()//初始化1602
{
write_cmd(0x38);//显示模式设置
write_cmd(0x0c);//显示开关,光标没有闪烁
write_cmd(0x06);//显示光标移动设置
write_cmd(0x01);//清除屏幕
delay(1);
}
void keyscan()
{
uchar temp,num;
P1=0xff; //先向P1写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()//对输入密码进行判断
{ uchar j,Flag;
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
{
write_cmd(0x80+0x04);
write_str("Wrong! ");
warn++;
if(warn==3)
{ TR0=1; //定时器0启动
beep();
led=~led;
warn=0; //累计清零
}
}
delay(2000);
write_cmd(0x80);
write_str(" ");
write_cmd(0xc0);
write_str(" ");
}
}
void beep()
{ buzzer=1;
delay(200);
buzzer=0;
delay(200);
}
void T1_init()
{
TMOD=TMOD|0x20; //设置T1为方式2
TH1=256-200; //置T1高8位,备用初值
TL1=256-200; //置T1低8位,初值
ET1=1; //开T1中断
EA=1; //开总中断
}
/**********T1中断服务程序*********/
void T1_int() interrupt 3 //200us进入中断
{ TH1=(65536-50000)/256;
TL1=(65536-50000)%256;
time1++;
if(time1==20)//一秒
{
time2++;
time1=0;
}
} |