Hands on Interrupt Service Routine

Dear All,

At the moment i am stuck with my research because of lack of skills and experience while using ISR:s
As we all know there are many places where interrupts should be used. There one need to keep in mind couple of important things, which I dunno. For example one of them is flags! what exactly means flag=? and what is the meaning behind.

Here is a small program using interrupt service routine, where I have no idea why flag is used and actually what is does=? Any help is appreciated here ^^)...

the Code

volatile int state = HIGH;
volatile int flag = HIGH;
int count = 0;

void setup()
{
Serial.begin(9600);

pinMode(GREEN_LED, OUTPUT);

digitalWrite(GREEN_LED, state);

pinMode(PUSH2, INPUT_PULLUP);
attachInterrupt(PUSH2, blink, FALLING);
}

void loop()
{
digitalWrite(GREEN_LED, state);

if(flag) {
count++;
Serial.println(count);
flag = LOW;
}
}

void blink()
{
state=!state;
flag = HIGH;

Please read the forum "sticky" about how to post code.

There one need to keep in mind couple of important things, which I dunno

You need to keep in mind a couple of things, but you don't know what they are?

As we all know there are many places where interrupts should be used

and many where they shouldn't or are not necessary.

You have not posted the whole program, but it looks like the variable named flag (it could be named anything, even theInterruptHappened) is being set in the ISR so that the fact that the interrupt has occurred can be acted upon in the loop() function. This is a common technique used so that the ISR is short and returns as quickly as possible.

thanks for reply,

what means acted upon?, if i remove the line of code with flag = LOW in IF (), program is looping without end. Could you please explain me more elementary way?. Basically the hole program toggle the suitable led or or off, when the button is pushed, and counts the number of pushes in Serial monitor. But I cant understand why this program needs flag variable?.. ^^

Let us begin study:

  1. After power up, the ATmega328 Microcontroller (MCU) begins at location 0x0000 (= 0000h). From this
    location, it jumps to location 0x0040h (chosen by user) and keeps executing a program known as
    Main Line Program (MLP).

  2. Let us assume that this MLP is:
    Continuously blink the built-in LED (L) of the ArduinoUNo Learning Kit at 1-sec interval.

  3. Let us connect an external LED (LED1) with DPin-7 (Digital Pin 7) of arduino with a 2.2k resistor in
    series.

  4. Interrupting the MCU means telling it to suspend what is has been doing now (the MLP).

  5. The MCU has a physical pin (Pin-4/INT0) to receive interrupt request signal (IRQ signal) from
    outside. The IRQ-signal comes in the form of voltage which can be injected using a switch (K1).

  6. When an IRQ-signal arrives at IT)-pin of the MCU, the following events occur:
    (a) The MCU finishes the execution of the current instruction.
    (b) The MCU saves the return address onto Stack.
    (c) The MCU clears the I-bit of the SREG register to prevent the the occurence of another interrupt
    until the current Side Job (Interrupt Service Routune, ISR) is completed.
    (d) The MCU goes to ISR. Here the ISR program: Blink LED1 only for 3 times at 2-sec interval.
    (e) The MCU finishes the ISR.
    (f) The MCU re-enables the interrupt logic (put LH at I-bit of SREG), retrieves the return address
    from stack by executing the iret instruction.
    (g) The MCU resumes the MLP and keeps executing it.

  7. The following terminologies are involved in the contnet of interrupt process:
    Interrupt
    Interrupting Signal
    Interrupt Service Routine
    Main Line Prorgram
    Interrupt Pin
    etc.

Interrupt Flag (INTF0)
When an IRQsignal arrives at the INT0-pin the 1st bit (Bit-0) of the EIFR-register of the MCU
assumes LH-state. This bit is known INTF0 (Interrupt Flag Bit for INT0). If interrupt logic is enabled,
this flag bit generates the vectored interrupt signal for the MCU.

What is vectored interrupt?
What is polled interrupt?

thanks for detailed description, but lets say assume I have an attached interrupt. When the external impulse happens, and the MCU executes the interrupt protocol, then it gets back to the program where it left? and continues from there?, so basically I can write ton of stuff in code, and lets say if there is no interrupt the program will proceed according to code, and if an interrupt occurs, then it executes interrupt and then rolls back to the point of the code where it left and proceeds further from that point...

are there any essential things to keep in mind while using ISRs like some kind of precautions or specific things to take care of?

thanks for detailed description, but lets say assume I have an attached interrupt. When the external impulse happens, and the MCU executes the interrupt protocol, then it gets back to the program where it left? and continues from there?,

Yes. When an interrupt occurs, the current instruction will finish and then execute the ISR, then return to that point.

so basically I can write ton of stuff in code, and lets say if there is no interrupt the program will proceed according to code, and if an interrupt occurs, then it executes interrupt and then rolls back to the point of the code where it left and proceeds further from that point..

Yes.

are there any essential things to keep in mind while using ISRs like some kind of precautions or specific things to take care of?

Don't use functions that require interrupt to be enabled in an ISR. Includes, but not limited to, print(), delay().

are there any essential things to keep in mind while using ISRs like some kind of precautions or specific things to take care of?

Keep the code in the ISR to a minimum so that it runs quickly
No delay()s or Serial printing in ISRs. They are slow and use interrupts
Any variables used by both the ISR and other code in the program should be declared as volatile.
A little more esoteric, but an interrupt must not be allowed to happen whilst a variable larger than 1 byte, such as an int, is being updated in case the ISR interferes with the update. There are precautions that you can take about this.

The value of the variable named flag in the original program allows the code in loop() to determine that an interrupt has occurred and to act on it, ie in this case to print the value of count, thus avoiding the need to do it in the ISR