问题:单片机485通讯串口接受16进制数据数组
时间:10-02
整理:3721RD
点击:
我编写一个单片机485串口的程序。给传感器发送命令帧16进制数组11 04 00 00 00 02 73 5B,理论上单片机串口就有回应16进制数组11 04 04 00 00 00 00 AA 0A第四、五、六、七位是传感器数据位,但是我串口中断接收到的数据全是0x11,求大神解答(P2口接的是LED灯,用以判断接收到的数据是否正确)#include <reg52.h>
#include <intrins.h>
#define uchar unsigned char
#define uint unsigned int
unsigned char code send[]={0x11,0x04,0x00,0x00,0x00,0x02,0x73,0x5B};
unsigned char shuzhi[]={0x3f,0x06,0x5b,0x4f,0x66,0x6d,0x7d,0x07,
0x7f,0x6f,0x77,0x7c,0x39,0x5e,0x79,0x71,0x00};
uchar Rsd[8]={0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00};
sbit wei1=P0^0;
sbit wei2=P0^1;
sbit wei3=P0^2;
sbit wei4=P0^3;
uchar index=1,tt;
uchar temp1,flag;
void Delay(uint c) //延时10ms
{
uint i,j;
for(i=c;i>0;i--)
for(j=0;j<110;j++);
}
void main()
{
Delay(10); //
SCON = 0x50; // 串口工作方式1,REN=1:允许串口接受
TMOD = 0x20; // 定时器1,模式2,
TH1 = 0xfd; // 波特率9600
TL1 = 0xfd; //
TR1 = 1; // 启动定时器1
EA = 1; //打开总中断
ES = 1; //打开串口中断
Delay(50);
while(1)
{
ES = 0; //关闭串口中断
SBUF = send[0];
while(!TI);
TI = 0;
SBUF = send[1];
while(!TI);
TI = 0;
SBUF = send[2];
while(!TI);
TI = 0;
SBUF = send[3];
while(!TI);
TI = 0;
SBUF = send[4];
while(!TI);
TI = 0;
SBUF = send[5];
while(!TI);
TI = 0;
SBUF = send[6];
while(!TI);
TI = 0;
SBUF = send[7];
while(!TI);
TI = 0;
Delay(250);
ES = 1; //打开串口中断
}
}
void ser() interrupt 4
{
if(RI==1)
{
RI=0;
temp1=SBUF;
if(index==1)
{
Rsd[0]=temp1;
index=2;
}
else if(index==2)
{
Rsd[1]=temp1;
index=3;
}
else if(index==3)
{
Rsd[2]=temp1;
index=4;
}
else if(index==4)
{
Rsd[3]=temp1;
index=5;
}
else if(index==5)
{
Rsd[4]=temp1;
index=6;
}
else if(index==6)
{
Rsd[5]=temp1;
index=7;
}
else if(index==7)
{
Rsd[6]=temp1;
index=8;
}
else if(index==8)
{
Rsd[7]=temp1;
index=9;
}
if(index==9)
{
index=1;
if(Rsd[1]==0x04)
{
P2=0x66;
}
}
}
}
#include <intrins.h>
#define uchar unsigned char
#define uint unsigned int
unsigned char code send[]={0x11,0x04,0x00,0x00,0x00,0x02,0x73,0x5B};
unsigned char shuzhi[]={0x3f,0x06,0x5b,0x4f,0x66,0x6d,0x7d,0x07,
0x7f,0x6f,0x77,0x7c,0x39,0x5e,0x79,0x71,0x00};
uchar Rsd[8]={0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00};
sbit wei1=P0^0;
sbit wei2=P0^1;
sbit wei3=P0^2;
sbit wei4=P0^3;
uchar index=1,tt;
uchar temp1,flag;
void Delay(uint c) //延时10ms
{
uint i,j;
for(i=c;i>0;i--)
for(j=0;j<110;j++);
}
void main()
{
Delay(10); //
SCON = 0x50; // 串口工作方式1,REN=1:允许串口接受
TMOD = 0x20; // 定时器1,模式2,
TH1 = 0xfd; // 波特率9600
TL1 = 0xfd; //
TR1 = 1; // 启动定时器1
EA = 1; //打开总中断
ES = 1; //打开串口中断
Delay(50);
while(1)
{
ES = 0; //关闭串口中断
SBUF = send[0];
while(!TI);
TI = 0;
SBUF = send[1];
while(!TI);
TI = 0;
SBUF = send[2];
while(!TI);
TI = 0;
SBUF = send[3];
while(!TI);
TI = 0;
SBUF = send[4];
while(!TI);
TI = 0;
SBUF = send[5];
while(!TI);
TI = 0;
SBUF = send[6];
while(!TI);
TI = 0;
SBUF = send[7];
while(!TI);
TI = 0;
Delay(250);
ES = 1; //打开串口中断
}
}
void ser() interrupt 4
{
if(RI==1)
{
RI=0;
temp1=SBUF;
if(index==1)
{
Rsd[0]=temp1;
index=2;
}
else if(index==2)
{
Rsd[1]=temp1;
index=3;
}
else if(index==3)
{
Rsd[2]=temp1;
index=4;
}
else if(index==4)
{
Rsd[3]=temp1;
index=5;
}
else if(index==5)
{
Rsd[4]=temp1;
index=6;
}
else if(index==6)
{
Rsd[5]=temp1;
index=7;
}
else if(index==7)
{
Rsd[6]=temp1;
index=8;
}
else if(index==8)
{
Rsd[7]=temp1;
index=9;
}
if(index==9)
{
index=1;
if(Rsd[1]==0x04)
{
P2=0x66;
}
}
}
}
那应该还是你的程序问题了
程序写的太差..你多研究一下写法吧,哪里有写这么死的程序的..而且你这代码也不完整