Hot!PIC18F45K22 EEPROM read/write problem

Author
KiranVSutar
New Member
  • Total Posts : 10
  • Reward points : 0
  • Joined: 2007/06/08 20:26:02
  • Location: 0
  • Status: offline
2018/10/11 23:08:41 (permalink)
0

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
#1

6 Replies Related Threads

    qhb
    Superb Member
    • Total Posts : 9609
    • Reward points : 0
    • Joined: 2016/06/05 14:55:32
    • Location: One step ahead...
    • Status: offline
    Re: PIC18F45K22 EEPROM read/write problem 2018/10/12 01:18:33 (permalink)
    +2 (2)

    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

    Get rid of BOTH delays.
    They MUST NOT be there.
    Check the assembly output from the compiler. The sequence must be exactly this:
    MOVLW 55h ;
    MOVWF EECON2 ; Write 55h
    MOVLW 0AAh ;
    MOVWF EECON2 ; Write 0AAh
    BSF EECON1, WR ; Set WR bit to begin write

    #2
    KiranVSutar
    New Member
    • Total Posts : 10
    • Reward points : 0
    • Joined: 2007/06/08 20:26:02
    • Location: 0
    • Status: offline
    Re: PIC18F45K22 EEPROM read/write problem 2018/10/17 09:47:13 (permalink)
    0
    Thanks qhb,
    I removed the delay, but it was still NOT working.
     
    On further reasoning I found out the problem was in the setting of the PICKIT3.
     
    It was a mistake on my part.
     
    There is a Project Settings of the PICKIT3.
     
    Go to Project->Properties->PICKIT3
    On this screen, there is a setting for "PRESERVE EEPROM MEMORY"
    And DO NOT forget that, there is another setting just below the above setting "PRESERVE EEPROM Range(s)(hex)"
    Set this value that is required, for PIC18F45K22, I set it as 00-FF.
     
    After this the EEPROM read/write functions worked perfectly alright.
     
    Many thanks to qhb for the support. I had to remove the Delays as mentioned in your post, after which it really kicked off.
     
    I request the Moderator to treat this topic as "SOLVED".
     
    Thank You,
    Kiran V Sutar
    Mumbai, INDIA
    post edited by KiranVSutar - 2018/10/17 09:49:03
    #3
    jack@kksound
    code tags!
    • Total Posts : 3154
    • Reward points : 0
    • Joined: 2014/05/14 10:03:19
    • Location: 0
    • Status: offline
    Re: PIC18F45K22 EEPROM read/write problem 2018/10/17 09:54:38 (permalink)
    +2 (2)
     Later I searched the internet, and found that Microchip has stopped the support of EEPROM in the 18F series of devices.
    This is not accurate, only the older peripheral libraries have been discontinued/unsupported. On newer parts that are supported by the MCC the EEPROM support is there (this should include the 18F45K22) or write your own  (as you have). For older parts not supported by MCC you can write your own functions (as you have) or copy the source code from the old libraries and incorporate that into your projects (I don't recommend this, usually, much better to write it yourself).
    #4
    KiranVSutar
    New Member
    • Total Posts : 10
    • Reward points : 0
    • Joined: 2007/06/08 20:26:02
    • Location: 0
    • Status: offline
    Re: PIC18F45K22 EEPROM read/write problem 2018/10/17 10:03:10 (permalink)
    +1 (1)
    Thanks Jack@KKSound,
    That was informative, and useful.
    In some of the links (I don't remember which ones), but there were posts mentioning that Microchip stopped support for EEPROM functions in the latest version of MPLAB X and XC8.
     
    I was wrong. Sorry for that.
     
    Thank You.
    post edited by KiranVSutar - 2018/10/17 10:06:25
    #5
    jack@kksound
    code tags!
    • Total Posts : 3154
    • Reward points : 0
    • Joined: 2014/05/14 10:03:19
    • Location: 0
    • Status: offline
    Re: PIC18F45K22 EEPROM read/write problem 2018/10/17 11:05:49 (permalink)
    +1 (1)
    No need to be sorry.
    The datasheet does contain sample code and a good explanation of the EEPROM operation, although the code is in ASM not c but is so simple it is easily translated. The unlock sequence is probably the part that trips up most people trying to write to the EEPROM the first time, it must be correct or nothing works.
    #6
    SpokaneNexus
    Super Member
    • Total Posts : 405
    • Reward points : 0
    • Joined: 2013/02/05 14:53:25
    • Location: 0
    • Status: offline
    Re: PIC18F45K22 EEPROM read/write problem 2018/10/18 09:32:17 (permalink)
    +1 (1)
    FYI: I use this family, and its EEPROM, and for the EEPROM write sequence I wrote actual Assembly and inlined it with the C source. Works perfectly. So don't despair, it does work, just follow the advice in this thread and read the spec sheet carefully.
    #7
    Jump to:
    © 2019 APG vNext Commercial Version 4.5