实验3串行口应用一、实验目的1.了解串行通信的基本应用2.掌握51单片机通过串行口扩展输出口的工作原理和编程方法3.掌握51单片机串行口工作于UART方式时的工作原理和编程方法二、实验器材1.计算机2.KeilµVision5(C51)软件3.Proteus8Professionalv8.5SP0软件三、实验原理1.串行通信的基本概念2.串行口的结构组成及相关特殊功能寄存器3.串行口的工作方式与控制方法四、实验内容[项目1]主-从多机通信系统程序代码:1)主机:#includereg51.h#defineucharunsignedchar#defineNODE1_ADDR1#defineNODE2_ADDR2ucharKeyValue=0;ucharcodestr[]=0123456789ABCDEF;ucharpointer_1=0,pointer_2=0;voiddelay(uchartime){图3.1.1主-从多机通信系统原理图uchari,j;for(i=0;i130;i++)for(j=0;jtime;j++);}voidproc_key(ucharnode_number){delay(200);SCON=0xc0;TMOD=0x20;TH1=TL1=0xfd;TR1=1;TB8=1;SBUF=node_number;while(TI==0);TI=0;TB8=0;switch(node_number){case1:{SBUF=str[pointer_1++];if(pointer_1=16)pointer_1=0;break;}case2:{SBUF=str[pointer_2++];if(pointer_2=16)pointer_2=0;break;}default:break;while(TI==0);TI=0;}}voidmain(void){while(1){P1=0xff;while(P1==0xff);switch(P1){case0xfe:proc_key(NODE1_ADDR);break;case0xef:proc_key(NODE2_ADDR);break;}}}2)从机1:#includereg51.h#defineNODE1_ADDR1#defineucharunsignedcharsbitP3_7=P3^7;ucharcodetable[16]={0xc0,0xf9,0xa4,0xb0,0x99,0x92,0x82,0xf8,0x80,0x90,0x88,0x83,0xc6,0xa1,0x86,0x8e};voiddisplay(ucharch){if((ch=48)&&(ch=57))P2=table[ch-48];elseif((ch=65)&&(ch=70))P2=table[ch-55];}voidmain(void){SCON=0xf0;TMOD=0x20;TH1=TL1=0xfd;TR1=1;ES=1;EA=1;while(1);}voidreceive(void)interrupt4{RI=0;if(RB8==1){if(SBUF==NODE1_ADDR){SM2=0;P3_7=!P3_7;}return;}display(SBUF);SM2=1;}3)从机2:#includereg51.h#defineNODE2_ADDR2#defineucharunsignedcharsbitP3_7=P3^7;ucharcodetable[16]={0xc0,0xf9,0xa4,0xb0,0x99,0x92,0x82,0xf8,0x80,0x90,0x88,0x83,0xc6,0xa1,0x86,0x8e};voiddisplay(ucharch){if((ch=48)&&(ch=57))P2=table[ch-48];elseif((ch=65)&&(ch=70))P2=table[ch-55];}voidmain(void){SCON=0xf0;TMOD=0x20;TH1=TL1=0xfd;TR1=1;ES=1;EA=1;while(1);}voidreceive(void)interrupt4{RI=0;if(RB8==1){if(SBUF==NODE2_ADDR){SM2=0;P3_7=!P3_7;}return;}display(SBUF);SM2=1;}五、报告要求1.完整阐述相关实验原理。2.实验详细步骤须阐述清楚。3.电路所用元件以列表的形式给出,标号、名称、参数等内容应清楚注明。4.仿真电路图打印后贴于报告相应位置,图纸清晰,大小合适,剪贴规范。5.画出实验内容相应程序代码的流程图。6.程序代码书写工整,注释清楚(可打印)。7.报告结尾附“实验总结”,对相关实验结果、问题进行详细分析。