/*实验名称:字符型LCD接口实验
功能:在LCD上显示
AT89S51 DEMO
www.mcuprog.com
作者:http://www.mcuprog.com
05-02-01
*/
#include <reg51.h>
sbit E=P3^5;
sbit RW=P3^6;
sbit RS=P3^7;
typedef unsigned char uchar;
//-------------------------------------
void Delay(unsigned int t) // delay 40us
{
for(;t!=0;t--) ;
}
//=============================================
void SendCommandByte(unsigned char ch)
{
RS=0;
RW=0;
P0=ch;
E=1;
Delay(1);
E=0;
Delay(100); //delay 40us
}
//-------------------------------------------------------
void SendDataByte(unsigned char ch)
{ RS=1;
RW=0;
P0=ch;
E=1;
Delay(1);
E=0;
Delay(100); //delay 40us
}
//-------------------------------------------------
void InitLcd()
{SendCommandByte(0x30);
SendCommandByte(0x30);
SendCommandByte(0x30);
SendCommandByte(0x38); //设置工作方式
SendCommandByte(0x0c); //显示状态设置
SendCommandByte(0x01); //清屏
SendCommandByte(0x06); //输入方式设置
}
//=============================================
void DisplayMsg1(uchar *p)
{
unsigned char count;
SendCommandByte(0x80); //设置DDRAM地址
for(count=0;count<16;count++)
{SendDataByte(*p++);
}
}
//=============================================
void DisplayMsg2(uchar *p)
{
unsigned char count;
SendCommandByte(0xc0); //设置DDRAM地址
for(count=0;count<16;count++)
{SendDataByte(*p++);
}
}
//=============================================
main()
{char msg1[16]=" AT89S51 DEMO ";
char msg2[16]="www.mcuprog.com ";
InitLcd();
DisplayMsg1(msg1);
DisplayMsg2(msg2);
while(1);
}