#include<reg52.h>
#define uchar unsigned char
#define uint unsigned int
#define LCD_DATA P0 //定义数据线D0-D7
sbit LCD_RS=P1^0; //定义寄存器选择位
sbit LCD_RW=P1^1; //定义读写信号线位
sbit LCD_E=P1^2; //定义使能信号位
sbit LCD_BUSY=LCD_DATA^7; //检测LCD状态字中BUSY标识
sbit hang1=P2^5; //键控第一行显示
sbit hang2=P2^6; //键控第二行显示
sbit both=P2^7; //键控两行显示
void LCD_Init(void);
void LCD_Write_Data(uchar dat);
void LCD_Write_Cmd(uchar cmd,BusyC);
uchar LCD_Read_Data(void);
void LCD_Disp_Char(uchar X,uchar Y,uchar DData);
void LCD_Disp_String(uchar X,uchar Y,uchar code *DData);
void LCD_Delay(uint ms);
uchar LCD_Check_Busy(void);
uchar code school_name[]={"wodedianzimeng"};
uchar code email[]={"39543855@163.com"};
void main(void)
{
while(1)
{
if(hang1==0) //按键1按下控制第一行显示
LCD_Delay(5) ; //延时去抖动
if(hang1==0) //再次确认键1按下
{
while(hang1==0); //等待键起 显示
LCD_Init();
LCD_Disp_String(0,0,school_name);
}
if(hang2==0) //按键2控制第二行显示
LCD_Delay(5);
if(hang2==0)
{
while(hang2==0);
LCD_Init();
LCD_Disp_String(0,1,email);
}
if(both==0) //按键3控制两行都显示
LCD_Delay(1);
if(both==0)
{
while(both==0);
LCD_Delay(400) ;
LCD_Init();
LCD_Disp_String(0,0,school_name);
LCD_Disp_String(0,1,email);
}
}
}
void LCD_Init(void)
{
LCD_DATA=0;
LCD_Write_Cmd(0x38,0); //三次显示模式设置,不检测忙信号
LCD_Delay(5);
LCD_Write_Cmd(0x38,0);
LCD_Delay(5);
LCD_Write_Cmd(0x38,0);
LCD_Delay(5);
LCD_Write_Cmd(0x38,1); //显示模式设置,开始要求检测忙信号:8位、2行、5X7点阵
LCD_Write_Cmd(0x08,1); //关闭显示
LCD_Write_Cmd(0x01,1); //清屏
LCD_Write_Cmd(0x06,1); //显示光标移动设置:文字不动,光标自动右移
LCD_Write_Cmd(0x0C,1); //显示开及光标设置:光标关、光标不闪烁
}
void LCD_Write_Data(uchar dat)
{
LCD_Check_Busy();
LCD_DATA=dat;
LCD_RS=1;
LCD_RW=0;
LCD_E=0;
LCD_Delay(5);
LCD_E=1;
}
void LCD_Write_Cmd(uchar cmd,BusyC)
{
if(BusyC) LCD_Check_Busy();
LCD_DATA=cmd;
LCD_RS=0;
LCD_RW=0;
LCD_E=0;
LCD_Delay(5);
LCD_E=1;
}
uchar LCD_Read_Data(void)
{
LCD_RS=1;
LCD_RW=1;
LCD_E=0;
LCD_Delay(5);
LCD_E=1;
return(LCD_DATA);
}
uchar LCD_Check_Busy(void)
{
LCD_DATA=0xFF;
LCD_RS=0;
LCD_RW=1;
LCD_E=0;
LCD_Delay(5);
LCD_E=1;
while(LCD_DATA & 0x80);
return(LCD_DATA);
}
void LCD_Disp_Char(uchar X,uchar Y,uchar DData)
{
Y &=0x01;
X &=0x0F;
if(Y) X|=0x40;
X|=0x80;
LCD_Write_Cmd(X,0);
LCD_Write_Data(DData);
}
void LCD_Disp_String(uchar X,uchar Y,uchar code *DData)
{
uchar StringLength;
StringLength=0;
Y &=0x01;
X &=0x0F;
while(DData[StringLength]>0x20)
{
if(X<=0x0F)
{
LCD_Disp_Char(X,Y,DData[StringLength]);
StringLength++;
X++;
}
}
}
void LCD_Delay(uint ms)
{
uchar i;
while(ms–)
{
for(i=0;i<250;i++);
}
}