Continue to Site

Welcome to our site!

Electro Tech is an online community (with over 170,000 members) who enjoy talking about and building electronic circuits, projects and gadgets. To participate you need to register. Registration is free. Click here to register now.

  • Welcome to our site! Electro Tech is an online community (with over 170,000 members) who enjoy talking about and building electronic circuits, projects and gadgets. To participate you need to register. Registration is free. Click here to register now.

Can't get EEPROM to work.

Pommie

Well-Known Member
Most Helpful Member
I have a (long and complex) program that saves data to EEPROM. It doesn't work. To test this out I wrote a small program,
Code:
const uint8_t data[10] __at(0xF00000)={0,1,2,3,4,5,6,7,8,9};

uint8_t data2[10]={0,0,0,0,0,0,0,0,0,0},temp;

void writeEEPROM(uint8_t,uint8_t);
uint8_t readEEPROM(uint8_t);

void main(void) {
    for(uint8_t i=0;i<sizeof(data);i++){
        data2[i]=readEEPROM(i);
    }
    for(uint8_t i=0;i<sizeof(data);i++){
        writeEEPROM(i,i+'A');
    }
    for(uint8_t i=0;i<sizeof(data);i++){
        data2[i]=readEEPROM(i);
    }
    temp=readEEPROM(0); 
    while(1);
}
uint8_t readEEPROM(uint8_t add){
    EEADR=add;
    EECON1bits.EEPGD=0;
    EECON1bits.CFGS=0;
    EECON1bits.RD=1;
    return(EEDATA);
}
void writeEEPROM(uint8_t add,uint8_t dat){
    //interrupts must be disabled.
    while(WR);      //allow any existing write to complete
    EEADR=add;
    EEDATA=dat;
    EEPGD=0;
    CFGS=0;
    WREN=1;
    EECON2=0x55;
    EECON2=0xAA;
    WR=1;
    WREN=0;
}
This places (compiles) 0 to 9 in EEPROM at address 0 (0xF00000) - this works.
It copies this data into a RAM array in SFRs - this works.
It then writes A to J into EEPROM - this works. I can read the memory and see the contents of the EEPROM.
It then copies the EEPROM into the RAM (SFR) array - this doesn't work.
I also added the temp=readEEPROM(o) as a test.
ALL of data2 and temp contain 'J' which was the last character written.
I know this will be something simple that I've missed but I just can't see it.

Thanks for any and all help.

Mike.
P.S. if anyone wants to see any thing further (EEPROM readout etc.) then just ask.
Edit, the config bits are set to, CONFIG5H
#pragma config CPD = OFF // Data EEPROM Code Protection bit (Data EEPROM not code-protected)
 
Last edited:
Additional information,
I'm using a Pic18F25K22 and the datasheet says,
1704935416089.png
 
Worked it out. Looks like the previous write hadn't completed so read wouldn't work.
I changed the readEEPROM by adding a while(WR) and that fixed it.
Final readEEPROM is,
Code:
uint8_t readEEPROM(uint8_t add){
    while(WR);
    EEADR=add;
    EECON1bits.EEPGD=0;
    EECON1bits.CFGS=0;
    EECON1bits.RD=1;
    return(EEDATA);
}

Mike.
Edit, more elegant solution - I added while(WR) to the end of the writeEEPROM routine.
 
Last edited:
Just looked at my code. I have just the one while (WR) at the end of the write function. I never placed one at the front.
C:
void WriteEeprom( unsigned int badd,unsigned char bdata )
    {
    EEADR = (badd & 0x0ff);
      EEDATA = bdata;
      EECON1bits.EEPGD = 0;
    EECON1bits.CFGS = 0;
    EECON1bits.WREN = 1;
    INTCONbits.GIE = 0;
    EECON2 = 0x55;
    EECON2 = 0xAA;
    EECON1bits.WR = 1;
    while(EECON1bits.WR);
    INTCONbits.GIE = 1;
    EECON1bits.WREN = 0;
    }
 

Latest threads

New Articles From Microcontroller Tips

Back
Top