lcd1602显示串口发送的数据
时间:10-02
整理:3721RD
点击:
之前找了一阵没找到,后来琢磨透了自己写了一个,送给还不会串口和1602的发烧友
#include<reg52.h>
#define uchar unsigned char
#define uint unsigned int
uchar flag=0,a;
uint i=0,j;
uchar word[20]=0;
#define lcdrs p2_5
#define lcdrw p2_6
#define lcden p2_7
void delay(uint z)
{
uint x,y;
for(x=z;x>0;x--)
for(y=110;y>0;y--);
}
void write_data(uchar x)
{
lcdrs=1;
lcdrw=0;
P0=x;
delay(5);
lcden=1;
delay(5);
lcden=0;
delay(20);
}
void write_ins(uchar x)
{
lcdrs=0;
lcdrw=0;
P0=x;
delay(5);
lcden=1;
delay(5);
lcden=0;
delay(5);
}
void lcd_init()
{
lcden=0;
write_ins(0x38);
write_ins(0x0c);
write_ins(0x06);
write_ins(0x01);
write_ins(0x80);
}
void main()
{
TMOD=0x20;//设置定时器1为工作方式2
TH1=0xfd;
TL1=0xfd;
TR1=1;
SCON=0X50;
EA=1;
ES=1;
lcd_init();
while(1)
{
if(flag)
{
write_ins(0x01);
for(j=0;word[j]!='\0';j++)
write_data(word[j]);
for(j=0;j<20;j++)
word[j]='\0';
i=0;
flag=0;
}
}
}
void ser() interrupt 4
{
if(RI)
{
a=SBUF;
SBUF=SBUF;
word=a;
i++;
flag=1;
RI=0;
}
if(TI)
{
TI=0;
}
}
#include<reg52.h>
#define uchar unsigned char
#define uint unsigned int
uchar flag=0,a;
uint i=0,j;
uchar word[20]=0;
#define lcdrs p2_5
#define lcdrw p2_6
#define lcden p2_7
void delay(uint z)
{
uint x,y;
for(x=z;x>0;x--)
for(y=110;y>0;y--);
}
void write_data(uchar x)
{
lcdrs=1;
lcdrw=0;
P0=x;
delay(5);
lcden=1;
delay(5);
lcden=0;
delay(20);
}
void write_ins(uchar x)
{
lcdrs=0;
lcdrw=0;
P0=x;
delay(5);
lcden=1;
delay(5);
lcden=0;
delay(5);
}
void lcd_init()
{
lcden=0;
write_ins(0x38);
write_ins(0x0c);
write_ins(0x06);
write_ins(0x01);
write_ins(0x80);
}
void main()
{
TMOD=0x20;//设置定时器1为工作方式2
TH1=0xfd;
TL1=0xfd;
TR1=1;
SCON=0X50;
EA=1;
ES=1;
lcd_init();
while(1)
{
if(flag)
{
write_ins(0x01);
for(j=0;word[j]!='\0';j++)
write_data(word[j]);
for(j=0;j<20;j++)
word[j]='\0';
i=0;
flag=0;
}
}
}
void ser() interrupt 4
{
if(RI)
{
a=SBUF;
SBUF=SBUF;
word=a;
i++;
flag=1;
RI=0;
}
if(TI)
{
TI=0;
}
}
如果发现错误欢迎大家纠正
亲测好使
谢谢分享! 小编有没有连接线路图?能不能也分享一下?
网上有很多,比如这个,D0-D7对应连p0.0-p0.7
rs连p2.5
rw连p2.6(也可以像图中那样一直接地)
en连p2.7
谢谢分享
谢谢!