Top Banner
EXTERNAL INTERRUPT IN ATMEGA 16 -KELVIN MANOJ
19

External Interrupt in Atmega 16

Nov 25, 2015

Download

Documents

kelvinmanoj

detailed explanation about external interrupt in atmega 16
Welcome message from author
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
Transcript

External interrupt in Atmega 16

External interrupt in Atmega 16

-KELVIN MANOJWhat is interrupt ?Interruptis 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 themicrocontroller or microprocessorand 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 exampleIf 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 occursMicrocontroller 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 InterruptsThere are 21 interrupts in ATMEGA16 microcontroller,.4 are External interrupts and remaining 17 are Internal interrupts. External Hardware interrupt

ATmega16 external interrupt registerThe 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)ofSREGregister 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.MCUCRRegister

MCUCSR Register

Steps FOR PROGRAMMINGClear 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) // interrupt handler function#define SIGNAL(vector) // interrupt handler function#define sei() // enable global interrupt #define cli() // disable global interrupt#define reti() //return interrupt

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

PROGRAM:#include //io port registers#include //to use delay function#include //interrupt macros eg. sei(), cli()..ISR (INT0_vect) //ISR for INT0 { PORTA = ~PORTA; //toggle PORTA _delay_ms(100); //wait time for toggle } int main(void) { DDRA = 0xFF; //configure PORTA as output port PORTA = 0xFF; //Make all 8 pins in PORTA High cli(); //Disable Global Interrupts GICR =(1