欢迎来到天天文库
浏览记录
ID:62826328
大小:43.50 KB
页数:6页
时间:2021-06-16
《多机通信主从机程序成功.doc》由会员上传分享,免费在线阅读,更多相关内容在教育资源-天天文库。
1、-//---------------------------------------------------------------------------------------主机程序//---------------------------------------------------------------------------------------#include#defineucharunsignedchar#defineuintunsignedint#defineslave0x03#defineBN4ucharcodetbuf[BN]
2、={0x3f,0x5b,0x66,0x7d};//ucharcodetab[]={0x3F,0x06,0x5B,0x4F,0x66,0x6D,0x7D,0x07,0x7F,0x6F};voidinit()//初始化{TR1=1;TH1=0xf3;TL1=0xf3;TMOD=0x20;SM0=0;SM1=1;SM2=1;REN=1;}voiderror()//错误{TB8=1;SBUF=0xf0;while(TI!=1);TI=0;}voidsend_date(unsignedchardata1){SBUF=data1;//将待发送的字符送入发送缓冲器while(!TI);
3、//等待发送完成TI=0;//发送中断标志请0}/*//向串口发送一个字符串,strlen为该字符串长度voidsend(uchar*str,uintstrlen){ucharp=0;uintk;do---总结.-{for(k=0;k4、p+=tbuf[i];P1=0xf0;while(!RI);RI=0;}send_date(p);P1=0x00;while(!RI);RI=0;}while(SBUF!=0);}voidchuankou(ucharadd,ucharcommand)//串口函数(地址,命令){uchara;while(1){send_date(add);while(!RI);RI=0;if(SBUF!=add)error();else---总结.-{TB8=0;send_date(command);while(!RI);RI=0;a=SBUF;if((a&0x80)==0x80)erro5、r();else{if((a&0x01)==0x01)//从机准备好接收并主机发送数据{P1=0xef;send(/*tbuf,BN*/);TB8=1;}}}}}voidmain(){init();chuankou(slave,0x01);while(1);}//-------------------------------------------------------------------------------------------------从机程序//------------------------------------------------------6、--------------------------------------------#include#defineucharunsignedchar#defineuintunsignedint#defineslave0x03#defineBN4ucharcodetab[]={0x3F,0x06,0x5B,0x4F,0x66,0x6D,0x7D,0x07,0x7F,0x6F};ucharrbuf[BN];bitrready;voiddelay(void)//误差0us{unsignedchara,b,c;for(c=167;c>0;c--)for(b=7、171;b>0;b--)---总结.-for(a=16;a>0;a--);}voidinit()//初始化{TR1=1;TH1=0xf3;TL1=0xf3;TMOD=0x20;SM0=0;SM1=1;SM2=1;REN=1;EA=1;ES=1;P2=0xff;}voiderror()//错误{SM2=1;ES=1;}voidsend_date(unsignedchardata1)//发送字符{SBUF=data1;//将待发送的字符送入发送缓冲器while(!TI);//等待发送完成TI=0;//发送中断标志请0}voidre
4、p+=tbuf[i];P1=0xf0;while(!RI);RI=0;}send_date(p);P1=0x00;while(!RI);RI=0;}while(SBUF!=0);}voidchuankou(ucharadd,ucharcommand)//串口函数(地址,命令){uchara;while(1){send_date(add);while(!RI);RI=0;if(SBUF!=add)error();else---总结.-{TB8=0;send_date(command);while(!RI);RI=0;a=SBUF;if((a&0x80)==0x80)erro
5、r();else{if((a&0x01)==0x01)//从机准备好接收并主机发送数据{P1=0xef;send(/*tbuf,BN*/);TB8=1;}}}}}voidmain(){init();chuankou(slave,0x01);while(1);}//-------------------------------------------------------------------------------------------------从机程序//------------------------------------------------------
6、--------------------------------------------#include#defineucharunsignedchar#defineuintunsignedint#defineslave0x03#defineBN4ucharcodetab[]={0x3F,0x06,0x5B,0x4F,0x66,0x6D,0x7D,0x07,0x7F,0x6F};ucharrbuf[BN];bitrready;voiddelay(void)//误差0us{unsignedchara,b,c;for(c=167;c>0;c--)for(b=
7、171;b>0;b--)---总结.-for(a=16;a>0;a--);}voidinit()//初始化{TR1=1;TH1=0xf3;TL1=0xf3;TMOD=0x20;SM0=0;SM1=1;SM2=1;REN=1;EA=1;ES=1;P2=0xff;}voiderror()//错误{SM2=1;ES=1;}voidsend_date(unsignedchardata1)//发送字符{SBUF=data1;//将待发送的字符送入发送缓冲器while(!TI);//等待发送完成TI=0;//发送中断标志请0}voidre
此文档下载收益归作者所有