#include<P16f877a.inc>__CONFIG _WDT_OFF & _PWRTE_ON & _XT_OSC & _LVP_OFF load equ 0x20 buffer equ 0x21Start: bsf STATUS,RP0 clrf TRISD movlw 0x80 ;RC0-RC3 as output movwf TRISC movlw D'25' ;baudrate = 9600 movwf SPBRG movlw b'00100100' ; brgh = 1 movwf TXSTA ; enable Async Transmission, set brgh bcf STATUS,RP0 movlw b'10010000' ; enable Async Reception movwf RCSTA clrf PORTCsettle: decfsz load,1 goto settle movf RCREG,W movf RCREG,W movf RCREG,W ; flush receive buffer Loop: call Receive ; wait for a char movwf buffertest_a: movf buffer,W ;comprare the received data xorlw 'a' ;with ascii 'a' btfss STATUS,Z goto test_s goto leds1 ;if the received data is 'a', goto leds1test_s: movf buffer,W ;comprare the received data xorlw 's' ;with ascii 's' btfss STATUS,Z goto Loop goto leds2 ;if the received data is 'a', goto leds2 leds1: movlw 01h ;an 'a' will turn-on LED at RC0 movwf PORTC goto Loopleds2: movlw 02h ;an 's' will turn-on LED at RC1 movwf PORTC goto LoopReceive: clrf RCREG btfss PIR1,RCIF ;check for received data goto Receive movf RCREG,W ; copy the received data from RCREG to W return end
Receive: ; clrf RCREG btfss PIR1,RCIF ;check for received data goto Receive movf RCREG,W ; copy the received data from RCREG to W return
;-----------------------------------------------------------DO_RX: BTFSS RCSTA,OERR GOTO DO_RX2; BCF RCSTA,CREN; MOVF RCREG,W ; Purge the fifo MOVF RCREG,W ; Purge the fifo;DO_RX2: BTFSS RCSTA,CREN BSF RCSTA,CREN;-----------------------------------------------------------DO_RX3: BTFSS PIR1,RCIF GOTO DO_RX3; MOVF RCREG,W; RETURN;-----------------------------------------------------------