Programming External hardware Interrupts in 8051 Microcontroller

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

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.

STEPS TO PROGRAM INTERRUPTS:

  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.

SAMPLE CODE:

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.

#include<regx51.h>
void main()
  {
   IE=0x85;                    // Activating both the interrupts EX0 and EX1
   while(1);
  }
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'
  {
    P0=0x00;
  }

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.


Leave a Comment

Your email address will not be published. Required fields are marked *