© 2004 Microchip Technology Inc. DS70052C-page 5-11
Section 5. Flash and EEPROM Programming
Flash and EEPROM
Programming
5
5.4.2.2 Erasing a Row of Program Memory
The following is a code sequence that can be used to erase a row (32 instructions) of program
memory. The NVMCON register is configured to erase one row of program memory. The
NVMADRU and NVMADR registers are loaded with the address of the row to be erased. The
program memory must be erased at ‘even’ row boundaries. Therefore, the 6 LSbits of the value
written to the NVMADR register have no effect when a row is erased.
The erase operation is initiated by writing a special unlock, or key sequence to the NVMKEY
register before setting the WR control bit (NVMCON<15>). The unlock sequence needs to be
executed in the exact order shown without interruption. Therefore, interrupts should be disabled
prior to writing the sequence.
Two NOP instructions should be inserted in the code at the point where the CPU will resume
operation. Finally, interrupts can be enabled (if required).
; Setup NVMCON to erase one row of Flash program memory
MOV #0x4041,W0
MOV W0
,
NVMCON
; Setup address pointer to row to be ERASED
MOV #tblpage(PROG_ADDR),W0
MOV W0
,
NVMADRU
MOV #tbloffset(PROG_ADDR),W0
MOV W0,NVMADR
; Disable interrupts, if enabled
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 erase operation
BSET NVMCON,#WR
; Insert two NOPs after the erase cycle (required)
NOP
NOP
; Re-enable interrupts, if needed
POP SR
Note: When erasing a row of program memory, the user writes the upper 8 bits of the
erase address directly to the NVMADRU and NVMADR registers. Together, the
contents of the NVMADRU and NVMADR registers form the complete address of
the program memory row to be erased.
The NVMADRU and NVMADR registers specify the address for all Flash erase and
program operations. However, these two registers do not have to be directly written
by the user for Flash program operations. This is because the table write instruc-
tions used to write the program memory data automatically transfers the TBLPAG
register contents and the table write address into the NVMADRU and NVMADR
registers.
The above code example could be modified to perform a ‘dummy’ table write
operation to capture the program memory erase address.