0% found this document useful (0 votes)
7 views

Polling and Interrupt Handling

Uploaded by

tekiyagaming
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
7 views

Polling and Interrupt Handling

Uploaded by

tekiyagaming
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 40

Polling and Interrupt

Handling

Thareendhra Keerthi Wijayasiriwardhane


Department of Industrial Management
Faculty of Science
University of Kelaniya
[email protected]
Learning Outcomes
 On completion of this session, you should be able to
 Compare and contrast polling vs. interrupts
 Construct a microcontroller-based system and program it
to preform more than one tasks seemingly at the same
time using polling
 Explain how interrupt handing works
 Explain the functionality of interrupt registers
 Construct a microcontroller-based system and program it
to preform more than one tasks seemingly at the same
time using interrupts

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)

 ISC01 (Interrupt Sense Control 01) and ISC00 bits


specify when to interrupt CPU on INT0 pin
ISC01 ISC00 Interrupt on
0 0 Low level of INT0 pin
0 1 Logic change of INT0 pin
1 0 Falling edge of INT0 pin
1 1 Rising edge of INT0 pin

18
External Interrupt Registers
 EICRA (External Interrupt Control Register A)

 ISC11 (Interrupt Sense Control 11) and ISC10 bits


specify when to interrupt CPU on INT1 pin
ISC11 ISC10 Interrupt on
0 0 Low level of INT1 pin
0 1 Logic change of INT1 pin
1 0 Falling edge of INT1 pin
1 1 Rising edge of INT1 pin

19
External Interrupt Registers
 EIMSK (External Interrupt MaSK) register

 INTn (INTerrupt n) bit enables (or disables) interrupts


on INTn pin

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)

 PCIE2 (Pin Change Interrupt Enable 2), PCIE1 and


PCIE0 bits enable interupts on Port D, Port C and Port
B, respectively

31
Pin Change Interrupt Registers
 PCMSKn (Pin Change MaSK n) register

 PCINTm (Pin Change INTerrupt m) bits enable (or


disable) interrupts on pins of an enabled Port

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

You might also like