Trouble reading/writing internal EEPROM PIC24F16KA101

1.4k views Asked by At

I am trying to get interact with the internal memory of the PIC24F16KA101 MCU. After reading the data-sheet and the discussion on this site (which offer a pretty helpful sample code)used in the project Now if I put the code below the program work just fine, since I am able to read successfully the same value that I wrote previously. However if after writing I unplug the MCU and perform only a read of the EEPROOM it is not going to return the value written. What could be the problem here?. Why can I write and then read successfully but can not read after a power off?. Thanks in advance to all for the help Damian

int __attribute__ ((space(eedata))) ee_addr;
void EepSetup();
void EepErase(void);
int EepWrite(int index, int data);
int EepRead(int index);

int main(int argc, char** argv) 
{
    unsigned int data = 123;
    unsigned int data_read = 0;

    Init_UART1();
    UART1WriteString("START EEPROM PROGRAM \n");
    EepSetup();
    UART1WriteString("WRITING DATA TO MEMORY \n");
    EepWrite(1,data);

    //if the code works, just comment the upper section and read eeprom after
    //disconecting the power source
    UART1WriteString("READING DATA FROM MEMORY \n");
    data_read = EepRead(1);
    UART1WriteString("Value Read: ");
    UART1WriteInt(data_read,16);
    UART1WriteString("\n");
    __delay_ms(1000);
    return (EXIT_SUCCESS);
}
void EepSetup(){
    //Disable Interrupts For 5 instructions
    asm volatile("disi #5");
    //Issue Unlock Sequence
    asm volatile("mov #0x55, W0 \n"
    "mov W0, NVMKEY \n"
    "mov #0xAA, W1 \n"
    "mov W1, NVMKEY \n");
}
void EepErase(void) {
    NVMCON = 0x4050;            // Set up NVMCON to bulk erase the data EEPROM
    asm volatile ("disi #5");   // Disable Interrupts For 5 Instructions
    __builtin_write_NVM();      // Issue Unlock Sequence and Start Erase Cycle
    while(_WR)
    ;
}

int EepRead(int index){
    unsigned int offset;

    TBLPAG = __builtin_tblpage(&ee_addr);    // Initialize EE Data page pointer
    offset = __builtin_tbloffset(&ee_addr);  // Initizlize lower word of address
    offset += index * sizeof(int);
    return __builtin_tblrdl(offset);    // read EEPROM data
}

int EepWrite(int index, int data){
    unsigned int offset;
    NVMCON = 0x4004;    // Set up NVMCON to erase one word of data EEPROM
    TBLPAG = __builtin_tblpage(&ee_addr);    // Initialize EE Data page pointer
    offset = __builtin_tbloffset(&ee_addr);  // Initizlize lower word of address
    offset += index * sizeof(int);
    __builtin_tblwtl(offset, data);
    asm volatile ("disi #5");   // Disable Interrupts For 5 Instructions
    __builtin_write_NVM();      // Issue Unlock Sequence and Start Erase Cycle
    while(_WR);
    return (EXIT_SUCCESS);
}
1

There are 1 answers

0
Damian Miralles On

I just figured out what the problem was, it happens that if you use the PICkit 3 with MPLABX you have to check an option in the programmer to preserve the EEPROM memory,so the code was functional, you just need to check the option of "Preserve EEPROM Memory" in the programmer settings. I hope this help others.

Cheers, Damian