Reputation: 11112
I'm brainstorming an application where there could be several interrupts per second from two different sources (separate interrupts), each running a function that simply adds a number to a count. I need my void loop()
to perform simple analysis with that data. I was wondering if the interrupts ran asynchronously while the main loop is running or if they stopped the main loop in the middle of its processing?
My main loop does require the millis()
function to be working properly, which I know isn't possible in an interrupt per the Arduino reference, and if the interrupts run synchronously I will have to look at other solutions.
Upvotes: 3
Views: 5837
Reputation: 959
All hardware external interrupts are async, that's generally the whole idea behind having interrupts. Now if you're asking how to code that into your loop I won't be any help because it's been a LONG time since I played with any ATMega chips, or an Arduino. But take a look at the link. Specifically section 12 on interrupts. This is assuming your using an Ardunio with an ATMega128 which is what the newest are I believe.
But the same concepts work for almost all the ATMega chips, especially ones used in the Arduino boards. The documentation has sample code to work with too. It's an essential document if you want to get the most of the chip.
Upvotes: 1
Reputation: 66243
I'm not sure what you mean that interrupts run synchronously or asynchronously.
When an interrupt occurs, the main program is stopped and the interrupt service routine (ISR) is executed in a mode where no new interrupts are recognized. Upon leaving the ISR the main program will be continued where it has been interrupted.
Real parallel execution is not possible on the Arduino, because the ATMega is a single-core CPU and can do only one thing at a time. But it can switch fast :-) Therefore:
My main loop does require the millis() function to be working properly,
As long as you don't call millis()
inside the ISR this is OK because your ISR is
a function that simply adds a number to a count
and therefore very fast. This will not disturb millis()
enough to be noticed by anybody.
Upvotes: 5