0% found this document useful (0 votes)
31 views8 pages

Lab5 Interupt 8051 2023

The document summarizes interrupt programming in 8051 microcontrollers. It discusses the different interrupt sources including reset, timer, external, and serial interrupts. It describes the registers used for interrupt handling including the interrupt enable (IE), interrupt priority (IP), and timer control (TCON) registers. It provides examples of using timer 0 and serial interrupts to blink an LED and transmit data. Finally, it discusses programming multiple interrupts and examples of using timer and external interrupts to toggle different LED pins.

Uploaded by

k12onlinenro
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
31 views8 pages

Lab5 Interupt 8051 2023

The document summarizes interrupt programming in 8051 microcontrollers. It discusses the different interrupt sources including reset, timer, external, and serial interrupts. It describes the registers used for interrupt handling including the interrupt enable (IE), interrupt priority (IP), and timer control (TCON) registers. It provides examples of using timer 0 and serial interrupts to blink an LED and transmit data. Finally, it discusses programming multiple interrupts and examples of using timer and external interrupts to toggle different LED pins.

Uploaded by

k12onlinenro
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 8

LAB 5: INTERUPT

1. Objectives:
To learn interrupt programming of 8051 microcontroller

2. Theory
There are five interrupt sources for the 8051. Since the main RESET input can also be
considered as an interrupt, six interrupts in the order of priority can be listed as follows:

When an interrupt is received, the controller stops after executing the current instruction.
It transfers the content of the program counter into the stack. It also stores the current
status of the interrupts internally but not on the stack.
RESET interrupt
This is also known as Power-on Reset (POR). When the RESET interrupt is received, the
controller restarts executing code from the 0000H location. This is an interrupt that is not
available to or, better to say, need not be available to the programmer.
Timer interrupts
Each Timer is associated with a Timer interrupt. A timer interrupt notifies the
microcontroller that the corresponding Timer has finished counting.
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.
Serial interrupt
This interrupt is used for serial communication. When enabled, it notifies the controller
whether a byte has been received or transmitted.
Before going into programming we have to go through the registers used in the Interrupt.
Registers used for Interrupt
IE Register (Interrupt Enable Register)
IP Register (Interrupt Priority Register)
TCON Register (Timer Control Register) – This is used for External Interrupts only.
IE Register (Interrupt Enable Register)
This register is responsible for enabling and disabling the interrupt. EA register is set to
one for enabling interrupts and set to 0 for disabling the interrupts. Its bit sequence and its
meanings are shown in the following figure.

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 the 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 a bit addressable and individual interrupt bits can also be
accessed.
For example –
IE = 0x81; enables External Interrupt0 (EX0)
IE = 0x88; enables Serial Interrupt
IP (Interrupt Priority) Register
The 8051 offers two levels of interrupt priority: High and Low. By using interrupt
priorities you may assign higher priority to certain interrupt conditions. We can change
the priority levels of the interrupts by changing the corresponding bit in the Interrupt
Priority (IP) register as shown in the following figure.
A low priority interrupt can only be interrupted by the high priority interrupt, but not
interrupted by another low priority interrupt.
If two interrupts of different priority levels are received simultaneously, the request of a
higher priority level is served.
If the requests of the same priority levels are received simultaneously, then the internal
polling sequence determines which request is to be serviced.

TCON Register (Timer Control Register)


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. We
have already discussed this register in our Timer/Counter session. The TCON register
has the following bits:

Here MSB four bits are used for Timers. But LSB four bits are used for External
Interrupts. We will see that bits.
Setting the IT0 and IT1 bits makes the external interrupt 0 and 1 edge-triggered
respectively. By default, these bits are cleared and so the external interrupt is level
triggered.
Note: For a level trigger interrupt, the INTx pin must remain low until the start of the ISR
and should return to high before the end of the ISR. If the low at INTx pin goes high
before the start of ISR, an interrupt will not be generated. Also if the INTx pin remains
low even after the end of ISR, the interrupt will be generated once again. This is the
reason why the level trigger interrupt (low) at the INTx pin must be four machine cycles
long and not greater than or smaller than this.
So, these all are the registers used in the Interrupt. These registers are not enough for play
with interrupt. We have to write the ISR or Interrupt Handler.
ISR or Interrupt Handler – 8051 Interrupts
Setting the bits of IE register is necessary and sufficient to enable the interrupts. The next
step is to specify to the controller what to do when an interrupt occurs. This is done by
writing a subroutine or function for the interrupt. This is the ISR and gets automatically
called when an interrupt occurs. It is not required to call the Interrupt Subroutine
explicitly in the code.
An important thing 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.
Programming (Timer 0 Interrupt) – 8051 Interrupts
Timer interrupts to blink an LED; Time delays in mode 1 using interrupt method.
#include<reg51.h>
sbit LED = P1^0; //LED connected to P1.0
Void 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
}
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;
}
Programming (Serial Interrupt) – 8051 Interrupts
Send ‘A’ from the serial port when it receives anything via Rx.
#include<reg51.h>
void main()
{
TMOD = 0x20;
TH1 = TL1=0xfd;
SCON = 0x50;
TR1 = 1;
IE = 0x90;
while(1);
}

void ISR_sc(void) interrupt 4


{
if(RI==1){
SBUF = 'A';
while(TI==0);
TI = 0;
RI = 0;
}
}
Programming multiple interrupts – 8051 Interrupts
Multiple interrupts can be enabled by setting more than one interrupts in the IE register.
If more than one interrupts occurs at the same time, the interrupts will be serviced in
order of their priority. The priority of the interrupts can be changed by programming the
bits of the Interrupt Priority (IP) register.
Setting a particular bit in IP register makes the corresponding interrupt of the higher
priority. For example, IP = 0x08; will make Timer1 priority higher. So the interrupt
priority order will change. More than one bit in IP register can also be set. In such a case,
the higher priority interrupts will follow the sequence as they follow in the default case.
For example,
IP = 0x0A; will make Timer0 and Timer1 priorities higher.
Note that in the below example external interrupt 1 is set to be a higher priority than the
Timer 0 and external 0 in this case.
//Toggle LEDs by Timer and External Interrupt

#include<reg51.h>
void delay();
int i,a;

void main()
{
P1=P2=P0=0x00;
IP=0x04;
EA=1;
EX0=1;
ET0=1;
EX1=1;
TMOD=0X01;
TH0=0X4b;
TL0=0Xfd;
TR0=1;
while(1);
}

void ex0() interrupt 0


{
P0=0xff;
delay();
P0=0x00;
delay();
}

void et0() interrupt 1


{
a++;
if(a==20)
{
P1=~P1;
a=0;
}
}
void ex1() interrupt 2
{
P2=0x81;
delay();
P2=0x18;
delay();
}
void delay()
{
for(i=0;i<30000;i++);
}

3. Lab exercise
3.1. Use Timer1 interrupt to blink the LED every 2ms - 1 time.
3.2. Use a push button to simulate a product counter with a 7-segment LED display.
Name: Student Code: Class: Lab:
1. Circuit
2. Algorithm flowchart
3. Code and explanation
4. Summary

You might also like