0
\$\begingroup\$

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
     EEADR = address;
     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
    EEADR = address;            //setup Address
    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?

\$\endgroup\$
  • \$\begingroup\$ The EEPROM is internal to the PIC18 \$\endgroup\$ – Dennis Kerrisk Mar 11 '15 at 16:32
  • \$\begingroup\$ 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. \$\endgroup\$ – doynax Mar 11 '15 at 16:41
1
\$\begingroup\$

here is an example function to write to the eeprom.

void write_16f84_eeprom(unsigned char adr, unsigned char d)
{
   EEADR = adr;  // the address within the EEPROM
   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 
}
| improve this answer | |
\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy