at89c51单片机多机通信问题,怎么通信不了呢,请高手帮忙看看,谢谢
[复制链接]
主机程序 #include
#include
#define BUF_MAX_LEN 3
#define ADDR_ACK 0x1A
#define DATA_ACK 0x2A
unsigned char send_buf[BUF_MAX_LEN];
void serial_init(void);
void send_addrframe( unsigned char addr );
void send_data_frame( void);
void main( void )
{
unsigned char recv_tmp=0;
send_buf[0]=1;
send_buf[1]=2;
send_buf[2]=3;
serial_init();
while( recv_tmp!= ADDR_ACK )
{
send_addrframe( 0x01 );
RI = 0;
while(!RI);
RI = 0;
recv_tmp = SBUF;
}
while( recv_tmp != DATA_ACK )
{
send_data_frame();
RI =0;
while(!RI);
RI = 0;
recv_tmp = SBUF;
}
}
void serial_init(void)
{
TMOD = 0x20 ;
TH1 = 0xFD ;
TL1 = 0xFD ;
EA = 1;
ET0 = 0;
ES = 1;
SCON = 0xD0 ;
PCON = 0x00 ;
TR1 = 1;
}
void send_addrframe( unsigned char addr )
{
TB8 = 1;
SBUF= addr;
while( !TI );
TI = 0;
TB8 = 0;
}
void send_data_frame( void )
{
unsigned char i;
unsigned char check_sum=0;
for( i=0;i;
}
for( i=0 ;i< BUF_MAX_LEN;i++)
{
TI = 0;
TB8 =0;
SBUF = send_buf;
while( !TI);
TI = 0;
}
SBUF = check_sum;
while(!TI);
TI = 0;
}
从机1程序
#include
#include
#define BUF_MAX_LEN 10
#define ADDR_ACK 0x1A
#define DATA_ACK 0x2A
#define DATA_ERR 0x3A
unsigned char recv_buf[BUF_MAX_LEN+1];
unsigned char recv_data_frame(void);
void send_ack( unsigned char ack );
void recv_addrframe( void );
void serial_init(void);
void main( void )
{
unsigned char recv_tmp=0;
serial_init();
while(1)
{
recv_addrframe();
send_ack( ADDR_ACK );
if( recv_data_frame() == DATA_ACK )
{
send_ack( DATA_ACK );
}
else if(recv_data_frame == DATA_ERR )
{
send_ack( DATA_ERR );
}
}
}
unsigned char recv_data_frame( void )
{
unsigned char i;
unsigned char check_sum=0;
SM2= 0;
for( i=0;i = SBUF;
}
for( i=0;i;
}
if( recv_buf[ BUF_MAX_LEN+1]==check_sum )
{
return DATA_ACK;
}
else
{
return DATA_ERR;
}
}
void send_ack( unsigned char ack )
{
TI = 0;
TB8 = 0;
SBUF= ack;
while( !TI );
TI = 0;
}
void recv_addrframe( void )
{
unsigned char client_addr,recv_tmp;
client_addr = P0&0x0f;
recv_tmp = 0;
SM2 = 1;
while( recv_tmp != client_addr )
{
RI = 0;
while( !RI );
RI = 0;
recv_tmp = SBUF;
}
}
void serial_init(void)
{
TMOD = 0x20 ;
TH1 = 0xFD;
TL1 = 0xFD;
EA = 1;
ET0 = 0;
ES = 1;
SCON = 0xD0 ;
PCON = 0x00 ;
TR1 = 1;
}
从机2程序
#include
#include
#define BUF_MAX_LEN 10
#define ADDR_ACK 0x1A
#define DATA_ACK 0x2A
#define DATA_ERR 0x3A
unsigned char recv_buf[BUF_MAX_LEN+1];
unsigned char recv_data_frame(void);
void send_ack( unsigned char ack );
void recv_addrframe( void );
void serial_init(void);
void main( void )
{
unsigned char recv_tmp=0;
serial_init();
while(1)
{
recv_addrframe();
send_ack( ADDR_ACK );
if( recv_data_frame() == DATA_ACK )
{
send_ack( DATA_ACK );
}
else if(recv_data_frame == DATA_ERR )
{
send_ack( DATA_ERR );
}
}
}
unsigned char recv_data_frame( void )
{
unsigned char i;
unsigned char check_sum=0;
SM2= 0;
for( i=0;i = SBUF;
}
for( i=0;i;
}
if( recv_buf[ BUF_MAX_LEN+1]==check_sum )
{
return DATA_ACK;
}
else
{
return DATA_ERR;
}
}
void send_ack( unsigned char ack )
{
TI = 0;
TB8 = 0;
SBUF= ack;
while( !TI );
TI = 0;
}
void recv_addrframe( void )
{
unsigned char client_addr,recv_tmp;
client_addr = P0&0x0f;
recv_tmp = 0;
SM2 = 1;
while( recv_tmp != client_addr )
{
RI = 0;
while( !RI );
RI = 0;
recv_tmp = SBUF;
}
}
void serial_init(void)
{
TMOD = 0x20 ;
TH1 = 0xFD;
TL1 = 0xFD;
EA = 1;
ET0 = 0;
ES = 1;
SCON = 0xD0 ;
PCON = 0x00 ;
TR1 = 1;
}
|