You are on page 1of 19

EXTERNAL INTERRUPT IN ATMEGA 16

-KELVIN MANOJ

WHAT IS INTERRUPT ?

Interrupt is very important and powerful concepts and features in microcontroller and microprocessor applications, Almost all the Embedded Application are make use of interrupts.

HOW DOES IT WORKS?

When an interrupt occurs, the normal flow of

instructions is suspended by the microcontroller or microprocessor and the code corresponding to the interrupt which has occurred is executed. Once the code corresponding to the interrupt is executed completely the execution again begins from the same instruction where it was stopped.

A SIMPLE REAL TIME EXAMPLE

If we want to move to the next slide we need to

press any key on keyboard now.


According to X86 PC Interrupt number 9 is

generated whenever a key is press on a keyboard.

INTERRUPT SERVICE ROUTINE (ISR)

Interrupt Service Routines are the smallest portions

of the program code that handle the interrupt requests. Each interrupt have an associated ISR. When an Interrupt is triggered, the processor breaks away from the current task, moves the instruction pointer to the ISR, and then continues operation. When the ISR has completed, the processor returns execution to the previous location.

WHAT HAPPENS WHEN AN INTERRUPT OCCURS


Microcontroller normally runs the main program instruction which is

being executed.
When interrupt is occurred program control is transfers to Interrupt

Service Routine (ISR).


ISR is performed by loading the starting address of the corresponding

ISR into program counter.


ISR code executes until the return from the interrupt instruction (RETI)

is encountered.
When ISR is over, the microcontroller resumes processing where it left

off before the interrupt occurred, i.e., program control is transfer back to the main program.

ATMEGA16 INTERRUPTS

There are 21 interrupts in ATMEGA16

microcontroller,.
4 are External interrupts and remaining 17 are

Internal interrupts.

EXTERNAL HARDWARE INTERRUPT

ATMEGA16 EXTERNAL INTERRUPT REGISTER

The main registers for External interrupt.


GICR (General Interrupt Control Register). MCUCR (MCU Control register). MCUCSR (MCU Control and Status

Register).

GICR REGISTER

Bit5, Bit6 and Bit7 called the interrupt masks are used to disable/enable the respective interrupt. Interrupt is disabled when bit value is set to 0 and enabled when bit value is set to 1. By default all the interrupts are disabled.

I-bit (Bit7, Global Interrupt Enable) of SREG register must also be set to 1. If Global Interrupt enable bit is set to 0, none of the interrupts will work irrespective of the other register settings. The set and clear of I-bit is done by SEI and CLI instructions.

MCUCR REGISTER

MCUCSR REGISTER

STEPS FOR PROGRAMMING

Clear Global Interrupt enable bit in SREG register. Initialize the interrupt by appropriately configuring the

GICR, MCUCR and MCUCSR registers.


Set Global Interrupt Enable bit in SREG register.

Define the appropriate Interrupt service routine (ISR) for

the interrupt.

USEFUL MACROS IN avr/interrupt.h

#define ISR(vector,attributes)
#define SIGNAL(vector) #define sei() #define cli() #define reti()

// interrupt handler function


// interrupt handler function // enable global interrupt

// disable global interrupt //return interrupt

TWO WAYS OF WRITING ISR

ISR for INT0 can be written in following two ways: ISR (INT0_vect) SIGNAL (SIG_INTERRUPT0)

PROGRAM:
#include <avr/io.h> #include <util/delay.h> #include <avr/interrupt.h> ISR (INT0_vect) { PORTA = ~PORTA; _delay_ms(100); } int main(void) { DDRA = 0xFF; PORTA = 0xFF; cli(); GICR =(1<<INT0); MCUCR =(3<<ISC00); sei(); while(1); } //io port registers //to use delay function //interrupt macros eg. sei(), cli().. //ISR for INT0 //toggle PORTA //wait time for toggle

//configure PORTA as output port //Make all 8 pins in PORTA High //Disable Global Interrupts //Set Bit6 of GICR to unmask INT0 interrupt. //Configuring MCUCR for Rising Edge interrupt for INT0 //Enable Global Interrupts

CIRCUIT:

APPLICATIONS OF EXTERNAL INTERRUPT

To interface IR module. To Interface buttons and keyboard.

Sensors like proximity sensor, Heart beat sensor, reed switch etc..
It is efficient to write a code for External Interrupts whenever

there is an asynchronous interaction between microcontroller/processor and outside world.

THANK YOU