[51] Help me to Program / toggle LED with Switch using external interrupt in STC89C52RC Micro controller

ahmar123

Newbie level 5
Newbie level 5
Joined
Jul 10, 2024
Messages
8
Helped
0
Reputation
0
Reaction score
0
Trophy points
1
Activity points
50
I write this code to Control the Led with Switch using external Interrupt INT0 , the code compiled and have no error but led is not controlled with Pushbutton , dont know whats the Problem is...


Code C - [expand]
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
#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;
}
}


if this code is not
then this one is also not working


Code C - [expand]
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
#include<reg51.h>
 
sbit LED = P2^0;   // LED at P2.0
sbit Switch = P3^2; // Switch at External Interrupt Pin P3.2
 
void external_ISR(void) interrupt 0 { // ISR for External Interrupt 0
    if (Switch == 1) {               // If switch is HIGH
        LED = 1;                    // LED ON
    } else {                         // If switch is LOW
        LED = 0;                    // LED OFF
    }
}
 
void main() {
    EA = 1;    // Enable Global Interrupts
    EX0 = 1;   // Enable External Interrupt 0
    IT0 = 1;   // Interrupt on Falling Edge
 
    LED = 0;   // Initial LED OFF
 
    while (1) {
        // Infinite Loop - Waiting for Interrupt
    }
}

 
Last edited by a moderator:

Hi,

think about it:

"Interrupt on falling edge" ...
so ever time the ISR is called the signal just turend from HIGH to LOW ...
So it´makes no sense to check on SWITCH state ... you know it is LOW.

....

You get no interrupt when SWITCH turns HIGH unless you set up the microcontroler to do so.

***
I can´t tell about the features of your (in my opinion outdated) microcontroller.
I needed to read the datasheet ... but I recommend to do so first.

Klaus
 

#include<reg51.h>
sbit LED = P2^0; // LED at P2.0
sbit Switch = P3^1; //
void delay_ms(unsigned int ms) {
unsigned int i, j;
for (i = 0; i < ms; i++) {
for (j = 0; j < 120; j++); // Approx 1ms delay in 8051 with 11.0592 MHz Crystal
}
}

void ISR_External0(void) interrupt 0 // ISR for External Interrupt 0
{
// Debounce delay of 500ms
LED = ~LED;
delay_ms(300); // Toggle LED
}

void main()
{
EA = 1; // Enable Global Interrupts
EX0 = 1; // Enable External Interrupt 0
IT0 = 0; // Rising Edge Mode for Pull-Down Resistor

LED = 1; // LED OFF Initially

while(1)
{
// MCU will wait and only wake up on Rising Edge
}
}
Write this code , but when i press switch 3 which is on P3^2 and mu Interrupt INT0 which is also on P3^2 so upon pressing the button on P3^1 nothing happen but when i pressed switch on P3^2 the interrupt occur and led start togliing , I dont why , rather then i want to understand Programming Interurpt with Switch in 8051 Micro controller so i can get an idea , my microcontroller is 89C52RC
--- Updated ---

 

I have not worked on STC. Still assuming necessary External Interrupt related registers are set properly, please try this code and see if LED toggles.


Code:
sbit LED = P2^0;   // LED at P2.0
sbit Switch = P3^2; // Switch at External Interrupt Pin P3.2
 
void external_ISR(void) interrupt 0 { // ISR for External Interrupt 0
    LED = ~LED;
Delay_ms(1000);
}
 
void main() {
    EA = 1;    // Enable Global Interrupts
    EX0 = 1;   // Enable External Interrupt 0
    IT0 = 1;   // Interrupt on Falling Edge
 
    LED = 0;   // Initial LED OFF
 
    while (1);
        // Infinite Loop - Waiting for Interrupt
    }
--- Updated ---

upon pressing the button on P3^1 nothing happen
Couldn't get you. P3^2 has been defined as External Interrupt source, then why interrupt will work upon pressing button on P3^1? Also P3^1 is not External Interrupt pin as shown in datasheet.
 
Last edited:

#include<reg51.h>
is it asking too much to press the [ c o d e ] button .. for inserting code?


void ISR_External0(void) interrupt 0 // ISR for External Interrupt 0
{
// Debounce delay of 500ms
LED = ~LED;
delay_ms(300); // Toggle LED
}
Tbh .. this is the worst misuse of interrupt I´ve seen.
Interrupts are invented to:
* save processing power (but your´s waste it)
* get fast response
* not to misss (oher) events. But your code is made to miss events.

And one of the major rules for ISRs: They should be as fast as possible to return. Should not include busy_waits.

// MCU will wait and only wake up on Rising Edge
It can´t wake up, because it is not sleeping at all.
You just make it busy all the time. That´s the opposite of sleeping.

***
My recommendation:
* describe what you want to do. Including a timing diagram and a flow chart. Nothing fancy, just one minut using pencil and paper.
* go through some Interrupt tutorials. To learn what they are made for and how to setup and use them.

Is this a school project, a job, or hobby?

Klaus
 

I got you , but how can I map the external interrupt P3^2 with Switch K1 P3^1 so when I press K1 interrupt occur and toggle the Led , I know that interrupt is at p3^2 and switch is also at P3^2 so the interrupt will occur when I press switch 3 , k3 on P3^2 , but I want that K1 p3^1 controll the toggle of Led
If there is any resources you can share...
--- Updated ---

Got it...
 

Similar threads