Calling an eeprom write during an interrupt

Discussion in 'Programmer's Corner' started by ke5nnt, Apr 4, 2013.

  1. ke5nnt

    Thread Starter Active Member

    Mar 1, 2009
    Is it safe to allow an eeprom write sequence inside an interrupt service routine, or is that bad form?

    For instance:
    Code ( (Unknown Language)):
    2. Program running normally
    3. If something is true, start timer0 to count
    4. When timer 0 overflows, T0IF is set
    5. Enter TMR0 Interrupt routine
    6.    write to eeprom
    7.    clear T0IF
    8.    turn off TMR0
    9. Return to normal program
  2. JohnInTX


    Jun 26, 2012
    If its internal you have to 1)ensure that any previous writes have completed AND 2)only write one byte when it IS ready then wait again as internal EEPROM takes a few ms to complete writing one byte. If you have to wait on EEready, you run the risk of hanging an interrupt routine too long. Interrupts, especially in PIC where you don't have a lot of priorities, should be kept as short as possible. A better way to write multiple bytes would be to interrupt when EE was ready and fetch the next byte out of a buffer. The buffer could be a little as one byte (a prefetch buffer) as long as the main code got it reloaded within the 2ms.

    External EE's (I2C, SPI etc) have the same problem but they will write a page of bytes 16,32, 64 bytes, depending on the device, in one shot so that helps.

    EDIT: I'm not sure what you are trying to do with your code snippet. So... what are you trying to do?
    Last edited: Apr 4, 2013
    ke5nnt likes this.
  3. MrChips


    Oct 2, 2009
    A generally sound rule to follow is do as little as possible in an interrupt routine.

    On interrupt, clear the interrupt request, set a semaphore and get out.
  4. tshuck

    Well-Known Member

    Oct 18, 2012
    I agree with MrChips. In general, you should return from an interrupt as soon as possible. However, there are, as always, exceptions. It looks like you are trying to write to the EEPROM sporadically, provided that you aren't neglecting any other interrupt sources, you can do it this way, it would usually be bad form, but gets the job done.