Professional Documents
Culture Documents
10 Interrupt v21 Rev1
10 Interrupt v21 Rev1
10 Interrupt v21 Rev1
Chapter 10
Sepehr Naimi
www.NicerLand.com
Contents
Polling Vs. interrupt
Interrupt unit
Steps in executing an interrupt
Edge trigger Vs. Level trigger in external
interrupts
Timer interrupt
Interrupt priority
Interrupt inside an interrupt
C programming
2
Polling Vs. Interrupt
Two methods by which a device receives service from a MCU:
Interrupt
Polling Whenever a device needs the MCU
MCU monitors the status of the service, it notifies the MCU by
device and performs the service sending an interrupt signal. The
when status condition is met. MCU stops whatever it is doing and
Ties down the CPU serves the device.
Efficient CPU use
while (true)
Has priority
{ Can be masked
if(PIND.2 == 0) main( )
//do something; {
} Do your common task
}
whenever PIND.2 is 0 then
do something
3
Interrupt Controllers
Peripherals
Timers
IRQ0
CPU IRQ1 I/Os
Interrupt IRQ2
Controller IRQ3 USART
….
IRQn
SPI
4
Interrupt control unit in AVR
Program
Bus Bus
CPU
SREG
EIMSK
PCICR
PCMSK0
PCMSK1 Other
OSC PCMSK2 Ports
Peripherals
Interrupt Unit
TIMSK0
TIMSK1
TIMSK2
I/O
PINS
5
Interrupt vectors in ATmega328
Interrupt vector
Interrupt Address
table (hex)
Reset 0000
External Interrupt Request 0 0002
External Interrupt Request 1 0004
Pin Change Interrupt Request 0006
Pin Change Interrupt Request 0008
Pin Change Interrupt Request 000A
Watchdog Time-out Interrupt 000C
Timer/Counter2 Compare Match A 000E
Timer/Counter2 Compare Match B 0010
Timer/Counter2 Overflow 0012
Timer/Counter1 Capture Event 0014
Timer/Counter1 Compare Match A 0016
Timer/Counter1 Compare Match B 0018
Timer/Counter1 Overflow 001A
Timer/Counter0 Compare Match A 001C
6
Interrupt execution steps
The MCU finishes the instruction it is currently executing and
saves the address of the next instruction (PC) on the stack
It jumps to a fixed location in memory called “interrupt vector
table”, which directs the MCU to the address of the interrupt
service routine (ISR).
The MCU starts to execute the ISR until it reaches the last
instruction of the subroutine, which is the RETI (return from
interrupt)
The MCU returns to the place where it was interrupted: first it
gets the PC address from the stack by popping the top byte of
the stack into the PC, then it starts to execute from that addr.
7
Interrupt sources in AVR
At least 2 interrupts for each timer (overflow and compare
match).
Three external hardware interrupts. In atmega32, Pins PD2,
PD3 and PB2 are for external hardware interrupts INT0, INT1,
and INT2 respectively.
3 interrupt for Serial communication’s USART: one for receive
and two for transmit (to cover in ChP11)
SPI interrupts (Chp17)
ADC Chp 13
8
Steps in enabling an interrupt
Bit D7 (I) of the SREG register set to high to allow interrupts.
This is done with the SEI (set interrupt) instruction or macro (for C)
If I=1, each interrupt is enabled by setting to HIGH the
interrupt enable (IE) flag bit for that interrupt.
TIMSK register holds the interrupt enable bits for Timer0,1 and 2.
If I=0, no interrupt will be responded to, even if its IE bit is high.
9
Timer interrupt Mask
10
Review Questions
1. Of the interrupt and polling methods, which one avoids tying
down the MCU?
2. Give the name of the interrupts in the TIMSK register.
3. Upon power-on reset of the ATmega32, what memory area is
assigned to the interrupt vector table? Can the programmer
change the memory space assigned to the table?
4. What is the content of D7 (I) of the SREG register upon
reset, and what does it mean?
5. What address in the interrupt vector table is assigned to
Timer1 overflow and INT0 interrupts?
11
Review Questions- answer
1. Of the interrupt and polling methods, which one avoids tying
down the MCU? interrupts
2. Give the name of the interrupts in the TIMSK register.
Timer0 overflow, Timer0 compare match, Timer1
overflow, Timer1 compare match, Timer2 overflow,
Timer2 compare match,
3. Upon power-on reset of the ATmega32, what memory area is
assigned to the interrupt vector table? 0x00 to 0x28, Can
the programmer change the memory space assigned to the
table? No, it is set when the processor is designed
4. What is the content of D7 (I) of the SREG register upon
reset, and what does it mean? I=0, meaning that all
interrupts are masked (no interrupt will be responded
to by AVR)
5. What address in the interrupt vector table is assigned to
Timer1 overflow and INT0 interrupts? 0x12 (for Timer1
overflow) and 0x02 for INT0
12
External Interrupts
13
External Interrupts (ATmega328)
D7 D0
EIMSK: - - - - - - INT1 INT0
28 pin
(PCINT14/RESET) PC6 1 28 PC5 (ADC5/SCL/PCINT13)
(PCINT16/RXD) PD0 2 27 PC4 (ADC4/SDA/PCINT12)
(PCINT17/TXD) PD1 3 26 PC3 (ADC3/PCINT11)
(PCINT18/INT0) PD2 4 MEGA328 25 PC2 (ADC2/PCINT10)
(PCINT19/OC2B/INT1) PD3 5 24 PC1 (ADC1/PCINT9)
(PCINT20/XCK/T0) PD4 6 23 PC0 (ADC0/PCINT8)
VCC 7 22 GND
GND 8 21 AREF
(PCINT6/XTAL1/TOSC1) PB6 9 20 AVCC
(PCINT7/XTAL2/TOSC2) PB7 10 19 PB5 (SCK/PCINT5)
(PCINT21/OC0B) PD5 11 18 PB4 (MISO/PCINT4)
(PCINT22/OC0A/AIN0) PD6 12 17 PB3 (MOSI/OC2A/PCINT3)
(PCINT23/AIN1) PD7 13 16 PB2 (SS/OC1B/PCINT2)
(PCINT0/CLKO/ICP1) PB0 14 15 PB1 (OC1A/PCINT1)
Edge/level
PD2(INT0)
detector
EIMSK.INT0 PC=0x0002
I bit of SREG
EICRA.ISC0x
14
External Interrupts (ATmega32)
15
Edge trigger Vs. Level trigger in external interrupts
Bit D7 D0
ISCx1 ISCx0
0 0
0 1
1 0
1 1
16
Review Questions (ext int)
1. True or false. Upon reset, the external hardware INT0-INT2
are edge triggered.
2. For Atmega32, what pins are assigned to INT0-INT2?
3. Show how to enable the INT1 interrupt.
4. Assume that the external hardware interrupt INT0 is
enabled, and is set to the low-edge trigger. Explain how
this interrupt works when it is activated.
5. True or false. Upon reset, the INT2 interrupt is falling edge
triggered.
6. Assume that INT0 is falling edge triggered. How do we
make sure that a single interrupt is not recognised as
multiple interrupts?
17
Review Questions-answers (ext int)
1. True or false. Upon reset, the external hardware INT0-
INT2 are edge triggered (only INT2 is).
2. For Atmega32, what pins are assigned to INT0-INT2?
• Bits PD2, PD3, and PB2 are assigned to INT0-2.
3. Set the INT1 bit of GICR register (in addition to the I-
bit)
4. Upon application of a high to low pulse to pin PD2, the
INTF0 flag will be set; as a result, the AVR is
interrupted in whatever is doing, clears the INTF0 flag,
and jumps to ROM location 0x02 to execute the ISR.
5. True
6. When the CPU jumps to the interrupt vector to execute
the ISR, it clears the flag that has caused the interrupt
(INTF0). The INTF0 flag will be set only if a new high-to
low pulse is applied to the pin.
18
Program Priority
19
Interrupts priority
What happens when two interrupts are activated at the same
time?
•If 2 interrupts are activated at the same time, the one with the
highest priority is served first.
•The interrupt that has a lower address in the interrupt vector
has a higher priority.
• For instance external interrupt 0 has a higher priority
than ext interrupt 2.
20
Interrupts inside an interrupt
What happens if the AVR is executing an ISR belonging to an
interrupt and another interrupt is activated?
21
Interrupts inside an interrupt
What happens if the AVR is executing an ISR belonging to an
interrupt and another interrupt is activated?
•When the AVR begins to execute an ISR, it disables the I bit of
the SREG register, causing all the interrupts to be disabled, and
no other interrupt occurs while serving the interrupt.
•When the RETI instruction is executed, the AVR enables the I
bit causing the other interrupts to be served.
•If you want another interrupt (any priority) to be served while the
current interrupt is being served, you can set the I bit using the
SEI instruction.
•Do that with care as it may lead to undesirable results (overflow
of stack….)
22
Interrupts latency
The time from the moment an interrupt is activated to the
moment the CPU starts to execute the task is called “interrupt
latency”.
•During this time the PC register is pushed on the stack and
the I bit of the SREG register clears (causing interrupts to be
disabled)
•Its duration can be affected by the type of instruction the CPU
is executing when the interrupt comes in.
•Can last 2 or 4 machine cycles (see datasheet)
23
Review Questions (int priority)
• True or false. In Atmega 32, if the Timer1 and Timer0
interrupts are activated at the same time, the Timer0
interrupt is served first.
• What happens if two interrupts are activated at the same
time?
• What happens if an interrupt is activated while the CPU is
serving another interrupt?
• What factor affect the interrupt latency.
24
Review Questions-answers (int priority)
• False. As denoted in the vector table, the address of Timer0
overflow interrupt is 0x16, while that of Timer1 overflow is
0x12.
• The interrupt whose vector is first in the interrupt vector, is
served first.
• The flag of the interrupt will be set, but since I=0, the new
interrupt will not be served. The last instruction of the old
interrupt is REIT, which causes the I falg to be set and the
new interrupt to be served.
• The type of instruction the CPU is executing when the
interrupt comes in.
25
Program interrupts in C
26
Program interrupts in C
In WinAVR C language, the following have been added to
manage the interrupts:
1.Interrupt include file: we should include the interrupt header
file (#include <avr/interrupt.h>) if we want to use
interrupts in our program.
2.cli() and sei(): In WinAVR, the cli() and sei() macros are
used to clear and set the I bit of the SREG register.
3.Defining ISR: To write an ISR for an interrupt we use the
following structure (where the interrupt vector name is as
defined in datasheet):
27
Program interrupts in C
28
Program interrupts in C (example1)
29
Program interrupts in C (example1)
30
Program interrupts in C (example2)
31
Program interrupts in C (example1)
32
External Interrupt Sample Code
Write a program that whenever INT0 goes low, it
toggles PB5 once.
33
Steps in executing an interrupt
PC6 1 28 PC5
Code
PD0 2 27 PC4
Address .ORG 0
26 PC3
PD1 3 0000 JMP MAIN
(INT0) PD2 4 25 PC2 0002 .ORG 0x02
Stack
34
Timer Interrupts
35
Timer Interrupts
TOV0
TOIE0 PC=0x0020
I bit of SREG
36
Using Timer0 overflow interrupt
ATmega328
This program uses Timer0 to generate a square +5
38
Interrupt priority
Interrupt Address (hex)
Reset 0000
External Interrupt Request 0 0002 Highest
External Interrupt Request 1 0004 priority
Pin Change Interrupt Request 0006
Pin Change Interrupt Request 0008
Pin Change Interrupt Request 000A
Watchdog Time-out Interrupt 000C
Timer/Counter2 Compare Match A 000E
Timer/Counter2 Compare Match B 0010
Timer/Counter2 Overflow 0012
Timer/Counter1 Capture Event 0014
Timer/Counter1 Compare Match A 0016
Timer/Counter1 Compare Match B 0018
Timer/Counter1 Overflow 001A
Timer/Counter0 Compare Match A 001C
Timer/Counter0 Compare Match B 001E
Timer/Counter0 Overflow 0020
SPI Serial Transfer Complete 0022
USART Rx Complete 0024
USART Data Register Empty 0026 Lowest
USART Tx Complete 0028 priority
ADC Conversion Complete 002A
EEPROM ready 002C
Analog Comparator 002E 39
Interrupt inside an interrupt
The I flag is cleared when the AVR begins
to execute an ISR. So, interrupts are
disabled.
The I flag is set when RETI is executed.
40
C programming
Using Timer0 generate a square wave on pin PORTB.5,Vector
Interrupt while at the
Name
same time transferring data External
from PORTC to PORTD.
Interrupt Request 0 INT0_vect
External Interrupt Request 1 INT1_vect
#include "avr/io.h"
Pin Change Interrupt Request PCINT0_vect
#include "avr/interrupt.h"
int main () Pin Change Interrupt Request PCINT1_vect
OCR1A = 15624;
TCCR1A = 0x00; //CTC mode, internal clk, prescaler=1024
TCCR1B = 0x0D;
TIMSK1 = (1<<OCIE1A); //enable Timer1 compare match A int.
sei (); //enable interrupts
43