Erasing the Data EEPROM Memory

Data EEPROM Memory can be erased by writing 0xFF to all locations in the Data EEPROM Memory that need to be erased.

Data EEPROM Erase Routine

        CLRF    NVMADRL             ; Clear address low byte
        CLRF    NVMADRH             ; Clear address high byte (if applicable)
        BCF     NVMCON1, NVMREG0    ; Set access for EEPROM
        BCF     NVMCON1, NVMREG1    ; Set access for EEPROM
        SETF    NVMDAT              ; Load 0xFF to data register
        BCF     INTCON, GIE         ; Disable interrupts
        BSF     NVMCON1, WREN       ; Enable writes
Loop:                               ; Loop to refresh array
        MOVLW   0x55                ; Initiate unlock sequence
        MOVWF   NVMCON2             ;
        MOVLW   0xAA                ;
        MOVWF   NVMCON2             ;
        BSF     NVMCON1, WR         ; Set WR bit to begin write
        BTFSC   NVMCON1, WR         ; Wait for write to complete
        BRA     $-2
        INCFSZ  NVMADRL, F          ; Increment address low byte
        BRA     Loop                ; Not zero, do it again
; The following 4 lines of code are not needed if the part doesn't have NVMADRH register
        INCF    NVMADRH, F          ; Decrement address high byte
        MOVLW   0x03                ; Move 0x03 to working register
        CPFSGT  NVMADRH             ; Compare address high byte with working register
        BRA     Loop                ; Skip if greater than working register
                                    ; Else go back to erase loop
        BCF     NVMCON1, WREN       ; Disable writes
        BSF     INTCON, GIE         ; Enable interrupts