|
本帖最后由 yangwanxue99 于 2020-8-17 15:12 编辑
#include<reg52.h>
#include <stdio.h>
int init(); //声明初始化函数
int write_com(unsigned char);//声明写命令函数
int write_date(unsigned char);//声明写数据函数
int delay(unsigned char);//声明延迟函数
unsigned char x;
sbit RS = P1^0;
sbit RW = P1^1;
sbit EN = P1^2;
unsigned char code table[]="Welcome to the";
unsigned char code table1[]="MCU learning!";
void LcdWaitReady() //判忙子程序
{
unsigned char sta;
P0 = 0xFF;
RS = 0;
RW = 1;
EN = 0;
EN = 0;
EN = 1;
delay(5) ;
//while((P2&0x80));//proteus仿真时将该行注释掉,实际板上运行时去掉注释即可
EN = 0;
}
int main(void)//主函数
{ char a,b;
init();
write_com(0x80);//第一行
a =sizeof(table); //计算字符串的长度
for(x=0;x<a;x++)
{
write_date(table[x]);
delay(150);
}
write_com(0x80+0x40);//第二行
b =sizeof(table1); //计算字符串的长度
for(x=0;x<b;x++)
{
write_date(table1[x]);
delay(150);
}
while(1);
return 0;
}
int init()//初始化函数体
{
EN = 0;
write_com(0X38);//设置16*2显示,5*7点阵,8位数据接口
write_com(0X0C);//设置开显示,不显示光标
write_com(0X06);//写一个字符时,整屏右移
write_com(0X01);//显示清零
return 0;
}
int write_com(unsigned char com)//写命令的函数体
{
//EN=0;
LcdWaitReady();
RS = 0;
RW = 0;
P2 = com;
delay(5);
EN = 1;
delay(5);
EN = 0;
return 0;
}
int write_date(unsigned char date)//写数据的函数体
{
LcdWaitReady();
RS = 1;
RW = 0;
P2 = date;
delay(5);
EN = 1;
delay(5);
EN = 0;
return 0;
}
int delay(unsigned char xms)
{
unsigned char x,y;
for(x=xms;x>0;x--)
for(y=110;y>0;y--);
return 0;
}
|
|