PIC16F818/819
An example of the complete four-word write sequence
is shown in Example 3-5. The initial address is loaded
into the EEADRH:EEADR register pair; the four words
of data are loaded using indirect addressing, assuming
that
a row erase sequence has already been
performed.
EXAMPLE 3-5:
WRITING TO FLASH PROGRAM MEMORY
; This write routine assumes the following:
; 1. The 32 words in the erase block have already been erased.
; 2. A valid starting address (the least significant bits = '00') is loaded into EEADRH:EEADR
; 3. This example is starting at 0x100, this is an application dependent setting.
; 4. The 8 bytes (4 words) of data are loaded, starting at an address in RAM called ARRAY.
; 5. This is an example only, location of data to program is application dependent.
; 6. word_block is located in data memory.
BANKSEL EECON1
;prepare for WRITE procedure
;point to program memory
;allow write cycles
BSF
BSF
BCF
EECON1, EEPGD
EECON1, WREN
EECON1, FREE
;perform write only
BANKSEL word_block
MOVLW
MOVWF
.4
word_block
;prepare for 4 words to be written
;Start writing at 0x100
;load HIGH address
BANKSEL EEADRH
MOVLW
MOVWF
MOVLW
MOVWF
0x01
EEADRH
0x00
EEADR
;load LOW address
BANKSEL ARRAY
MOVLW
MOVWF
ARRAY
FSR
;initialize FSR to start of data
LOOP
BANKSEL EEDATA
MOVF
MOVWF
INCF
MOVF
MOVWF
INCF
INDF, W
EEDATA
FSR, F
INDF, W
EEDATH
FSR, F
;indirectly load EEDATA
;increment data pointer
;indirectly load EEDATH
;increment data pointer
;required sequence
BANKSEL EECON1
MOVLW
MOVWF
MOVLW
MOVWF
BSF
0x55
EECON2
0xAA
EECON2
EECON1, WR
;set WR bit to begin write
NOP
;instructions here are ignored as processor
NOP
BANKSEL EEADR
INCF
EEADR, f
;load next word address
BANKSEL word_block
DECFSZ
GOTO
word_block, f
loop
;have 4 words been written?
;NO, continue with writing
BANKSEL EECON1
BCF
BSF
EECON1, WREN
INTCON, GIE
;YES, 4 words complete, disable writes
;enable interrupts
2004 Microchip Technology Inc.
DS39598E-page 31