10 Interrupts
10 Interrupts
Chapter 10
Interrupts
Contents
■ Upon completion of this chapter you will be able to:
Compare polling vs. interrupts.
Explain the purpose of Interrupt Service Routine (ISR).
Understand the role of Interrupt Vector Table (IVT).
List major interrupts of AVR.
Describe the external hardware interrupts of the AVR.
Program AVR external interrupts in C.
Program AVR Timers using interrupts.
2
Polling vs. Interrupt
3
Polling vs. Interrupt
■ 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
4
Polling vs. Interrupt
5
Polling Vs. Interrupt
■ Polling ■ Interrupt
■ Ties down the CPU ■ Efficient CPU use
■ Has priority
■ Can be masked
6
Interrupt Sources
■ Microcontroller has various on-chip peripheral
devices (interrupt sources) that can interrupt the
main program:
■ external switches
■ timers,
■ serial port,
■ PWM, and
■ ADC.
7
Interrupt Controllers
Peripherals
Timers
IRQ 0
CPU IRQ 1 I/Os
Interrupt IRQ 2
Controller IRQ 3 USART
….
IRQn SPI
8
Interrupt control unit in AVR
9
Interrupt Service Routine (ISR)
■ ISR: Interrupt Service Routine
■ The function that is executed when an
interrupt is enabled
10
Interrupt Vector Table (IVT)
Highest
priority
Lowest
priority
11
Interrupt Vector Table (IVT)
■ Vector Number
■ An interrupt with a lower ‘Vector No.’ has a higher
priority.
■ E.g., INT0 has a higher priority than INT1.
■ Program Address
■ The fixed memory location for a given interrupt
handler.
13
Enabling Interrupt
■ Interrupt Enable, Flag bits
■ Each interrupt source has an ENABLE bit that
14
Interrupt Priority
■ What if two or more interrupts occur at the same
time?
■ The interrupt with lower ISR address is
15
How to Program Interrupt
■ To program an interrupt, 5 steps are required:
1. Include header file <avr/interrupt.h>.
2. Use C macro ISR() to define the interrupt
handler and update IVT.
3. Enable the specific interrupt.
4. Configure details of the interrupt by setting
relevant registers.
5. Enable the interrupt subsystem globally using
sei().
16
How to Program Interrupt
■ The C macro ISR() is used to define the handler
for a given interrupt.
■ Its syntax is given as:
ISR(interrupt_vector_name){
// … body of ISR
17
External Interrupts
18
Slide-19
ECCE4227-FL16
External Interrupt
19
External Interrupts
■ Interrupts can be edge triggered or level triggered
■ Edge trigger:
■ activated when a change in
a specific value
20
External Interrupt in AVR
■ Specify the events that trigger an external interrupt
by using:
■ MCU Control Register (MCUCR) (INT0 and INT1).
01 00
21
External Interrupt
■ To enable an external interrupt, set a flag in General Interrupt
Control Register (GICR).
■ Example:
■ To enable INT1 on pin D.3, we can write
■ Note that INT1, INT0, and GICR names are already defined in
<avr/io.h> using ‘#define INT1 7’ and ‘#define INT0 6’
22
Example 1
■ Write a C program to toggle port B whenever a switch is
pressed. (The program should use an external interrupt).
■ Let’s use interrupt INT1. This interrupt is triggered on pin
PD.3.
■ To enable interrupt INT1:
PD.3:
■ MCUCR |= (1 << ISC10); #define ISC10 3
subsystem.
23
C program Example 1
24
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 = 0x2; //make INT0 falling edge triggered
25
Example 3
■ Consider a switch connected
to the external interrupt pin
INT0 of an ATMega8 AVR as
shown in figure
■ A 7-segment display with
decoder is connected to
port PB0-PB3 on the same
AVR.
26
C program Example 3
#include <avr/io.h>
#include <avr/interrupt.h>
int main ()
{
DDRB =0x0F; //PB0-PB3 as an output
MCUCR = 0x2; //make INT0 falling edge triggered
GICR |= (1<<INT0); //enable external interrupt 0
sei (); //enable interrupts
27
Timer Interrupts
28
Timer Interrupt Registers
■ Timer Interrupt Registers (Mask and Flag Registers) are
Common to all Timers (0,1,2)
■ Timer/Counter Interrupt Mask TIMSK Register
■ Bit 2 – TOIE1 – Timer/Counter1 Overflow Interrupt Enable
29
Using Timer0 overflow interrupt
■ This program uses Timer0 to generate a square
wave with a period of 10 µs on pin PORTB.5, while
at the same time data is being transferred from
PORTC to PORTD. Assuming that XTAL = 10 MHz
206 ; 5
206 ;
30
Timer2 Compare Match Interrupt
■ Using Timer2 and CTC mode write a program that toggles pin
PORTB.5 every 100 μs, while at the same time transferring data
from PORTC to PORTD. Assume XTAL = 1 MHz.
#include <avr/io.h>
#include <avr/interrupt.h>
int main () {
DDRB |= (1<<5);//make DDRB.5 output
OCR2 = 99;
TCCR2 = 0x09; //CTC mode, internal clk, no prescaler
TIMSK = (1<<OCIE2); //enable Timer 2 compare match int.
sei (); //enable interrupts
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).
32