PIC Specific RS232 IO Routine

for 16F877

by Claudio Rachiele

    Title  "USART TESTER"
     ;
     ; Author  Claudio RACHIELE IW0DZG
     ;
     ; Working  20000318
     ;
     list p=16f877,c=140    ; processor type
     errorlevel          1, -(305)
     #include "p16f877.inc"
     __CONFIG  _PWRTE_ON&_HS_OSC&_LVP_OFF&_WDT_OFF

temp  equ   7Fh               ;
;
; -----------
; DESCRIPTION
; -----------
;
; Baud Rate = 9600, No Parity, 8 bits & 1 Stop Bit
;
; -------------
; PROGRAM START
; -------------
;
     org  0         ; startup = 0000H
     clrf STATUS         ; bank 0
     goto BootStart
     org  4
ISR  goto 5         ;
     org  5
;
; --------------------------------------------------
; SET UP THE PORTS TO SUIT YOUR CIRCUIT REQUIREMENTS
; --------------------------------------------------
;

BootStart
     banksel PORTA       ; bank0
     movlw     b'00000000'
     movwf     PORTA
     movlw     b'00000000'
     movwf     PORTB
     movlw     b'01000000'
     movwf     PORTC
     movlw     b'00000000'
     movwf     PORTD
     movlw     b'00000000'
     movwf     PORTE

     banksel TRISA       ; bank1
     movlw     b'00000001'
     movwf     TRISA
     movlw     b'00000000'
     movwf     TRISB
     movlw     b'11000000'    ; set RC6 & RC7 as input
     movwf     TRISC
     movlw     b'00000000'
     movwf     TRISD
     movlw     b'11101000'
     movwf     TRISE
     movlw     b'00000111'
     movwf     ADCON1         ; porta inputs = digital not analog
;
; BAUD RATE SETTINGS
;
                    ;  Baud Values with BRGH = 0
                    ;  ((20000000/9600)/64)-1 = 32
;    movlw     d'207'    ;  1200 baud @ 16 Mhz Fosc +0.16 err
;    movlw     d'103'          ;  2400 baud @ 16 Mhz Fosc +0.16 err
;    movlw     d'25'           ;  9600 baud @ 16 Mhz Fosc +0.16 err
;    movlw     d'12'           ; 19200 baud @ 16 Mhz Fosc +0.16 err
;    movlw     d'255'          ;  1200 baud @ 20 Mhz Fosc +1.73 err
;    movlw     d'129'          ;  2400 baud @ 20 Mhz Fosc +0.16 err
;    movlw     d'32'          ;  9600 baud @ 20 Mhz Fosc -1.36 err
;    movlw     d'15'          ; 19200 baud @ 20 Mhz Fosc +1.73 err

                    ;  Baud Values with BRGH = 1
                    ;  ((20000000/9600)/16)-1 = 32
     movlw     d'129'         ;  9600 baud @ 20 Mhz Fosc +0.16 err
     movwf     SPBRG
     movlw     b'00100100'    ; brgh = 1
     movwf     TXSTA           ; enable Async Transmission, set brgh
     banksel RCSTA       ; bank0
     movlw     b'10010000'
     movwf     RCSTA          ; enable Async Reception
     movf RCREG,w
     movf RCREG,w
     movf RCREG,w        ; flush receive buffer
;
;
; ------------------------------------
; PROVIDE A SETTLING TIME FOR START UP
; ------------------------------------
;
     clrf temp
Settle
     decfsz  temp
     goto Settle

LWaitCom
     call      RecLoop         ; wait and read from RS232
     movwf     PORTB          ; show value for diagnostic purpose
TxLoop
     nop
     btfss     PIR1,TXIF ;xmit buffer empty?
     goto TxLoop         ;no, wait

     movwf     TXREG

     goto LWaitCom  ; no
;
; ----------------------------
; RECEIVE CHARACTER FROM RS232
; ----------------------------
; This routine does not return until a character is received.

RecLoop
     nop
     btfss     PIR1,RCIF ; check for received data
     goto RecLoop
     movf RCREG,w
     return
     end

Interested:

Questions:

Comments: