15.3.3 NVMREG Erase of Program Memory

Before writing to program memory, the word(s) to be written must be erased or previously unwritten. Program memory can only be erased one row at a time. No automatic erase occurs upon the initiation of the write to program memory. To erase a program memory row:
  1. Clear the NVMREGS bit to erase program memory locations, or set the NMVREGS bit to erase User ID locations.
  2. Write the desired address into the NVMADRH:NVMADRL register pair.
  3. Set the FREE and WREN bits.
  4. Perform the unlock sequence as described in the “NVM Unlock Sequence” section.
If the program memory address is write-protected, the WR bit will be cleared and the erase operation will not take place.

While erasing program memory, CPU operation is suspended, and resumes when the operation is complete. Upon completion, the NVMIF bit is set, and an interrupt will occur if the NVMIE bit is also set.

Write latch data is not affected by erase operations, and WREN will remain unchanged.

Figure 15-3. NVM Erase Sequence
Note:
  1. See Figure 15-2.

Erasing One Row of Program Flash Memory


; This sample row erase routine assumes the following:
  ; 1.A valid address within the erase row is loaded in variables ADDRH:ADDRL
  ; 2.ADDRH and ADDRL are located in common RAM (locations 0x70 - 0x7F)

   BANKSEL    NVMADRL
   MOVF       ADDRL,W
   MOVWF      NVMADRL            ; Load lower 8 bits of erase address boundary
   MOVF       ADDRH,W
   MOVWF      NVMADRH            ; Load upper 6 bits of erase address boundary
   BCF        NVMCON1,NVMREGS    ; Choose PFM memory area
   BSF        NVMCON1,FREE       ; Specify an erase operation
   BSF        NVMCON1,WREN       ; Enable writes
   BCF        INTCON,GIE         ; Disable interrupts during unlock sequence

    ; ---------------------REQUIRED UNLOCK SEQUENCE:--------------------

   MOVLW      0x55               ; Load 0x55 to get ready for unlock sequence
   MOVWF      NVMCON2            ; First step is to load 0x55 into NVMCON2
   MOVLW      0xAA               ; Second step is to load 0xAA into W
   MOVWF      NVMCON2            ; Third step is to load 0xAA into NVMCON2
   BSF        NVMCON1,WR         ; Final step is to set WR bit
	
   ; ------------------------------------------------------------------

   BSF        INTCON,GIE         ; Re-enable interrupts, erase is complete
   BCF        NVMCON1,WREN       ; Disable writes
Table 15-1. NVM Organization and Access Information
Master ValuesNVMREG AccessFSR Access
Memory FunctionMemory TypeProgram Counter (PC), ICSP™ AddressNVMREGS bit (NVMCON1)NVMADR[14:0]Allowed OperationsFSR AddressFSR Programming Access
Reset VectorProgram Flash Memory0x000000x0000Read/Write0x8000Read-Only
User Memory0x000100x00010x8001
0x00030x00030x8003
INT Vector0x000400x00040x8004
User Memory0x000500x00050x8005
0x3FFF(1)0x3FFF(1)0xFFFF
User IDProgram Flash Memory0x800010x0000Read/WriteNo Access
0x80030x0003
Reserved0x0004
Revision IDHard Coded in Program Flash Memory0x800510x0005Read
Device ID0x800610x0006
CONFIG1Program Flash Memory0x800710x0007Read/Write
CONFIG20x800810x0008
CONFIG30x800910x0009
CONFIG40x800A10x000A
CONFIG50x800B10x000B
DIA and DCIHard Coded in Program Flash Memory0x810010x0100Read
0x82FF10x02FF
Reserved0xF0000x70000x7000
0xF0FF0x7FFF0x7FFF
Note:
  1. The maximum Program Flash Memory address for the 0x3FFF family is 0x07FF.