Thursday, March 30, 2006

PIC: EXAMPLE 16

EXAMPLE 16

RS232 serial communication

This example illustrates the use of the microcontroller's EUSART module. Connection to the PC is enabled through RS232 standard. The program works in the following way: Every byte received via the serial communication is displayed using LED diodes connected to port B and is automatically returned to the transmitter thereafter. If an error occurs on receive, it will be signalled by switching the LED diode on. The easiest way to test device operation in practice is by using a standard Windows program called Hyper Terminal.
Example 16 - RS232 serial communication

Source Code

;*********************** Header ***************************************
;                  DEFINING VARIABLES IN PROGRAM

       w_temp      EQU 0x7D        ; Variable for saving W register
       status_temp EQU 0x7E        ; Variable for saving STATUS register
       pclath_temp EQU 0x7F        ; Variable for saving PCLATH w register

       cblock      0x20            ; Block of variables starts at address 20 h
       Port_A                      ; Variable at address 20 h
       Port_B                      ; Variable at address 21 h
       RS232temp                   ; Variable at address 22 h
       RXchr                       ; Variable at address 23 h
       endc                        ; End of block of variables
;**********************************************************************
       ORG         0x0000          ; Reset vector
       nop
       goto        main            ; Go to beginning of program (label "main")
;**********************************************************************
       ORG         0x0004          ; Interrupt vector address
       
       movwf       w_temp          ; Save value of W register
       movf        STATUS,w        ; Save value of STATUS register
       movwf       status_temp
       movf        PCLATH,w        ; Save value of PCLATH register
       movwf       pclath_temp
;**********************************************************************
; This part of the program is executed in interrupt routine
       banksel     PIE1
       btfss       PIE1, RCIE
       goto        ISR_Not_RX232int
       banksel     PIE1
       btfsc       PIR1, RCIF
       call        RX232_int_proc
       
ISR_Not_RX232int
       movf        pclath_temp,w
       movwf       PCLATH          ; PCLATH is given its original value
       
       movf        status_temp,w
       movwf       STATUS          ; STATUS is given its original value
       swapf       w_temp,f
       swapf       w_temp,w        ; W is given its original value
       
       retfie                      ; Return from interrupt routine
;**********************************************************************
RX232_int_proc                     ; Check if error has occurred
       banksel     RCSTA
       movf        RCSTA, w
       movwf       RS232temp
       btfsc       RS232temp, FERR
       goto        RX232_int_proc_FERR
       btfsc       RS232temp, OERR
       goto        RX232_int_proc_OERR
       goto        RX232_int_proc_Cont
       
RX232_int_proc_FERR
       bcf         RCSTA, CREN     ; To clear FERR bit, receiver is first
                                   ; switched off and on afterwards
       nop                         ; Delay ...
       nop
       bsf         RCSTA, CREN
       movf        RCREG, w        ; Reads receive register and clears FERR bit
       bsf         Port_A, 0       ; Switches LED on ( UART error indicator)
       movf        Port_A, w
       movwf       PORTA
       goto        RS232_exit
       
RX232_int_proc_OERR
       bcf         RCSTA, CREN     ; Clears OERR bit
       nop                         ; Delay ...
       nop
       bsf         RCSTA, CREN
       movf        RCREG, w        ; Reads receive register and clears FERR bit
       bsf         Port_A, 1       ; Switches LED on ( UART error indicator)
       movf        Port_A, w
       movwf       PORTA
       goto        RS232_exit
       
RX232_int_proc_Cont
       movf        RCREG, W        ; Reads received data
       movwf       RXchr
       movwf       PORTB
       movwf       TXREG           ; Sends data back to PC
       
RS232_exit
       return                      ; Return from interrupt routine
;**********************************************************************
; Main program

main
       banksel     ANSEL           ; Selects bank containing ANSEL
       clrf        ANSEL           ; All inputs are digital
       clrf        ANSELH
       
       ;---------------------------------------
       ; Port configuration
       ;---------------------------------------
       banksel     TRISA
       movlw       b'11111100'
       movwf       TRISA
       movlw       b'00000000'
       movwf       TRISB
       ;---------------------------------------
       ; Setting initial values
       ;---------------------------------------
       banksel     PORTA
       movlw       b'11111100'
       movwf       PORTA
       movwf       Port_A
       movlw       b'00000000'
       movwf       PORTB
       movwf       Port_B
       ;---------------------------------------
       ; USART - setting for 38400 bps
       ;---------------------------------------
       banksel     TRISC
       bcf         TRISC, 6        ; RC6/TX/CK = output
       bsf         TRISC, 7        ; RC7/RX/DT = input
       
       banksel     BAUDCTL       
       bsf         BAUDCTL, BRG16
       banksel     SPBRG
       movlw       .51             ; baud rate = 38400
                                   ; ( Fosc/(4*(SPBRG+1)) ) Error +0.16%
       movwf       SPBRG
       clrf        SPBRGH
       
       banksel     TXSTA
       bcf         TXSTA, TX9      ; Data is 8-bit wide
       bsf         TXSTA, TXEN     ; Data transmission enabled
       bcf         TXSTA, SYNC     ; Asynchronous mode
       bsf         TXSTA, BRGH     ; High-speed Baud rate
       
       banksel     RCSTA
       bsf         RCSTA, SPEN     ; RX/DT and TX/CK outputs configuration
       bcf         RCSTA, RX9      ; Select mode for 8-bit data receive
       bsf         RCSTA, CREN     ; Receive data enabled
       bcf         RCSTA, ADDEN    ; No address detection, ninth bit may be
                                   ; used as parity bit
       movf        RCSTA, W
       movf        RCREG, W
       ;---------------------------------------
       ; Interrupts enabled
       ;---------------------------------------
       banksel     PIE1
       bsf         PIE1, RCIE      ; USART Rx interrupt enabled
       
       bsf         INTCON, PEIE    ; All peripheral interrupts enabled
       bsf         INTCON, GIE     ; Global interrupt enabled
       
       ;---------------------------------------
       ; Remain here
       ;---------------------------------------
       goto $
       
       end                         ; End of program