PIC Specific RS232 routine

by Tony Nixon

These simple serial routines may help.19200 baud Fosc = 4MHz Receive can be polled or interrupt driven.

;
; ----------------
; TRANSMIT ROUTINE
; ----------------
;
TxRoutine       movwf TxSend            ; temp store data to be sent
        bcf PORT,TX             ; do start bit
        call HBDelay5
        call HBDelay2
;
        movlw 8h                        ; 8 data bits to send
        movwf BCount
;
TxLoop  rrf TxSend
        btfsc STATUS,C
        goto TxHiBit
;
        nop
        bcf PORT,TX
        goto DoneBit
;
TxHiBit bsf PORT,TX
        goto $ + 1
;
DoneBit call HBDelay5           ; wait 1 bit length
        call HBDelay5
        decfsz BCount
        goto TxLoop
;
        call DoRet
        nop
        bsf PORT,TX             ; stop bit
        call HBDelay1
        call HBDelay
DoRet   return

;
;
; ---------------
; RECEIVE ROUTINE
; ---------------
;
RxRoutine       bcf flag1,data
        btfsc PORT,RX           ; wait for start bit
        return
;
        call HBDelay2           ; wait 1/2 bit length - 2 cycles
        movlw 9h
        movwf BCount
;
RxLoop  btfsc PORT,RX           ; start bit is lost during routine
        goto RxHiBit
;
        bcf STATUS,C            ; receive start bit and 8 data bits
        goto RxLoBit
;
RxHiBit bsf STATUS,C
        nop
RxLoBit rrf RxHold
        call HBDelay4           ; wait 1/2 bit length - 4 cycles
        call HBDelay5           ; wait 1/2 bit length - 5 cycles
        decfsz BCount
        goto RxLoop
;
        movf RxHold,W
        bsf flag1,data
        return                  ; data returned in W Reg
;
; --------------
; HALF BIT DELAY
; --------------
;
HBDelay nop
HBDelay1        nop
HBDelay2        goto $ + 1
HBDelay4        nop
HBDelay5        movlw 0x05
        movwf BitWait
BW      decfsz BitWait
        goto BW
        nop
        return

Questions:

Comments: