|
如果用的是1602的话,我这儿有模块化的驱动程序,常用,没有问题,参考一下:
#include
#define line1 0
#define line2 1
#define line1_head 0x80 //第一行首地址
#define line2_head 0xc0 //第二行首地址
#define LCDIO P0
sbit RS=P2^0;
sbit RW=P2^1;
sbit EN=P2^2;
void delay(unsigned char n)//延时
{
unsigned char i,j;
for(i=0;i
for(j=0;j<125;j++)
;
}
void lcd_cmd(unsigned char cmd)//写指令
{
delay(200);
RS=0;
RW=0;
EN=1;
LCDIO=cmd;
EN=0;
}
void lcd_data(unsigned char dat)//写数据
{
delay(200);
RS=1;
RW=0;
EN=1;
LCDIO=dat;
EN=0;
}
void set_ads(unsigned char x,unsigned char y)//设置显示地址
{
unsigned char address;
if(y==line1)
address=line1_head+x;
else
address=line2_head+x;
lcd_cmd(address);
}
lcd_init(void)//初始化
{
lcd_cmd(0x01);//清屏
lcd_cmd(0x38);//设置八位数据传送模式
lcd_cmd(0x0c);//开显示
lcd_cmd(0x80);//设置LCD第一个字符显示的地址
lcd_cmd(0x01);//清屏
}
/****写一个字符******/
void write_char(unsigned char location,unsigned char num,unsigned char zi)
{
set_ads(location,num);//第num行第location个位置显示字符
lcd_data(zi);//显示zi
}
/***写一个字符串*****/
void write_str(unsigned char location,unsigned char num,unsigned char *s)//*s为字符串的地址,也就是指向字符串
{
set_ads(location,num);//第num行第location个位置显示字符
while(*s)//当字符地址不为空时,执行这个循环
{
LCDIO=*s;
lcd_data(*s);
s++;
}
}
void main()
{
lcd_init();
while(1)
{
write_char(5,line1,0xac);//显示0
//write_str(3,line2,"hard work");//显示"hard work"
//write_char(13,line2,0x21);//显示!
}
}
|
|