Module - 5: 8051 Interrupts and Interfacing Applications
Module - 5: 8051 Interrupts and Interfacing Applications
Module - 5: 8051 Interrupts and Interfacing Applications
Multiple interrupts
What would happen if multiple interrupts are received by a
microcontroller at the same instant?
In such a case, the controller assigns priorities to the interrupts.
Thus the interrupt with the highest priority is served first.
However the priority of interrupts can be changed configuring
the appropriate registers in the code.
8051 Interrupts
The 8051 controller has six hardware interrupts
of which five are available to the programmer.
These are as follows:
1. RESET interrupt – This is also known as Power on Reset
(POR). When the RESET interrupt is received, the controller
restarts executing code from 0000H location. This is an interrupt
which is not available to the programmer.
2. Timer interrupts – Each Timer is associated with a Timer
interrupt. A timer interrupt notifies the microcontroller that the
corresponding Timer has finished counting.
3. External interrupts – There are two external interrupts EX0
and EX1 to serve external devices. Both these interrupts are
active low. In AT89C51, P3.2 (INT0) and P3.3 (INT1) pins are
available for external interrupts 0 and 1 respectively. An external
interrupt notifies the microcontroller that an external device needs
its service.
4. 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?
Every interrupt is assigned a fixed memory area inside the
processor/controller. The Interrupt Vector Table (IVT) holds the
starting address of the memory area assigned to it (corresponding
to every interrupt).
The interrupt vector table (IVT) for AT89C51 interrupts is as
follows :
• When an interrupt is received, the controller stops
after executing the current instruction. It transfers the
content of program counter into stack. It also stores
the current status of the interrupts internally but not
on stack.
• After this, it jumps to the memory location specified
by Interrupt Vector Table (IVT). After that the code
written on that memory area gets executed. This code
is known as the Interrupt Service Routine (ISR) or
interrupt handler. ISR is a code written by the
programmer to handle or service the interrupt
Programming Interrupts
While programming interrupts, first thing to do is to specify the
microcontroller which interrupts must be served. This is done by
configuring the Interrupt Enable (IE) register which enables or
disables the various available interrupts. The Interrupt Enable
register has following bits to enable/disable the hardware
interrupts of the 8051 controller.
• To enable any of the interrupts, first the EA bit must be set to 1.
After that the bits corresponding to the desired interrupts are
enabled. ET0, ET1 and ET2 bits are used to enable the Timer
Interrupts 0, 1 and 2, respectively. In AT89C51, there are only
two timers, so ET2 is not used. EX0 and EX1 are used to enable
the external interrupts 0 and 1. ES is used for serial interrupt.
• EA bit acts as a lock bit. If any of the interrupt bits are enabled
but EA bit is not set, the interrupt will not function. By default
all the interrupts are in disabled mode.
• Note that the IE register is bit addressable and individual
interrupt bits can also be accessed.
For example –
IE = 0x81; enables External Interrupt0 (EX0)
IE = 0x88; enables Serial Interrupt
• A subroutine for a particular interrupt is identified by
this number.
• These subroutine numbers corresponding to different
interrupts are tabulated below.
1. Programming Timer Interrupts
The timer interrupts IT0 and IT1 are related to Timers 0 and 1,
respectively. (Please refer 8051 Timers for details on Timer
registers and modes.) The interrupt programming for timers
involves following steps :
1. Configure TMOD register to select timer(s) and its/their mode.
2. Load initial values in THx and TLx for mode 0 and 1; or in THx only
for mode 2.
3. Enable Timer Interrupt by configuring bits of IE register.
4. Start timer by setting timer run bit TRx.
5. Write subroutine for Timer Interrupt. The interrupt number is 1 for
Timer0 and 3 for Timer1.
6. Note that it is not required to clear timer flag TFx.
7. To stop the timer, clear TRx in the end of subroutine. Otherwise it will
restart from 0000H in case of modes 0 or 1 and from initial values in case
of mode 2.
8. If the Timer has to run again and again, it is required to reload initial
values within the routine itself (in case of mode 0 and 1). Otherwise after
one cycle timer will start counting from 0000H.
• Example code
Timer interrupt to blink an LED; Time delay in mode1 using interrupt method
// Use of Timer mode0 for blinking LED using interrupt method
// XTAL frequency 11.0592MHz
#include<reg51.h>
sbit LED = P1^0; //LED connected to D0 of port 1
void timer(void) interrupt 1 //interrupt no. 1 for Timer 0
{
led=~led; //toggle LED on interrupt
TH0=0xFC; // initial values loaded to timer
TL0=0x66;
}
main()
{
TMOD = 0x01; // mode1 of Timer0
TH0 = 0xFC; // initial values loaded to timer
TL0 = 0x66;
IE = 0x82; // enable interrupt
TR0 = 1; //start timer
while(1); // do nothing
}
2. Programming External Interrupts
• The external interrupts are the interrupts received from the
(external) devices interfaced with the microcontroller.
• They are received at INTx pins of the controller. These can be
level triggered or edge triggered. In level triggered, interrupt is
enabled for a low at INTx pin; while in case of edge
triggering, interrupt is enabled for a high to low transition at
INTx pin.
• The edge or level trigger is decided by the TCON register. The
TCON register has following bits:
• Setting the IT0 and IT1 bits make the external
interrupt 0 and 1 edge triggered respectively. By
default these bits are cleared and so external interrupt
is level triggered.
Interfacing Stepper Motor with
8051Microcontroller
Stepper Motor
• Stepper motors are used to translate electrical
pulses into mechanical movements. In some
disk drives, dot matrix printers, and some
other different places the stepper motors are
used. The main advantage of using the stepper
motor is the position control.
• Stepper motors generally have a permanent
magnet shaft (rotor), and it is surrounded by a
stator.
• Some parameters of stepper motors −
• Step Angle − The step angle is the angle in which the
rotor moves when one pulse is applied as an input of
the stator. This parameter is used to determine the
positioning of a stepper motor.
• Steps per Revolution − This is the number of step
angles required for a complete revolution. So the
formula is 360° /Step Angle.
• Steps per Second − This parameter is used to measure
a number of steps covered in each second.
• RPM − The RPM is the Revolution Per Minute. It
measures the frequency of rotation. By this parameter,
we can measure the number of rotations in one minute.
Fig: Interfacing Stepper Motor with 8051 Microcontroller
• Why are we using ULN2003A driver?
• A Stepper motor consumes a current of 0.1 – 1
A during step rotation with the load. An
AT89c51 produces a maximum current of
0.045A through the ports. Therefore, the
pulses sent from Port 2 are not sufficient to run
a stepper motor. Hence, we cannot directly
interface stepper motors with microcontrollers
like AT89C51 microcontroller.
• We are using Port P0 of 8051 for connecting
the stepper motor. HereULN2003 is used. This
is basically a high voltage, high current
Darlington transistor array.
• Each ULN2003 has seven NPN Darlington
pairs. It can provide high voltage output with
common cathode clamp diodes for switching
inductive loads.
The Unipolar stepper motor works in three modes.
#include<reg52.h>
void delay(int);
void main()
{
do
{
P2=0x01; //0001
delay(1000);
P2=0x02; //0010
delay(1000);
P2=0x04; //0100
delay(1000);
P2=0x08; //1000
delay(1000);
}
while(1);
}
void delay(int k)
{
int i,j;
for(i=0;i<k;i++)
{
for(j=0;j<100;j++)
{}
}
}
Analog-to-digital converter (ADC)
interfacing
• ADCs (analog-to-digital converters) are among the most widely used
devices for data acquisition.
• A physical quantity, like temperature, pressure, humidity, and velocity,
etc., is converted to electrical (voltage, current) signals using a device
called a transducer, or sensor
• We need an analog-to-digital converter to translate the analog signals to
digital numbers, so microcontroller can read them.
• ADC804 chip: ADC804 IC is an analog-to-digital converter. It works
with +5 volts and has a resolution of 8 bits. Conversion time is another
major factor in judging an ADC. Conversion time is defined as the time
it takes the ADC to convert the analog input to a digital (binary) number.
In ADC804 conversion time varies depending on the clocking signals
applied to CLK R and CLK IN pins, but it cannot be faster than 110μs.
Pin Description of ADC804:
• CLK IN and CLK R: CLK IN is an input pin connected to an
external clock source. To use the internal clock generator (also
called self-clocking), CLK IN and CLK R pins are connected
to a capacitor and a resistor and the clock frequency is
determined by: f=
– Typical values are R = 10K ohms and C =150pF. We
get f = 606 kHz and the conversion time is 110μs.
• CS: It is an active low input used to activate
ADC804.
• Vref/2 : It is used for the reference voltage. If this pin
is open (not connected), the analog input voltage is in
the range of 0 to 5 volts (the same as the Vcc pin). If
the analog input range needs to be 0 to 4 volts, Vref/2
is connected to 2 volts. Step size is the smallest
change can be discerned by an ADC
• D0-D7: The digital data output pins. These are tri-
state buffered. The converted data is accessed only
when CS =0 and RD is forced low. To calculate the
output voltage, use the following formula