程序如下:
#include<reg51.h>
#include<intrins.h>
#define uint unsigned int
#define uchar unsigned char
uchar code message[][16]={"It is the first",
"time i say hello","haha","I figure it out"};
//二维数组,第一行结束会自动加/0,显示结束!
uchar Disp_buffer[32];
void initialize_lcd();
void showstring(uchar,uchar*);
void lcd_command(uchar);
void Display();
void Delay1(uint x)
{
uchar i;
while(x--)
for(i=0;i<120;i++);
}
void Display()//将要显示数据存入缓存区
{uchar i,j,*p,k=0;
for(j=0;j<4;j++)
{p=message[j];
for(i=0;i<16&&*p!='\0';i++)
{Disp_buffer=*p;
p++;}
while(i<16)
Disp_buffer[i++]=' ';
if(k++<1)
{showstring(0x80,Disp_buffer);
if(j==4) //j为偶数,则是奇数行,奇数行且为最后一行
//(这里的4就是数组的最后一行)只显示一行
{lcd_command(0x01); //清屏命令
showstring(0x80,Disp_buffer);
Delay1(4000);
}
} //
else
{k=0;showstring(0xc0,Disp_buffer);
}
if(j%2==1) Delay1(4000); //偶数行则显示两行
}
}
void main()
{
initialize_lcd();
while(1) Display();
}
———————————————————————————————————————————————————————————————————
//display.c
#include<reg51.h>
#include<intrins.h>
#define uchar unsigned char
#define uint unsigned int
sbit RS=P2^0;
sbit RW=P2^1;
sbit EN=P2^2;
void Delay(uint x)
{
uchar i;
while(x--)
for(i=0;i<120;i++);
}
uchar busy_check() //检查是否忙//
{uchar lcd_status;
RS=0;
RW=1;
EN=1;
Delay(1);
lcd_status=P0;
EN=0;
return lcd_status;
}
void lcd_command(uchar cmd) //写LCD命令//
{
while((busy_check()&0x80)==0x80);
RS=0;
RW=0;
EN=0;
P0=cmd; EN=1;Delay(1);EN=0;
}
void lcd_data(uchar dat) //发送数据//
{
while((busy_check()&0x80)==0x80);
RS=1;
RW=0;
EN=0;
P0=dat;
EN=1;
Delay(1);EN=0;
}
void initialize_lcd() //LCD初始化//
{lcd_command(0x01);
Delay(1);
lcd_command(0x38);
Delay(1);
lcd_command(0x06);
Delay(1);
lcd_command(0x0c);
Delay(1);
}
void showstring(uchar po,uchar str[]) //显示字符串//
{uchar i;
lcd_command(po);
for(i=0;i<16;i++)
{lcd_data(str);
}
Delay(1);
} |