|
本帖最后由 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;
}
|
|