AVR Interrupts  
What are Interrupts ???

Consider that a teacher is conducting a lecture. He wants to make sure that at all times everyone undersatnds what he is explaining. He has two choices to ensure this:

  • Ask every student in class if he/she has understood the concept.

  • Any student raises his/her hand and asks him when he/she has a doubt.

    So the thing is, the first of these two alternatives is called polling. whereas the second is called interrupt. So which one is better ??? Ofcourse first approach is very tedious and time consuming.

    In above consideration, lets take teacher is our CPU which provide resources to its student (Peripheral devices).So the approach of interruption is much better in most of the cases.

    Upon receiving interrupt signal from the peripheral device, CPU stop doing its current routine and jumps to a different code segment or we can say it as an "Interrupt Service Routine". ISR contains the tasks to be handeled by CPU whenever it acknowledge some interrupt signal.

    Interrupts in AVR Microcontroller.

    AVR is very rich in number of interrupt sources. From all its internal/ external perpherals ATmega16 has total 21 Interrupt sources.

    Any interrupt source can interrupt CPU only if Global interrupt enable bit 'I' in SREG register is set. This bit can be set by calling function sei(); and can be cleared by function cli();

    Some of the interrupt controls are done by modifying GICR register (General Interrupt Control Register).

    BIT7 - INT1 (if '1' external interrupt1 enable)
    BIT6 - INT0 (if '1' external interrupt0 enable)
    BIT5 - INT1 (if '1' external interrupt2 enable)
    BIT1 - IVSEL (if '0' interrupt vectors are placed at start of Flash        memory, if '1' vectors are moved to start of boot loader     section of flash.)
    BIT0 - IVCE (if '1' enable changes in IVSEL)

    External Interrupts
    External interrupts are those interrupts that interrupt CPU when logic level on pins INT0, INT1 or INT2 changes, accordingly these are known as external interrupt 0 ,1 and 2 respectively.

    MCU Control Register -
    MCUCR register contains bits to control interrupt sense control.

    General Interrupt Flag register - All interrupts basically gets CPU attention on interrupt request by asserting some flag.

    BIT7 - INF1 (INTF1 =1 when INT1 request for attention)
    BIT6 - INF0 (INTF0 =1 when INT0 request for attention)
    BIT5 - INF2 (INTF2 =1 when INT2 request for attention)

    Suppose we have a task to blink a led whenever a high to low edge is detected on the corresponding External Interrupt Pin. So when i write the code for that purpose it is said that i am writing an external interrupt service routine or interrupt handeller.

    1. We must set I (Interrupt) Flag high in Status register taht is SREG to use any kind of interrupts. This flag can be set by software using instruction like SEI or CLI in asm but in ‘C’ we can directly call functions like “sei()” to set I flag and “cli()” to reset I flag.

    2. 6th bit of GICR that is General Interrupt Control Register must keep high to use external interrupt 0 in your application. INT0 bit can be set to one by executing following statment.

    GICR=(1<<INT0); // similarly for INT1 or INT2

    /******************************************************************* Example program to demonstrate External interrupt 0
    Blink LEDs if INT0 occurs.

    #include<avr/interrupt.h> // header that defines addresses for INT vect

    int main(void)
    PORTD=0xFF; // PIN INT0 initially pulled high
    PORTA=0x00; // LED initially Off
    sei(); // Interrupt flag set
    GICR=1<<INT0; // INT0 enabled
    sbi(MCUCR,1); // falling edge at INT0 generates interrupt
    return 0 ;

    SIGNAL( SIG_INTERRUPT0 ) // SIGNAL is a macro for interrupts

    Best viewed at Firefox 1024x768 resolution