You are on page 1of 6

Interrupts & Programming 8051 Hardware

Interrupts
Link: http://www.engineersgarage.com/tutorials/interrupts-8051-interrupt-programming?page=4
Interrupt is one of the most important and powerful concepts and features in
microcontroller/processor applications. Almost all the real world and real time systems built
around microcontrollers and microprocessors make use of interrupts.

What is Interrupt
The interrupts refer to a notification, communicated to the controller, by a hardware device or
software, on receipt of which controller momentarily stops and responds to the interrupt.
Whenever an interrupt occurs the controller completes the execution of the current instruction
and starts the execution of an Interrupt Service Routine (I!" or Interrupt #andler. ISR is a
piece of code that tells the processor or controller what to do when the interrupt occurs. After
the execution of I!, controller returns back to the instruction it has $umped from (before the
interrupt was received". The interrupts can be either hardware interrupts or software interrupts.
Why need interrupts
An application built around microcontrollers %enerally has the followin% structure. It takes
input from devices like keypad, A&' etc( processes the input usin% certain al%orithm( and
%enerates an output which is either displayed usin% devices like seven se%ment, )'& or used
further to operate other devices like motors etc. In such desi%ns, controllers interact with the
inbuilt devices like timers and other interfaced peripherals like sensors, serial port etc. The
pro%rammer needs to monitor their status re%ularly like whether the sensor is %ivin% output,
whether a si%nal has been received or transmitted, whether timer has finished countin%, or if
an interfaced device needs service from the controller, and so on. This state of continuous
monitorin% is known as pollin%.

In pollin%, the microcontroller keeps checkin% the status of other devices( and while doin% so it
does no other operation and consumes all its processin% time for monitorin%. This problem
can be addressed by usin% interrupts. In interrupt method, the controller responds to only
when an interruption occurs. Thus in interrupt method, controller is not re*uired to re%ularly
monitor the status (fla%s, si%nals etc." of interfaced and inbuilt devices.

To understand the difference better, consider the followin%. The pollin% method is very much
similar to a salesperson. The salesman %oes door+to+door re*uestin% to buy its product or
service. )ike controller keeps monitorin% the fla%s or si%nals one by one for all devices and
caters to whichever needs its service. Interrupt, on the other hand, is very similar to a
shopkeeper. Whosoever needs a service or product %oes to him and apprises him of his/her
needs. In our case, when the fla%s or si%nals are received, they notify the controller that they
need its service.
Hardware and Software interrupt
The interrupts in a controller can be either hardware or software. If the interrupts are
%enerated by the controller,s inbuilt devices, like timer interrupts( or by the interfaced devices,
they are called the hardware interrupts. If the interrupts are %enerated by a piece of code,
they are termed as software interrupts.

Multiple interrupts
What would happen if multiple interrupts are received by a microcontroller at the same
instant- In such a case, the controller assi%ns priorities to the interrupts. Thus the interrupt
with the hi%hest priority is served first. #owever the priority of interrupts can be chan%ed
confi%urin% the appropriate re%isters in the code.
8051 Interrupts
The ./01 controller has six hardware interrupts of which five are available to the pro%rammer.
These are as follows2


1. RESE interrupt + This is also known as 3ower on !eset (34!". When the !55T
interrupt is received, the controller restarts executin% code from ////# location. This is an
interrupt which is not available to or, better to say, need not be available to the pro%rammer.

!. i"er interrupts + 5ach Timer is associated with a Timer interrupt. A timer interrupt notifies
the microcontroller that the correspondin% Timer has finished countin%.

#. E$ternal interrupts + There are two external interrupts 56/ and 561 to serve external
devices. 7oth these interrupts are active low. In AT.8'01, 39.: (I;T/" and 39.9 (I;T1" pins
are available for external interrupts / and 1 respectively. An external interrupt notifies the
microcontroller that an external device needs its service.

%. Serial interrupt + This interrupt is used for serial communication. When enabled, it notifies
the controller whether a byte has been received or transmitted.
How is an interrupt serviced&
5very interrupt is assi%ned a fixed memory area inside the processor/controller. The Interrupt
<ector Table (I<T" holds the startin% address of the memory area assi%ned to it
(correspondin% to every interrupt".

The interrupt vector table (I<T" for AT.8'01 interrupts is as follows 2
Interrupt R'M (ocation )He$* +in ,la- clearin-
Reset 0000 . /uto
E$ternal interrupt 0 000# 1! /uto
i"er interrupt 0 0000 1 /uto
E$ternal interrupt 1 001# 1# /uto
i"er interrupt 1 0010 1 /uto
Serial 2'M interrupt 00!# 1 +ro-ra""er clears it

When an interrupt is received, the controller stops after executin% the current instruction. It
transfers the content of pro%ram counter into stack. It also stores the current status of the
interrupts internally but not on stack. After this, it $umps to the memory location specified by
Interrupt 3ector a4le (I<T". After that the code written on that memory area %ets executed.
This code is known as the Interrupt ervice !outine (I!" or interrupt handler. I! is a code
written by the pro%rammer to handle or service the interrupt.
+ro-ra""in- Interrupts
While pro%rammin% interrupts, first thin% to do is to specify the microcontroller which interrupts
must be served. This is done by confi%urin% the Interrupt 5nable (I5" re%ister which enables
or disables the various available interrupts. The Interrupt 5nable re%ister has followin% bits to
enable/disable the hardware interrupts of the ./01 controller.



To enable any of the interrupts, first the 5A bit must be set to 1. After that the bits
correspondin% to the desired interrupts are enabled. 5T/, 5T1 and 5T: bits are used to
enable the Timer Interrupts /, 1 and :, respectively. In AT.8'01, there are only two timers, so
5T: is not used. 56/ and 561 are used to enable the external interrupts / and 1. 5 is used
for serial interrupt.
5A bit acts as a lock bit. If any of the interrupt bits are enabled but 5A bit is not set, the
interrupt will not function. 7y default all the interrupts are in disabled mode.

;ote that the I5 re%ister is bit addressable and individual interrupt bits can also be accessed.
=or example >
I5 ? /x.1( enables 5xternal Interrupt/ (56/"
I5 ? /x..( enables erial Interrupt

ettin% the bits of I5 re%ister is necessary and sufficient to enable the interrupts. 5e$t step is
to specify the controller what to do when an interrupt occurs. This is done by writin% a
subroutine or function for the interrupt. This is the I! and %ets automatically called when an
interrupt occurs. It is not re*uired to call the Interrupt ubroutine explicitly in the code.

An important thin% is that the definition of a subroutine must have the keyword interrupt
followed by the interrupt number. A subroutine for a particular interrupt is identified by this
number. These subroutine numbers correspondin% to different interrupts are tabulated below.

5u"4er Interrupt Sy"4ol
0 E$ternal0 E60
1 i"er0 I0
! E$ternal1 E61
# i"er1 I1
% Serial ES
5 i"er! E!

=or example 2 Interrupt routine for Timer1
void ISR_timer1(void) interrupt 3
{
<Body of ISR>
}
=or example 2 Interrupt routine for 5xternal Interrupt/ (56/"
void ISR_ex0(void) interrupt 0
{
<Body of ISR>
}
;ote that the interrupt subroutines always have void return type. They never return a value.

You might also like