#include "reg51.h" //包含頭文件
#define uchar unsigned char
#define uint unsigned int
sbit RS=P2^5; //命令/數(shù)據(jù)選擇
sbit RW=P2^4; //讀寫口
sbit E=P2^3; //鎖存控制
sbit RES = P2^0; //復(fù)位
sbit PSB = P2^2; //串并選擇
//**************************************************************************************************
//延時(shí)函數(shù)
//**************************************************************************************************
delay(uint time) //int型數(shù)據(jù)為16位,所以最大值為65535
{
uint i,j; //定義變量i,j,用于循環(huán)語(yǔ)句
for(i=0;i<time;i++) //for循環(huán),循環(huán)50*time次
for(j=0;j<100;j++); //for循環(huán),循環(huán)50次
}
//**************************************************************************************************
//查忙
//**************************************************************************************************
checkbusy()
{
RS=0; //命令/數(shù)據(jù)選擇,為0時(shí)選擇命令
RW=1; //讀/寫選擇,為1時(shí)選擇讀
E=1; //使能
while((P0&0x80)==0x80); //查忙標(biāo)志位,等待標(biāo)志位為0,即表示寫入完畢
E=0; //關(guān)閉讀寫
}
//**************************************************************************************************
//向LCD寫一命令
//**************************************************************************************************
wcode(uchar cmdcode)
{
checkbusy(); //查忙
RS=0; //命令/數(shù)據(jù)選擇,為0時(shí)選擇命令
RW=0; //讀/寫選擇,為0時(shí)選擇寫
E=1; //使能
P0=cmdcode; //送入命令
delay(10); //等待寫入
E=0; //關(guān)閉讀寫
}
//**************************************************************************************************
//向LCD寫一數(shù)據(jù)
//**************************************************************************************************
wdata(uchar dispdata)
{
checkbusy(); //查忙
RS=1; //命令/數(shù)據(jù)選擇,為1時(shí)選擇數(shù)據(jù)
RW=0; //讀/寫選擇,為0時(shí)選擇寫
E=1; //使能
P0=dispdata; //送入數(shù)據(jù)
delay(10); //等待寫入
E=0; //關(guān)閉讀寫
}
//**************************************************************************************************
//LCD 初始化
//**************************************************************************************************
InitLCD()
{
PSB=1; //設(shè)置為8BIT并口工作模式
RES=0; //復(fù)位
delay(10); //延時(shí)
RES=1; //關(guān)復(fù)位
wcode(0x30); //選擇基本指令集
wcode(0x0c); //開顯示(無(wú)游標(biāo)、不反白)
wcode(0x01); //清除顯示,并且設(shè)定地址指針為00H
wcode(0x06); //指定在資料的讀取及寫入時(shí),設(shè)定游標(biāo)的移動(dòng)方向及指定顯示的移位
}
//**************************************************************************************************
//任意位置顯示字符串
//**************************************************************************************************
void dis(uchar x,uchar y,uchar code *s)
{ //x為橫坐標(biāo),y位縱坐標(biāo),*s表示指針,為數(shù)據(jù)的首地址
switch(y) //選擇縱坐標(biāo)
{
case 0: wcode(0x80+x);break; //第1行
case 1: wcode(0x90+x);break; //第2行
case 2: wcode(0x88+x);break; //第3行
case 3: wcode(0x98+x);break; //第4行
default:break;
}
while(*s>0) //寫入數(shù)據(jù),直到數(shù)據(jù)為空
{
wdata(*s); //寫數(shù)據(jù)
delay(10); //等待寫入
s++; //下一字符
}
}
//**************************************************************************************************
//主函數(shù)
//**************************************************************************************************
main()
{
InitLCD(); //初始化液晶
while(1) //進(jìn)入死循環(huán),防止看門狗復(fù)位
{
dis(0,0,"劉曉靜"); //顯示第1行
dis(0,1,"我愛你"); //顯示第2行
dis(0,2,"我喜歡你"); //顯示第3行
dis(0,3,"我要你"); //顯示第4行
delay(5000); //保持顯示一段時(shí)間
wcode(0x01); //清屏
}
}