PIC18F45K22 EEPROM read/write problem
Hi All,
I have been using Microchip devices since many years, using the previous versions of MPLAB.
Recently I shifted to MPLAB-X using XC8.
I have built many projects using the MPLAB-X with XC8, and are working well.
But in the present project I am using the PIC18F45K22, without MCC.
The entire project is ready, except for one small thing, I am NOT able to write or read from the EEPROM correctly.
I tried using the <EEP.H> and its functions, I tried writing into the registers related EEPROM directly, but the problem remains the same.
The problem is:
I write into location 0x10 with data=0x45, I always get the value of 0xff when I read from EEPROM location 0x10.
I really do NOT understand what has gone wrong with this. Later I searched the internet, and found that Microchip has stopped the support of EEPROM in the 18F series of devices. I am NOT sure about this, but I would appreciate if I can get a sample code of reading and writing into the EEPROM of PIC18F45K22.
Thank You,
Kiran V Sutar
Present Code:
Backup():
GIE=0; // Disbale Interrupts
Delay (1000); // Give a small delay
EEADR = 0x10; //Address of EEPROM memory location to write
EEDATA = 0x45; // Write data we want to write to SFR
Delay (1000); // Give a small delay
EECON1=0x04; // Select the EEPROM and make CFGS=0, WREN=1 , I tried with this method
// ...and the following method also....
// EECON1bits.EEPGD = 0; // Select EEPROM data memory
// EECON1bits.CFGS = 0; // Access flash/EEPROM NOT config. registers
// EECON1bits.FREE=0; // Perform Write-only
// EECON1bits.WREN = 1; // Enable writing of EEPROM (this is disabled again after the write completes)
Delay (1000);
// The next three lines of code perform the required operations to
// initiate a EEPROM write
EECON2 = 0x55; // Part of required sequence for write to internal EEPROM
Delay (1000); // I also tried without this delay
EECON2 = 0xAA; // Part of required sequence for write to internal EEPROM
Delay (1000);
EECON1bits.WR = 1; // Part of required sequence for write to internal EEPROM
EECON1=0x06; // I also tried by removing this statement also, but did not work
Delay (1000);
// Loop until write operation is complete
if (EECON1bits.WR == 1);
Delay (10000);
PIR2bits.EEIF = 0; //Clearing EEIF bit (this MUST be cleared in software after each write)
EECON1bits.WREN = 0; // Disable write (for safety, it is re-enabled next time a EEPROM write is performed)
Delay (1000);
GIE=1 // Enable the interrupts
Restore ():
EEADR = 0x10; // Address of the EEPROM Memory location
Delay (1000); // Small delay
EEDATA=0x55; // Store some random value in EEDATA register...to know whether the function has worked
Delay (1000); // small delay
EECON1=0x00; // Select EEPROM, CFGS and FREE=0, WREN=0 //...I tried this method....
//....and the following commented methods also...it did NOT work
// EECON1bits.EEPGD = 0; // Select EEPROM Data Memory
// EECON1bits.CFGS = 0; // Access flash/EEPROM NOT config. registers
// EECON1bits.FREE=0; // Perform Write-only
// EECON1bits.WREN = 0; // Start a read cycle
Delay (1000); // Small delay
EECON1=0x01; // make RD=1
// EECON1bits.WR = 0; // Start a read cycle
// EECON1bits.RD = 1; // Start a read cycle
Delay (1000); // Give small delay
// A read should only take one cycle, and then the hardware will clear
// the RD bit
Delay (1000); //Give small delay
// while(EECON1bits.RD == 1);
i=EEDATA; // Return data
Count=i;
post edited by KiranVSutar - 2018/10/17 09:57:13