10.4.1 Reading the DFM

To read a DFM location, the user must write the address to the NVMADR register, set the NVMCMD bits for a single read operation (NVMCMD = ‘b000), and then set the GO control bit. The data is available on the very next instruction cycle. Therefore, the NVMDATL register can be read by the next instruction. NVMDATL will hold this value until another read operation or until it is written to by the user (during a write operation).

Note: Only byte reads are supported for DFM. Reading DFM with the Read Page operation is not supported.

The sequence of events for reading a byte of DFM is:

  1. Set the NVMADR registers to an address within the intended page.
  2. Set the NVMCMD control bits to ‘b000 (Byte Read).
  3. Set the GO bit to start the DFM byte read.
  4. Monitor the GO bit or NVMIF interrupt flag to determine when the read has completed.

This process is also shown in the following flowchart.

Figure 10-8. DFM Read Flowchart

Reading a Byte from Data Flash Memory in C

// Code sequence to read one byte from DFM
// DFM target address is specified by DFM_ADDR

// Variable to store the byte value from desired location in DFM
uint8_t ByteValue; 

// Load NVMADR with the desired byte address
NVMADR = DFM_ADDR;
NVMCON1bits.CMD = 0x00;            // Set the byte read command
NVMCON0bits.GO = 1;                // Start byte read
while (NVMCON0bits.GO);            // Wait for the read operation to complete
ByteValue = NVMDATL;               // Store the read value to a variable