Calling an ISR using external interrupt INT0.

 

Code 

#include<reg51.h>                     //  Include 8051 library.
sbit led = P1^0;                          //  Set the port where the interrupt led is connected.
sbit led1 = P1^1;                       //  Set the port where the led is connected.
void delay(unsigned int t);       // Delay for turning ON and OFF  interrupt LED,
void delay1(int s);                     // Delay for turning ON and OFF LED,
void exo_isr() interrupt 0          // Interrupt function.
{
led = ~led;                    // Led turned ON & OFF.
delay(1000);                //  Delay calling for interrupt LED,
}
void main()                          // Main function starts
{
EA = 1;                                     // All interrupts enabled.
EX0 = 1;                                  // External interrupt enabled.
IT0 =0;                                   // Interrupt triggered on level.
while(1)                                   // Infinite loop.
{
led1 = ~led1;                 // Led1 turned ON & OFF.
delay1(1000);                 //  Delay calling for LED1,
  }
}

void delay(unsigned int t)        // Delay function
{
int i;                                        // Assigning integer i.
TMOD = 0X00;                       // Setting the Timer 0 in Mode 0 config. 
for(i=0;i<t;i++)                     // For loop for delay
{
TH0 = 0X19;                          // Assigning 19hex value to TH for 8millisec delay.
TL0 = 0X13;                          // Assigning 13hex value to TL for 8millisec delay.
TR0 = 1;                                // Initializing Timer 0. 
while(TF0==0);                    // Waiting for timer to complete 8millisec.
        TF0 = 0;                                // Timer overflow flag reset.
        }
}

void delay(int s)                                               // Delay function
{
    int i ,j;                                                          // Assigning integer i & j. 
       for(i=0;i<100*s ; i++)                              // For loop for delay
           for(j=0;j<=1275;j++);                         // Nested  For loop for delay
}


 :  Circuit Diagram  

Two leds are connected in sinking mode. One is for normal led function and the other one is for interrupt function.


A switch is connected to external interrupt INT0 to provide an external interrupt when switch is pressed.







Interrupt  Result   : 

Here you can see the switch acting as an external interrupt. We have configured the microcontroller in level external interrupt .


 It means that as long as the switch is pressed the interrupt function will be active . Once released it will jump back to main function that is blinking of led1.


We have added an infinite while loop so that we can use the interrupt as long as we want.
 


Comments