标题:
求助51单片机串口接收字符串程序问题
[打印本页]
作者:
1045964948
时间:
2022-1-13 16:56
标题:
求助51单片机串口接收字符串程序问题
最近在做一个和模块相互工作的一个程序,具体实现的功能就是串口发送什么数据过去,然后单片机就会向串口发送,实现串口回显的功能,还需要将串口接收到的数据存放在数组,在网上搜索一下午没有解决问题所以想求助一下论坛里面的各位,希望有大佬帮助解决一下问题
单片机源程序如下:
#include <REGX51.H>
#include "stdio.h"
int i=123;
unsigned char table[20]={"AT"};
void main()
{
P1=0x00;
TMOD=0x21; //设T0为方式1,GATE=1;
SCON=0x50;
TH1=0xFD;
TL1=0xFD;
TR1=1; //开启定时器
TI=1;
EA=1;
ES=1;
printf("123\r\n");
while(1)
{
}
}
void zd() interrupt 4
{
static unsigned char num=0,q;
if(RI==1)
{
q=SBUF;
while(q!='\n')
{
table[num]=SBUF;
num++;
}
RI=0;
}
}
复制代码
作者:
1045964948
时间:
2022-1-13 21:29
这一段代码它的问题就是我在开头发送一串字符不能完成串口发送出去,还有就是我电脑第一次发送回显没有问题,后来就不能回显,按一下复位键发送正常接收,第二次就不行了
#include <REGX51.H>
#include "stdio.h"
#define u8 unsigned char
int i=123;
unsigned char table[10];
bit flag=0;
void send_byte(u8 str)
{
SBUF=str;
while(!TI);
TI=0;
}
void send_string(u8 *str)
{
while(*str!='\0')
{
send_byte(*str);
str++;
}
}
void main()
{
P1=0x00;
TMOD=0x21; //设T0为方式1,GATE=1;
SCON=0x50;
TH1=0xFD;
TL1=0xFD;
TR1=1; //开启定时器
TI=1;
EA=1;
ES=1;
send_string("asdfg");
while(1)
{
static unsigned char j;
if(flag==1)
{
// printf("%s\r\n",&table);
send_string(&table);
flag=0;
for(j=0;j<10;j++)
{
table[j]='\0';
}
}
}
}
void zd() interrupt 4
{
static unsigned char num=0,temp;
num=0;
if(RI==1)
{
temp=SBUF;
if(temp!='\n')
{
table[num++]=temp;
}
else
flag=1;
RI=0;
}
}
复制代码
作者:
TEC
时间:
2022-1-13 23:26
把你的中断函数修改一下,num定义到外部去,以'\n'作为结束字符,试了一下,是可以回传的。
void zd() interrupt 4
{
static unsigned char temp;
if(RI==1)
{
RI=0;
temp=SBUF;
if(temp!='\n')
{
table[num++]=temp;
}
else if(temp=='\n')
{
flag=1;
num=0;
}
}
}
复制代码
作者:
1045964948
时间:
2022-1-18 22:58
TEC 发表于 2022-1-13 23:26
把你的中断函数修改一下,num定义到外部去,以'\n'作为结束字符,试了一下,是可以回传的。
好的谢谢你我去试试
欢迎光临 (http://www.51hei.com/bbs/)
Powered by Discuz! X3.1