#include
#include
#define uchar unsigned char
#define DPORT P0
sbit RS = P3^7 ; //P3_7
sbit RW = P3^6 ;
sbit E = P3^5 ; //p3_5 WR P3_6
#define NODISP 0
#define NOCUR 1
#define CURNOFLASH 2
#define CURFLASH 3
#define CLEARSCREEN 0x01 //清屏控制字
uchar Xpos,Ypos;i;j;
//#define NOP _nop_
/**************Lcd驱动部分函数声明**************************/
void SetCur(uchar c);
void ClearLcd(void);
void ReadStatus(void);
void LcdWriteData(uchar c);
void LcdWriteCommand(uchar c,uchar Busy);
//void LcdPos();
void LcdInit( );
void DispOneChar(uchar x, uchar y,uchar Data);
void DispListChar(uchar x, uchar y,uchar code *Data);
/**************光标(cur)设置命令***************************/
void delay(uchar z)
{
for (i=0;i0;j--);
}
void SetCur( uchar cur )
{
switch( cur )
{
case 0x00:
{
LcdWriteCommand(0x08,1); //LCD关显示
break;
}
case 0x01:
{
LcdWriteCommand(0x0c,1); //LCD开显示但是无光标
break;
}
case 0x02:
{
LcdWriteCommand(0x0e,1); //LCD显示有光标但光标不闪烁
break;
}
case 0x03:
{
LcdWriteCommand(0x0f,1); //LCD开显示光标闪烁
break;
}
default:break;
}
}
/************************************************************/
/**************清屏命令**************************************/
void ClearLcd()
{
LcdWriteCommand(CLEARSCREEN,1);
}
/************************************************************/
/************正常读写前检测LCD控制器处于忙状态?*************/
void ReadStatus()
{
uchar temp;
DPORT=0xff;
RS=0;
RW=1;
/**/
E=0;
E=0;
/**/ //读状态
E=1;
_nop_();
for(;;)
{
temp=DPORT; //读状态控制字
temp&=0x80; //if the station of control word--DPORT^7=0;enable;
if(temp==0)
break;
}
E=0;
}
/************************************************************/
/**************不带忙检测的写字符(数据)子程序****************/
void LcdWriteData(uchar c)
{
DPORT=c;
RS=1; //写数据
RW=0;
E=0; //下降沿表示enable端有效
_nop_();
E=1;
_nop_();
E=0;
}
/************************************************************/
/**************不带忙检测的送控制字子程序********************/
void LcdWriteCommand(uchar c, uchar Busy)
{
if(Busy)
{
ReadStatus();
}
DPORT=c;
RS=0; //写数据
RW=0;
E=0; //下降沿表示enable端有效
_nop_();
E=1;
_nop_();
E=0;
}
/************************************************************/
/**************LcdPos************************************
void LcdPos()
{
uchar temp;
Xpos&=0x0f;
Ypos&=0x01;
temp=Xpos;
if(Ypos==1)
{
temp+=0x40;
}
temp|=0x80; //80h+地址码(2-27h 或者40-67h)
LcdWriteCommand(temp,1); //读写操作
}
************************************************************/
/*************Lcd复位程序************************************/
void LcdInit()
{
delay(15);
LcdWriteCommand(0x38,0);
delay(5);
LcdWriteCommand(0x38,0);
delay(5);
LcdWriteCommand(0x38,0);
LcdWriteCommand(0x38,1);
LcdWriteCommand(0x08,1);
LcdWriteCommand(0x01,1);
LcdWriteCommand(0x06,1);
LcdWriteCommand(0x0c,1);
}
/************************************************************/
/**************LcdDispOneChar(在制定的行和列*******************/
void DispOneChar(uchar x, uchar y, uchar Data)
{
x&=0x0f;
y&=0x01;
if(y==0x01)
{
x|=0x40;
}
x|=0x80; //80h+地址码(2-27h 或者40-67h)
LcdWriteCommand(x,0); //忽略忙信号检测 读写操作
LcdWriteData(Data);
}
/************************************************************/
/**************DispOneChar***********************************/
voidDispListChar(uchar x, uchar y,uchar code *Data)
{
uchar ListLength=0;
x&=0x0f;
y&=0x01;
while(x