An interrupt is a hardware initiated procedure that interrupts whatever program is currently executing. In AVR an interrupt is a service routine that executed when it is called and back to the main program where it is halted after executing service routine. In AVR there are two types of interrupts.
i. External interrupts
In external interrupt external source causes interrupt. Some pins are defined as external pin for generating Interrupt, whenever any eternal pin is pressed if active, then the corresponding service routine will execute first halting the main program. After the executing the interrupt service routine the main program will continue with where it has been halting.

ii. Internal interrupts
Those interrupts can be occurred by using program. In interrupts there is an important factor which is purity. Each interrupt will execute according to there purity. Suppose both INT0 and INT1 external interrupt pin has been pressed at the same time, then only INT0 will be executed. Lets look at the purity according to the datasheet of ATmega32 provide-

In this article we begin our journey with external interrupt only. Internal interrupt will be discussed later but the fundamental remain the same. Before we discuss about the register available and our task lets discuss about the bits of a register and there operation.
Bit Operation -> SREG(Status Register)

In the Status Register there is 8bit=1byte. To enable all interrupt we need to set the bit 7 i.e. I bit. Since according to C program to set a particular bit
variable_name |=(1<<bit position); // To set a particular bit
And to clear a particular bit
variable_name &=~(1<<bit position); // To clear a particular bit
We need to set the I bit of Status register without affecting the other bits. So we use C control logic to all of our register section. So that the condition to

The R/W indicates that those bits can be both read and write. Note that the initial value indicates that the default value is 0, means that initially the bit is clear. So no need to clears the bits first. Now looks at the registers that are available for external interrupts

To work with interrupts three steps must be setup
- Set the global Interrupt Enable bit in SREG Register.
- Initialize the interrupt by appropriate configuring the MCUCR, MCUCSR and GICR register.
- Define the appropriate Interrupt Service Routine (ISR) for interrupt.
Step1: To set the Global Interrupt Enable bit
#include<avr/interrupt.h>
sei(); or SREG|=(1<<I);
To clear Global Interrupt Enable bit
#include<avr/interrupt.h>
cli(); or SREG&=~(1<<I);
Note that to use an interrupt we should need a header file- #include<avr/interrupt.h>
Step2: Lets make a simple program that set PORTC of Atmega 32 from up to down and when an interrupt occur it set PORTC from down to up. In this case we use INT0 pin and the control logic is a rising edge of INT0 generates an interrupt. In first case we already enable the Global interrupt enable bit. So our target is to enable the INT0 (External Interrupt request 0) bit and then the control logic.
sei(); //SREG|=(1<<I); //Global interrupt enable
GICR |=(1<<INT0); //External Interrupt Request 0 Enable
MCUCR|=(1<<ISC00)|(1<<ISC01); //The rising edge of INT0 generates an interrupt request
PORTC from up to down
void uptodown(void)
{
for(int16_t i=0;i<=7;i++){
PORTC |=(1<<i); _delay_ms(50);
PORTC &=(~(1<<i));_delay_ms(50);
}
}
PORT from down to up
void downtoup(void)
{
for(int16_t i=7;i>=0;i--){
PORTC |=(1<<i); _delay_ms(50);
PORTC &=(~(1<<i)); _delay_ms(50);
}
}
Step3: In this step we have to define appropriate Interrupt service routine (ISR). An interrupt routine is defined with ISR(). This micro register and () mark the routine as an interrupt handler for the specific peripheral. The following is an example definition of a handler for the INT0 interrupt.
#include<avr/interrupt.h>
ISR(INT0_vect)
{
;//user defined code here
}
OR simply open any internet browser and go to:
file:///C:/WinAVR-20100110/doc/avr-libc/avr-libc-user-manual/index.html
OR open it in your computer -> C:/WinAVR-20100110/doc/avr-libc/avr-libc-user-manual/index.html
–>Library Reference
–><avr/interrupt.h>: Interrupts
Download the int0.c and int0.h files. The main program as follow –
/************************************************************************************************
*************************************************************************************************
This program will set PORTC form up-to-down and down-to-up when external trigger to INT0 bit
************************************************************************************************
***********************************************************************************************/
#include<avr/io.h>
#include<util/delay.h>
#include<avr/interrupt.h>
#include"int0.h"
uint16_t sample=0;
int main(void)
{
DDRC=0xFF; // port c for output
int_int0(); //INT0 interrupt enable call
while(1)
{
sample ? uptodown() : downtoup();
}
return 0;
}
ISR(INT0_vect)
{
sample^=0x01; //Toggle condition
}
Note that in header file we include #include<avr/interrupt.h> , that is because in INT0 initialization we include sei(); function that is the function of #include<avr/interrupt.h> header file








Visit Today : 49
Total Visit : 28464