如何通过串口调试软件发送字符串到单片机上,然后单片机将接收到的字符串显示在LCD1602上? 20
单个的字符好解决,可是字符串如何解决?我在网上找到一个程序,可是发送失控,LCD显示也乱。不知什么原因,请指教。下面是我找的一个程序:#include<reg51.h>#...
单个的字符好解决,可是字符串如何解决?我在网上找到一个程序,可是发送失控,LCD显示也乱。不知什么原因,请指教。
下面是我找的一个程序:
#include<reg51.h>
#define uchar unsigned char
#define uint unsigned int
sbit lcden = P3^5;
sbit lcdrw = P3^6;
sbit lcdrs = P3^7;
uchar table[10];
uchar i, j, flag;
void delay(uint n)
{
uint x,y;
for( x = n; x > 0; x--)
for( y = 110; y > 0; y-- );
}
//lcd写命令函数
void lcdwrite_com( uchar com )
{
lcdrs = 0;
lcdrw = 0;
P0 = com;
delay(5);
lcden = 1;
delay(5);
lcden = 0;
}
//lcd写数据
void lcdwrite_date( uchar date )
{
lcdrs = 1;
lcdrw = 0;
P0 = date;
delay(5);
lcden = 1;
delay(5);
lcden = 0;
}
//lcd初始化
void lcd_init()
{
lcden = 0;
lcdwrite_com( 0x38 );//显示模式设置
lcdwrite_com( 0x0e );//显示光标
lcdwrite_com( 0x01 );//清屏
lcdwrite_com( 0x80 + 2 );//数据指针设置
}
//串口初始化
void serial_init()
{
TMOD = 0x20;
TH1 = 0xfd; //9600赫兹
TL1 = 0xfd;
TR1 = 1; //开启时间中断
SCON = 0x50;//方式2
SM2 = 1; //可以向cpu请求中断
EA = 1; //开总中断
ES = 1; //开串口中断
}
void putchar( uchar ch )//发送数据
{
SBUF = ch;
while( !TI );
TI = 0;
}
void main()
{
lcd_init();
serial_init();
while(1)
{
delay(20);
while( table[j] != '\0' && flag == 1 )
{
j = j >= 10 ? 0 : j;
putchar( table[j] );
lcdwrite_date( table[j] );
j++;
// delay(10);
}
}
}
void Serial() interrupt 4
{
RI = 0;
if( SBUF != '#')//遇到#终止写
{
table[i] = SBUF;
i++;
}
else
flag = 1;
} 展开
下面是我找的一个程序:
#include<reg51.h>
#define uchar unsigned char
#define uint unsigned int
sbit lcden = P3^5;
sbit lcdrw = P3^6;
sbit lcdrs = P3^7;
uchar table[10];
uchar i, j, flag;
void delay(uint n)
{
uint x,y;
for( x = n; x > 0; x--)
for( y = 110; y > 0; y-- );
}
//lcd写命令函数
void lcdwrite_com( uchar com )
{
lcdrs = 0;
lcdrw = 0;
P0 = com;
delay(5);
lcden = 1;
delay(5);
lcden = 0;
}
//lcd写数据
void lcdwrite_date( uchar date )
{
lcdrs = 1;
lcdrw = 0;
P0 = date;
delay(5);
lcden = 1;
delay(5);
lcden = 0;
}
//lcd初始化
void lcd_init()
{
lcden = 0;
lcdwrite_com( 0x38 );//显示模式设置
lcdwrite_com( 0x0e );//显示光标
lcdwrite_com( 0x01 );//清屏
lcdwrite_com( 0x80 + 2 );//数据指针设置
}
//串口初始化
void serial_init()
{
TMOD = 0x20;
TH1 = 0xfd; //9600赫兹
TL1 = 0xfd;
TR1 = 1; //开启时间中断
SCON = 0x50;//方式2
SM2 = 1; //可以向cpu请求中断
EA = 1; //开总中断
ES = 1; //开串口中断
}
void putchar( uchar ch )//发送数据
{
SBUF = ch;
while( !TI );
TI = 0;
}
void main()
{
lcd_init();
serial_init();
while(1)
{
delay(20);
while( table[j] != '\0' && flag == 1 )
{
j = j >= 10 ? 0 : j;
putchar( table[j] );
lcdwrite_date( table[j] );
j++;
// delay(10);
}
}
}
void Serial() interrupt 4
{
RI = 0;
if( SBUF != '#')//遇到#终止写
{
table[i] = SBUF;
i++;
}
else
flag = 1;
} 展开
1个回答
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询