How long does an interrupt take
I sadly can't find those posts anymore, so I can't link them. As far as I can remember, they all said, that if you spent to much time in the ISR, the microcontroller might crash. Is that true? And if so, is there an exact time limit after that this might happen?
But , this is a bad habit to get into, and it means that if you want to do anything else, you'll likely have to rework the code. A particular case is if the MCU is using a serial library, that expects interrupts to be working often enough to service individual characters received.
If you block the interrupts for longer than that, you risk losing input characters. As a general rule, do the absolute minimum in an ISR. In your application, a reasonable design would be to have an interrupt every ms, which increments and checks a counter value.
I'm sure you can work out some suitable logic to set and test the counter to get ms between turn on and turn off events. But in general, it's poor design practice to spend more time in an ISR than absolutely necessary, because it prevents all other code from running at all. This is a big issue for anything other than trivial programs. While the practice is to allocate the minimum possible execution cycles inside an interruption, and beside other general hardware specifications, there are not technical limitations for increasing them, if there are not any other interruption to be executed.
At attachInterrupt Arduino Reference :. Generally, an ISR should be as short and fast as possible. If your sketch uses multiple ISRs, only one can run at a time, other interrupts will be executed after the current one finishes in an order that depends on the priority they have. Since delay requires interrupts to work, it will not work if called inside an ISR.
Having 25 possible interruptions in this processor family, it is encouraged to deal with them like punctual events, for allowing other interruptions to happen. Technically, it's not forbidden to even start an infinite loop inside an interrupt, so there is no top limit on ISR execution time.
However, interrupts are most useful when you want to, well, interrupt your normal program flow to take a short action which must be carried out immediately , and the qualifiers "short" and "immediately" are naturally related: if your longest ISR takes 1ms, then an incoming interrupt of the same priority will have 1ms response time. If your program spends a long time waiting in an interrupt, it may be easier to drop interrupts completely and use polling, which makes programming substantially easier.
There are cases where you can abuse interrupts to run more or less regular code. One example would be to implement task priority: a low-priority task is started from the main loop, while a higher-priority task is a periodically-triggered timer interrupt. You have a single-core, single-threaded CPU. This means that at any given time, it's doing exactly one thing. If your application requires it to do several things, then the code has to be designed to switch between all of them.
This can be as trivial or as complex as you like. Others automatically switch to a special ISR stack or to a kernel stack. The kernel may switch the stack as well, if needed.
Yep, or you're risking to hang your computer. You see, interrupts interrupt processes and threads. It hangs, especially if it's a single-CPU system. In fact, in Windows since Vista?
Anyway, it's not a normal situation and typically there's no way out of it other than a system reset. Look up watchdog timers. They help to discover such bad hangs and perform a reset. Many electronic devices have them. Think about interrupt handler as a function running in its own thread with high priority. When interrupt is set by device, any other activity with lowest priority is suspended, and ISR is executed. This is like thread context switch.
Any activity with lower that ISR priority is not allowed, so computer looks dead. However, it still reacts on the hardware remote debugger commands, if one is attached.
Stack Overflow for Teams — Collaborate and share knowledge with a private group. Create a free Team What is Teams? Collectives on Stack Overflow. Learn more. The urgent event must set the interrupt flag inside the CPU. Interrupts may be disabled. So, the processor must wait for the interrupts to be enabled again. There may be two reasons for that - either you disabled interrupts intentionally, or ISR is already executing. In the later case, the interrupts will be re-enabled only when the ISR quits.
The hardware takes some time to remember where the execution is, save some registers, and then call the first instruction in the ISR 4. If you program in C, the prolog code executes which saves more registers. If there's a CPU with different interrupt levels such as PIC18 and the interrupt in question is low priority, then there may be additional delays.
Anywhere during steps 2 to 4, a high priority interrupt may happen. Worse yet, there may be several higher priority interrupts. And once one of them takes over, even more high priority interrupts may happen.
So, the low priority interrupt will have to wait for all of them to execute. In short, you can create ideal conditions which will produce very low latency. Or your latency may be very long and unpredictable. Latest Posts. Active Posts. All FAQs.
0コメント