Disable interrupt during EEPROM write in Arduino mega??


I am writing a data in the EEPROM in main loop in arduino mega. I am using timer interrupt to do my required function. Did I have to disable interrupt during the writing the data into the EEPROM. Please let us know??


Does your EEPROM routine use variables that can be changed by code in the ISR?

If reading a variable that's written to in an interrupt is non-atomic you always need to disable interrupts while reading. If whatever-task you want to do takes more time than you would like to disable interrupts for, just disable interrupts, make a local copy, enable interrupts again and us the local copy to do the whatever-task.

Why not just set a global flag in your interrupt routine , then outside that in the sketch , check to see if the flag is set , and if set write to the Eeprom and then unset the flag . Best to do as little as possible in an interrupt routine as strange things can happen

If you are inside an interrupt service routine , I believe other interrupts are kept in wait until that routine ends.

I agree that writing to EEPROM is slow and should not happen in the ISR. But as I read it, the data to be written is generated in an ISR and written to EEPROM in the main program. But that's up to the OP :slight_smile:


Does your EEPROM routine use variables that can be changed by code in the ISR


EEPROM.write function will take how many microsecs??
How many clock cycles??


So how are are de ISR and the EEPROM.write() related?

For your question, see the datasheet of the uC you use (ATmega328p for the Uno/Nano/Pro Mini).