Calling an eeprom write during an interrupt

Thread Starter

ke5nnt

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

For instance:
Rich (BB code):
Program running normally
If something is true, start timer0 to count
When timer 0 overflows, T0IF is set
Enter TMR0 Interrupt routine
   write to eeprom
   clear T0IF
   turn off TMR0
Return to normal program
 

JohnInTX

Joined Jun 26, 2012
4,787
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:

MrChips

Joined Oct 2, 2009
35,089
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.
 

tshuck

Joined Oct 18, 2012
3,534
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.
 
Top