資源描述:
《單片機(jī)課件 串口編程》由會(huì)員上傳分享,免費(fèi)在線閱讀,更多相關(guān)內(nèi)容在行業(yè)資料-天天文庫。
1、串口編程1、串行口方式1應(yīng)用編程補(bǔ)充例1本例采用方式1進(jìn)行雙機(jī)通信,收發(fā)雙方均采用6MHz晶振,波特率為2400,每一幀信息為10位,第0位為起始位,1~8位為數(shù)據(jù)位,最后1位為停止位。發(fā)送方將78H、77H單元的內(nèi)容為首地址,以76H、75H單元內(nèi)容減1為末地址的數(shù)據(jù)塊內(nèi)容通過串行口發(fā)送給接收方。發(fā)送方要發(fā)送的數(shù)據(jù)塊的地址為2000H~201FH。發(fā)送時(shí)先發(fā)送地址幀,再發(fā)送數(shù)據(jù)幀;接收方在接收時(shí),使用一個(gè)標(biāo)志位來區(qū)分接收的數(shù)據(jù)是地址還是數(shù)據(jù),然后將其分別存放在指定的單元中。發(fā)送方可采用查詢方式或中斷方式發(fā)送數(shù)據(jù),接收方采用中斷或查詢方式接收。本例子僅給出中斷方式的發(fā)送和接收的程
2、序。1)甲機(jī)發(fā)送程序中斷方式的發(fā)送程序如下:ORG0000HLJMPMAINORG0023HLJMPCOM_INTORG1000HMAIN:MOVSP,#53H;setstocktoppointerMOV78H,#20H;setsendingdatafirst-bottomaddressMOV77H,#00HMOV76H,#20HMOV75H,#40HACALLTRANS;callsendprogSJMP$TRANS:MOVTMOD,#20H;settimer/counterworkingmodelMOVTH1,#0F3H;setcounterinitialvalueMOVTL1
3、,#0F3HMOVPCON,#80H;setbaudratedoubleSETBTR1;opencounterMOVSCON,#40H;setserialworkingmodelMOVIE,#00H;Firstcloseinterrupt,usequerymodelsendingaddressCLRF0MOVSBUF,78H;sendfirst-addresshigh8bitWAIT1:JNBTI,WAIT1CLRTIMOVSBUF,77H;sendfirst-addresslow8bitWAIT2:JNBTI,WAIT2CLRTIMOVSBUF,76H;sendend-addr
4、esshigh8bitWAIT3:JNBTI,WAIT3CLRTIMOVSBUF,75H;sendend-addresslow8bitWAIT4:JNBTI,WAIT4CLRTIMOVIE,#90H;openinterrupt,interruptmodelsenddataMOVDPH,78HMOVDPL,77HMOVXA,@DPTRMOVSBUF,A;sendfirstdataWAIT:JNBF0,WAIT;waitsendingRETCOM_INT:CLRTI;closesending-interruptflagbitTIINCDPTR;datapointeradd1,inte
5、ndsendingnextdataMOVA,DPH;determinecurrentsendingdataaddresswetherornotbottomaddressCJNEA,76H,END1;nobottomaddress,jumpMOVA,DPLCJNEA,75H,END1SETBF0;datasendingend,setF0equ1CLRES;closeserialinterruptCLREA;closeinterruptRETEND1:MOVXA,@DPTR;waitsendingdatagiveA,intendtosendMOVSBUF,A;senddataRETI
6、END2)乙機(jī)接收程序中斷方式的接收程序如下:ORG0000HLJMPMAINORG0023HLJMPCOM_INTORG1000HMAIN:MOVSP,#53H;setstocktoppointerACALLRECEISJMP$RECEI:MOVR0,#78HMOVTMOD,#20HMOVTH1,#0F3H;setcounterinitialvalueMOVTL1,#0F3HMOVPCON,#80H;setbaudratedoubleSETBTR1;opencounterMOVSCON,#50H;setserialworkingmodelMOVIE,#90H;openinter
7、ruptCLRF0;clearflagF0CLR7FHWAIT:JNB7F,WAIT;queryflag-bit,waitreceivedataRETCOM_INT:PUSHDPL;保護(hù)現(xiàn)場(chǎng)PUSHDPHPUSHACCCLRRI;清接收中斷RIJBF0,R_DATA;判斷接收的是數(shù)據(jù)還是地址,F(xiàn)0=0為地址MOVA,SBUF;接收數(shù)據(jù)MOV@R0,A;將地址幀送指定的寄存器DECR0CJNER0,#74H,RETNSETBF0;置標(biāo)志位,地址接收完畢RETN:POPACC;出棧P