Disabeling Interrupts

Using the interrupts(); and noInterrupts(); allows for the interrupts to enabled or disabled. If interrupts are disabled, and an interrupt signal comes in, does the system service the interrupt once interrupts are re-enabled? Or does that interrupt request get lost?

I have a chunk of code that writes data to an external device and do not want an interrupt (timer to send data from external device) to execute unless full records are written.

does the system service the interrupt once interrupts are re-enabled

Yes. But (of course) at most one of each kind.

I dont understand what one of each kind means.
Is this accurate?

//code <--------Interrupt 0 falling edge
//execute ISR for interrupt 0

I dont understand what one of each kind means.

If there are two or more falling edge 0 interrupts (or any other kind of interrupt), while you have interrupt handling disabled, only the last interrupt (of any given type) will still be pending when interrupts are again enabled.

I gotcha. So basically the arduino can only remember one interrupt while interrupts are disabled and the last interrupt will be the one remembered when they are enabled. Hmmm....is there a way to set priority for triggered interrupts? For example, if INT0 is triggered while interrupts are disabled, it cannot be overwritten if INT1 is triggered later. In other words, INT0 NEEDS to be serviced but INT1 can go without. Thanks again!


the arduino can only remember one interrupt while interrupts are disabled

No. From the ATmega data sheet:

...if one or more interrupt conditions occur while the Global Interrupt
Enable bit is cleared, the corresponding Interrupt Flag(s) will be set
and remembered until the Global Interrupt Enable bit is set, and will
then be executed by order of priority.


is there a way to set priority

No. Priorities are fixed. Here’s the list for the '328p. Highest priority is the first one in the list. Lowest priority is the last.

[*] RESET            External Pin, Power-on Reset, Brown-out Reset and Watchdog System Reset
[*] INT0             External Interrupt Request 0
[*] INT1             External Interrupt Request 1
[*] PCINT0           Pin Change Interrupt Request 0
[*] PCINT1           Pin Change Interrupt Request 1
[*] PCINT2           Pin Change Interrupt Request 2
[*] WDT Watchdog     Time-out Interrupt
[*] TIMER2 COMPA     Timer/Counter2 Compare Match A
[*] TIMER2 COMPB     Timer/Counter2 Compare Match B
[*] TIMER2 OVF       Timer/Counter2 Overflow
[*] TIMER1 CAPT      Timer/Counter1 Capture Event
[*] TIMER1 COMPA     Timer/Counter1 Compare Match A
[*] TIMER1 COMPB     Timer/Coutner1 Compare Match B
[*] TIMER1 OVF       Timer/Counter1 Overflow
[*] TIMER0 COMPA     Timer/Counter0 Compare Match A
[*] TIMER0 COMPB     Timer/Counter0 Compare Match B
[*] TIMER0 OVF       Timer/Counter0 Overflow
[*] SPI STC          SPI Serial Transfer Complete
[*] USART RX         USART Rx Complete
[*] USART UDRE       USART Data Register Empty
[*] USART TX         USART Tx Complete
[*] ADC              ADC Conversion Complete
[*] EE READY         EEPROM Ready
[*] ANALOG COMP      Analog Comparator
[*] TWI 2-wire       Serial Interface
[*] SPM READY        Store Program Memory Ready

What PaulS and deSilva are getting at is this: If two interrupts of the same type occur, it doesn’t keep count of them. So, if two INT0 events occur while interrupts are disabled, then when interrupts are enabled again, the INT0 service routine will be executed once, not twice.

Other interrupt flags are not inhibited (or affected in any way) by the INT0 flag, so after the INT0 interrupt is serviced, the others will be serviced in order of their respective priorities.



Very useful post Dave.

Also I think a lot of sketch examples posted around here using interrupts where the ISR changes the value of a globle variable do not protect the globle variable if a int or long (or other multi-byte) variable is being used. The interrupts() / noInterrupts() are important functions to use if wanting to avoid very hard to troubleshoot problems. Atomic and non-atomic operations should be understood if one is going to write their own ISR functions.