Skip to main content
Arduino

You are not logged in. Your edit will be placed in a queue until it is peer reviewed.

We welcome edits that make the post easier to understand and more valuable for readers. Because community members review edits, please try to make the post substantially better than how you found it, for example, by fixing grammar or adding additional resources and hyperlinks.

Required fields*

Required fields*

Using interrupts as flags/latches without any ISR

I've got an Arduino Mega periodically running an (~400ms) operation that is sensitive to repeatable timing, so I don't ever want it to be interrupted. But, I'd like to be able to use an interrupt flag from one of the built-in peripherals (PCINT, comparator, etc.) to check afterward if an external event (say, a pin going high momentarily) ever happened during the operation.

That is, it'd be nice if I could just treat one of the interrupt flags as a kind of async hardware latch that I can check (and reset) at my next earliest convenience instead of having my code interrupted.

It wouldn't be hard to add a discrete latch IC, but I was hoping I could get away with just using what was already built into the AVR without adding more hardware (and using an extra pin to reset the latch).

Looking at the documentation for each of the peripherals, it was unclear whether the flags will still be set when the interrupts themselves are disabled. I saw a mention someplace of an "EMPTY_INTERRUPT" macro, but I'm guessing that still generates an interrupt vector with a single "return" instruction, so I'd still be losing cycles to the interrupt preamble, etc.

Can this be done without any ISR being called at all? Or do I need to add more hardware? I'm not sure wrapping the long-running task in noInterrupts()/interrupts() is a choice unless that also doesn't interfere with UART receiving. Thanks!

Answer*

Draft saved
Draft discarded
Cancel
8
  • Are you certain about that? In the datasheet it says The External Interrupts 7 - 4 are activated by the external pins INT7:4 if the SREG I-flag and the corresponding interrupt mask in the EIMSK is set. My reading of that was that the flag is set if the interrupt is enabled. Otherwise, how would it know if it is rising or falling? Commented Jul 2, 2023 at 10:29
  • I might be wrong about that, I admit. Commented Jul 2, 2023 at 10:31
  • @NickGammon: In edge-detection mode, the flag is set even if the interrupt is disabled (I just checked). However, this is a bad example, as the interrupt flag does not work in level-detection mode: "These flags are always cleared when INT7:0 are configured as level interrupt." I changed the example in my answer. Commented Jul 2, 2023 at 12:34
  • 2
    I left a comment above, too, but the actual process period is ~200µs (repeated many times) while exposing an image to a camera sensor. And it's less sensitive to any particular clock rate. I just want each identical busy-wait to take the same wall-clock time so there isn't any brightness difference between each exposure. Even that 2.6µs gives around 1.3% additional illumination. On a 12-bit sensor, that starts to add up quickly. The UART intentionally isn't sending or receiving during that period, so hopefully it's alright to just leave it enabled... Commented Jul 2, 2023 at 23:43
  • It seems to me that the question of whether the flag is set or not might be somewhat implementation-specific. Even if it is 100% guaranteed for AVR chips it might not be for others, hence relying on that behaviour is not very good practice. Commented Jul 3, 2023 at 6:49

AltStyle によって変換されたページ (->オリジナル) /