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.

PIC18FxxKxx EEPROM read / write

Status
Not open for further replies.

granddad

Well-Known Member
Most Helpful Member
Microchip , for whatever reason , have stopped including the PIC18 peripheral library with XC8(1.45) , so when I came to want access to the EEprom on a PIC18F14K22 … tough , not supported … “use MCC was the message...” so three options... 1/ use MCC. 2 /go back to old XC that had the peripheral library. 3 /write your own EEprom stuff. 1 / Can't spend the time learning MCC at the moment, 2 / go back to an old XC8 seems a daft move. 3 . DIY … this is my result … May save someone a couple of hours ...or not !

Code:
unsigned char readEEPROM(unsigned char ee_addr)
{
  EEADR = ee_addr; //Address to be read
  EECON1bits.EEPGD = 0;//Selecting EEPROM Data Memory
  EECON1bits.RD = 1; //Initialise read cycle
  return EEDATA; //Returning data
}
void writeEEPROM(unsigned char ee_addr, unsigned char nv_data)
{
  unsigned char INTCON_SAVE;//To save INTCON register value
  EEADR = ee_addr; //Address to write
  EEDATA = nv_data; //Data to write
  EECON1bits.EEPGD = 0; //Selecting EEPROM Data Memory
  EECON1bits.WREN = 1; //Enable writing of EEPROM
  INTCON_SAVE=INTCON;//Backup INCON interrupt register
  INTCON=0; //Disables the interrupts
  EECON2=0x55; //Required sequence for write to internal EEPROM
  EECON2=0xAA; //Required sequence for write to internal EEPROM
  EECON1bits.WR = 1; //Initialise write cycle
  INTCON = INTCON_SAVE;//Enables Interrupt from saved
  EECON1bits.WREN = 0; //To disable write
  while(PIR2bits.EEIF == 0)//Check for completion of write
  {
   Nop(); //do nothing
  }
  PIR2bits.EEIF = 0; //Clearing EEIF bit
}
 
They make ever more strange moves! C18 help files were alot of help, the docs were good. How many years now with MPLAB X? more bugs than windows Vista
 
Hi LG, I still with ( free ) MPLABX , 4.10 ,( started with ver 2) but for 8 bit PIC I run it on linux mint. for PIC24 on Win 7 It does the job for me , X runs a hell of a lot better on linux.. Debug works , watches work , breakpoints work, PK3 behaves ,and is happy working off-line, probably the only thing that is a Q is optimisation ...
 
A slight improvement to the write speed may be achieved by testing the WR bit at the beginning of the routine.
Code:
void writeEEPROM(unsigned char ee_addr, unsigned char nv_data)
{
  unsigned char INTCON_SAVE;//To save INTCON register value
  while(EECON1bits.WR);        //wait for previous write to complete
  EEADR = ee_addr; //Address to write
  EEDATA = nv_data; //Data to write
  EECON1bits.EEPGD = 0; //Selecting EEPROM Data Memory
  EECON1bits.WREN = 1; //Enable writing of EEPROM
  INTCON_SAVE=INTCON;//Backup INCON interrupt register
  INTCON=0; //Disables the interrupts
  EECON2=0x55; //Required sequence for write to internal EEPROM
  EECON2=0xAA; //Required sequence for write to internal EEPROM
  EECON1bits.WR = 1; //Initialise write cycle
  INTCON = INTCON_SAVE;//Enables Interrupt from saved
  EECON1bits.WREN = 0; //To disable write
}

Mike.
 
Hi LG, I still with ( free ) MPLABX , 4.10 ,( started with ver 2) but for 8 bit PIC I run it on linux mint. for PIC24 on Win 7 It does the job for me , X runs a hell of a lot better on linux.. Debug works , watches work , breakpoints work, PK3 behaves ,and is happy working off-line, probably the only thing that is a Q is optimisation ...
Hmm a VM with Linux!! Didnt think of that! most things run better under linux (even windoze) ;). What processor you running? i have NEVER got mint running! every other version almost but not mint!!

Might be my slightly obscure graphics card.
 
LG I was given an really old Compaq Presario, Pentium 4 515 (2.93GHz) .. to recover some photos off , so I used it as a Linux tester... I put an extra 1Gig of memory took out a small drive and 'modem' it works well perhaps because it has minimum software loaded... graphics is ati radeon x300se... I put a tp-link wifi USB stick in it and it connected no hassle ( FireFox)... it usually works off line...
 
Last edited:
Thanks Mike. I am revamping a LCD BackPak I2C previously PIC16F1827 in asm, now PIC18F14K22, on-board EE is used to store / recover Custom characters during LCD startup so speed not an issue ... but who knows ...
 
Linux comes into its own on older machines, i love the way it runs with the cpu hardly breaking sweat. I have to run win 8.1 on a machine with 2 processors and 32 gig memory. I was using kali then unbuntu, on an old laptop.
I have used Mint on a friends laptop and liked it, but never got it to run on my hardware. I might stick unbuntu on a VM again.
 
Thanks Mike. I am revamping a LCD BackPak I2C previously PIC16F1827 in asm, now PIC18F14K22, on-board EE is used to store / recover Custom characters during LCD startup so speed not an issue ... but who knows ...
I'd love to see what your 16F1827 I2C driver code looks like...

Cheerful regards, Mike
 
Status
Not open for further replies.

New Articles From Microcontroller Tips

Back
Top