发个例程给你试试,1602通用的
#include <reg52.h>
#define uint unsigned int
#define uchar unsigned char
#define comm 0
#define dat 1
sbit e = P3^2; //input enable;
sbit rw = P3^1; //H=read; L=write;
sbit rs = P3^0; //H=data; L=command;
uchar code tab1[]={
"NS:WWW.RXLCD.COM"
"TEL:020-62619437"
};
//===========延时函数===============
void delay (int ms) //delay time
{
int i,j;
for(i=0;i<ms;i++)
for(j=0;j<123;j++);
}
//=======写命令或者数据子函数=======
void wr_lcd (uchar dat_comm,uchar content)
{
if(dat_comm)
{
rs=1; //data
rw=0; //write
}
else
{
rs=0; //command
rw=0; //write
}
P1=content; //output data or comm
e=1;
delay(1);
e=0;
}
//==========初始化=============
void init_lcd (void)
{
wr_lcd (comm,0x01); /*清屏,地址指针指向00H*/
wr_lcd (comm,0x06); /*光标的移动方向*/
wr_lcd (comm,0x0c); /*开显示,关光标*/
wr_lcd (comm,0x38); /*8位接口两行显示模式*/
}
//==========显示字符============
void chrt_disp (uchar code *chrt)
{
uchar i,j;
wr_lcd (comm,0x80);
for (j=0;j<2;j++)
{
for (i=0;i<16;i++)
wr_lcd (dat,chrt[j*16+i]);
wr_lcd (comm,0xc0);
}
}
//==========自做字模==============
void cgram_wr (uchar zm_data1,uchar zm_data2)
{
uchar i,j;
wr_lcd (comm,0x40);
for(j=0;j<8;j++)
{
for(i=0;i<4;i++)
{
wr_lcd (dat,zm_data1);
wr_lcd (dat,zm_data2);
}
}
}
//=========显示自做字模==========
void cgram_disp (void)
{
uchar i,j;
wr_lcd (comm,0x80);
for (j=0;j<2;j++)
{
for (i=0;i<8;i++)
wr_lcd (dat,i);
for (i=0;i<8;i++)
wr_lcd (dat,i);
wr_lcd (comm,0xc0);
}
}
/*----------主程序------------*/
void main ()
{
init_lcd ();
while (1)
{
chrt_disp (tab1);
delay (2000);
cgram_wr (0x1f,0x1f);
cgram_disp();
delay (2000);
cgram_wr (0x15,0x15);
cgram_disp();
delay (2000);
cgram_wr (0x1f,0x00);
cgram_disp();
delay (2000);
cgram_wr (0x15,0x0a);
cgram_disp();
delay (2000);
}
} |