51串口如何把接收数据存到数组,再把数组的数据发到电脑?
时间:10-02
整理:3721RD
点击:
各位大侠看一下,我下面的程序为什么不能把接收的数据存到一个数组,再把该数组的数据上传到电脑上?(我参考了之前的一篇有关 串口如何接收多位字节数据 的文档写的程序),希望大家能给指正指正,万分感谢!
#include <reg52.h> // 包含51单片机寄存器定义的头文件
#define uchar unsigned char
#define uint unsigned int
uchar k=0; //以k做为判断是否接收到数据的依据
uchar c; //记录接收到多少个字节的数据
uchar receive[12]={0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff};
void delay1ms(uint i)
{
unsigned char j;
while(i--)
{for(j=0;j<114;j++) //基准延时程序
{;}
}
}
void send_char(unsigned char txd) // 传送一个字符
{
SBUF = txd;
while(!TI); // 等特数据传送
TI = 0; // 清除数据传送标志
}
void fasong(){ //发送数组receive[];
uchar i;
for(i=0;i<c;i++){
send_char(receive);
}
}
void panduan_k(){ //判断k,若为0,说明开始接收数据
if(k!=0){
delay1ms(1); //延迟1ms,等待把数据接收完
k=0;
fasong();
}
}
main()
{
uchar i;
uchar j=0;
uchar receive[12]={0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff};
SCON= 0x40; //串口方式1
PCON=0; //SMOD=0
REN=1; //允许接收
TMOD= 0x20; //定时器1定时方式2
TH1= 0xfd; //11.0592M 9600波特率
TL1= 0xfd;
TR1= 1; //启动定时器
while(1)
{ panduan_k();
}
}
void ser()interrupt 4
{ if(RI==1){
RI=0;
receive[k++]=SBUF; //存数据到接收缓存
c=k;
}
}
怎么无论是发什么数据,电脑串口都没有接收到任何的单片机的回复呢?
#include <reg52.h> // 包含51单片机寄存器定义的头文件
#define uchar unsigned char
#define uint unsigned int
uchar k=0; //以k做为判断是否接收到数据的依据
uchar c; //记录接收到多少个字节的数据
uchar receive[12]={0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff};
void delay1ms(uint i)
{
unsigned char j;
while(i--)
{for(j=0;j<114;j++) //基准延时程序
{;}
}
}
void send_char(unsigned char txd) // 传送一个字符
{
SBUF = txd;
while(!TI); // 等特数据传送
TI = 0; // 清除数据传送标志
}
void fasong(){ //发送数组receive[];
uchar i;
for(i=0;i<c;i++){
send_char(receive);
}
}
void panduan_k(){ //判断k,若为0,说明开始接收数据
if(k!=0){
delay1ms(1); //延迟1ms,等待把数据接收完
k=0;
fasong();
}
}
main()
{
uchar i;
uchar j=0;
uchar receive[12]={0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff};
SCON= 0x40; //串口方式1
PCON=0; //SMOD=0
REN=1; //允许接收
TMOD= 0x20; //定时器1定时方式2
TH1= 0xfd; //11.0592M 9600波特率
TL1= 0xfd;
TR1= 1; //启动定时器
while(1)
{ panduan_k();
}
}
void ser()interrupt 4
{ if(RI==1){
RI=0;
receive[k++]=SBUF; //存数据到接收缓存
c=k;
}
}
怎么无论是发什么数据,电脑串口都没有接收到任何的单片机的回复呢?
大家,不好意思,刚刚又查了一下,那个中断开关IE没开。但开了以后,发现另外一个问题,为什么数组的数据有缺失,第偶数个数据会丢失(最后两个数据没有丢),诚心希望各位大侠帮我分析分析。
可以改一下试试
void fasong(){ //发送数组receive[];
uchar i;
for(i=0;i<c;i++){
send_char(receive);此处改成send_char(receive[i])
}
}
好东西谢谢分享辛苦了小编
额,不懂
