Polling and Interrupt Handling
Polling and Interrupt Handling
Handling
2
Polling and Interrupt
Two approaches to preform more than one tasks
seemingly at the same time
Used to provide CPU’s service to the peripheral devices
when CPU is busy with another task
3
Polling
CPU keeps on checking status of peripheral devices at
regular intervals repeatedly while performing another
task
When the status condition of a device is met, CPU
provides its service to the device and then resumes the
task it was doing
CPU then starts again monitoring the status of devices
4
System 5
pollingBasedTrigger - Toggles an
LED on user input while blinking
another LED in every 2 seconds
using polling
5
Hardware
Schematic
Diagram
6
Hardware
Wiring Diagram
7
Firmware
Algorithm
8
C Program
9
Firmware
Algorithm for Arduino
10
Firmware
Source Code (using Arduino Wrapper Functions)
11
Polling
Problems
If the state of a device changes too fast or the CPU’s
device monitoring interval is too long, the state change of
the device may go undetected
No support for the concept of priority as CPU repeatedly
performs all tasks in the same order
12
Interrupt
Peripheral devices interrupt the CPU when they need
CPU’s service while CPU is busy with another task
CPU suspends the currently preforming task and
provides its service to the interrupted device
CPU then resumes the task it was doing
Frees CPU from monitoring (polling) the state of
peripheral devices
Supports the concept of priority
13
Interrupt Handling
Steps
14
Interrupt Vector Table
Widely used Interrupt Vectors
Address Interrupt Interrupt_Vector
0x0002 External interrupt on INT0 pin INT0_vect
0x0004 External interrupt on INT1 pin INT1_vect
0x0006 Pin change interrupt on Port B pins PCINT0_vect
0x0008 Pin change interrupt on Port C pins PCINT1_vect
0x000A Pin change interrupt on Port D pins PCINT2_vect
0x0012 Timer/Counter 2 overflow interrupt TIMER2_OVF_vect
0x001A Timer/Counter 1 overflow interrupt TIMER1_OVF_vect
0x0020 Timer/Counter 0 overflow interrupt TIMER0_OVF_vect
0x0024 USART receive complete interrupt USART_RX_vect
0x0028 USART transmit complete interrupt USART_TX_vect
0x002A ADC conversion complete interrupt ADC_vect
15
Interrupt Routines
Interrupt Service Routine
SEt Interrupt
CLear Interrupt
16
Interrupt Registers
17
External Interrupt Registers
EICRA (External Interrupt Control Register A)
18
External Interrupt Registers
EICRA (External Interrupt Control Register A)
19
External Interrupt Registers
EIMSK (External Interrupt MaSK) register
20
System 6
externalInterruptBasedTrigger -
Toggles an LED on user input
while blinking another LED in
every 2 seconds using external
interrupts
21
ATmega328P External Interrupt Pinout
22
Capacitor
23
Hardware
Schematic
Diagram
24
Debouncing Inputs
Contacts bounce on mechanical switches
Results a series of rapid
voltage changes
Microcontroller may
misinterpret
the input
Use a
capacitor to
suppress the voltage changes
and debounce the input
25
Hardware
Wiring Diagram
26
Firmware
Algorithm
27
C Program
28
Firmware
Algorithm for Arduino
29
Firmware
Source Code (using Arduino Wrapper Functions)
30
Pin Change Interrupt Registers
PCICR (Pin Change Interrupt Control Register)
31
Pin Change Interrupt Registers
PCMSKn (Pin Change MaSK n) register
32
System 7
pinChangeInterruptBasedTrigger -
Toggles an LED on user input
while blinking another LED in
every 2 seconds using pin change
interrupts
33
ATmega328P Interrupt Pinout
34
Firmware
Algorithm
35
C Program
36
Using Hexadecimal Constants
37
Using Bit-wise Shift Operators
38
Using Labeled Constants
39
Questions
40