2675|3

6

帖子

0

TA的资源

一粒金砂(初级)

楼主
 

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; }
此帖出自51单片机论坛
点赞 关注
 

回复
举报

6

帖子

0

TA的资源

一粒金砂(初级)

沙发
 
开头包括了#include
此帖出自51单片机论坛
 
 
 

回复

6

帖子

0

TA的资源

一粒金砂(初级)

板凳
 
此帖出自51单片机论坛
 
 
 

回复

6

帖子

0

TA的资源

一粒金砂(初级)

4
 
包括了 reg51.h
此帖出自51单片机论坛
 
 
 

回复
您需要登录后才可以回帖 登录 | 注册

随便看看
查找数据手册?

EEWorld Datasheet 技术支持

相关文章 更多>>
关闭
站长推荐上一条 1/10 下一条

 
EEWorld订阅号

 
EEWorld服务号

 
汽车开发圈

About Us 关于我们 客户服务 联系方式 器件索引 网站地图 最新更新 手机版

站点相关: 国产芯 安防电子 汽车电子 手机便携 工业控制 家用电子 医疗电子 测试测量 网络通信 物联网

北京市海淀区中关村大街18号B座15层1530室 电话:(010)82350740 邮编:100190

电子工程世界版权所有 京B2-20211791 京ICP备10001474号-1 电信业务审批[2006]字第258号函 京公网安备 11010802033920号 Copyright © 2005-2024 EEWORLD.com.cn, Inc. All rights reserved
快速回复 返回顶部 返回列表