1. 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.
    Dismiss Notice

Programming External Interrupts in 8051

Discussion in 'Microcontrollers' started by Parth86, Jun 28, 2017.

  1. Parth86

    Parth86 Member

    Joined:
    Dec 25, 2011
    Messages:
    546
    Likes:
    5
    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 ?
    Code (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)
       {
        }
    }
     
     
  2. Ian Rogers

    Ian Rogers Super Moderator Most Helpful Member

    Joined:
    Mar 28, 2011
    Messages:
    9,304
    Likes:
    914
    Location:
    Rochdale UK
    ONLINE
  3. Parth86

    Parth86 Member

    Joined:
    Dec 25, 2011
    Messages:
    546
    Likes:
    5
    I have seen your example . look at this code its not complete but does it make any sense

    Code (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)
      {
      }
    }

     
     
  4. dave

    Dave New Member

    Joined:
    Jan 12, 1997
    Messages:
    -
    Likes:
    0


     
  5. Ian Rogers

    Ian Rogers Super Moderator Most Helpful Member

    Joined:
    Mar 28, 2011
    Messages:
    9,304
    Likes:
    914
    Location:
    Rochdale UK
    ONLINE

    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!
     
  6. Parth86

    Parth86 Member

    Joined:
    Dec 25, 2011
    Messages:
    546
    Likes:
    5
    I have complied code without error but my interrupt not working. I think I have not written code for LED. it may reason
     

Share This Page