基于51单片机的LCD12864串行显示。/******************************************************************************
LCD12864显示,
*********************************************************************************/
#include<reg51.h>
sbit LCD12864_RS=P2^3;
sbit LCD12864_RW=P2^4;
sbit LCD12864_EN=P2^5;
sbit LCD12864_PSB=P3^2;
unsigned char num;
unsigned char code table[]="滕王阁序";
unsigned char code table1[]="——王勃";
unsigned char code table2[]="落霞与孤鹜齐飞";
unsigned char code table3[]="秋水共长天一色";
void delay(unsigned int z)
{
unsigned char x,y;
for(;z>0;z--)
for(x=38;x>0;x--)
for(y=130;y>0;y--);
}
void write_com(unsigned char com)
{
LCD12864_RW=0;
LCD12864_RS=0;
P0=com;
delay(5);
LCD12864_EN=1;
delay(5);
LCD12864_EN=0;
}
void write_data(unsigned char dat)
{
LCD12864_RW=0;
LCD12864_RS=1;
P0=dat;
delay(5);
LCD12864_EN=1;
delay(5);
LCD12864_EN=0;
}
void init()
{
LCD12864_PSB=1;
LCD12864_RW=0;
LCD12864_RS=0;
write_com(0x30); //功能设定.RE=0:基本指令操作
delay(5);
write_com(0x0c); // 1DCB
delay(5);
write_com(0x01); //清屏
write_com(0x80); //设置数据起点
}
void main()
{
while(1) //把动态显示的数据在while里不断循环,对数据进行不断地刷新.
{
init();
write_com(0x82); // 设置显示字符位置
num=0;
while(table[num]!='\0')
{
write_data(table[num]);
num++;
delay(5);
}
write_com(0x93);
num=0; //num每次都清零
while(table1[num]!='\0')
{
write_data(table1[num]);
num++;
delay(5);
}
write_com(0x88);
num=0;
while(table2[num]!='\0')
{
write_data(table2[num]);
num++;
delay(5);
}
write_com(0x99);
num=0;
while(table3[num]!='\0')
{
write_data(table3[num]);
num++;
delay(5);
}
}
}
|