Help! - EEPROM is acting like RAM !? PIC18F4520

Author
Analog
Starting Member
  • Total Posts : 35
  • Reward points : 0
  • Joined: 2006/12/18 19:21:17
  • Location: 0
  • Status: offline
2016/01/23 07:05:50 (permalink)
0

Help! - EEPROM is acting like RAM !? PIC18F4520

Greetings.
I could use some advice here - I am using a PIC18F4520 using MPLABx assembly language and a PICKIT3.  I took the code essentially directly from the datasheet, with one minor addition that I thought would be necessary and I get some interesting results.
 
The minor addition that I added waits for the write to finish before reading - this is something that I believe should be done, as I am familiar with EEPROMs in other devices.
 
My code is set up to simply pick an arbitrary EEPROM address, write an arbitrary byte to that address, then read the value from that address to verify that it worked.  I use the MPLAB debug to watch everything, setting a breakpoint at the end of the simple code.
 
1st run - read and write:
When I run the routine, the watch value that is read back from the arbitrary EEPROM location looks great - it reads back what I wrote, which leads me to believe it wrote just fine to EEPROM!
 
2nd run, read only:
Then, when I stop the debugger and remove a commented out GOTO that jumps around the write code, I would assume that it would read back what I wrote the first time.  However, it always reads back an '0xFF', which means nothing was ever written there - acting like RAM!  How could this be?  Since the routine did read it back correctly right after the write the instance before!?
 
In addition, in both of the above cases - I open a memory window into EEPROM, and all the values are 0xFF, even though in the first instance, I have written and read the value "correctly" - I am suspicious that this part of MPLAB and the PICKIT are buggy, and I don't trust it.
 
I'm obviously missing something fundamental here - as the EEPROM is used all the time without issue, please give me some advice on what I'm missing, this should be pretty basic.
 
Thank you!
 
        ;goto here ; jump around the write sequence to verify

        movlw adr ; EEPROM Address to write
        movwf EEADR ; do it
        movlw wval ; load value to write
        movwf EEDATA ; do it
        bcf EECON1, EEPGD ; point to data EEPROM
        bcf EECON1, CFGS ; Access EEPROM
        bsf EECON1, WREN ; Write Enable EEPROM
        bcf INTCON, GIE ; Disable Interrupts
        movlw 0x55 ; w = 0x55
        movwf EECON2 ; load required test value
        movlw 0xAA ; w = 0xAA
        movwf EECON2 ; load required test value
        bsf EECON1, WR ; set write bit to start write process
       bsf INTCON, GIE ; Enable Interrupts
wait1: btfsc EECON1,WR ; poll WR, will be 0 when done writing
        goto wait1 ; poll loop
        bcf EECON1, WREN ; end of write sequence, disable write Enable EEPROM

here:
; read back EEPROM to verify that it worked.

        movlw adr ; EEPROM Address to read
        movwf EEADR ; do it
        bcf EECON1, EEPGD ; point to data EEPROM
        bcf EECON1, CFGS ; Access EEPROM
        bsf EECON1, RD ; Read Enable EEPROM
        movff EEDATA,rval ; read value

        nop ; placeholder for breakpoint in MPLABx

#1

14 Replies Related Threads

    DarioG
    Allmächtig.
    • Total Posts : 53746
    • Reward points : 0
    • Joined: 2006/02/25 08:58:22
    • Location: Oesterreich
    • Status: online
    Re: Help! - EEPROM is acting like RAM !? PIC18F4520 2016/01/23 07:57:54 (permalink)
    0
    Bad power supply, for example missing decoupling capacitors, did cause this to me in the past.
     
    But in your case I suppose it could also be the debugger that is somehow tricking you.
    Try writing to some cell, without any breakpoint: then Read EEprom using debugger and see if it's ok. You can test it with the SIMulator too, before.

    god will pay for what he does to me.
    #2
    drh
    Arrrr
    • Total Posts : 1052
    • Reward points : 0
    • Joined: 2004/07/12 11:43:22
    • Location: Hemet, Calif. USA
    • Status: offline
    Re: Help! - EEPROM is acting like RAM !? PIC18F4520 2016/01/23 08:08:12 (permalink)
    0 (1)
    Click on Debugger, then Settings... , then Limitations.
     
    Program/EEPROM data not updated during debugging.

    David
    #3
    Analog
    Starting Member
    • Total Posts : 35
    • Reward points : 0
    • Joined: 2006/12/18 19:21:17
    • Location: 0
    • Status: offline
    Re: Help! - EEPROM is acting like RAM !? PIC18F4520 2016/01/23 08:22:10 (permalink)
    +1 (1)
    DarioG
    Bad power supply, for example missing decoupling capacitors, did cause this to me in the past.
     
    But in your case I suppose it could also be the debugger that is somehow tricking you.
    Try writing to some cell, without any breakpoint: then Read EEprom using debugger and see if it's ok. You can test it with the SIMulator too, before.




    Dear Dario,
     
    Thank you for your advice.  I am using a PICDEMO2+ board from Microchip using a 9V battery.  I am hoping bypassing shouldn't be an issue.
     
    I took your advice in the debugger tricking me, so I decided to modify the code slightly so that it reports the value read from the EEPROM directly to the LEDS which are connected to PORTB on this board.  Then I eliminated the debugger altogether by programming the device and running without any debug.  I put an infinite loop at the end to halt the program since I wouldn't have a breakpoint.
     
    The results were identical.  The first instance where I wrote to the EEPROM, then read it back worked as expected, with the correct value returned.
     
    The 2nd instance where I bypassed the write routine and just read the EEPORM, I got 0xFF on the LEDs.  So the debugger and the directly programmed version seem to agree.  Any other hints to try?
     
    Thank you.
     
            clrf TRISB ; PORTB all outputs

           ; goto here ; jump around the write sequence to verify

            movlw adr ; EEPROM Address to write
            movwf EEADR ; do it
            movlw wval ; load value to write
            movwf EEDATA ; do it
            bcf EECON1, EEPGD ; point to data EEPROM
            bcf EECON1, CFGS ; Access EEPROM
            bsf EECON1, WREN ; Write Enable EEPROM
         bcf INTCON, GIE ; Disable Interrupts
            movlw 0x55 ; w = 0x55
            movwf EECON2 ; load required test value
            movlw 0xAA ; w = 0xAA
            movwf EECON2 ; load required test value
            bsf EECON1, WR ; set write bit to start write process
         bsf INTCON, GIE ; Enable Interrupts
    wait1: btfsc EECON1,WR ; poll WR, will be 0 when done writing
            goto wait1 ; poll loop
            bcf EECON1, WREN ; end of write sequence, disable write Enable EEPROM

    here:
    ; read back EEPROM to verify that it worked.

            movlw adr ; EEPROM Address to read
            movwf EEADR ; do it
            bcf EECON1, EEPGD ; point to data EEPROM
            bcf EECON1, CFGS ; Access EEPROM
            bsf EECON1, RD ; Read Enable EEPROM
            movff EEDATA,PORTB ; read value

    hxx: goto hxx ; halt program

    #4
    Analog
    Starting Member
    • Total Posts : 35
    • Reward points : 0
    • Joined: 2006/12/18 19:21:17
    • Location: 0
    • Status: offline
    Re: Help! - EEPROM is acting like RAM !? PIC18F4520 2016/01/23 08:28:49 (permalink)
    0 (1)
    drh
    Click on Debugger, then Settings... , then Limitations.
     
    Program/EEPROM data not updated during debugging.




    David,
     
    I'm not seeing what you are saying.  I have a 'Debug' menu at the top of MPLABx, but it doesn't have the "settings" menu below it, I'm not sure where you are referring to.
    #5
    drh
    Arrrr
    • Total Posts : 1052
    • Reward points : 0
    • Joined: 2004/07/12 11:43:22
    • Location: Hemet, Calif. USA
    • Status: offline
    Re: Help! - EEPROM is acting like RAM !? PIC18F4520 2016/01/23 09:05:43 (permalink)
    0
    Analog
    drh
    Click on Debugger, then Settings... , then Limitations.
     
    Program/EEPROM data not updated during debugging.




    David,
     
    I'm not seeing what you are saying.  I have a 'Debug' menu at the top of MPLABx, but it doesn't have the "settings" menu below it, I'm not sure where you are referring to.




    I use MPLAB V8.92. There should be something similar in MPLABX. This is a limitation of the PICKit3, not the IDE.

    David
    #6
    DarioG
    Allmächtig.
    • Total Posts : 53746
    • Reward points : 0
    • Joined: 2006/02/25 08:58:22
    • Location: Oesterreich
    • Status: online
    Re: Help! - EEPROM is acting like RAM !? PIC18F4520 2016/01/23 09:06:19 (permalink)
    0
    It all looks good enough (apart from possibly writing to PORTB instead of LATB, but it won't hurt now)
     
    So... strange...
    Are those literal values "correct" ?

    god will pay for what he does to me.
    #7
    drh
    Arrrr
    • Total Posts : 1052
    • Reward points : 0
    • Joined: 2004/07/12 11:43:22
    • Location: Hemet, Calif. USA
    • Status: offline
    Re: Help! - EEPROM is acting like RAM !? PIC18F4520 2016/01/23 10:57:21 (permalink)
    0
    Change the GOTO to a BRA and see what happens.
           bsf INTCON, GIE ; Enable Interrupts
    wait1: btfsc EECON1,WR ; poll WR, will be 0 when done writing
            BRA wait1 ; poll loop
            bcf EECON1, WR

    David
    #8
    jtemples
    Super Member
    • Total Posts : 10815
    • Reward points : 0
    • Joined: 2004/02/13 12:31:19
    • Location: Southern California
    • Status: offline
    Re: Help! - EEPROM is acting like RAM !? PIC18F4520 2016/01/23 11:51:51 (permalink)
    +3 (3)
    when I stop the debugger and remove a commented out GOTO

     
    When you do that, you have to reprogram, which erases the PIC, including the EEPROM.  You need to tell your programmer to preserve EEPROM when programming.
    #9
    Ian.M
    Super Member
    • Total Posts : 13114
    • Reward points : 0
    • Joined: 2009/07/23 07:02:40
    • Location: UK
    • Status: offline
    Re: Help! - EEPROM is acting like RAM !? PIC18F4520 2016/01/23 15:53:03 (permalink)
    +4 (3)
    There's a further nasty little 'gotcha' - EEPROM takes a significant time to write to, typically 4ms for your PIC (parameter D122), and if that write is aborted by power cycling or /MCLR, the memory location is likely to be left in an invalid state.  It may read as erased, it may read as just about anything else!  
     
    Unfortunately the programming algorithm implementation of Microchip's budget programmers tends to let your code run for a short time between reading the device ID and the rest of the programming or reading process, and if you are programming, it can also run briefly between the program and verify stages.   If it reaches your EEPROM write routine,  it can screw up the EEPROM contents.
     
    There are three keys to avoiding this problem with simple EEPROM test code:
    • External /MCLR must be enabled
    • MPLAB must be configured to hold the target in RESET after programming
    • Your code needs to wait for either a one second delay, or for a pin level change, (choosing a pin that wont be active during ICSP), before running your EEPROM write routine.
    Its also good practise to *ALWAYS* enable BOR if your code contains an EEPROM or FLASH write routine to prevent memory corruption during a slow powerup or powerdown or other brownout event. 
     
    You also cannot single step, or animate through a FLASH or EEPROM write under any of the ICSP/ICD interface hardware debuggers, as the debug NMI that invokes the debug executive after each instruction while stepping breaks the EECON2 critical unlock sequence that enables the write.  Its OK to run at full speed, either to the cursor or a breakpoint in the code after the write.
     
    As you are using a PICDEM 2 Plus, you've got two user switches - S2 and S3.  I would suggest that you should modify your code to sit in a loop on startup polling those switches and blinking a LED so you know its waiting for user input.   Then depending on which switch is pressed, jump to your EEPROM write or EEPROM read routine.     Its also worth preloading the EEPROM at build time with something other than 0x00 or 0xFF so you can check the read routine on known data. e.g. in MPASM absolute mode:

            ORG    0xf00000
            DE    "Test Data",0,1,2,3,4,5

    or in MPASM/MPLINK relocatable mode:

    DATA_EEPROM    CODE    0xf00000
            DE    "Test Data",0,1,2,3,4,5


    --
    NEW USERS: Posting images, links and code - workaround for restrictions.
    I also support http://picforum.ric323.com because this forum is sometimes too broken to use!
    #10
    bellstudio
    New Member
    • Total Posts : 1
    • Reward points : 0
    • Joined: 2007/11/19 02:20:39
    • Location: 0
    • Status: offline
    Re: Help! - EEPROM is acting like RAM !? PIC18F4520 2016/01/24 05:35:09 (permalink)
    0 (1)
    You might ignored when you hit debug the 2nd time, in default setting, IDE will erase all program memory including EEPROM, and program the PIC again. That might be the result you saw.
    #11
    Analog
    Starting Member
    • Total Posts : 35
    • Reward points : 0
    • Joined: 2006/12/18 19:21:17
    • Location: 0
    • Status: offline
    Re: Help! - EEPROM is acting like RAM !? PIC18F4520 2016/01/24 09:21:50 (permalink)
    +3 (3)
    Ian,
     
    I took your advice and re-wrote my program utilizing the two switches on the PICDEMO2+ board as follows:
     
    1.  Upon startup, the program reads the value from the EEPROM location and places it on the LEDs connected to PORTB.
    2.  The program waits for the user to press one of the two switches.
    3.  If the first switch is pressed, a value 'x' is placed in the EEPROM location.  If the 2nd switch is pressed, a value 'y' is placed in the EEPROM location.
    4.  The program branches back to the beginning.
     
    The program worked flawlessly, always updating the LEDs based on the switch being pressed.  In addition, I powered down the board and repowered.  Upon power up, the LEDs displayed the last state of the EEPROM, thus proving that the write worked correctly.
     
    Also, I used the upload button on MPLABx and the EEPROM memory view correctly shows the value in the EEPROM as predicted by the last state of the LEDs.
     
    Thank you for your insight, and I am satisfied that the write to EEPROM routine was working all along, and I understand better the limitations of MPLABx and the debugger via PICKIT3.
     
    I wanted to conclude my findings as well to let anyone else that may search this topic understand as well.
     
    -Cheers :)
    #12
    Ian.M
    Super Member
    • Total Posts : 13114
    • Reward points : 0
    • Joined: 2009/07/23 07:02:40
    • Location: UK
    • Status: offline
    Re: Help! - EEPROM is acting like RAM !? PIC18F4520 2016/01/24 11:06:56 (permalink)
    0 (1)
    Smile
    Now you need to look into write endurance and wear levelling.  Get it wrong and your can kill the EEPROM in under a day if its looping writing it!

    --
    NEW USERS: Posting images, links and code - workaround for restrictions.
    I also support http://picforum.ric323.com because this forum is sometimes too broken to use!
    #13
    Ian.M
    Super Member
    • Total Posts : 13114
    • Reward points : 0
    • Joined: 2009/07/23 07:02:40
    • Location: UK
    • Status: offline
    Re: Help! - EEPROM is acting like RAM !? PIC18F4520 2016/01/24 11:25:47 (permalink)
    0 (2)
    Its good practice to delay writes triggered by the user adjusting setting for a short time so that if they are frobulating switches or twiddling knobs, you don't wear the EEPROM by writing on every aimless change.   Also you have to consider how you are going to recover from EEPROM corruption due to power failure during write.  The answer to that may be a power fail interrupt, so you can detect when the upstream supply to your PSU goes down leaving you enough time with Vdd>Vbor to write all the state you need to save, but done wrong that can be disastrous.  e.g. If you detect every power glitch as a power fail, and write in every one, in an electrically noisy environment, you can wear out your EEPROM in under a year!
     
    See http://www.microchip.com/forums/m748230.aspx
     
    Replying to my own post because the forum is borked again and wont let me edit it. sad

    --
    NEW USERS: Posting images, links and code - workaround for restrictions.
    I also support http://picforum.ric323.com because this forum is sometimes too broken to use!
    #14
    ric
    Super Member
    • Total Posts : 22101
    • Reward points : 0
    • Joined: 2003/11/07 12:41:26
    • Location: Australia, Melbourne
    • Status: offline
    Re: Help! - EEPROM is acting like RAM !? PIC18F4520 2016/01/24 14:28:28 (permalink)
    0 (1)
    Another point to ensure good reliability.
     
    In my experience, under low voltage conditions, an EEPROM read can turn into a write.
    Enabling the BOR is the best defense against this, but it's also useful to make sure you do not attempt to read the EEPROM in the first few milliseconds after power on.

    I also post at: PicForum
    Links to useful PIC information: http://picforum.ric323.co...opic.php?f=59&t=15
    NEW USERS: Posting images, links and code - workaround for restrictions.
    To get a useful answer, always state which PIC you are using!
    #15
    Jump to:
    © 2018 APG vNext Commercial Version 4.5