打造国内最大的IC交易平台
技术资料 行业资讯 PDF资料 IC价格 IC替换 缩略语 IC供应 IC采购
用软件仿真串口
类别:电子综合  
 
;*******************************************************************************

;DuplexUARTRoutinesforthe8xC751and8xC752Microcontrollers

;*******************************************************************************

 

;ThisisademoprogramshowingawaytoperformsimultaneousRS-232

;transmitandreceiveusingonlyoneHARDWAREtimer.

;Thetransmitandreceiveroutinesdivideeachbittimeinto4slicesto

;allowsynchronizingtoincomingdatathatmaybeoutofsynchwithoutgoing

;data.

;Themainprogramloopinthisdemoprocessesreceiveddataandsendsit

;BACKtothetransmitterinhexadecimalformat.Thisinsuresthatwecan

;alwaysfillupthereceiverbuffer(sincethereturneddataislongerthan

;thereceiveddata)forTESTingpurposes.Example:iftheletter"A"is

;received,wewillecho"A41".

;*******************************************************************************

 

;$Title(DuplexUARTRoutinesforthe751/752)

;$Date(8/20/92)

;$MOD751

 

;*******************************************************************************

;Definitions

;*******************************************************************************

 

;Miscellaneous

TxBitLenEQU-4+1;Timerslicesperserialbittransmit.

RxBitLenEQU-4+1;Timerslicesperserialbitreceive.

RxHalfBitEQU(RxBitLen/4)+1;Timerslicesforapartialbittime.

;Usedtoadjusttheinputsampling

;timepoint.

;Note:TxBitLenandRxBitLenarekeptseparateinordertofacilitatethe

;possibilityofhavingdifferenttransmitandreceivebaudrates.Thetimer

;wouldbesetuptogivefourslicesforthefasTESTbaudrate,andthe

;BitLenfortheslowerchannelwouldbesetlongerfortheslowerbaudrate.

;BitLen=-4+1givesfourtimerinterruptsperbit.BitLen=-8+1would

;give8slices,BitLen=-16+1wouldgive16slices,etc.

TxPinBITP1.0;RS-232transmitpin(output).

RxPinBITP3.2;RS-232receivepin(input).

RTSBITP1.3;RS-232requesttosendpin(output).

CTSBITP1.6;RS-232cleartosendpin(input).

;Note:P1.1andP1.2areusedtoinputthebaudrateselection.

 

;RAMLocations

FlagsDATA20h;Miscellaneousbitflags(seebelow).

TxOnBITFlags.0;Indicatestransmitterison(busy).

RxOnBITFlags.1;Indicatesreceiverison(busy).

TxFullBITFlags.2;Transmitbuffer(1byteonly)isfull.

RxFullBITFlags.3;Receiverbufferisfull.

RxAvailBITFlags.4;RXbufferisnotempty.

OverrunErrBITFlags.6;Overrunerrorflag.

FramingErrBITFlags.7;Framingerrorflag.

BaudHighDATA21h;Highbytetimervalueforbaudrate.

BaudLowDATA22h;Lowbytetimervalueforbaudrate.

TxCntDATA23h;RS-232bytetransmitbitcounter.

TxTimeDATA24h;RS-232transmittimeslicecount.

TxShiftDATA25h;Transmittershiftregister.

TxDatDATA26h;Transmitterholdingregister.

RxCntDATA27h;RS-232bytereceivebitcounter.

RxTimeDATA28h;RS-232receivetimeslicecount.

RxShiftDATA29h;Receivershiftregister.

RxDatCntDATA2Ah;Receivedbytecount.

RxBufDATA2Bh;Receivebuffer(3byteslong).

TempDATA2Fh;Temporaryholdingregister.

RTHDATA40H

RTLDATA41H

 

;*******************************************************************************

;InterruptVectors

;*******************************************************************************

ORG00h;Resetvector.

AJMPRESET

 

ORG03h;Externalinterrupt0

AJMPIntr0;(receivedRS-232startbit).

 

ORG0Bh;Timer0overflowinterrupt.

AJMPTimer0;(4XtheRS-232bitrate).

 

ORG13h;Externalinterrupt1.

RETI;(notused).

ORG1Bh;TimerIinterrupt.

RETI;(notused).

ORG23h;I2Cinterrupt.

RETI;(notused).

 

;*******************************************************************************

;InterruptHandlers

;*******************************************************************************

 

;ExternalInterruptInt0.

;RS-232startbittransition.

Intr0:PUSHACC;Saveaccumulator,

PUSHPSW;andstatus.

CLRIE.0;DisablemoreRXinterrupts.

SETBRxOn;Setreceiveactiveflag.

MOVRxCnt,#11h;Setbitcountertoexpectastart.

MOVA,#RXHALFBIT

MOVRxTime,#RxHalfBit;FirstSAMPLEisatapartialbittime.

JBTxOn,I0TimerOn;IfTXactivethentimerison.

MOVRTH,BaudHigh;Setuptimerforselectedbaudrate.

MOVRTL,BaudLow

MOVTH0,BaudHigh

MOVTL0,BaudLow

SETBTR0;Starttimer0.

I0TimerOn:MOVA,RxDatCnt;Checkforbufferabouttobefull:

CJNEA,#2,Int0Ex;onespaceleftandabytestarting.

SETBRTS;Ifso,tellwhoeverisonthe

;otherendtowait.

Int0Ex:POPPSW;Restorestatus,

POPACC;andaccumulator.

RETI

 

;Timer0Interrupt

;Thisisusedtogeneratetimeslicesforbothserialtransmitandreceive

;functions.

Timer0:PUSHACC;Saveaccumulator,

PUSHPSW;andstatus.

JNBTxTime.7,RS232TX;Isthisanactivetimeslice

;foranRS-232transmit?

JNBTxOn,CheckRx;Iftransmitisactive,

INCTxTime;incrementthetimeslicecount.

CheckRx:JNBRxTime.7,RS232RX;Isthisanactivetimeslice

;foranRS-232receive?

JNBRxOn,T0Ex;Ifreceiveisactive,increment

INCRxTime;thetimeslicecount.

MOVA,RXTIME

T0Ex:POPPSW;Restorestatus,

POPACC;andaccumulator.

;MOVP3,Flags;Fordemopurposes,outputstatus

;onanextraPORT.

RETI

 

 

;*******************************************************************************

;RS-232TransmitRoutine

;*******************************************************************************

 

RS232TX:JNBTxCnt.4,TxData;Goifdatabit.

JNBTxCnt.0,TxStop;Goifstopbit.

 

;Sendstartbitanddobufferhousekeeping.

TxStart:JBCTS,TxEx1;IsCTSasserted(low)socanwesend?

;Ifnot,tryagainafter1bittime.

CLRTxPin;Setstartbit.

MOVTxShift,TxDat;Getbytetotransmitfrombuffer.

CLRTxFull

MOVTxCnt,#08h;Initbitcountfor8bitsofdata.

;(note:countsUP).

TxEx1:MOVTxTime,#TxBitLen;Resettimeslicecount.

SJMPCheckRx;Restorestateandexit.

 

;SendNextDataBit.

TxData:MOVA,TxShift;Getun-transmittedbits.

RRCA;ShiftnextTXbittocarry.

MOVTxPin,C;MovecarryouttotheTXDpin.

MOVTxShift,A;SavebitsstilltobeTX'd.

INCTxCnt;IncrementTXbitcounter

MOVTxTime,#TxBitLen;Resettimeslicecount.

SJMPCheckRx;Restorestateandexit.

 

;SendStopBitandCheckforMoretoSend.

TxStop:SETBTxPin;Sendstopbit.

JBTxFull,TxEx2;Moredatatotransmit?

CLRTxOn;Ifnot,turnoffTXactiveflag,and

CLRRTS;makesurethatwhoeverisonthe

;otherendknowsit'sOKtosend.

JBRxOn,TxEx2;Ifreceiveactive,timerstayson,

CLRTR0;otherwiseturnofftimer.

TxEx2:MOVTxCnt,#11h;SetTXbitcounterforastart.

MOVTxTime,#TxBitLen-1;Resettimeslicecount,stopbit

;>1bittimeforsynch.

SJMPCheckRx;Restorestateandexit.

 

 

;*******************************************************************************

;RS-232ReceiveRoutine

;*******************************************************************************

 

RS232RX:MOVC,RxPin;Getcurrentserialbitvalue.

JNBRxCnt.4,RxData;Goifdatabit.

JNBRxCnt.0,RxStop;Goifstopbit.

 

;Verifystartbit.

RxStart:JCRxErr;Ifbit=1,thennotavalidstart.

MOVRxCnt,#08h;Initcountertoexpectdata.

MOVA,#RXBITLEN

MOVRxTime,#RxBitLen;Resettimeslicecount.

SJMPT0Ex;Restorestateandexit.

 

;GetNextDataBit.

RxData:MOVA,RxShift;Getpartialreceivedbyte.

RRCA;Shiftinnewreceivedbit.

MOVRxShift,A;Storepartialresultinbuffer.

INCRxCnt;Incrementreceivedbitcount.

MOVRxTime,#RxBitLen;Resettimeslicecount.

SJMPT0Ex;Restorestateandexit.

 

;StoreDataByte,"push"ingitintotheFIFObuffer.

RxStop:CLREA;Don'tinterruptthefollowing.

MOVA,RxBuf;"PUSH"thereceivebuffer.

XCHA,RxBuf+1

XCHA,RxBuf+2

MOVRxBuf,RxShift;Addjustcompleteddatatobuffer.

INCRxDatCnt;Incrementthereceivedbytecount.

SETBEA;Re-enableinterrupts.

SETBRxAvail;ThereisdataintheRXbuffer.

PUSHPSW;SaveCarry(receivedbit)forlater.

MOVA,RxDatCnt;Checkreceiverbufferstatus.

CJNEA,#4,RxChk1;IsRXbufferoverrun?

SETBOverrunErr;Setstatusregoverrunerrorflag.

MOVRxDatCnt,#3;Re-setbuffercounterto"full".

RxChk1:CJNEA,#3,RxChk2;IsRXbufferfull?

SETBRxFull;Setbufferfullstatus.

RxChk2:POPPSW;RetrievelastreceivedbitinCarry.

JCRxEx;Ifbit=0,thennotavalidstop.

RxErr:SETBFramingErr;Rememberbadstartorstopstatus.

RxEx:JBTxOn,RxTimerOn;Iftransmitactive,timerstayson,

CLRTR0;otherwiseturntimeroff.

RxTimerOn:CLRRxOn;Turnoffreceiveactive.

SETBRxTime.7;Setbitfornoserviceto

;RXTimeSliceBranches.

SETBIE.0;Re-enableRS-232receiveinterrupts.

AJMPT0Ex;Restorestateandexit.

 

;*******************************************************************************

;Subroutines

;*******************************************************************************

 

;BaudRate-Determineandsetthebaudratefromswitches.

;Note:ifthebaudrateisaltered,theactualchangewillonlyoccurwhen

;atransmitorreceiveisbegunwhilethetimerwasnotalreadyrunning

;(i.e.:notalreadybusytransmittingorreceiving).

BaudRate:MOVDPTR,#BaudTable;Setpointertobaudratetable.

ANLA,#03h;Limitdisplacementforlookup.

RLA;Doublethetableindexsincethese

;are2byteentries.

PUSHACC;Savethetableindexforsecondbyte.

MOVCA,@A+DPTR;Getfirstbyte,andsaveasthehigh

MOVBaudHigh,A;byteofthebaudratetimervalue.

POPACC;GetBACKthetableindex.

INCA;Advancetonexttableentry.

MOVCA,@A+DPTR;Getsecondbyte,andsaveasthelow

MOVBaudLow,A;byteofthebaudratetimervalue.

RET

 

;EntriesinBaudTableareforatimersettingof1/4ofabittimeatthegiven

;baudrate.Thetwovaluesperentryarethehighandlowbytesofthevalue

;respectively.

;Valuesarecalculatedasfollows:

;OscFrequency

;1/4Bitcelltime(inmachinecycles)=-----------------

;BaudRate*48

;Examplefor9600baudwitha16MHZcrystal:

;16,000,000/9600*48=34.7222...machinecyclesperquarterbittime.

;Rounded,thisis35.Thehexadecimalvaluefor35is23.

;10000hex-23hex(truncatedto16bits)=FFDD.Thus,theBaudTableentry

;for9600baudisFF,DDhex.

BaudTable:DB0FEh,0EAh;1200baud.

DB0FFh,75h;2400baud.

DB0FFh,0BBh;4800baud.

DB0FFh,0DDh;9600baud.

 

;TxSend-InitiateRS-232Transmit.

TxSend:JBTxFull,$;MakesureTXbufferisfree.

SETBTxFull;Reservethebufferforouruse.

MOVTxDat,A;Putcharacterinbuffer.

JBTxOn,TSTimerOn;Exitiftransmitteralreadyrunning.

SETBTxOn;Transmitactiveflagset.

MOVTxCnt,#11h;Initbitcountertoexpectastart.

MOVTxTime,#TxBitLen;Resettimeslicecount.

JBRxOn,TSTimerOn;Exitifreceiveralreadyactive.

MOVRTH,BaudHigh;Setuptimerforselectedbaudrate.

MOVRTL,BaudLow

MOVTH0,BaudHigh

MOVTL0,BaudLow

SETBTR0;Startupthebittimer.

TSTimerOn:RET

 

;PrByte-OutputabyteasASCIIhexadecimalformat.

PrByte:PUSHACC;PrintACCcontentsasASCIIhex.

SWAPA

ACALLHexAsc;Printuppernibble.

ACALLTxSend

POPACC

ACALLHexAsc;Printlowernibble.

ACALLTxSend

RET

 

;HexAsc-ConvertahexadecimalnibbletoitsASCIIcharacterequivalent.

HexAsc:ANLA,#0Fh;Makesurewe'reworkingwithonly

;onenibble.

CJNEA,#0Ah,HA1;TESTvaluerange.

HA1:JCHAVal09;Valueis0to9.

ADDA,#7;ValueisAtoF,needspre-adjustment.

HAVal09:ADDA,#'0';AdjustvaluetoASCIIhex.

RET

 

;GetRx-Retrieveabytefromthereceivebuffer,andreturnitinA.

GetRx:CLREA;Makesurethisisn'tinterrupted.

DECRxDatCnt;Decrementthebuffercount.

MOVA,RxDatCnt;Getbuffercount.

JNZGRX1;TESTforemptyreceivebuffer.

CLRRxAvail;Ifempty,cleardataavailablestatus.

GRX1:ADDA,#RxBuf;Createapointertoendofbuffer.

MOVTemp,R0;SaveR0.

MOVR0,A;Putpointerwherewecanindirect.

MOVA,@R0;Getlastbufferdata.

MOVR0,Temp;RestoreR0.

CLRRxFull;Buffercan'tbefullanymore.

SETBEA;Re-enableinterrupts.

RET

 

;*******************************************************************************

;Reset

;*******************************************************************************

Reset:MOVSP,#50h;Initializestackstart.

MOVTCON,#0;Settimeroff,INT0toleveltrigger.

MOVP2,#0H;Turnoffallstatusoutputs.

CLRP3.2;MYADD

;Forthisdemo,weonlysetupthebaudrateonceatreset:

MOVA,P1;ReadbaudratebitsfromP1.

RRA;Theswitchesareonbits2and1.

ACALLBaudRate;Setuptheselectedbaudrate.

MOVFLAGS,#0;Initallstatusflags.

MOVRxDatCnt,#0;Clearbuffercount.

MOVIE,#93h;Turnontimer0interruptand

;externalinterrupt0.

CLRRTS;AssertRTSsowecanreceive.

 

;ThemainprogramloopprocessesreceiveddataandsendsitBACKtothe

;transmitterinhexadecimalformat.Thisinsuresthatwecanalwaysfill

;upthereceiverbuffer(sincethereturneddataislongerthanthe

;receiveddata)forTESTingpurposes.Example:iftheletter"A"is

;received,wewillecho"A41".

MainLoop:JNBRxAvail,$;Makesureaninputbyteisavailable.

ACALLGetRx;Getdatafromthereceiverbuffer.

ACALLTxSend;Echooriginalcharacter.

ACALLPrByte;Outputthecharinhexadecimalformat,

MOVA,#20h;followedbyaspace.

ACALLTxSend

SJMPMainLoop;Repeat.

END