各位前辈,帮忙看一下这两段程序(分别是主机和从机),要实现的功能是两单片机进行握手通信,成功后发送字符串,并在从机的数码管上显示出来,过程中会在程序的不同的位置点亮LED灯作为指示。
/*本例是单片机双机通信主机程序*/
/*预定义及全局变量*/
#include<reg52.h>
#include<string.h>
#define uchar unsigned char
/*握手信号宏定义*/
#define _RDY_ 0x06
#define _BUSY_ 0x15
#define _OK_ 0x00
#define _SUCC_ 0x0f
#define _ERR_ 0xf0
sbit led0=P1^0;
sbit led1=P1^1;
sbit led2=P1^2;
sbit led3=P1^3;
sbit led4=P1^4;
sbit led5=P1^5;
sbit led6=P1^6;
sbit led7=P1^7;
uchar code str[]="520";
uchar len=0,CheckNum=0;
/*函数声明*/
void init();
void SendAChar(uchar);
void SendData(uchar *);
uchar ReceiveAChar();
void communication();
/*主函数*/
void main()
{
uchar ReplyFromGuest_main=0;
/*初始化*/
init();
while(1)
{
/*握手通信*/
communication();
/*发送数据*/
ReplyFromGuest_main=_ERR_;
while(ReplyFromGuest_main!=_SUCC_)
{
/*发送长度*/
len=strlen(str);
SendAChar(len);
led1=0;
/*发送数据*/
SendData(str);
led2=0;
/*发送校验码*/
SendAChar(CheckNum);
led3=0;
/*等待从机反馈*/
ReplyFromGuest_main=ReceiveAChar();
led4=0;
}
led5=0;
}
}
/*函数实现*/
void init()
{
/*波特率设置*/
TMOD=0x20;
TH1=0xfd;
TL1=0xfd;
PCON=0x00;
TR1=1;
/*串口初始化*/
SCON=0x50;//工作方式1,可接收
}
void SendAChar(uchar ch)
{
TI=0;
SBUF=ch;
while(TI==0);
TI=0;
}
void SendData(uchar *p)
{
uchar i;
for(i=0;i<len;i++)
{
CheckNum=CheckNum^(*p);
SendAChar(*p);
p++;
}
}
uchar ReceiveAChar()
{
uchar ReplyFromGuest_FuncRec;
RI=0;
while(RI==0);
ReplyFromGuest_FuncRec=SBUF;
RI=0;
return ReplyFromGuest_FuncRec;
}
void communication()
{
uchar ReplyFromGuest_FuncCom;
ReplyFromGuest_FuncCom=_BUSY_;
while(ReplyFromGuest_FuncCom!=_OK_)
{
/*发送_RDY_到从机,询问是否可以接收*/
TI=0;
SBUF=_RDY_;//可以直接用SendAChar(_RDY_)吗?
while(TI==0);
TI=0;
/*等待从机回复*/
ReplyFromGuest_FuncCom=ReceiveAChar();
}
led0=0;
}
:):):):):):):):):):):):):):):):)
:):):):):):):):):):):):):):):):)
/*本例是单片机双机通信从机程序*/
/*预定义及全局变量*/
#include<reg52.h>
#define uchar unsigned char
/*握手信号宏定义*/
#define _RDY_ 0x06
#define _BUSY_ 0x15
#define _OK_ 0x00
#define _SUCC_ 0x0f
#define _ERR_ 0xf0
sbit dula=P2^6;
sbit wela=P2^7;
sbit led0=P1^0;
sbit led1=P1^1;
sbit led2=P1^2;
sbit led3=P1^3;
sbit led4=P1^4;
sbit led5=P1^5;
sbit led6=P1^6;
sbit led7=P1^7;
uchar code table[]={//此处只给出0-9的数码
0x3f,0x06,0x5b,0x4f,
0x66,0x6d,0x7d,0x07,
0x7f,0x6f};
uchar code BUF[10];
uchar len=0,CheckNum=0;
/*函数声明*/
void init();
uchar ReceiveAChar();
void ReceiveData(uchar *p);
void SendAChar(uchar);
/*主函数*/
void main()
{
uchar ReplyFromHost,ReplyFromGuest_ReceiveData;
/*初始化*/
init();
while(1)
{
/*接收握手信号_RDY_*/
ReplyFromHost=_RDY_-1;//确保能够进入下面的循环
while(ReplyFromHost!=_RDY_)
{
ReplyFromHost=ReceiveAChar();
led0=0;
}
led1=0;
/*向主机发送已经准备好的信号_OK_*/
SendAChar(_OK_);
led2=0;
/*接收数据*/
ReplyFromGuest_ReceiveData=0xff;
while(ReplyFromGuest_ReceiveData==0xff)
{
/*接收长度*/
len=ReceiveAChar();
led3=0;
/*接收数据*/
ReceiveData(BUF);
led4=0;
/*接收校验码*/
ReplyFromHost=ReceiveAChar();
led5=0;
/*进行校验*/
if(CheckNum==ReplyFromHost)
{
SendAChar(_SUCC_);
ReplyFromGuest_ReceiveData=0x00;
}
else
{
SendAChar(_ERR_);
ReplyFromGuest_ReceiveData=0xff;
}
}
led6=0;
P1=BUF[0];
}
}
/*函数实现*/
void init()
{
/*波特率设置*/
TMOD=0x20;
TH1=0xfd;
TL1=0xfd;
PCON=0x00;
TR1=1;
/*串口初始化*/
SCON=0x50;//工作方式1,可接收
}
uchar ReceiveAChar()
{
uchar ReplyFromHost;
RI=0;
while(RI==0);
ReplyFromHost=SBUF;
RI=0;
return ReplyFromHost;
}
void ReceiveData(uchar *p)
{
uchar i;
for(i=0;i<len;i++)
{
*p=ReceiveAChar();
CheckNum=CheckNum^(*p);
p++;
}
*p='\0';
}
void SendAChar(uchar ch)
{
TI=0;
SBUF=ch;
while(TI==0);
TI=0;
}
|