• Welcome to our site! Electro Tech is an online community (with over 170,000 members) who enjoy talking about and building electronic circuits, projects and gadgets. To participate you need to register. Registration is free. Click here to register now.

Programming External Interrupts in 8051

Status
Not open for further replies.

Parth86

Member
Hello everyone
I want to understand programming of external interrupts in 8051. I want use external interrupt for LED .Whenever interrupt occur the LED should be On/ off, depending upon the input switch at pin P3.2.if switch is high the LED should be turn ON, when switch is low LED should be off.
I have written this program. what should be in main function for proper working ?
C:
#include<reg51.h>

// LED Pin
sbit LED = P1^0;          // Pin P1.0 is named as LED

//Function declarations
void init(void);
void InitINT0(void);

// Init  function
void init(void)
{
   P0 = 0x00;    // Make all pins zero
   P1 = 0x00;    // Make all pins zero
   P2 = 0x00;    // Make all pins zero
   P3 = 0x04;    // Make P3.2 (INT0) pin high only
}
// External INT0 pin interrupt init function
void InitINT0(void)
{
   IT0 = 1;      //Edge triggered interrupt mode (Neg Edge)
   EX0 = 1;      //Enable external interrupt INT0
   EA  = 1;      //Enable global interrupts
}
// INT0 ISR
void external0_isr(void) interrupt 0     
{
   LED = ~LED;   // Toggle LED pin
}
// Main function
void main(void)
{
 
   while(1)
   {
    }
}
 

Parth86

Member
Look at my tutorials.... All has been done for you!!
There are quite a few.
Simple interrupts on the 8051..
http://www.electro-tech-online.com/articles/authors/ian-rogers.163748/
I have seen your example . look at this code its not complete but does it make any sense

C:
#include<reg51.h>
// LED Pin
sbit LED = P1^0;          // Pin P1.0 is named as LED

unsigned char ex0_isr_counter = 0;

void ex0_isr (void) interrupt 0
{
ex0_isr_counter++;   // Increment the count
}

void main (void)
{

IT0 = 1;   // Configure interrupt 0 for falling edge on /INT0 (P3.2)
EX0 = 1;   // Enable EX0 Interrupt
EA = 1;    // Enable Global Interrupt Flag

while (1)
  {
  }
}
 

Ian Rogers

User Extraordinaire
Forum Supporter
Most Helpful Member
Your code is okay... But make the global variable "volatile" or the compiler may optimize it.. This is because you are not doing anything with it... Normally you ned the variable in the main so if its volatile the compiler won't delete it!
 

Parth86

Member
Your code is okay... But make the global variable "volatile" or the compiler may optimize it.. This is because you are not doing anything with it... Normally you ned the variable in the main so if its volatile the compiler won't delete it!
I have complied code without error but my interrupt not working. I think I have not written code for LED. it may reason
 
Status
Not open for further replies.

EE World Online Articles

Loading
Top