欢迎来到天天文库
浏览记录
ID:56818873
大小:43.00 KB
页数:6页
时间:2020-07-13
《多机通信(主从机程序成功).doc》由会员上传分享,免费在线阅读,更多相关内容在教育资源-天天文库。
1、.//---------------------------------------------------------------------------------------主机程序//---------------------------------------------------------------------------------------#include#defineucharunsignedchar#defineuintunsignedint#defineslave0x0
2、3#defineBN4ucharcodetbuf[BN]={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(u
3、nsignedchardata1){SBUF=data1;//将待发送的字符送入发送缓冲器while(!TI);//等待发送完成TI=0;//发送中断标志请0}/*//向串口发送一个字符串,strlen为该字符串长度voidsend(uchar*str,uintstrlen){ucharp=0;uintk;do..{for(k=0;k4、/voidsend()//发送数据并校验和{uchari;uintp;do{p=0;for(i=0;i5、I=0;if(SBUF!=add)error();else..{TB8=0;send_date(command);while(!RI);RI=0;a=SBUF;if((a&0x80)==0x80)error();else{if((a&0x01)==0x01)//从机准备好接收并主机发送数据{P1=0xef;send(/*tbuf,BN*/);TB8=1;}}}}}voidmain(){init();chuankou(slave,0x01);while(1);}//---------------------------6、----------------------------------------------------------------------从机程序//--------------------------------------------------------------------------------------------------#include#defineucharunsignedchar#defineuintunsignedint#defineslave0x03#defineB7、N4ucharcodetab[]={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=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;RE8、N=1;EA=1;ES=1;P2=0xff;}voiderror()//错误{SM2=1;ES=1;}voidsend_date(unsignedchardata1)//发送字符{SBUF=data1;//将待发送的字符送入发送缓冲器while(!TI);//等待发送完成TI=0;//发送中断标志请0}voidreceive()//接收并校验和
4、/voidsend()//发送数据并校验和{uchari;uintp;do{p=0;for(i=0;i5、I=0;if(SBUF!=add)error();else..{TB8=0;send_date(command);while(!RI);RI=0;a=SBUF;if((a&0x80)==0x80)error();else{if((a&0x01)==0x01)//从机准备好接收并主机发送数据{P1=0xef;send(/*tbuf,BN*/);TB8=1;}}}}}voidmain(){init();chuankou(slave,0x01);while(1);}//---------------------------6、----------------------------------------------------------------------从机程序//--------------------------------------------------------------------------------------------------#include#defineucharunsignedchar#defineuintunsignedint#defineslave0x03#defineB7、N4ucharcodetab[]={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=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;RE8、N=1;EA=1;ES=1;P2=0xff;}voiderror()//错误{SM2=1;ES=1;}voidsend_date(unsignedchardata1)//发送字符{SBUF=data1;//将待发送的字符送入发送缓冲器while(!TI);//等待发送完成TI=0;//发送中断标志请0}voidreceive()//接收并校验和
5、I=0;if(SBUF!=add)error();else..{TB8=0;send_date(command);while(!RI);RI=0;a=SBUF;if((a&0x80)==0x80)error();else{if((a&0x01)==0x01)//从机准备好接收并主机发送数据{P1=0xef;send(/*tbuf,BN*/);TB8=1;}}}}}voidmain(){init();chuankou(slave,0x01);while(1);}//---------------------------
6、----------------------------------------------------------------------从机程序//--------------------------------------------------------------------------------------------------#include#defineucharunsignedchar#defineuintunsignedint#defineslave0x03#defineB
7、N4ucharcodetab[]={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=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;RE
8、N=1;EA=1;ES=1;P2=0xff;}voiderror()//错误{SM2=1;ES=1;}voidsend_date(unsignedchardata1)//发送字符{SBUF=data1;//将待发送的字符送入发送缓冲器while(!TI);//等待发送完成TI=0;//发送中断标志请0}voidreceive()//接收并校验和
此文档下载收益归作者所有