# PIC18 EEPROM fails to write

I am working on a project using a PIC18F26K20. I need to store several value in EEPROM, but it does not store any data, all I evers get back is 0xff.

Here is the code I am using:

 unsigned char ReadEEPROM(unsigned char address){
EECON1=0;                   //ensure CFGS=0 and EEPGD=0
EECON1bits.RD = 1;
return(EEDATA);
}
void WriteEEPROM(unsigned char address,unsigned char data){
char SaveInt;
SaveInt=INTCON;             //save interrupt status
EECON1=0;                   //ensure CFGS=0 and EEPGD=0
EECON1bits.WREN = 1;        //enable write to EEPROM
EEDATA = data;              //and data
INTCONbits.GIE=0;           //No interrupts
EECON2 = 0x55;              //required sequence #1
EECON2 = 0xaa;              //#2
EECON1bits.WR = 1;          //#3 = actual write
INTCON=SaveInt;             //restore interrupts
while(!PIR2bits.EEIF);      //wait until finished
EECON1bits.WREN = 0;        //disable write to EEPROM
}


Is there something else I need to do to store Data in EEPROM? Or am I doing some thing wrong?

• The EEPROM is internal to the PIC18 – Dennis Kerrisk Mar 11 '15 at 16:32
• Have you tried reading back EEPROM written by the debugger and vice versa? Anyway, you don't seem to be clearing EEIF so perhaps subsequent writes are getting cancelled early by the lowering of the enable bit the end. Polling WR directly might be a better bet. – doynax Mar 11 '15 at 16:41

here is an example function to write to the eeprom.

void write_16f84_eeprom(unsigned char adr, unsigned char d)
{
EEDATA = d;   // the data to be written to that address

WREN = 1;     // write enable
EECON2 = 0x55; // protection sequence
EECON2 = 0xaa;

WR = 1;      // begin programming sequence

// wait, in loop, for write operation to complete
// I.E, WR control bit is 0
//     and EEIF interrupt flag bit is 1

WREN = 0;  // disable write enable

// after completion of the write operation
EEIF = 0
}