The sequence of events for erasing a page of internal program memory is:
‘b110.‘b000.If the PFM address is write-protected, the GO bit will be cleared, the erase operation will not take place, and the WRERR bit will be set.
While erasing the PFM page, the CPU operation is suspended then resumes when the operation is complete. Upon erase completion, the GO bit is cleared in hardware, the NVMIF is set, and an interrupt will occur (after Step 6), if the NVMIE bit is set and interrupts are enabled.
The buffer data are not affected by erase operations and the NVMCMD bits will remain unchanged.
; This sample sector erase routine assumes that the target address
; specified by CODE_ADDR_UPPER and CODE_ADDR_HIGH contain a value within
; the PFM address range of the device.
; Load NVMADR with the base address of the memory page
MOVLW CODE_ADDR_UPPER
MOVWF NVMADRU
MOVLW CODE_ADDR_HIGH
MOVWF NVMADRH
ERASE_PAGE:
MOVLW 0x06 ; Erase Page command
MOVWF NVMCON1 ; set the command
BCF INTCON0, GIE ; disable interrupts
;–––––––––– Required Sequence -––––––––––
MOVLW 0x55 ; first unlock byte for erase
MOVWF NVMLOCK ; write 0x55
MOVLW 0xAA ; second unlock byte for erase
MOVWF NVMLOCK ; write 0xAA
BSF NVMCON0, GO ; start erase (CPU stalls)
;–––––––––––––––––––––––––––––––––––––––––––
BSF INTCON, GIE ; re-enable interrupts
BTFSC NVMCON1, WRERR ; verify operation success
GOTO ERASE_FAULT_RECOVERY
CLRF NVMCON1 ; disable writes to memory