• 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.

MPlab XC8 eeprom access PIC18F4685

Status
Not open for further replies.

revave

Member
Hello,

For a project i need access to the internal eeprom of a PIC18F4685.
I use the functions below, but it didn't work. Can someone tell what i'm doing wrong? I'm using the latest version of MPlab X and XC8.
Eeprom memory is not code protected and no interrupts are used..

//EEPROM routines
char rd (char e_addr)
{
EEADR = e_addr; //Data Memory Address to read
EECON1bits.EEPGD = 0 ; //Point to DATA memory
EECON1bits.RD = 1; //EEPROM Read
return EEDATA;
}

void wr (char e_addr, char e_value)
{
EEADR = e_addr; //Data Memory Address to write
EEDATA = e_value; //Data Memory Value to write
EECON1bits.EEPGD = 0 ; //Point to DATA memory
EECON1bits.WREN = 1; //Enable writes
EECON2 = 0x55; //Write 55h
EECON2 = 0xAA; //Write AAh
EECON1bits.WR = 1; //Set WR bit to begin write
while (EECON1bits.WR)continue;
EECON1bits.WREN == 0;
}
 
Last edited:

Ian Rogers

User Extraordinaire
Forum Supporter
Most Helpful Member
The only difference between your code and mine (works okay ) is the CFGS register and I disable the global interrupts..
The code I use was ripped from the peripheral library on the old C18 compiler...

I had to change the interrupt as there was a bug in the C18 library..
C:
unsigned char ReadEeprom(unsigned int badd )
   {
   EEADR = (badd & 0x0ff);
   EECON1bits.CFGS = 0;
   EECON1bits.EEPGD = 0;
   EECON1bits.RD = 1;
   return ( EEDATA );  // return with read byte
   }

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;
   }
 

revave

Member
Dear Ian,

Thank you for your very quick reply.
Finally with your code it works.

Thanks for your help..

Best regards,
Reijnko Vast
 

Mikebits

Well-Known Member
Ian is like Wyle Coyote. Super Genious. :D
 
Status
Not open for further replies.

Latest threads

EE World Online Articles

Loading
Top