您好,欢迎访问三七文档
当前位置:首页 > 商业/管理/HR > 公司方案 > 基于Proteus的单片机串口通信仿真
Proteus班级:电信13-2姓名:段学亮邓成智崔俊杰邓石磊陈亮高金玉成绩:基于Proteus的串口通信仿真电子与信息工程学院信息与通信工程系11.设计要求1.1甲单片机向乙单片机机发送控制命令字符,甲单片机同时接收乙单片机机发送的数字,并显示在数码管上。1.2乙机程序接收甲机发送字符并完成相应动作乙机接收到甲机发送的信号后,根据相应信号控制LED完成不同闪烁动作。2.仿真电路图串口通信仿真电路图如图一图1:串口通信仿真电路图3.串口通信C51程序/*名称:甲机串口程序说明:甲机向乙机发送控制命令字符,甲机同时接收乙机发送的数字,并显示在数码管上。*/XTAL218XTAL119ALE30EA31PSEN29RST9P0.0/AD039P0.1/AD138P0.2/AD237P0.3/AD336P0.4/AD435P0.5/AD534P0.6/AD633P0.7/AD732P2.7/A1528P2.0/A821P2.1/A922P2.2/A1023P2.3/A1124P2.4/A1225P2.5/A1326P2.6/A1427P1.01P1.12P1.23P1.34P1.45P1.56P1.67P1.78P3.0/RXD10P3.1/TXD11P3.2/INT012P3.3/INT113P3.4/T014P3.7/RD17P3.6/WR16P3.5/T11580C51XTAL218XTAL119ALE30EA31PSEN29RST9P0.0/AD039P0.1/AD138P0.2/AD237P0.3/AD336P0.4/AD435P0.5/AD534P0.6/AD633P0.7/AD732P2.7/A1528P2.0/A821P2.1/A922P2.2/A1023P2.3/A1124P2.4/A1225P2.5/A1326P2.6/A1427P1.01P1.12P1.23P1.34P1.45P1.56P1.67P1.78P3.0/RXD10P3.1/TXD11P3.2/INT012P3.3/INT113P3.4/T014P3.7/RD17P3.6/WR16P3.5/T11580C51T1IN11R1OUT12T2IN10R2OUT9T1OUT14R1IN13T2OUT7R2IN8C2+4C2-5C1+1C1-3VS+2VS-6U3MAX232T1IN11R1OUT12T2IN10R2OUT9T1OUT14R1IN13T2OUT7R2IN8C2+4C2-5C1+1C1-3VS+2VS-6U4MAX232234567891LED1LED3LED4R3220R4220LED2C11nF162738495C21nF1uFC41uFC51nFC61nF162738495C71uFX1X2RSTX1X2RSTX2X147010kRSTVCC10uVCCVCC2#includereg51.h#defineucharunsignedchar#defineuintunsignedintsbitLED1=P1^0;sbitLED2=P1^3;sbitK1=P1^7;ucharOperation_No=0;//操作代码//数码管代码ucharcodeDSY_CODE[]={0x3f,0x06,0x5b,0x4f,0x66,0x6d,0x7d,0x07,0x7f,0x6f};//延时voidDelayMS(uintms){uchari;while(ms--)for(i=0;i120;i++);}//向串口发送字符voidPutc_to_SerialPort(ucharc){SBUF=c;while(TI==0);TI=0;}//主程序voidmain(){LED1=LED2=1;P0=0x00;SCON=0x50;//串口模式1,允许接收TMOD=0x20;//T1工作模式2PCON=0x00;//波特率不倍增TH1=0xfd;TL1=0xfd;TI=RI=0;TR1=1;IE=0x90;//允许串口中断while(1){DelayMS(100);if(K1==0)//按下K1时选择操作代码0,1,2,3{while(K1==0);Operation_No=(Operation_No+1)%4;switch(Operation_No)//根据操作代码发送A/B/C或停止发送3{case0:Putc_to_SerialPort('X');LED1=LED2=1;break;case1:Putc_to_SerialPort('A');LED1=~LED1;LED2=1;break;case2:Putc_to_SerialPort('B');LED2=~LED2;LED1=1;break;case3:Putc_to_SerialPort('C');LED1=~LED1;LED2=LED1;break;}}}}//甲机串口接收中断函数voidSerial_INT()interrupt4{if(RI){RI=0;if(SBUF=0&&SBUF=9)P0=DSY_CODE[SBUF];elseP0=0x00;}}/*名称:乙机程序接收甲机发送字符并完成相应动作说明:乙机接收到甲机发送的信号后,根据相应信号控制LED完成不同闪烁动作。*/#includereg51.h#defineucharunsignedchar#defineuintunsignedintsbitLED1=P1^0;sbitLED2=P1^3;sbitK2=P1^7;ucharNumX=-1;//延时voidDelayMS(uintms){uchari;while(ms--)for(i=0;i120;i++);}4//主程序voidmain(){LED1=LED2=1;SCON=0x50;//串口模式1,允许接收TMOD=0x20;//T1工作模式2TH1=0xfd;//波特率9600TL1=0xfd;PCON=0x00;//波特率不倍增RI=TI=0;TR1=1;IE=0x90;while(1){DelayMS(100);if(K2==0){while(K2==0);NumX=++NumX%11;//产生0~10范围内的数字,其中10表示关闭SBUF=NumX;while(TI==0);TI=0;}}}voidSerial_INT()interrupt4{if(RI)//如收到则LED则动作{RI=0;switch(SBUF)//根据所收到的不同命令字符完成不同动作{case'X':LED1=LED2=1;break;//全灭case'A':LED1=0;LED2=1;break;//LED1亮case'B':LED2=0;LED1=1;break;//LED2亮case'C':LED1=LED2=0;//全亮}}}4.交通灯仿真效果4.1按下甲单片机的按钮向单片机乙发送数据,再松开甲单片机的按钮,此时甲单片机的LED1和乙单片机的LED3亮如下图4-1。54-1:甲机的LED1和乙机的LED3分别点亮4.2再次按下甲单片机的按钮向单片机乙发送数据,再松开甲单片机的按钮,此时甲单片机的LED2和乙单片机的LED4亮如下图4-2。XTAL218XTAL119ALE30EA31PSEN29RST9P0.0/AD039P0.1/AD138P0.2/AD237P0.3/AD336P0.4/AD435P0.5/AD534P0.6/AD633P0.7/AD732P2.7/A1528P2.0/A821P2.1/A922P2.2/A1023P2.3/A1124P2.4/A1225P2.5/A1326P2.6/A1427P1.01P1.12P1.23P1.34P1.45P1.56P1.67P1.78P3.0/RXD10P3.1/TXD11P3.2/INT012P3.3/INT113P3.4/T014P3.7/RD17P3.6/WR16P3.5/T11580C51XTAL218XTAL119ALE30EA31PSEN29RST9P0.0/AD039P0.1/AD138P0.2/AD237P0.3/AD336P0.4/AD435P0.5/AD534P0.6/AD633P0.7/AD732P2.7/A1528P2.0/A821P2.1/A922P2.2/A1023P2.3/A1124P2.4/A1225P2.5/A1326P2.6/A1427P1.01P1.12P1.23P1.34P1.45P1.56P1.67P1.78P3.0/RXD10P3.1/TXD11P3.2/INT012P3.3/INT113P3.4/T014P3.7/RD17P3.6/WR16P3.5/T11580C51T1IN11R1OUT12T2IN10R2OUT9T1OUT14R1IN13T2OUT7R2IN8C2+4C2-5C1+1C1-3VS+2VS-6U3MAX232T1IN11R1OUT12T2IN10R2OUT9T1OUT14R1IN13T2OUT7R2IN8C2+4C2-5C1+1C1-3VS+2VS-6U4MAX232234567891LED1LED3LED4R3220R4220LED2C11nF162738495C21nF1uFC41uFC51nFC61nF162738495C71uFX1X2RSTX1X2RSTX2X147010kRSTVCC10uVCCVCC64-2:甲机的LED2和乙机的LED4分别点亮4.3再次按下甲单片机的按钮向单片机乙发送数据,再松开甲单片机的按钮,此时甲单片机的LED1、LED2和乙单片机的LED3、LED4亮如下图4-3。XTAL218XTAL119ALE30EA31PSEN29RST9P0.0/AD039P0.1/AD138P0.2/AD237P0.3/AD336P0.4/AD435P0.5/AD534P0.6/AD633P0.7/AD732P2.7/A1528P2.0/A821P2.1/A922P2.2/A1023P2.3/A1124P2.4/A1225P2.5/A1326P2.6/A1427P1.01P1.12P1.23P1.34P1.45P1.56P1.67P1.78P3.0/RXD10P3.1/TXD11P3.2/INT012P3.3/INT113P3.4/T014P3.7/RD17P3.6/WR16P3.5/T11580C51XTAL218XTAL119ALE30EA31PSEN29RST9P0.0/AD039P0.1/AD138P0.2/AD237P0.3/AD336P0.4/AD435P0.5/AD534P0.6/AD633P0.7/AD732P2.7/A1528P2.0/A821P2.1/A922P2.2/A1023P2.3/A1124P2.4/A1225P2.5/A1326P2.6/A1427P1.01P1.12P1.23P1.34P1.45P1.56P1.67P1.78P3.0/RXD10P3.1/TXD11P3.2/INT012P3.3/INT113P3.4/T014P3.7/RD17P3.6/WR16P3.5/T11580C51T1IN11R1OUT12T2IN10R2OUT9T1OUT14R1IN13T2OUT7R2IN8C2+4C2-5C1+1C1-3VS+2VS-6U3MAX232T1IN11R1OUT12T2IN10R2OUT9T1OUT14R1IN13T2OUT7R2IN8C2+4C2-5C1+1C1-3VS+2VS-6U4MAX232234567891LED1LED3LED4R3220R4220LED2C11nF162738495C21nF1uFC41uFC51nFC61nF162738495C71uFX1X2RSTX1X2RSTX2X147010kRSTVCC10uVCCVCC74-3:甲机的LED1、LED2和乙机的LED3、LED4全部点亮4.4按下乙单片机的按钮向单片机甲发送数据,再松开乙单片机的按钮,此时甲单片机的数码
本文标题:基于Proteus的单片机串口通信仿真
链接地址:https://www.777doc.com/doc-4539614 .html