Timer Interrupt

Hi

Does a timer interrupt run parallel to the loop? If there are 5 timer interrupts they all run in parallel? Which one is served first?

Is this separate part of the processor? How can I imagine that?

Thanks

When an interrupt is triggered by a Hardware Timer (or by anything else) the code that is currently executing is halted, the Interrupt Service Routine (ISR) runs and when it completes execution returns to the next instruction iin the main code.

There is an order of priority among the interrupts (see the relevant microprocessor datasheet for details) which determines which ISR runs first if two interrupts happen at the same time.

While an ISR is running interrupts are disabled so the ISR should be designed to complete very quickly - 100 microsecs would be a long time for an ISR.

Whether you really need a Timer interrupt is another thing entirely.

...R

gab27:
Does a timer interrupt run parallel to the loop? If there are 5 timer interrupts they all run in parallel? Which one is served first?

1. Does a timer interrupt run parallel to the loop?
The MCU continuously performs the tasks that are within the loop() function. In the following example, the MCU keeps printing the name Arduino at 1-sec interval (+ some delay due to other codes within the loop).

void loop()
{
   Serial.peintln("Arduino");
   delay(1000);
}

TC1 (Timer/Counte-1) Module of Atmega328P MCH has a dedicated hardware which can count pulses coming from external/internal sources in parallel to the loop() function. It means that while the loop() function is printing the name Arduino at 1-sec interval, the TC1 is also counting pulses.

At the end of counting a pre-set number of pulses (equivalent to 2-sec time elapse, for example), the following events occur:
(1) the TC1's overflow flag interrupts the MCU;
(2) the MCU suspends (halt?) the execution of the current program (the loop() function);
(3) the MCU disables interrupt logic to prevent itself from responding to another interrupt until the current ISR (Interrupt Sub Routine) is completed;
(4) the MCU goes to ISR and toggles the state of L (the built-in LED of UNO, for example);
(5) the MCU returns (during this return time, the interrupt logic is automatically enabled) to the suspended program (the loop() function).

2. If there are 5 timer interrupts they all run in parallel?
All the 5 TC Modules have independent hardware, and they are counting pulses in their own ways (in parallel mode).

3. Which one is served first?
The Atmega328P MCU has 3 TC Modules -- TC0, TC1, and TC2. Assume that they are configured to generate interrupts at 1-sec interval and they are started at the same time. After 1-sec interval, all 3 TCs will generate interrupt signals to the MCU. It is which TC that will draw MCU's attention first? The priority is automatically determined by the MCU depending on the values of the vector addresses of the TC interrupts -- the interrupt with lower value of vector address will be served first; the other interrupts remain pending but will be executed later on when their turns arrive. In ATmega328P, the TCs have the following vector addresses:

TC2 Overflow Interrupt : 0x0012
TC1 Overflow Interrupt : 0x001A
TC0 Overflow Interrupt: 0x0020