![](http://datasheet.mmic.net.cn/Microchip-Technology/PIC24FV32KA304-I-MV_datasheet_99574/PIC24FV32KA304-I-MV_66.png)
PIC24FV32KA304 FAMILY
DS39995C-page 66
2011-2012 Microchip Technology Inc.
6.4.1
ERASE DATA EEPROM
The data EEPROM can be fully erased, or can be
partially erased, at three different sizes: one word, four
words or eight words. The bits, NVMOP<1:0>
(NVMCON<1:0>), decide the number of words to be
erased. To erase partially from the data EEPROM, the
following sequence must be followed:
1.
Configure NVMCON to erase the required
number of words: one, four or eight.
2.
Load TBLPAG and WREG with the EEPROM
address to be erased.
3.
Clear the NVMIF status bit and enable the NVM
interrupt (optional).
4.
Write the key sequence to NVMKEY.
5.
Set the WR bit to begin the erase cycle.
6.
Either poll the WR bit or wait for the NVM
interrupt (NVMIF is set).
This example shows how to do a one-word erase.
Similarly, a four-word erase and an eight-word erase
can be done. This example uses C library procedures to
manage the Table Pointer (builtin_tblpage and
builtin_tbloffset
) and the Erase Page Pointer
(builtin_tblwtl). The memory unlock sequence
(builtin_write_NVM) also sets the WR bit to initiate
the operation and returns control when complete.
EXAMPLE 6-2:
SINGLE-WORD ERASE
int __attribute__ ((space(eedata))) eeData = 0x1234;
/*--------------------------------------------------------------------------------------------
The variable eeData must be a Global variable declared outside of any method
the code following this comment can be written inside the method that will execute the erase
----------------------------------------------------------------------------------------------
*/
unsigned int offset;
// Set up NVMCON to erase one word of data EEPROM
NVMCON = 0x4058;
// Set up a pointer to the EEPROM location to be erased
TBLPAG = __builtin_tblpage(&eeData);
// Initialize EE Data page pointer
offset = __builtin_tbloffset(&eeData);
// Initizlize lower word of address
__builtin_tblwtl(offset, 0);
// Write EEPROM data to write latch
asm volatile ("disi #5");
// Disable Interrupts For 5 Instructions
__builtin_write_NVM();
// Issue Unlock Sequence & Start Write Cycle
while(NVMCONbits.WR=1);
// Optional: Poll WR bit to wait for
// write sequence to complete