Programming External hardware Interrupts in 8051 Microcontroller

External Interrupts in 8051 Microcontroller

Programming External hardware interrupts paves way for the users to interfere the process of Microcontroller externally and force it to execute a specific set of commands. Since the interrupt is generated from a external source it is named as External interrupts and here we are about to use a simple switch button to generate the interrupt to the Microcontroller.

I assume you are familiar with the concept, working and registers involved in the Interrupts, if not kindly go through this 8051 Interrupt introduction and programming.


IE register

IE register is meant for activating the interrupts we are about to use in 8051 programming. As you can see in the above diagram EX1 and EX0 bits are used to enable/disable external interrupts in 8051. The MSB of the IE register EA is meant to enable or disable the interrupts, writing 1 to it will tell the controller a interrupt is used and 0 will tell that no interrupt will be used.

So to activate both the external interrupts EX0 and EX1 we must write

IE= 1000 0101

IE=0x85 will activate both the interrupts.

Another thing involved in programming interrupts is they should be referred based on their numbers in the subroutine programs. The numbers of the respective numbers is given below.

Interrupt number table

Load the hex value 0x85 in the IE register in case we are about to use both the interrupts or 0x81 for EX0  and 0x84 for EX1 alone.


  1. Subroutine for each of the interrupt should be followed by interrupt number
  2. Write commands you need to execute inside the subroutine when interrupt is encountered and that’s it you are done.


The objective of the below code is to tun off the LED’s connected to Port 0 when interrupt 0 occurs and turn it on when interrupt 1 occurs. This code is built using Keil C compiler.

void main()
   IE=0x85;                    // Activating both the interrupts EX0 and EX1
void extr0(void) interrupt 0     //Subroutine EX0 with interrupt number '0'
    P0=0xff;                            //Your command goes here
void extr1(void) interrupt 2    //Subroutine EX1 with Interrupt Number '1'

The above code is simple as it looks and you have to be careful for writing the correct interrupt number for the respective interrupt. You must place the commands for execution inside the Subroutines.

The following two tabs change content below.
Frank Donald

Frank Donald

Junior Network Engineer at Tata Communications
Frank Donald is an Electronics and Communication Engineer who loves building stuff in his free time. Video games, blogging and programming are the things he loves most

Ask your query

Be the First to Comment!

Notify of