Interrupt AVR

What is Interrupt ?

Interrupt are the powerful signal that interfere the main process being done inside the system .IT perform events that require immediate attention by the microprocessor.

When an interrupt occurs, the current program execution is stopped, the context is saved and the control jumps to ISR( Interrupt Control Routine ). At the end of ISR, the microcontroller returns to the task it had paused and continue its normal operations.

What is Interrupt application ?

  • Switching off the device when it get hang.
  • Performing time Critical and time specific task(i.e alarm clock)
  • Interrupts are used in type ahead feature for buffering events like keystrokes.
  • Different device generate signal as interrupt to denote the state of work so the critical task can be performed on time

What are Type of Interrupt and their number present ?

The AVR 8-bits microcontroller provide two type of interrupt

  1. Internal interrupt sources : These interrupt signal are generated within microcontrollers by peripheral devices i.e Timer/Counter, Analog Comparator, Serial, ADC( Analog to Digital Converter),etc. They generally denote the state of work being performed by peripheral.

  2. External interrupt sources: These Interrupt are generated as result of user activities such as pressing switch. There are 4 external interrupts.

    • External interrupt 0 (INT0)
    • External interrupt 1 (INT1)
    • External interrupt 2 (INT2)

How Does Interrupt work ?

Upon the triggering of an interrupt the following sequence is followed by the microcontroller providing that the both the specific interrupt and global interrupts are enabled in the microcontroller:

  1. Upon the generation of interrupt signal , microcontroller completes the execution of the current instruction, stores the address of the next instruction (the content of the PC) on the stack.

PC : A program counter is a memory that contains the address (location) of the instruction being executed at the current time. The CPU execute the instruction which address is contain in the PC

  1. The function of the triggered interrupt signal is then loaded in the PC and the microcontroller starts execution from that point up until is reaches a end instruction.

  2. Upon the end of execution of function , the address that was stored on the stack in step 1 is reloaded in the PC

  3. The microcontroller then start executing instructions from that point. That is the point that it left off when the interrupt was triggered.

Step to use Interrupt (for only external interrupt) ?

  1. Include necessary header file : Header file necessary for the interrupt is :
#include <avr/interrupt.h>
1
  1. Enable the global interrupt: Enabling global interrupt can be done by the below code
sei(); //enable global interrupt
1
  1. Enable Specific Interrupt : It can done by setting bits on the GICR register (General Interrupt Control Register)
GICR = 1<<INT0; //enable int0
GICR = 1<<INT1; //enable int1
GICR = 1<<INT2; //enable int2
1
2
3
  1. Specify the nature of the signal for interrupt generation (i.e rising edge , falling edge): It is done by setting ISC11, ISC10 in MCUCR (MCU Control Register) for INT1 , ISC00, ISC01 in MCUCR (MCU Control Register) for INT0 and ISC2in MCUCSR (MCU Control and Status Register) for INT2
MCUCR = 1<<ISC01 | 1<<ISC00; //rising edge for INT0
1
  1. Make the interrupt pin as Input

  2. Define Function for Interrupt

ISR(INTn_vect)
{
  // work to be performed
}
1
2
3
4

Demo Program

WAP to blink led when switch is pressed , it must be interrupt signal to Controller .

#include <inttypes.h>
#include <avr/io.h>
#include <avr/interrupt.h>
#include <avr/sleep.h>
#include <util/delay.h>


ISR(INT0_vect)
{
    //turning on led only when switch is turned on
    PORTC |= (1<<PC0);
    _delay_ms(500);
    PORTC &= ~(1<<PC0);
   _delay_ms(500);
   
}
int main()
 { 
   DDRD = 1<<PD2; //setting PD2 as input
   PORTD = 1<< PD2; // pull up resistor
   DDRC = 0xFF; //setting DDRC as output
    
    GICR = 1<<INT0; //enable int0
    MCUCR = 1<<ISC01 | 1<<ISC00; //rising edge
    
    sei(); //enable global interrupt
  
  while(1){
    }
    
      
   return 0;
 }
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33

Practise Question

  • make use of interrupt 1?