资源描述:
《单片机课件 串口编程》由会员上传分享,免费在线阅读,更多相关内容在行业资料-天天文库。
1、串口编程1、串行口方式1应用编程补充例1本例采用方式1进行双机通信,收发双方均采用6MHz晶振,波特率为2400,每一帧信息为10位,第0位为起始位,1~8位为数据位,最后1位为停止位。发送方将78H、77H单元的内容为首地址,以76H、75H单元内容减1为末地址的数据块内容通过串行口发送给接收方。发送方要发送的数据块的地址为2000H~201FH。发送时先发送地址帧,再发送数据帧;接收方在接收时,使用一个标志位来区分接收的数据是地址还是数据,然后将其分别存放在指定的单元中。发送方可采用查询方式或中断方式发送数据,接收方采用中断或查询方式接收。本例子仅给出中断方式的发送
2、和接收的程序。1)甲机发送程序中断方式的发送程序如下:ORG0000HLJMPMAINORG0023HLJMPCOM_INTORG1000HMAIN:MOVSP,#53H;setstocktoppointerMOV78H,#20H;setsendingdatafirst-bottomaddressMOV77H,#00HMOV76H,#20HMOV75H,#40HACALLTRANS;callsendprogSJMP$TRANS:MOVTMOD,#20H;settimer/counterworkingmodelMOVTH1,#0F3H;setcounterinitialv
3、alueMOVTL1,#0F3HMOVPCON,#80H;setbaudratedoubleSETBTR1;opencounterMOVSCON,#40H;setserialworkingmodelMOVIE,#00H;Firstcloseinterrupt,usequerymodelsendingaddressCLRF0MOVSBUF,78H;sendfirst-addresshigh8bitWAIT1:JNBTI,WAIT1CLRTIMOVSBUF,77H;sendfirst-addresslow8bitWAIT2:JNBTI,WAIT2CLRTIMOVSBUF,7
4、6H;sendend-addresshigh8bitWAIT3:JNBTI,WAIT3CLRTIMOVSBUF,75H;sendend-addresslow8bitWAIT4:JNBTI,WAIT4CLRTIMOVIE,#90H;openinterrupt,interruptmodelsenddataMOVDPH,78HMOVDPL,77HMOVXA,@DPTRMOVSBUF,A;sendfirstdataWAIT:JNBF0,WAIT;waitsendingRETCOM_INT:CLRTI;closesending-interruptflagbitTIINCDPTR;
5、datapointeradd1,intendsendingnextdataMOVA,DPH;determinecurrentsendingdataaddresswetherornotbottomaddressCJNEA,76H,END1;nobottomaddress,jumpMOVA,DPLCJNEA,75H,END1SETBF0;datasendingend,setF0equ1CLRES;closeserialinterruptCLREA;closeinterruptRETEND1:MOVXA,@DPTR;waitsendingdatagiveA,intendtos
6、endMOVSBUF,A;senddataRETIEND2)乙机接收程序中断方式的接收程序如下:ORG0000HLJMPMAINORG0023HLJMPCOM_INTORG1000HMAIN:MOVSP,#53H;setstocktoppointerACALLRECEISJMP$RECEI:MOVR0,#78HMOVTMOD,#20HMOVTH1,#0F3H;setcounterinitialvalueMOVTL1,#0F3HMOVPCON,#80H;setbaudratedoubleSETBTR1;opencounterMOVSCON,#50H;setserialwo
7、rkingmodelMOVIE,#90H;openinterruptCLRF0;clearflagF0CLR7FHWAIT:JNB7F,WAIT;queryflag-bit,waitreceivedataRETCOM_INT:PUSHDPL;保护现场PUSHDPHPUSHACCCLRRI;清接收中断RIJBF0,R_DATA;判断接收的是数据还是地址,F0=0为地址MOVA,SBUF;接收数据MOV@R0,A;将地址帧送指定的寄存器DECR0CJNER0,#74H,RETNSETBF0;置标志位,地址接收完毕RETN:POPACC;出栈P