基于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);
}
}
}
|