list p=16F628
include <16F628.INC>
__config 0x3D30
;Code Protection Disabled (CP)
;Data Code Protection Disabled (CPD)
;Low Voltage Programming Disabled (LVP)
;Brown Out Detection Disabled (BODEN)
;Watchdog Timer Disabled (WDTE)
;Power Up Timer Enabled (PWRTE)
;External Master Reset Enabled (MCLRE)
;Internal Oscillator (INTRC_OSC)
;RAM Address Labels
cblock 0x20
eefile ;0x20
W_Save ;0x21
STATUS_Save ;0x22
PCLATH_Save ;0x23
endc
;*******************************************************
;Reset and Interrupt Vectors/Interrupt Service Routine
;*******************************************************
org 0x00 ;reset vector
goto START
org 0x04 ;interrupt vector
movwf W_Save ;back up current state of W register
swapf STATUS,W
movwf STATUS_Save ;back up current state of STATUS register
movfw PCLATH
movwf PCLATH_Save ;back up current state of PCLATH register
movlw 0x00
movwf eefile
movfw RCREG
bsf STATUS,RP0
movwf EEDATA
bcf STATUS,RP0
movfw eefile
bsf STATUS,RP0
movwf EEADR
bcf STATUS,RP0
call write
movfw PCLATH_Save
movwf PCLATH
swapf STATUS_Save,W
movwf STATUS
movfw W_Save
retfie
;*******************************************************
;Port Setup
;*******************************************************
START bsf STATUS,RP0 ;bank 1
bsf PCON,3 ;set internal oscillator to 4MHz
movlw b'00100000' ;enable USART receive interrupt only
movwf PIE1
bcf STATUS,RP0 ;bank 0
movlw b'11000000' ;enable unmasked peripheral interrupts only
movwf INTCON
;*******************************************************
;Port A/B direction config
;*******************************************************
bsf STATUS,RP0 ;bank 1
movlw b'00000000'
movwf TRISA ;Port A all outputs
movlw b'00000010'
movwf TRISB ;RB1 is the rx pin
;*******************************************************
;USART config
;*******************************************************
movlw d'25' ;baud rate = 9600bps
movwf SPBRG
clrf TXSTA ;transmit disabled, asynchronous serial enabled
bsf TXSTA,BRGH ;Baud Rate High
bcf STATUS,RP0 ;bank 0
bsf RCSTA,SPEN ;enable serial port
bsf RCSTA,CREN ;enable continuous receive
bcf RCSTA,RX9 ;enable 9 bit receive
;*******************************************************
;Program Start
;*******************************************************
MAIN goto MAIN ;delete this instruction & put the first instruction of the rest of your program on this line
;*******************************************************
;Subroutines
;*******************************************************
write bsf STATUS,RP0 ;select bank1
bsf EECON1,WREN ;enable write
bcf INTCON,GIE ;disable all unmasked interrupts
movlw 0X55 ;unlock codes
movwf EECON2
movlw 0xAA
movwf EECON2
bsf EECON1,WR ;write begins
bcf EECON1,WREN ;disable write after write is complete
bcf STATUS,RP0 ;select bank0
btfss PIR1,EEIF ;wait for write to complete
goto $-1
bcf PIR1,EEIF ;clear EEPROM write interrupt flag
bsf INTCON,GIE ;enable all unmasked interrupts
return
end