总图从机主机从机的程序#ifndef_receive_data_h#define_receive_data_h#includereg52.h#defineucharunsignedchar#defineuintunsignedintucharbuf;ucharaddr;sbitP23=P2^3;#endif#includereceive_data.hvoiddelay(uintt){uinti;while(t--){for(i=0;i125;i++);}}ucharrecvdata(){while(!RI);if(RB8==1)return0xee;buf=SBUF;RI=0;return0;}voidinit(){TMOD=0x20;TH1=250;TL1=250;TR1=1;PCON=0x80;SCON=0xd0;REN=1;//当REN等于0时,禁止接收数据}voidmain(){uchari=0;uchartmp=0xff;P1=0xff;addr=P1&0x0f;P23=1;init();EA=1;while(1){SM2=1;while(tmp!=addr){RI=0;while(!RI);tmp=SBUF;RI=0;}SM2=0;tmp=recvdata();if(tmp==0x00){P0=buf;P23=0;delay(30);P23=1;}}}主机的程序#ifndef_send_data_h#define_send_data_h//防止send_data.h被重复引用#includereg52.h#defineucharunsignedchar#defineuintunsignedint#defineSUCC0x2a//接收成功#defineERR0xf0//接收错误#defineMAXLEN64//缓冲区最大长度ucharbuf;ucharget_key,key,addr;#endif#includesend_data.hvoiddelay(uintt){uinti;while(t--){for(i=0;i125;i++);}}ucharkeyscan(){ucharscancode,tmpcode;P1=0xf0;if((P1&0xf0)!=0xf0){delay(10);if((P1&0xf0)!=0xf0){scancode=0xfe;while((scancode&0x10)!=0){P1=scancode;if((P1&0xf0)!=0xf0){tmpcode=((P1&0xf0)|0x0f);return((~scancode)+(~tmpcode));}elsescancode=(scancode1)|0x01;}}}return0;}voidsenddata(ucharbuf){TI=0;TB8=0;SBUF=buf;while(!TI);TI=0;}voidinit(){TMOD=0X20;TH1=250;TL1=250;TR1=1;PCON=0X80;SCON=0XD0;}voidgetkey(){switch(get_key){case0x11:key=0xc0;break;case0x21:key=0xf9;break;case0x41:key=0xa4;break;case0x81:key=0xb0;break;case0x12:key=0x99;break;case0x22:key=0x92;break;case0x42:key=0x82;break;case0x82:key=0xf8;break;case0x14:key=0x80;break;case0x24:key=0x90;break;case0x44:key=0x88;break;case0x84:key=0x83;break;case0x18:key=0xc6;break;case0x28:key=0xa1;break;case0x48:key=0x86;break;case0x88:key=0x8e;break;default:break;}}voidmain(){uchari=0;key=0xc0;while(1){get_key=keyscan();getkey();buf=key;P0=0xff;addr=P0&0x0f;init();TI=0;TB8=1;SBUF=addr;while(!TI);TI=0;senddata(buf);}}