10 Interrupts
10 Interrupts
2
Interrupts: Definition
Interrupt is a mechanism that allows
modules such as I/O and memory to
interrupt the normal processing of the
processor
Progra
ms
Analogy:
Teacher and Students CPU
Memor
I/O
y
3
Polling vs. Interrupt
4
Polling vs. Interrupt [cont.]
Using polling,
The CPU must continually check the device’s status.
Using interrupt:
A device will send an interrupt signal when needed.
In response, the CPU will execute an interrupt service
routine (ISR),
Then, the CPU resumes its normal execution.
5
Polling vs. Interrupt [cont.]
6
Polling vs. Interrupt [cont.]
Polling Interrupt
Ties Efficient
CPU use
down the CPU
Has priority
Can be masked
main( )
while (true)
{
{
Do your common task
if(PIND == 0)
}
//do something;
whenever PIND is 0 then
}
do something
7
Interrupt Sources
8
Interrupt Controllers
Peripherals
Timers
IRQ0
CPU IRQ1 I/Os
Interrupt IRQ2
Controller IRQ3 USART
….
IRQn SPI
9
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
10
Interrupt Service Routine (ISR)
11
Interrupt Service Routine (ISR)
Interrupt Occurred
13
Interrupt Service Routine (ISR)
14
Interrupt Service Routine (ISR)
15
Interrupt Service Routine (ISR)
4(a): The ISR reads data from 0x8000, Program memory μP Data memory
modifies the data, and writes the ISR
resulting data to 0x8001. 16: LDS R0, 0x8000
17: # modifies R0 System bus
4(b): After being read, P1 deasserts Int. 18: STS 0x8001, R0
19: RETI # ISR return
... Int P1 P2
Main program 0
... PC 0x8000 0x8001
100: instruction
101: instruction 100
16
Interrupt Service Routine (ISR)
17
Interrupt Vector Table (IVT)
Highest
priority
Lowest
priority
18
Interrupt Vector Table (IVT) [cont.]
Vector Number
An interrupt with a lower ‘Vector No.’ has a higher priority.
Example: INT0 has a higher priority than INT1.
Program Address
The fixed memory location for a given interrupt handler.
19
Enabling Interrupt
Interrupts can be enabled or disabled by the
programmer
21
Interrupt Priority
22
How to Program Interrupt
23
How to Program Interrupt [cont.]
24
External
Interrupts
S
C
E
l
h
2
:
E
I6
4
n
t
2
Two external interrupts
e
2
r
r
7
u
p
-t can be triggered:
F INT0
L
1
INT1
6
01 00
28
Enabling the External Interrupt
To enable an external interrupt, set the corresponding
flag in General Interrupt Control Register (GICR).
Example:
To enable INT1 on pin D.3, we can write
GICR |= (1 << INT1);
To enable both INT1 and INT0 sources, we can write
GICR |= (1 << INT1) | (1 << INT0);
Note that INT1, INT0, and GICR names are already defined
in <avr/io.h> using ‘#define INT1 7’ and ‘#define INT0 6’
29
Example 1
30
Example 1 (C Code)
|
|
31
Example 2
Assume that the INT0 pin is connected to a switch that is
normally high. Write a program that toggles PORTB.5, whenever
INT0 pin goes low.
#include <avr/io.h>
#include <avr/interrupt.h>
int main ()
{
DDRB |= 1<<5; // PB5 as an output
MCUCR |= 1<<ISC01; // Set INT0 falling-edge
triggered
PB0
PB1
PB2
PB3
33
Example 3 (C Code)
#include <avr/io.h>
#include <avr/interrupt.h>
int main ()
{
DDRB = 0x0F; // PB0-PB3 as an
output
MCUCR |= 1<<ISC01; // INT0 is falling-edge
triggered
GICR |= (1<<INT0); //enable external interrupt 34
0
RECOMMENDED PROBLEMS
CHAPTER-10 [Interrupts]
Problems:
S10.1 (1, 2, 4, 5, 6, 9, 12, 14, 16, 17, 25, 26) S10.2 (22,
24, 29) S10.3 (31, 32, 35, 37, 42, 45, 46).
35