© 2004 Microchip Technology Inc. DS70052C-page 5-19
Section 5. Flash and EEPROM Programming
Flash and EEPROM
Programming
5
5.5.6 Write One Row of Data EEPROM Memory
To write a row of data EEPROM, all sixteen write latches must be written before the program-
ming sequence is initiated. The TBLPAG register is loaded with the 8 MSbs of the EEPROM
address. The 16 LSbs of the EEPROM address are automatically captured into the NVMADR
register when each table write is executed. Data EEPROM row programming must occur at
even address boundaries, so the 5 LSbs of the NVMADR register have no effect on the row that
is programmed.
Setting the WR control bit (NVMCON<15>) initiates the programming operation. A special unlock
or key sequence should be written to the NVMKEY register before setting the WR control bit. The
unlock sequence needs to be executed in the exact order shown without interruption. Therefore,
interrupts should be disabled prior to writing the sequence.
; Set up a pointer to the EEPROM row to be programmed.
MOV #tblpage(EE_ADDR),W0
MOV W0
,
TBLPAG
MOV #tbloffset(EE_ADDR),W0
; Write the data to the programming latches.
MOV data_ptr,W1 ; Use W1 as pointer to the data.
TBLWTL [W1++]
,
[W0++] ; Write 1st data word
TBLWTL [W1++]
,
[W0++] ; Write 2nd data word
TBLWTL [W1++]
,
[W0++] ; Write 3rd data word
TBLWTL [W1++]
,
[W0++] ; Write 4th data word
TBLWTL [W1++]
,
[W0++] ; Write 5th data word
TBLWTL [W1++]
,
[W0++] ; Write 6th data word
TBLWTL [W1++]
,
[W0++] ; Write 7th data word
TBLWTL [W1++]
,
[W0++] ; Write 8th data word
TBLWTL [W1++]
,
[W0++] ; Write 9th data word
TBLWTL [W1++]
,
[W0++] ; Write 10th data word
TBLWTL [W1++]
,
[W0++] ; Write 11th data word
TBLWTL [W1++]
,
[W0++] ; Write 12th data word
TBLWTL [W1++]
,
[W0++] ; Write 13th data word
TBLWTL [W1++]
,
[W0++] ; Write 14th data word
TBLWTL [W1++]
,
[W0++] ; Write 15th data word
TBLWTL [W1++]
,
[W0++] ; Write 16th data word
; The NVMADR captures last table access address.
; Setup NVMCON to write one row of EEPROM
MOV #0x4005,W0
MOV W0
,
NVMCON
; Disable interrupts while the KEY sequence is written
PUSH SR
MOV #0x00E0,W0
IOR SR
; Write the KEY sequence
MOV #0x55,W0
MOV W0,NVMKEY
MOV #0xAA,W0
MOV W0,NVMKEY
; Start the programming operation
BSET NVMCON,#WR
;Re-enable interrupts, if needed
POP SR
Note: Sixteen table write instructions have been used in this code segment to provide
clarity in the example. The code segment could be simplified by using a single table
write instruction in a REPEAT loop.