单片机多机通信主机程序:#includereg52.h/*52系列单片机头文件*/#defineucharunsignedchar/*宏定义*/#defineuintunsignedint#defineBN6ucharrbuf[6];/*接收数据存储数组*/ucharSLAVE;/*从机地址*/ucharcodetbuf[6]={0x12,0x23,0x34,0x45,0x46,0x47};/*待发送的数据*/voiderr(void)/*地址错,发复位信号*/{SBUF=0xff;while(TI!=1);TI=0;}ucharmaster(ucharaddr,ucharcommand)/*发送与接收功能函数*/{ucharaa,i,p;while(1){SBUF=SLAVE;/*发呼叫地址*/while(TI!=1);TI=0;while(RI!=1);RI=0;/*等待从机回答*/if(SBUF!=addr)err();/*若地址错,发复位信号*/else{/*地址相符*/TB8=0;/*清地址标志*/SBUF=command;/*发命令*/while(TI!=1);TI=0;while(RI!=1);RI=0;aa=SBUF;/*接收状态*/if((aa&0x08)==0x08)/*若命令未被接收,发复位信号*/{TB8=1;err();}else{if(command==0x01)/*是发送命令*/{if((aa&0x01)==0x01)/*从机准备好接收*/{do{p=0;/*清校验和*/for(i=0;iBN;i++){SBUF=tbuf[i];/*发送一数据*/p+=tbuf[i];while(TI!=1);TI=0;}SBUF=p;/*发送校验和*/while(TI!=1);TI=0;while(RI!=1);RI=0;}while(SBUF!=0);/*接收不正确,重新发送*/TB8=1;/*置地址标志*/return(0);}else{if((aa&0x02)==0x02)/*是接收命令,从机准备好发送*/{while(1){p=0;/*清校验和*/for(i=0;iBN;i++){while(RI!=1);RI=0;rbuf[i]=SBUF;/*接收一数据*/p+=rbuf[i];}while(RI!=1);RI=0;if(SBUF==p){SBUF=0X00;/*校验和相同发00*/while(TI!=1);TI=0;break;}else{SBUF=0xff;/*校验和不同发0FF,重新接收*/while(TI!=1);TI=0;}}TB8=1;/*置地址标志*/P2=rbuf[1];return(0);}}}}}}}voidmain(){TMOD=0x20;/*T/C1定义为方式2*/TL1=0xfd;TH1=0xfd;/*置初值,波特率9600,晶振11.0592MHz*/PCON=0x00;/*不倍频*/TR1=1;/*启动定时器*/SCON=0xf8;/*串行口为方式3*/SLAVE=0x01;/*1号从机地址*/master(SLAVE,0x01);master(SLAVE,0x02);SLAVE=0x02;/*2号从机地址*/master(SLAVE,0x01);master(SLAVE,0x02);SLAVE=0x03;/*3号从机地址*/master(SLAVE,0x01);master(SLAVE,0x02);while(1);}单片机多机通信从机程序(以01H从机为例):#includereg52.h/*52系列单片机头文件*/#defineucharunsignedchar/*宏定义*/#defineSLAVE0x01/*1号从机地址*/#defineBN6uchartrbuf[6]={0x12,0x13,0x14,0x15,0x16,0x17};/*发送数据存储数组*/ucharrebuf[6];/*接收数据存储数组*/bittready;bitrready;voidstr(void);/*发数据块函数声明*/voidsre(void);/*收数据块函数声明*/voidmain(void){TMOD=0x20;/*T/C1定义为方式2*/TL1=0xfd;TH1=0xfd;/*置初值,波特率9600,晶振11.0592MHz*/PCON=0x00;/*不倍频*/TR1=1;/*启动定时器*/SCON=0xf0;/*串行口为方式3*/ES=1;/*开串口中断*/EA=1;/*开串行口中断*/while(1){tready=1;rready=1;}/*假定准备好发送和接收*/}voidssio(void)interrupt4/*串行口中断*/{uchara;RI=0;ES=0;/*关串行口中断*/if(SBUF!=SLAVE){ES=1;gotoreti;}/*非本机地址,继续监听*/SM2=0;/*取消监听状态*/SBUF=SLAVE;/*从本地址发回*/while(TI!=1);TI=0;while(RI!=1);RI=0;if(RB8==1){SM2=1;ES=1;gotoreti;}/*是复位信号,恢复监听*/a=SBUF;/*接收命令*/if(a==0x01)/*从主机接收的数据*/{if(rready==1)SBUF=0x01;/*接收准备好发状态*/elseSBUF=0x00;while(TI!=1);TI=0;while(RI!=1);RI=0;if(RB8==1){SM2=1;ES=1;gotoreti;}sre();/*接收数据*/}else{if(a==0x02)/*从机向主机发送数据*/{if(tready==1)SBUF=0x02;/*发送准备好发状态*/elseSBUF=0x00;while(TI!=1);TI=0;while(RI!=1);RI=0;if(RB8==1){SM2=1;ES=1;gotoreti;}str();/*发送数据*/}else{SBUF=0x80;/*命令非法,发状态*/while(TI!=1);TI=0;SM2=1;ES=1;/*恢复监听*/}}reti:;}voidstr(void)/*发数据块*/{ucharp,i;tready=0;do{p=0;/*清校验和*/for(i=0;iBN;i++){SBUF=trbuf[i];/*发送一数据*/p+=trbuf[i];while(TI!=1);TI=0;}SBUF=p;/*发送校验和*/while(TI!=1);TI=0;while(RI!=1);RI=0;}while(SBUF!=0);/*主机接收不正确,重新发送*/SM2=1;ES=1;}voidsre(void)/*接收数据块*/{ucharp,i;rready=0;while(1){p=0;/*清校验和*/for(i=0;iBN;i++){while(RI!=1);RI=0;rebuf[i]=SBUF;/*接收数据*/p+=rebuf[i];}while(RI!=1);RI=0;if(SBUF==p){SBUF=0x00;break;}/*校验和相同发00*/else{SBUF=0xff;/*校验和不同发0FF,重新接收*/while(TI==0);TI=0;}}P1=rebuf[1];SM2=1;ES=1;}