Anda di halaman 1dari 19

AVR Microcontroller

Prepared by:
Eng. Ashraf Darwish

Ashraf.emad.darwish@gmail.com

Session 4

Interrupts (External interrupt)


Code wizard

Interrupts
Introduction:
how does program execution happen ?
program commands are executed one by one

( serially ) , microcontroller cannot handle more than


one task at a time.

on the rising edge of the clock, a new operation is

carried out .

Interrupts

what is the meaning of interrupts?


interrupts are internal or external events

that when occurred they get the


microcontroller attention.
In this case microcontroller gives that event
the first priority to execute and after finish it
returns back to the normal execution.

Interrupts

When interrupt occurs the following


sequence happens :

1. Microcontroller wait until the current instruction

completed.
2. The address of the next instruction is stored on
the stack ( apart of chip memory )
3. Address of Interrupt Service Routine (ISR) is
loaded into the program counter.
4. Microcontroller start execute the ISR
5. Program counter loaded with the value stored on
the stack and the program resumed its normal
operation .

Interrupts

there are many sources of interrupts,


all are arranged according to their
priorities in the Interrupt vector table

Interrupts

Interrupts

The important Interrupt sources :


External Interrupt ( INT0,INT1,INT2 )
Timer / counter Interrupts
USART Interrupt ( send , receive )
ADC Interrupt
SPI interrupt

External Interrupt
Interrupt registers :
1.

General Interrupt control Register (GICR)

External Interrupt 0,1,2


Enable
0=disable , 1=enable

External Interrupt
Interrupt registers :
2.

MCU Control Register (MCUCR)

This register detects the interrupt sense for INT0,INT1


( it could be rising edge , falling edge , low level , any change )

External Interrupt
Bit 3,2 ISC11 , ISC10 :
For INT1

External Interrupt
Bit 1,0 ISC01 , ISC00 :
For INT0

External Interrupt
Interrupt registers :
3.

MCU Control and status Register (MCUCSR)

For INT2
Bit6 ISC2 :
0
falling edge for INT2
1
Rising edge for INT2

External Interrupt
Interrupt registers :
4.

General Interrupt Flag register (GIFR)

Bit7 INTF1 : External interrupt 1 flag


when External INT1 source triggered this flag will be
high
Bit6 INTF0 : External interrupt 0 flag
Bit5 INTF2 : External interrupt 2 flag

External Interrupt
Very Important note:
You should set the global interrupt on
in your code as following
#asm(sei)

External Interrupt
Where would I write the code which will
be executed when interrupt occur ?
#include <mega16.h>
Interrupt [EXT-INT0] void ext_in0_isr(void)
{
// write your interrupt code here
}
Void main ( )
{
// write your normal code here
}

Codewizard

The codewizard is a wizard tool in


Codevision which easy writing the
code

You can use Codewizard to determine


all your I/O ports, Select chip , LCD,
Interrupts, Timers , ADC , USART
Etc

Codewizard
see the Attached Tutorial

Thank you
Contact:
http://www.facebook.com/groups/263197427113025
/
Ashraf.darwish@ymail.com
Ashraf.emad.darwish@gmail.com
+201064897791

Anda mungkin juga menyukai