External Interrupts 8051 Microcontroller – Example

USING INTERRUPTS 8051 MICROCONTROLLER. Interrupts are used for communication between the microcontroller and the external device. We already explained the purpose of interrupts used in the previous article of interrupts uses in PIC microcontroller in detail. Now the purpose of this article to explain how to use interrupts in 8051 microcontrollers. Before reading this article you should know how to use Keil for programming of 8051 and how to use input-output ports of 8051 microcontroller.

8051 Interrupts Types

8051 microcontroller can recognize six different types of events that request the microcontroller to stop to perform the current program temporarily and make time to execute a special code. The interrupts sources present in 8051 microcontrollers are:

  • Reset interrupt
  • Timer0 overflow interrupt TF0
  • Timer1 overflow interruptTF1
  • External hardware interrupt INT0
  • External hardware interrupt INT1
  • Serial communication interrupt (RI/TI)

Timers and Serial interrupts are internally generated by the microcontroller while the external interrupts are generated when externally interfacing devices or switches are connected to the microcontroller. These external interrupts can be edge-triggered or level triggered.

8051 microcontroller Interrupt Service

A fixed memory area is assigned for each interrupt inside the microcontroller. The Interrupt Vector Table contains the starting address of the memory location of every interrupt. When an interrupt occurs, the controller transfers the content of the program counter onto the stack. Then it jumps to the memory location which is specified by Interrupt Vector Table (IVT). The code written on that memory area by the programmer starts its execution. That code is called Interrupt Service Routine (ISR) or interrupt handler.

Interrupts 8051 microcontroller Vector Tabletypes of interrupts in 8051 microcontroller

  1. RESET INTERRUPT: When reset pin is activated, the program execution flow jumps to execute code from 0000H memory location. Mostly it is not used. It is also known as power-on reset.
  2. TIMER INTERRUPTS: Two timers (T0 and T1) are present in the 8051 microcontroller which is responsible for a Timer interrupt. A timer interrupt informs the microcontroller that the corresponding Timer has finished the counting. Memory locations 000BH and 001BH in the interrupt vector table belong to Timer0 and Timer1 respectively.
  3. EXTERNAL INTERRUPTS: There are two external interrupts (INT0 and INT1) to serve external devices. Pin numbers 12 and 13 in port 3 are for the external hardware interrupts. Both these interrupts are active low. An external interrupt informs the microcontroller that an external device needs its routine service. Memory locations 0003H and 0013H in the interrupt vector table belong to INT0 and INT1 respectively.
  4. SERIAL INTERRUPT: This interrupt is used for serial communication. It has a single interrupt that belongs to both receive and transmit. When enabled, it notifies the controller whether a byte has been received or transmitted. The interrupt vector table location 0023H belongs to this interrupt.

INTERRUPTS  ENABLE (IE) REGISTER 8051 Microcontroller

REGISTER CONFIGURATION (for interrupt selection): Now we have to specify the microcontroller that which interrupts must be served. All of the above interrupts can be used by configuring some bit in a special function register known as Interrupt Enabled (IE) register. These registers enable or disable the various available interrupts.

interrupt enable register in 8051

  • EA – Enable Interrupt: EA bit must be set to 1 to enable any of the interrupts. By default, all the interrupts are in disabled mode. if EA = 1 Enable Interrupt and if EA = 0 Disable Interrupt
  • ET2 – Timer2 interrupt enable bit: Enable or disable Timer2 overflow or capture interrupt only in 8052. In AT89C51, there are only two timers, so ET2 is not used.
  • ES – Serial port interrupt enable bit: Enable or disable Serial port interrupt.
  • ET1 -Timer1 interrupt enable bit: if ET0 = 1, Enable Timer1 overflow interrupt and if ET0 = 0, Disable Timer1 overflow interrupt.
  • EX1- External interrupt INT1 enable bit : if EX1 = 1, Enable INT1 and if EX1 = 0, Disable INT1
  • ET0:   Timer0 interrupt enable bit: ET0 = 1, Enable Timer0 overflow interrupt ET0 = 0, Disable Timer0 overflow interrupt
  • EX1:   External interrupt INT0 enable bit EX1 = 1, Enable INT0 EX1 = 0, Disable INT0

What is Interrupt Service routine?

After the selection of specific interrupt, the next step is to specify the microcontroller that what to do when an interrupt occurs. We write a subroutine or function for the interrupt which is the ISR. It is automatically called when an interrupt occurs. The definition of subroutine must have the keyword interrupt with the interrupt number. Each interrupt has its specific subroutine number.

EXTERNAL INTERRUPTS: Since we already discussed how to use timers in 8051, so in this article, we will just learn how to use external interrupts in 8051. External interrupts are received from the external interfaced devices at INTx pins of the microcontroller. These can be level-triggered or edge-triggered which is decided by the TCON register.

For level triggered:  interrupt is enabled for a low at INTx pin.
For edge triggered:  interrupt is enabled for a high to low transition at INTx pin.
external interrupt control register

TCON REGISTER:

Setting the IT0 and IT1 bits makes the external interrupt 0 and 1 edge-triggered respectively. By default, these bits are set to 0, so the external interrupt is level triggered.

IT1:     External interrupt1 signal type control bit.

IT1 = 1; to enable external interrupt 1 to be triggered by a falling edge signal
IT1 = 0; to enable a low level signal on external interrupt 1 to generate an interrupt

IT0:     External interrupt 0 signal type control bit, same as IT1.

IT0 = 1; to enable external interrupt 0 to be triggered by a falling edge signal
IT0 = 0; to enable a low level signal on external interrupt 1 to generate an interrupt

Step by step guide to writing code 

  1. Enable external interrupt 0 or external interrupt 1 by configuring IE register. For this we have to give the command as:
For INT0:        IE=0x81;
For INT1:        IE=0x84;
  1. Now we have to write a routine for external interrupt. For EX0 (INT0), the interrupt number is 0 and for EX1 (INT1) interrupt number is 1. For using external interrupt INT0 we have to write routine as:
void ISR_ex0(void) interrupt 0

For using external interrupt INT1 we have to write routine as:

void ISR_ex1(void) interrupt 2
  1. For edge-triggered external interrupt, IT0 or IT1 is set to 1.
For INT0:        IT0=1;
For INT1:        IT1=1;

PROTEUS SIMULATIONS

external interrupts 8051 microcontroller

We used the external interrupt INT0 of 8051 microcontroller. Port 2 is used to monitor the output. In the beginning, alternate values are passed to P2 LEDs. When the external interrupt is given through a push-button, the Interrupt service routine will start to execute and LEDs output will get toggle for 1 sec. This 1-sec delay is given through timer 0 when it is in mode 1.

CODE of external interrupts 8051

#include<reg51.h>
sbit led1= P2^0;
sbit led2= P2^1;
sbit led3= P2^2;
sbit led4= P2^3;
void ISR_ex0(void);
void Delay();
void main()
{
        P0 = 0x00;    // Make all pins zero
        P1 = 0x00;    // Make all pins zero
        P2 = 0x00;    // Make all pins zero
        P3 = 0x04;    // making Port3 pin 2 high
        IE=0x81;             // Enable INT0
        IT0=1;                 // Set Falling Edge Trigger for INT0
 while(1)
    {             
         led1= 1;
         led2= 0;
         led3= 1;
         led4= 0;
    }

}

//  Interrupt service routine function
void ISR_ex0(void) interrupt 0     // ISR for external interrupt INT0
      {

           led1=~led1;
           led2=~led2;
           led3=~led3;
           led4=~led4;
           Delay();              
     }

//  Initialize delay with timer0 
void Delay()
   {

     int i;
     TMOD = 0x01;      // Timer0 mode1

                for(i=0;i<200;i++)
                {

               TH0=0xF8;              //initial values for 1sec delay
                TL0=0xCC;
                TR0 = 1;               // timer0 start
            while (TF0 == 0);   // check overflow condition
                TR0 = 0;               // Stop Timer
                TF0 = 0;               // Clear flag
              }

}

This is all about how to use interrupts of 8051 microcontroller. How to use an external interrupt of 8051 microcontroller. If you feel the issue with programming and simulation, please let us know with your comments. Next time I will come with more interesting tutorials on 8051 microcontroller and their applications.

4 thoughts on “External Interrupts 8051 Microcontroller – Example”

  1. Hi, can u rearrange the code to turn on led at starting point for 1 second and after switch on the led need to off for 2 second. Then back to on stage.

    Reply
  2. good evng sir,
    for 1 sec delay i take
    for(i=0;i<15;i++)
    tl0=0xff;
    th0=0x0f;
    why it don't works?
    in general i applied it for many programs ,it works also.

    Reply

Leave a Comment

fourteen − three =