LIST P=16F628A
;
#include "P16F628A.INC"
;
__config _BOREN_ON & _CP_OFF & _PWRTE_OFF & _WDT_OFF & _LVP_OFF & _MCLRE_OFF & _INTOSC_OSC_NOCLKOUT
;
CBLOCK 0x20
addre ;PIC Address
ENDC
;
ORG 0x000
;
movlw 0x07
movwf CMCON ;Turn Comparators Off
movlw 0x61
movwf addre ;Address set at 127
;
; CONFIGURE USART
;
bsf STATUS,RP0 ;Select Bank 1
;
movlw b'00000110'
movwf TRISB ;Set bits <2:1> in TRISB [pg73]
;
movlw 0x19
movwf SPBRG ;9600 baud, 1 stop bit, no parity bit w/ 4MHz Int. Osc.
;
movlw b'00100100'
movwf TXSTA ;Configure Tx for Highspeed Asynchrounous comms.
bcf STATUS,RP0 ;Select Bank 0
movlw b'11011001'
movwf RCSTA ;Configure 9-bit reception with address enable
;
; MAIN PROGRAM
;
loop call address
call receive
movwf TXREG ;Echo back byte
bsf RCSTA,ADEN ;Re-enable address detection
goto loop
;
; ADDRESS SUBROUTINE
;
address btfss PIR1,RCIF
goto address
movf RCREG,W
subwf addre,W ;Subtract addre from W
btfss STATUS,Z ;If z bit is set then the result was zero. Address match.
goto address
return
;
; Rx SUBROUTINE
;
receive bcf RCSTA,ADEN ;Clear the ADEN bit
bcf PIR1,RCIF ;Clear RCIF bit
btfss PIR1,RCIF ;WAit to receive byte
goto receive
movf RCREG,W ;Move RCREG to W
return
END