pc用串口助手给单片机发一字符串,收到的数据在LCD1602上...
时间:10-02
整理:3721RD
点击:
我在中断用数组接收,可是只能收到第一个字节,用的是RS485电平转换(硬件已存在没法改),不知道是不是程序编的哪有问题
#include<reg52.h>
#define uchar unsigned char
#define uint unsigned int
uchar table[10]="send data:";
uchar table1[13]="receive data:";
sbit lcden=P3^6;
sbit lcdrs=P2^6;
sbit lcdrw=P2^7;
sbit RS485_E=P1^7;
uchar num,playflag;
uchar c,count;
uchar buf[10]={0};
void delay(uint z)
{
uint x,y;
for(x=z;x>0;x--)
for(y=110;y>0;y--);
}
// 写命令子函数
void write_cmd(uchar cmd)
{
lcdrs=0;
lcden=0;
P0=cmd;
lcdrw=0;
delay(5);
lcden=1;
delay(5);
lcden=0;
delay(5);
}
//写数据子函数
void write_data(uchar dataa)
{
lcdrs=1;
P0=dataa;
//lcdrw=0;
delay(5);
lcden=1;
delay(5);
lcden=0;
}
//液晶初始化
void init()
{
lcden=0;
write_cmd(0x38);// 设置16*2显示,5*7的点阵,8位数据口
write_cmd(0x0c);//开显示,不显示光标
write_cmd(0x06);
write_cmd(0x01);//清屏
write_cmd(0x80);//初始化数据指针
for(num=0;num<10;num++)
{
write_data(table[num]);
delay(5);
}
write_cmd(0x80+0x40);
for(num=0;num<14;num++)
{
write_data(table1[num]);
delay(5);
}
}
void change1(uchar s)
{
uchar p,q;
q=s/16;
if(q<10)
{q=q+0x30;}
else
{q=q+0x37;}
write_cmd(0x80+0x40+0x0d);
delay(5);
write_data(q);
delay(20);
p=s%16;
if(p<10)
{p=p+0x30;}
else
{p=p+0x37;}
write_cmd(0x80+0x40+0x0e);
delay(5);
write_data(p);
delay(20);
}
/********串行口初始化程序**********/
void init1()
{
SM0=0;
SM1=1;//设置为串行口工作方式1
REN=1;// 允许串行口接收
TI=0;
RI=0;
PCON=0;
TH1=0xFD;
TL1=0XFD;
TMOD=0X20;//用作定时器,工作在方式2
EA=1;
ET1=0;
ES=1;
TR1=1;
RS485_E=0;
}
void putchar( uchar ch )//发送数据
{
SBUF = ch;
while( !TI );
TI = 0;
}
//主函数
void main()
{ uchar i,j;
init();
init1();
while(1)
{
TI=0;
if(playflag==1)
{playflag=0;
RS485_E = 1;
for(i=0;i<10;i++)
{ putchar(buf[i]) ;}
RS485_E = 0;
for(j=0;j<10;j++)
{change1(buf[j]); }
delay(100);
}
TI=0;
}
}
void serial() interrupt 4
{
RS485_E=0;
ES=0;
if(RI)
{
buf[count++]=SBUF;
playflag=1;
if(count==10)
{count=0;}
RI=0;
}
ES=1;
}
#include<reg52.h>
#define uchar unsigned char
#define uint unsigned int
uchar table[10]="send data:";
uchar table1[13]="receive data:";
sbit lcden=P3^6;
sbit lcdrs=P2^6;
sbit lcdrw=P2^7;
sbit RS485_E=P1^7;
uchar num,playflag;
uchar c,count;
uchar buf[10]={0};
void delay(uint z)
{
uint x,y;
for(x=z;x>0;x--)
for(y=110;y>0;y--);
}
// 写命令子函数
void write_cmd(uchar cmd)
{
lcdrs=0;
lcden=0;
P0=cmd;
lcdrw=0;
delay(5);
lcden=1;
delay(5);
lcden=0;
delay(5);
}
//写数据子函数
void write_data(uchar dataa)
{
lcdrs=1;
P0=dataa;
//lcdrw=0;
delay(5);
lcden=1;
delay(5);
lcden=0;
}
//液晶初始化
void init()
{
lcden=0;
write_cmd(0x38);// 设置16*2显示,5*7的点阵,8位数据口
write_cmd(0x0c);//开显示,不显示光标
write_cmd(0x06);
write_cmd(0x01);//清屏
write_cmd(0x80);//初始化数据指针
for(num=0;num<10;num++)
{
write_data(table[num]);
delay(5);
}
write_cmd(0x80+0x40);
for(num=0;num<14;num++)
{
write_data(table1[num]);
delay(5);
}
}
void change1(uchar s)
{
uchar p,q;
q=s/16;
if(q<10)
{q=q+0x30;}
else
{q=q+0x37;}
write_cmd(0x80+0x40+0x0d);
delay(5);
write_data(q);
delay(20);
p=s%16;
if(p<10)
{p=p+0x30;}
else
{p=p+0x37;}
write_cmd(0x80+0x40+0x0e);
delay(5);
write_data(p);
delay(20);
}
/********串行口初始化程序**********/
void init1()
{
SM0=0;
SM1=1;//设置为串行口工作方式1
REN=1;// 允许串行口接收
TI=0;
RI=0;
PCON=0;
TH1=0xFD;
TL1=0XFD;
TMOD=0X20;//用作定时器,工作在方式2
EA=1;
ET1=0;
ES=1;
TR1=1;
RS485_E=0;
}
void putchar( uchar ch )//发送数据
{
SBUF = ch;
while( !TI );
TI = 0;
}
//主函数
void main()
{ uchar i,j;
init();
init1();
while(1)
{
TI=0;
if(playflag==1)
{playflag=0;
RS485_E = 1;
for(i=0;i<10;i++)
{ putchar(buf[i]) ;}
RS485_E = 0;
for(j=0;j<10;j++)
{change1(buf[j]); }
delay(100);
}
TI=0;
}
}
void serial() interrupt 4
{
RS485_E=0;
ES=0;
if(RI)
{
buf[count++]=SBUF;
playflag=1;
if(count==10)
{count=0;}
RI=0;
}
ES=1;
}