Continue to Site

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.

  • 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.

Help me to Program LED with Switch using external interrupt in STC89C52RC Micrcontroller

Ahmar-47

New Member
C:
#include<reg51.h>

sbit LED = P2^0;   // LED at P2.0
sbit Switch = P3^2; // Switch at External Interrupt Pin P3.2
void interrpt ()
{
    if (Switch == 1)
        {               // If switch is HIGH
        LED = 1;                    // LED ON
    }
        else
            {                         // If switch is LOW
        LED = 0;                    // LED OFF
    }
    }


void main() {
       
    LED = 0;   // Initial LED OFF
    Switch = 1;
    while (1) {
        EA = 1;   
    EX0 = 1;  
    IT0 = 0;
    }
}
I write this code to Control the Led with Switch using external Interrupt , the code compiled and have no error but led is not controlled with Pushbutton , dont know whats the Problem is
 
Last edited by a moderator:
When you post use code tags to preserve indentation/formatting, makes readability
easy. See highlighted tags selection below.

1741177006674.png



Not a ST user but did you enable interrupts ? Note below is Microchip code but you get the idea....

Code:
    INTERRUPT_GlobalInterruptEnable();                                          // Enable the Global Interrupts
    INTERRUPT_PeripheralInterruptEnable();                                      // Enable the Peripheral Interrupts

//OR

   // Enable global interrupts
    INTCONbits.GIE = 1;  // Enable global interrupts
    INTCONbits.PEIE = 1; // Enable peripheral interrupts
 
Last edited:
Not a microchip user but did you enable interrupts ?

Code:
    INTERRUPT_GlobalInterruptEnable();                                          // Enable the Global Interrupts
    INTERRUPT_PeripheralInterruptEnable();                                      // Enable the Peripheral Interrupts

//OR

   // Enable global interrupts
    INTCONbits.GIE = 1;  // Enable global interrupts
    INTCONbits.PEIE = 1; // Enable peripheral interrupts

He's not using a MicroChip device, he's using a 8051 type processor.

But while I'm not familiar with his device, I don't see where he's enabling interrupts?, nor is he checking what caused the interrupt in his ISR (I won't even mention de-bouncing!).
 
I did a tutorial on interrupts..

here.. https://www.electro-tech-online.com/articles/simple-interrupts-on-the-8051.654/

Firstly your interrupt has to point to one of the interrupt vectors
Secondly in your forever loop you keep setting interrupts place these before the while loop

Im trying to remember what IT0 is... if its Timer 1 then that is incorrect.

The interrupt should be like this

void int_ISR(void) __interrupt (0) // I think your compiler will accept the vector pointer.
 
Registers for ISR control

1741183823274.png


Register defs attached....page 87 C example for timer you could examine for rough applicable code to do pin based ISR....might be of help. Then look at pin based reg set for clues....

What IDE are you using, I saw a reference for an IDE for this series part that had example code.....
 

Attachments

  • STC89C51RC-en.pdf
    590 KB · Views: 7
Last edited:
Its even simpler than that... The file above was retrieved from Kiel but typed in wrong.

Here is the original ( that worked) All thats needed is to copy the new bits in

C:
#include <REG52.H>

/*=============================================================================
=============================================================================*/
unsigned char ex0_isr_counter = 0;

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

/*=============================================================================
=============================================================================*/
void main (void)
{

/*-----------------------------------------------
Configure INT0 (external interrupt 0) to generate
an interrupt on the falling-edge of /INT0 (P3.2).
Enable the EX0 interrupt and then enable the
global interrupt flag.
-----------------------------------------------*/
IT0 = 1;   // Configure interrupt 0 for falling edge on /INT0 (P3.2)
EX0 = 1;   // Enable EX0 Interrupt
EA = 1;    // Enable Global Interrupt Flag

/*-----------------------------------------------
Wait forever.
-----------------------------------------------*/
while (1)
  {
  }
}
 

Latest threads

New Articles From Microcontroller Tips

Back
Top