;
; ISR for Serial I/O
;
SerIsr  btfsc   P1Stat,RxBusy
	goto    P1busy                  ; in the middle of receiving
	btfss   RegPortb,P1RxIn
	goto    P1RxDone
;
	if DbgComm != 0
	bsf     RegPortb,ByteDbg
	bcf     RegPortb,ByteDbg
	endif
;
	movlw   SerCount                ; Start bit detected
	movwf   P1RxCount
	bsf     P1Stat,RxBusy
	goto    P1RxDone
P1busy  decf    P1RxCount,1
	movf    P1RxCount,0
	andlw   CountMask               ; is it the fourth count?
	JMPNZ   P1RxDone
;
	if DbgComm != 0
	bsf     RegPortb,BitDbg         ; Rising edge on debug line
	bcf     RegPortb,BitDbg         ; Falling edge on debug line
	endif
;
	movf    P1RxCount,0
	xorlw   StartBit                ; is it the start bit?
	JMPNZ   P1Recv1
	btfsc   RegPortb,P1RxIn
	goto    P1RxDone                ; Start bit is 0. Whew!
	bsf     P1Stat,FramErr          ; Framing error. Reset.
	bcf     P1Stat,RxBusy
	goto    P1RxDone
P1Recv1 movf    P1RxCount,0
	xorlw   StopBit                 ; is it the stop bit?
	JMPNZ   P1Recv2                 ; Nope. Just some old data bit.
	btfsc   RegPortb,P1RxIn
	bsf     P1Stat,FramErr          ; Stop bit was 0. Framing error.
	bcf     P1Stat,RxBusy
	movf    P1RxSr,0
	movwf   P1RxByte                ; Byte received
	btfsc   P1Stat,RxRdy
	bsf     P1Stat,RxOver           ; Byte overwritten. Receiver overrun.
	bsf     P1Stat,RxRdy
	goto    P1RxDone
P1Recv2 bcf     RegStatus,BitCarry      ; Assume bit to be 0
	rrf     P1RxSr,1
	btfss	RegPortb,P1RxIn
	bsf     P1RxSr,7                ; and fix it if not
P1RxDone
	btfss   P1Stat,TxBusy
	goto    P1TxDone                ; Not transmitting anything
	decf    P1TxCount,1
	movf    P1TxCount,0
	andlw   CountMask
	JMPNZ   P1TxDone
	movf    P1TxCount,0             ; The fourth tick. Get to work.
	xorlw   StartBit
	JMPNZ   P1Xmit1
	bsf     RegPortb,P1TxOut        ; Send start bit
	goto    P1TxDone
P1Xmit1 movf    P1TxCount,0
	xorlw   StopBit
	JMPNZ   P1Xmit2
	bcf     RegPortb,P1TxOut        ; Send stop bit
	goto    P1TxDone
P1Xmit2 movf    P1TxCount,0
	JMPNZ   P1Xmit3
	bcf     P1Stat,TxBusy
	btfsc	P1Stat,TxRdy            ; Finished transmiting byte
	goto    P1TxDone                ; Jump if nothing in transmit buffer
	movf    P1TxByte,0              ; Set up next byte for transmission
	movwf   P1TxSr
	bsf     P1Stat,TxBusy
	bsf     P1Stat,TxRdy
	movlw   SerCount
	movwf   P1TxCount
	goto    P1TxDone
P1Xmit3 rrf     P1TxSr,1
	btfss   RegStatus,BitCarry
	bsf     RegPortb,P1TxOut
	btfsc   RegStatus,BitCarry
	bcf     RegPortb,P1TxOut
P1TxDone
	return  
;
;
InitPort	bcf     P1Stat,RxRdy            ; Set all status bits
	bcf     P1Stat,RxBusy
	bsf     P1Stat,TxRdy
	bcf     P1Stat,TxBusy
	bcf     P1Stat,RxOver
	bcf     P1Stat,FramErr
	;
	bsf     RegStatus,BitRp0
	bcf     RegTrisb,P1TxOut        ; Set Tx to output
	bsf     RegTrisb,P1RxIn         ; Set Rx to input
	if DbgComm != 0
	bcf     RegTrisb,BitDbg         ; Bit tick for debugging
	bcf     RegTrisb,ByteDbg        ; Byte tick for debugging
	endif
	bcf     RegOption,BitRts        ; Enable RTCC timer
	bcf     RegStatus,BitRp0
	movlw   RtccCount
	movwf   RegRtcc                 ; Load RTCC
	bsf     RegIntcon,BitGie        ; Enable Global interrupt
	bsf     RegIntcon,BitRtie       ; Enable RTCC interrupt
	return
;
;
P1PutC  btfss   P1Stat,TxRdy
	goto    P1PutC                  ; Transmitter busy. keep looping
	btfss   P1Stat,TxBusy
	goto    TxEmpty
	movwf   P1TxByte
	bcf     P1Stat,TxRdy
	goto    PutDone
TxEmpty movwf   P1TxSr
	movlw   SerCount
	movwf   P1TxCount
	bsf     P1Stat,TxBusy
PutDone return
;
;
P1GetC  btfss   P1Stat,RxRdy
	goto    P1GetC                  ; No char received yet
	movf    P1RxByte,0
	bcf     P1Stat,RxRdy
	return


External Labels :