You are on page 1of 7

POLLING

1. INTRODUCTION
Interrupts and polling are the two methods used by the microcontroller to provide
service for several devices . An interrupt is a signal to the microcontroller provided by
software or hardware indicating an event that needs immediate attention. In polling method,
the microprocessor or microcontroller continuously monitors the device status and provides
service. Interrupts and polling can be used to perform the same task independently. A brief
comparison between interrupts and polling are discussed in this report.

2. POLLING
In polling, the microcontroller continuously polls or tests every device in turn as to
whether it requires attention. The polling is carried out by a polling program that shares
processing time with the currently running task. The microcontroller continuously monitors
the status of a given device; when the status condition is met, it performs the service. A
device indicates it requires attention by setting a bit in its device status register. After that, it
moves on to monitor the next device until each one is serviced. Most of the time, devices will
not require attention and when one does, it will have to wait until it is next interrogated by the
polling program. The polling method cannot assign priority since it checks all devices in a
round-robin fashion. Round robin architecture is characterized by the absence of interrupts.
It does not suffer from shared data problems. It is attractive for simple environments. The
polling method wastes much of the microcontrollers time by polling devices that do not need
service. The waste of the microcontrollers time can be used to perform some useful tasks.
Polling uses a lot of CPU horsepower and it checks whether the peripheral is ready or not.
Polled code is generally messy and unstructured and it is a big loop with often multiple calls
to check and see if peripheral is ready. Polled code leads to variable latency in servicing
peripherals. The general syntax for polling is given by

While(1)
{
get device status
if(service required)
{
service_routine ;
}
normal_execution;

}
Figure 3.1 Syntax for polling

3. COMPARISON BETWEEN INTERRUPTS AND POLLING


Table 1.Comparison between interrupts and polling
INTERRUPTS

POLLING

Interrupts signal is sent to microprocessor

Microprocessor continuously monitors the

whenever device needs its service.

status of given device, when the status


condition is met, it performs the service.

Priority based execution i.e. when its service

Not priority based execution since it checks

is required by many devices it provides

all devices in a round robin fashion.

service based on priority.

Microcontroller can ignore a device request

Microcontroller

for its service.

request for its service.

Interrupt

method

microcontroller's

time

do

not

waste

it

provides

since

Polling

cannot

method

wastes

ignore

much

device

of

the

microcontrollers time by polling devices that do

services to the required devices.

not need service.

ISR concentrates all peripheral code in one

Polling

place.

unstructured.

Processing time is less compared to polling

Processing time is more.

code

is

generally

messy

and

method.
Interrupts uses microcontroller only when

Polling uses lot of CPU horse power.

work is to be done.
Interrupts give highly predictable servicing

Polled code leads to variable latency in

latencies.

servicing peripherals.

4. POLLING USING ATMEGA 16 MICROCONTROLLER


Polling can be better understood by considering a specific application 'toggling a bit '
using ATMEGA 16 microcontroller. ATMEGA 16 microcontroller has several internal
peripherals that give powerful abilities to develop any application. The pin configuration of
ATMEGA 16 microcontroller is as shown in the figure

Figure5.1 Pin configuration

5.1 BLINKING AN LED USING INTERRUPTS


The program for toggling a bit using polling is given below. In this program, Timer/ counter
control register is used for initializing the timer and set up the prescaler value for the timer.
Timer/counter interrupt mask register is used to enable the timer overflow interrupt .The
Timer/counter 0 register is used for initialization of the timer
The ATmega16 and have three different timers of which the simplest is TIMER0. Its
resolution is 8 BIT i.e. it can count from 0 to 255. The Prescaler is a mechanism for
generating clock for timer by the CPU clock. The CPU has a clock source such as a external
crystal of internal oscillator. The Prescaler is used to divide this clock frequency and produce
a clock for TIMER. The Prescaler can be used to get the following clock for timer. The three
registers used in the program are discussed below .
5.1.1 TIMER/COUNTER CONTROL REGISTER
The timer/counter control register is used to configure the timer. The register is as shown in
the figure

Figure 5.2 Timer/Counter Control Register

Here CS02, CS01 and CS00 are the clock select bits. They are used to set up the Prescaler
for timer.

5.1.2 TIMER/COUNTER INTERRUPT MASK REGISTER


To enable the timer interrupt overflow interrupt , Timer interrupt mask register is used here .It
is given as shown in the figure

Figure 5.3 Timer/Counter Interrupt mask register`

Timer 0 is used in this program ,the last two bits controls the interrupts for timer 0.Here the
timer overflow interrupt is set to 1.

5.1.3 TIMER/COUNTER 0 REGISTER


To initialize the counter Timer/counter 0 register is used .This register is as shown in the
figure

Figure 5.4 Timer/Counter Register

Figure 5.5 Program to toggle bit using polling

In the above program ,Timer/counter control registers are used to define the
prescaler value for the timer .The initial value is loaded into the timer using timer/counter 0
register. The timer overflow interrupt is enabled using the timer/counter interrupt mask
register. Port A is configured as output.
Here in the main function an infinite while loop is present .This while loop execution
continues until the timer overflows . When the timer overflows, the control jumps to service
the routine and starts executing it. When it becomes equal to 61 ,it toggles the bit and the
count variable is reset to zero. It can be seen from the program that the CPU does not do
any other process other than checking for the status of the count during the entire operation.
From this program, it is understood that only when the timer overflows, the toggling
action is performed and no other task is performed in the normal case.
6

6. ADVANTAGES OF INTERRUPTS (COMPARED TO POLLING)


1.It can provide service for several devices
2.When there are several interrupts at the same time , they are executed in the order of
priority thereby saving microcontrollers processing time.
3.Decoupling of handling of I/O devices.
4.Makes easier to develop a multitasking OS
7. DISADVANTAGES OF INTERRUPTS
1.Latency for low priority tasks can increase.
2.Low priority tasks are less concentrated.
3.If a low priority follow up task is very time consuming the latency for higher priority
response times will suffer.

You might also like