• AVR Freaks

Hot!EEPROM need help

Author
keto
Starting Member
  • Total Posts : 58
  • Reward points : 0
  • Joined: 2018/08/28 06:30:20
  • Location: 0
  • Status: offline
2019/04/07 17:40:54 (permalink)
0

EEPROM need help

Hi i am using a PIC18 and i am trying to hold data in eeprom.While the pic is powered and running i can write to eeprom and then read correctly the data from eeprom.When i make a software or hardware reset or cut off the power the data after a read is corrupted.I used a delay of 3sec at the start of the main just to be sure that the voltages are correct after unsuccessfull try with BOR circuit.What i am missing?
 
// CONFIG2L
#pragma config nPWRTEN = ON // Power-up Timer Enable (Power up timer enabled)
#pragma config BOREN = NOSLP // Brown-out Reset Enable in hardware,disabled in sleep(BOR enabled in hardware (SBOREN is ignored))
#pragma config BORV = 285 // Brown-out Reset Voltage (BOR set to 2.85V nominal)
#pragma config nLPBOR = OFF // Low-Power Brown-out Reset (Low-Power Brown-out Reset disabled)

 
void initEEPROM(void){
    IPR2bits.EEIP = 1; //High priority interrupts for EEPROM write
    PIR2bits.EEIF = 0; //Clear interrupt flag
    PIE2bits.EEIE = 1; //Enable interrupt
}

//WARNING : User firmware must always check if writeEEPROM return 0 or 1 and if return 0 call writeEEPROM again
unsigned char writeEEPROM(unsigned char data_byte,unsigned char byte_address){
    if(!EECON1bits.WREN){
        EEADR = byte_address; //Set EEPROM Address
        EEDATA = data_byte; //Set EEPROM data
        EECON1bits.EEPGD = 0; //Access to data of EEPROM
        EECON1bits.CFGS = 0; //Access EEPROM and not config register or program memory
        EECON1bits.WREN = 1; //Prepare EEPROM to allow write cycles to flash data
        INTCONbits.GIE = 0; //Disable general interrupts,nothing must interrupt the required sequence of writing EEPROM
        //Required Secuence
        EECON2 = 0x55;
        EECON2 = 0x0AA;
        EECON1bits.WR = 1; //Set WR bit to begin write
        INTCONbits.GIE = 1; //Re-enable interrupts
        //End of //Required Secuence
    }else{
        return 0;
    }
    return 1;
}

//All the instructions it is single clock executed
//Bytes address from 0-255
unsigned char readEEPROM(unsigned char byte_address){
    unsigned char data_byte;
    EEADR = byte_address;
    EECON1bits.EEPGD = 0;
    EECON1bits.CFGS = 0;
    EECON1bits.RD = 1;
    data_byte = EEDATA;
    return data_byte;
}

 
void __interrupt(high_priority) high_isr(void){
    //EEPROM Write Complete
    if(PIR2bits.EEIF){
        EECON1bits.WREN = 0; //Set EEPROM to not allow other writes
        PIR2bits.EEIF = 0; //Clear interrupt flag
    }
}

#1

7 Replies Related Threads

    qhb
    Superb Member
    • Total Posts : 9998
    • Reward points : 0
    • Joined: 2016/06/05 14:55:32
    • Location: One step ahead...
    • Status: offline
    Re: EEPROM need help 2019/04/07 18:22:19 (permalink)
    +2 (2)
    I don't understand why you are using interrupts for the EEPROM.
    Why did you add that? I'd try it without the interrupt code (and without enabling EEIF at all).
     
    I would recommend adding a wait for the WR bit to return to zero at the end of your write code.
    If you don't have it there, then you need it at the start of BOTH your read and write routines.
     

    Nearly there...
    #2
    jtemples
    عُضْوٌ جَدِيد
    • Total Posts : 11243
    • Reward points : 0
    • Joined: 2004/02/13 12:31:19
    • Location: Southern California
    • Status: offline
    Re: EEPROM need help 2019/04/07 18:52:52 (permalink)
    +2 (2)
    Your problem is almost surely brownout.
     
    I used a delay of 3sec at the start of the main just to be sure that the voltages are correct

     
    You can't work around brownout in software.  The PIC can brown out before or during your delay.
     
    Is 2.85V a valid voltage for your PIC?  You haven't told us which one you're using.
    #3
    keto
    Starting Member
    • Total Posts : 58
    • Reward points : 0
    • Joined: 2018/08/28 06:30:20
    • Location: 0
    • Status: offline
    Re: EEPROM need help 2019/04/07 23:34:53 (permalink)
    0
    thanks for your replies
    qhb i use interrupt to eeprom to make my code better and don't wait for something to be writed,why eeprom to use interrupt then?
    jtemples i use PIC18F25K50 the voltages that is write to it's datasheet is between 2.3 - 5.5V
    #4
    qhb
    Superb Member
    • Total Posts : 9998
    • Reward points : 0
    • Joined: 2016/06/05 14:55:32
    • Location: One step ahead...
    • Status: offline
    Re: EEPROM need help 2019/04/08 00:08:56 (permalink)
    +2 (2)
    keto
    qhb i use interrupt to eeprom to make my code better and don't wait for something to be writed,why eeprom to use interrupt then?

    How do you think this is making it better?
    Just move the WR polling to the start of your read and write routines (as I already described), if you want to do other things while the write proceeds.
     

    jtemples i use PIC18F25K50 the voltages that is write to it's datasheet is between 2.3 - 5.5V

    How long does it take your power level to drop from 2.85V to 2.3V?
     

    Nearly there...
    #5
    keto
    Starting Member
    • Total Posts : 58
    • Reward points : 0
    • Joined: 2018/08/28 06:30:20
    • Location: 0
    • Status: offline
    Re: EEPROM need help 2019/04/08 00:25:46 (permalink)
    0
    In my mind i think that the mcu can do other things while waiting for WR that's why i use interrupts instead of waiting for WR,why to include an interrupt if not used like this?
    I don't measure the time the power drops because i added a 470uF capaciotor and i assumed that the mcu bor circuit will have the time it needs
     
    #6
    qhb
    Superb Member
    • Total Posts : 9998
    • Reward points : 0
    • Joined: 2016/06/05 14:55:32
    • Location: One step ahead...
    • Status: offline
    Re: EEPROM need help 2019/04/08 00:32:24 (permalink)
    +2 (2)
    keto
    In my mind i think that the mcu can do other things while waiting for WR that's why i use interrupts instead of waiting for WR,why to include an interrupt if not used like this?

    Yes, there may be some situations where you need to know the moment the write cycle completes, but think for a moment...
    What is it in your situation you need to do when it finishes writing one byte?
    The only thing you care about is if you can write another byte.
    So, you "add the wait for WR" loop to the start of your "write eeprom" function.
    If it gets there while it's still busy with a previous write, you just wait the millisecond or two it takes to complete, then immediately do the next write.
    Anything you do with interrupts will be SLOWER than this.
     
    If you allow the PIC to keep running while a write is being done, then you must also not try to read a byte until that write is completed.
    That is why I said you have to add the WR wait to the start of both your write AND your read functions.
     

    I don't measure the time the power drops because i added a 470uF capaciotor and i assumed that the mcu bor circuit will have the time it needs

    Maybe you are right, it all depends how much current your circuit is drawing, and how precise the BOR level detect is.
    You won't know until you measure it. You just need a storage scope to see what it does.
     

    Nearly there...
    #7
    keto
    Starting Member
    • Total Posts : 58
    • Reward points : 0
    • Joined: 2018/08/28 06:30:20
    • Location: 0
    • Status: offline
    Re: EEPROM need help 2019/04/08 00:46:50 (permalink)
    0
    In my code i don't have a if(!WR) in the read function beggining that may cause the problem.I will try it and come with results.
    Thanks qhb
     
    #8
    Jump to:
    © 2019 APG vNext Commercial Version 4.5