Continue to Site

Welcome to EDAboard.com

Welcome to our site! EDAboard.com is an international Electronics Discussion Forum focused on EDA software, circuits, schematics, books, theory, papers, asic, pld, 8051, DSP, Network, RF, Analog Design, PCB, Service Manuals... and a whole lot more! To participate you need to register. Registration is free. Click here to register now.

[51] led on off using interrupt

Status
Not open for further replies.

vikun

Newbie level 3
Joined
Aug 3, 2017
Messages
3
Helped
0
Reputation
0
Reaction score
0
Trophy points
1
Activity points
24
sir, i want ,
1) if i press switch first time the LED will b ON until delay ends and LED OFF when Delay ends.
also,
2)if i press switch before delay Ends LED should be OFF immediately.

i have tried this but, first if i press switch first 1st is ok but 2nd is not.

please tell me logic and code.
 

Hi,

Where is your code?
Best if you could show your schematic, too.

Klaus
 

Hi,

Where is your code?
Best if you could show your schematic, too.

Klaus




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
27
28
29
30
31
32
33
34
35
36
37
38
39
40
#include<REG51.h>
 
sbit in   = P1^2;
 
sbit out  = P1^7;
 
 
 void Delay( unsigned int msDelay )
{                     
        
    unsigned int i, j;
    for( i = msDelay; i > 0; --i)
    {
        for( j = 57600; j > 0; --j );
        {
            if(in==0)
            out=0;
            break;
         }   
     }
}
 
void main()
{
 
   in=1;
   out=0;
   
    while(1)
      {
         if(in==0)
           {
             out  =  1,
             
             Delay(3000);
             
             out =  0;
           }
      }
}

 
Last edited by a moderator:

Hi,

General rule for ISRs:
They should be as short as possible in time.
--> No loops, no busy_waits.

I assume
* you didn't draw a flow chart
* you didn't do a step-by-step trave (either with software or with paper and pencil)

Issues*
* you need some debouncing technique. Either hardware or software
* you need to detect key_press
* and additionally you need to detect key_release.

******
My recommended solution.
* don't use an input_pin_ISR
* use a fixed 5...50ms timer ISR. (Let's say 20ms)

Within the ISR:(pseudo code)
Code:
* key_actual = key
* if LED is ON
*    If (key_actual = active) AND (key_before = inactive): --> switch LED OFF
*    Else: decrement delay_counter; if delay_counter=0, then switch LED OFF

* ELSE (LED = OFF)
*   If (key_actual = active) AND (key_before = inactive): --> switch LED ON, set delay_counter to the desired value

* key_before = key_actual
* exit ISR
...not tested.

Klaus
 

Hi,

General rule for ISRs:
They should be as short as possible in time.
--> No loops, no busy_waits.

I assume
* you didn't draw a flow chart
* you didn't do a step-by-step trave (either with software or with paper and pencil)

Issues*
* you need some debouncing technique. Either hardware or software
* you need to detect key_press
* and additionally you need to detect key_release.

******
My recommended solution.
* don't use an input_pin_ISR
* use a fixed 5...50ms timer ISR. (Let's say 20ms)

Within the ISR:(pseudo code)
Code:
* key_actual = key
* if LED is ON
*    If (key_actual = active) AND (key_before = inactive): --> switch LED OFF
*    Else: decrement delay_counter; if delay_counter=0, then switch LED OFF

* ELSE (LED = OFF)
*   If (key_actual = active) AND (key_before = inactive): --> switch LED ON, set delay_counter to the desired value

* key_before = key_actual
* exit ISR
...not tested.

Klaus



could you plz send me code .. i am using AT89C2051 controller ic.
 

Hi,

No.
* I'm not familiar with the used microcontroller
* I don't know which compiler you use
* there are a lot of specifications missing
* maybe you find one if you pay him
* it's your job to do the work. We will only support you to solve problems

Klaus
 

Which MCU do you use?

2)if i press switch before delay Ends LED should be OFF immediately.

then what? will it remain in OFF for ever or will ON again?

Clarify your requirement. Do you want a continuous loop or a single shot?
 

Try this code.

You have to add debounce delay at two places.

Code:
#include <REG51.h>
 
sbit in  = P1^2; 
sbit out = P1^7;
 
 
 void Delay( unsigned int msDelay )
{       
    unsigned int i, j;

    for( i = msDelay; i > 0; --i)
    {
        for( j = 57600; j > 0; --j )
        {
            if(in == 0) {
		//debounce delay required here
		if(in == 0) 
			{
                    		out = 0;
                    		break;
			}
	    }
        }   
     }
}
 
void main()
{
 
   in = 1;
   out = 0;
   
    while(1)
      {
         if(in == 0)
           {
	   //debounce delay required here
	   if(in == 0)
		{
             		out  =  1,
             
             		Delay(3000);
             
             		out =  0;
	   	}
           }
      }
}
 

Hi,

the above code won´t work:
* it doesn`t follow the rule: "* and additionally you need to detect key_release." from post#4
* the OP wants it to be interrupt driven.
I assume (but don´t know for sure) not because he just wants to use "interrupt", but because the function should operate independently of other tasks running in main loop.

Klaus
 

Status
Not open for further replies.

Similar threads

Part and Inventory Search

Welcome to EDABoard.com

Sponsor

Back
Top