51單片機(jī)雙機(jī)通信硬件電路圖及C程序
需要注意的是,兩單片機(jī)要保證正確通信,主機(jī)的RXD連接從機(jī)的TXD,從機(jī)的RXD連接主機(jī)的TXD。
雙機(jī)通信C語(yǔ)言程序
(1)主機(jī)程序
#include
#include
#define _READY_ 0x06//主機(jī)握手信號(hào)
#define _BUSY_ 0x15//從機(jī)忙應(yīng)答
#define_OK_ 0x00//從機(jī)準(zhǔn)備好
#define _SUCC_ 0x0f//數(shù)據(jù)傳送成功
#define _ERR_ 0xf0//數(shù)據(jù)傳送失敗
unsigned charTable[9]={0x31,0x32,0x33,0x34,0x35,0x36,0x37,0x38,0x39};
unsigned char Buff[20]; //數(shù)據(jù)緩沖區(qū)
unsigned char temp=_BUSY_;
sbit KEY=P1^6;
//延時(shí)1ms函數(shù)
void delay_1ms(unsigned int t)
{
unsigned int x,y;
for(x=t;x>0;x--)
for(y=110;y>0;y--);
}
//緩沖區(qū)初始化
void Buff_init()
{
unsigned chari; //將Table里的數(shù)據(jù)放到緩沖區(qū)里
for(i=0;i
{
Buff= Table;
delay_1ms(100);
}
}
//串口初始化函數(shù)
void serial_init()
{
TMOD=0x20; //定時(shí)器1工作于方式2
TH1=0xfd;
TL1=0xfd; //波特率為9600
PCON=0;
SCON=0x50; //串口工作于方式1
TR1=1; //開(kāi)啟定時(shí)器
TI=0;
RI=0;
}
//發(fā)送數(shù)據(jù)函數(shù)
void SEND_data(unsigned char *Buff)
{
unsigned char i;
unsigned char lenth;
unsigned char check;
lenth=strlen(Buff); //計(jì)算數(shù)據(jù)長(zhǎng)度
check=lenth;
TI=0; //發(fā)送數(shù)據(jù)長(zhǎng)度
SBUF=lenth;
while(!TI);
TI=0;
for(i=0;i
{
check=check^Buff;
SBUF=Buff;
while(!TI);
TI=0;
}
SBUF=check; //發(fā)送校驗(yàn)字節(jié)
while(!TI);
TI=0;
}
void main()
{
Buff_init();
serial_init();
while(1)
{
if(KEY==0)
{
delay_1ms(5);
if(KEY==0)
{
while(!KEY);
TI=0; //主機(jī)發(fā)送握手信號(hào)
SBUF=_READY_;
while(!TI);
TI=0;
while(temp!=_OK_) //主機(jī)等待從機(jī)應(yīng)答信號(hào)
{
RI=0;
while(!RI);
temp=SBUF;
RI=0;
}
temp=_ERR_; //主機(jī)等待從機(jī)數(shù)據(jù)接收成功信號(hào)
while(temp!=_SUCC_)
{
SEND_data(Buff);
RI=0;
while(!RI);
temp=SBUF;
RI=0;
}
}
}
}
}
(2)從機(jī)程序
#include
#include
#define _READY_ 0x06//主機(jī)握手信號(hào)
#define _BUSY_ 0x15//從機(jī)忙應(yīng)答
#define_OK_ 0x00//從機(jī)準(zhǔn)備好
#define _SUCC_ 0x0f//數(shù)據(jù)傳送成功
#define _ERR_ 0xf0//數(shù)據(jù)傳送失敗
unsigned char aa=_BUSY_;//主機(jī)與從機(jī)之間通信標(biāo)志
unsigned char Buff[20];//數(shù)據(jù)緩沖區(qū)
//串口初始化函數(shù)
void serial_init()
{
TMOD=0x20; //定時(shí)器1工作于方式2
TH1=0xfd;
TL1=0xfd; //波特率為9600
PCON=0;
SCON=0x50; //串口工作于方式1
TR1=1; //開(kāi)啟定時(shí)器
TI=0;
RI=0;
}
//接收數(shù)據(jù)函數(shù) www.dgzj.com
unsigned char RECE_data(unsigned char *Buff)
{
unsigned char i,temp;
unsigned char lenth;
unsigned char check;
RI=0; //接收數(shù)據(jù)長(zhǎng)度
while(!RI);
lenth=SBUF;
RI=0;
check=lenth;
for(i=0;i
{
while(!RI);
Buff=SBUF;
check=check^(Buff);
RI=0;
}
while(!RI); //接收校驗(yàn)字節(jié)
temp=SBUF;
RI=0;
check=temp^check; //將從主機(jī)接收到的校驗(yàn)碼與自己計(jì)算的校驗(yàn)碼比對(duì)
if(check!=0) //校驗(yàn)碼不一致,表明數(shù)據(jù)接收錯(cuò)誤,向主機(jī)發(fā)送錯(cuò)誤信號(hào),函數(shù)返回0xff
{
TI=0;
SBUF=_ERR_;
while(!TI);
TI=0;
return 0xff;
}
TI=0; //校驗(yàn)碼一致,表明數(shù)據(jù)接收正確,向主機(jī)發(fā)送成功信號(hào),函數(shù)返回0x00
SBUF=_SUCC_;
while(!TI);
TI=0;
return 0;
}
void main()
{
serial_init();
while(1)
{
while(aa!=_READY_) //從機(jī)等待主機(jī)發(fā)送的握手信號(hào)
{
RI=0;
while(!RI);
aa=SBUF;
RI=0;
}
TI=0; //一旦接收到握手信號(hào),從機(jī)返回OK,等待接收數(shù)據(jù)
SBUF=_OK_;
while(!TI);
TI=0;
aa=0xff; //從機(jī)接收數(shù)據(jù),并將數(shù)據(jù)保存到數(shù)據(jù)緩沖區(qū)
while(aa==0xff)
{
aa=RECE_data(Buff);
}
P1=Buff[0]; //查看接收到的數(shù)據(jù)
}
}