关于51单片机多机通信的问题
我今天编了一个单片机多机通信的程序,使用单片机内部的串口2,可是程序运行传递数据没有问题,传递地址并对某分机独立传输数据时就不行。查错查了一天了,请高手帮我看看主机程序#...
我今天编了一个单片机多机通信的程序,使用单片机内部的串口2,可是程序运行传递数据没有问题,传递地址并对某分机独立传输数据时就不行。查错查了一天了,请高手帮我看看
主机程序
#include "reg51.h"
sbit k1=P3^4;
sbit k2=P3^5;
sbit k3=P3^6;
sbit k4=P3^7;
unsigned da[8]={0x07,0x06,0x05,0x04,0x02};
void Serial()
{
EA=1;
BRT=0xFD;S2CON=0xd0;AUXR=0x11;IE2=0x01; EA=1;
}
void nodese()
{
unsigned int x;
for(x=0;x<3000;x++);
}
void Send1(unsigned char SByte)
{
unsigned char temp;
IE2=0;
S2CON=S2CON&0xfd;
S2CON=S2CON|0x08;
S2BUF=SByte;
do
{
temp=S2CON;temp=temp&0x02;
}
while(temp==0);
S2CON=S2CON&0xfd;
IE2=1;
}
void Send2(unsigned char SByte)
{
unsigned char temp;
IE2=0;
S2CON=S2CON&0xfd;
S2CON=S2CON&0xf7;
S2BUF=SByte;
do
{
temp=S2CON;temp=temp&0x02;
}
while(temp==0);
S2CON=S2CON&0xfd;
IE2=1;
}
main()
{
unsigned char i,Led;
P2=0;
Led=0;
Serial();
while(1)
{
P2=Led;
P0=0xff;
if(k1==0)
{
nodese();
while(k1==0);
Send1(1);
Led=0x06;
}
if(k2==0)
{
nodese();
while(k2==0);
Send1(2);
Led=0x5b;
}
if(k3==0)
{
nodese();
while(k3==0);
Send1(3);
Led=0x4f;
}
if(k4==0)
{
nodese();
while(k4==0);
for(i=0;i<5;i++)
Send2(da[i]);
Led=0x7f;
}
}}
分机程序
#include "reg51.h"
sbit speaker=P0^5;
unsigned char adress=0x01;
unsigned char cnt=0,dat[5],RDat;
bit flag=0,flag2=0;
unsigned char tab[11]={0x3f,0x06,0x5b,0x4f,0x66,0x6d,0x7d,0x07,0x7f,0x6f,0x00};
unsigned char con[5]={0xc1,0xd0,0xc8,0xc4,0xc2};
void SerialInit()
{
BRT=0xfd;
S2CON=0xf0;//11110000
AUXR=0x11;
IE2=0x01;EA=1;
}
void Init()
{
P2=0x00;
speaker=0;
}
void delay(unsigned int x)
{
unsigned int i,j;
for(i=0;i<x;i++)
for(j=0;j<990;j++);
}
void ReadData() interrupt 8
{
unsigned char k,t;
k=S2CON;
k=k&0x01;
if(k==1)
{
S2CON=S2CON&0xfe;
RDat=S2BUF;
t=S2CON;
t=t&0x04;
if(t==1&&adress==RDat)
{
flag2=1;
}
else if(t==0)
{
if(!(S2CON&0x20))
{
if(cnt==4)
{
S2CON=S2CON|0x20;
flag=1;
cnt=0;
dat[4]=RDat;
}
else dat[cnt++]=RDat;
}
}
}
}
main()
{
unsigned char i=0;
SerialInit();
Init();
while(1)
{
if(flag==1)
{
flag=0;
speaker=1;
delay(1000);
speaker=0;
while(!flag)
{
P2=tab[dat[i]];
P0=con[i];
delay(2);
i=(i<4)? i+1:0;
}
}
if(flag2==1)
{
flag2=0;
speaker=1;
delay(100);
speaker=0;
S2CON=S2CON&0xdf;
P2=tab[adress];
}
}
} 展开
主机程序
#include "reg51.h"
sbit k1=P3^4;
sbit k2=P3^5;
sbit k3=P3^6;
sbit k4=P3^7;
unsigned da[8]={0x07,0x06,0x05,0x04,0x02};
void Serial()
{
EA=1;
BRT=0xFD;S2CON=0xd0;AUXR=0x11;IE2=0x01; EA=1;
}
void nodese()
{
unsigned int x;
for(x=0;x<3000;x++);
}
void Send1(unsigned char SByte)
{
unsigned char temp;
IE2=0;
S2CON=S2CON&0xfd;
S2CON=S2CON|0x08;
S2BUF=SByte;
do
{
temp=S2CON;temp=temp&0x02;
}
while(temp==0);
S2CON=S2CON&0xfd;
IE2=1;
}
void Send2(unsigned char SByte)
{
unsigned char temp;
IE2=0;
S2CON=S2CON&0xfd;
S2CON=S2CON&0xf7;
S2BUF=SByte;
do
{
temp=S2CON;temp=temp&0x02;
}
while(temp==0);
S2CON=S2CON&0xfd;
IE2=1;
}
main()
{
unsigned char i,Led;
P2=0;
Led=0;
Serial();
while(1)
{
P2=Led;
P0=0xff;
if(k1==0)
{
nodese();
while(k1==0);
Send1(1);
Led=0x06;
}
if(k2==0)
{
nodese();
while(k2==0);
Send1(2);
Led=0x5b;
}
if(k3==0)
{
nodese();
while(k3==0);
Send1(3);
Led=0x4f;
}
if(k4==0)
{
nodese();
while(k4==0);
for(i=0;i<5;i++)
Send2(da[i]);
Led=0x7f;
}
}}
分机程序
#include "reg51.h"
sbit speaker=P0^5;
unsigned char adress=0x01;
unsigned char cnt=0,dat[5],RDat;
bit flag=0,flag2=0;
unsigned char tab[11]={0x3f,0x06,0x5b,0x4f,0x66,0x6d,0x7d,0x07,0x7f,0x6f,0x00};
unsigned char con[5]={0xc1,0xd0,0xc8,0xc4,0xc2};
void SerialInit()
{
BRT=0xfd;
S2CON=0xf0;//11110000
AUXR=0x11;
IE2=0x01;EA=1;
}
void Init()
{
P2=0x00;
speaker=0;
}
void delay(unsigned int x)
{
unsigned int i,j;
for(i=0;i<x;i++)
for(j=0;j<990;j++);
}
void ReadData() interrupt 8
{
unsigned char k,t;
k=S2CON;
k=k&0x01;
if(k==1)
{
S2CON=S2CON&0xfe;
RDat=S2BUF;
t=S2CON;
t=t&0x04;
if(t==1&&adress==RDat)
{
flag2=1;
}
else if(t==0)
{
if(!(S2CON&0x20))
{
if(cnt==4)
{
S2CON=S2CON|0x20;
flag=1;
cnt=0;
dat[4]=RDat;
}
else dat[cnt++]=RDat;
}
}
}
}
main()
{
unsigned char i=0;
SerialInit();
Init();
while(1)
{
if(flag==1)
{
flag=0;
speaker=1;
delay(1000);
speaker=0;
while(!flag)
{
P2=tab[dat[i]];
P0=con[i];
delay(2);
i=(i<4)? i+1:0;
}
}
if(flag2==1)
{
flag2=0;
speaker=1;
delay(100);
speaker=0;
S2CON=S2CON&0xdf;
P2=tab[adress];
}
}
} 展开
4个回答
展开全部
首先检查点对点通信是否实现。如果实现,估计是总线干扰
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
唉~~~小弟刚学完单片机不久。学校没教单片机C语言,现在自学中……看来将来单片机开发普遍是用C的了
本回答被网友采纳
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
建议你到 soiseek 去看看。一个能搜中文的IC网站,前面加www,后面是.cn。
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
好好
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询