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.

8051 Microcontroller Switches to Control 2 Leds

Status
Not open for further replies.

vead

Full Member level 5
Joined
Nov 27, 2011
Messages
285
Helped
3
Reputation
6
Reaction score
3
Trophy points
1,298
Location
india
Activity points
3,815
switches to control 2 leds

Hello everyone

I want each switch to control its own led, so switch1 control red led and switch 2 control green led . 8051 and keil compiler

1. Does this program control two led's ?
2. Is it efficient program or it can be better ?

Code:
#include<REGX51.h>
	
  #define led_off          0
  #define led_on           1
 
  #define Switch_off       0
  #define Switch_on        1
  
 /*set two Switches*/
  sbit  Switch1 = P1^1;                    
  sbit  Switch2 = P1^2;                   
 
  /*set to LEDs*/
  sbit red_led = P2^1;                       
  sbit green_led = P1^2;                      

  /* Delay time  */ 
  void delay (unsigned long wait)            
  {
       unsigned int i;
       for (i = 0; i < wait;  i++);
  }
	
 void initialize (void)
  {		
      red_led = led_off;
      green_led = led_off;
			
      Switch1 = Switch_off;        
      Switch2 =Switch_off; 
    }			
 
  void main (void)
  {
     initialize ();
 
while (1)
  {
        while (Switch1 == Switch_on)
          {
		  red_led= led_on;
		  delay (1000);        
	          red_led = led_off;
	 }
 
    while (Switch2== Switch_on)
       {
	       green_led = led_on;
	       delay (1000);
	      green_led = led_off;		 
      }
  }
}
 
Last edited:

Does this program control two led's ?
No, the LED will be Off for just few microseconds (or rather, some instructions of the microcontroller) unless you add an extra delay. Anyway, you might start thinking about using some simulator or doing protoboard experiments. It is not good practice to expect others to confirm the supposed operation of your code. No one has the dexterity of a compiler, and everyone can fall into the same mistake, anyway you were already made aware of that in other threads.
Code:
green_led = led_on;
delay (1000);
green_led = led_off;	
delay (1000);

Is it efficient program or it can be better ?

Yes, by using timer interrupt to achieve the delay without blocking the CPU; take a look on examples for the compiler you are using.
 
  • Like
Reactions: vead

    vead

    Points: 2
    Helpful Answer Positive Rating
Hi,

Anyway, you might start thinking about using some simulator
... or at least a sheet of paper and a pencil.

Klaus
 
  • Like
Reactions: vead

    vead

    Points: 2
    Helpful Answer Positive Rating
I'm missing a description of intended program function. In so far it's impossible to answer the question.

The delay purpose isn't obvious. Presently it's acting as off-delay.

If both switches are activated simultaneously, the second one will be ignored. Is this what you want?
 
  • Like
Reactions: vead

    vead

    Points: 2
    Helpful Answer Positive Rating
If both switches are activated simultaneously, the second one will be ignored. Is this what you want?

I have tested below code in simulator. Both switch are working

Code:
#include<REGX51.h>
	
  #define led_off          0
  #define led_on           1
 
  #define Switch_off       0
  #define Switch_on        1
  
 /*set two Switches*/
  sbit  Switch1 = P0^5;                    
  sbit  Switch2 = P3^1;                   
 
  /*set to LEDs*/
  sbit red_led = P1^0;                       
  sbit green_led = P1^1;                      

  /* Delay time  */ 
  void delay (unsigned long wait)            
  {
       unsigned int i;
       for (i = 0; i < wait;  i++);
  }
	
 void initialize (void)
  {		
      red_led  =  led_off;
      green_led  =  led_off;
			
      Switch1 =  Switch_off;        
      Switch2 =  Switch_off; 
  }			
 
  void main (void)
  {
     initialize ();
 
while (1)
  {
        if (Switch1  ==  Switch_on)
          {
		 red_led =  led_on;
		 delay (2000);        
	         red_led =  led_off;
		 delay (2000); 
	 }
 
        if  (Switch2== Switch_on)
         {
	          green_led  =  led_on;
	          delay (4000);
	          green_led  =  led_off;	
                   delay (4000);	 
         }
  }
}
what do you think about code? Do you find any mistake? How to write better code that should be much faster and able to check all test?
 
Last edited:

Hi,

the simulator will give reliable results.

I wonder if you recognized the behaviour:
* the duty cycle of the red LED is different to the green LED.
* there is no defined LED state when you switch OFF (your switches).corrected: sorry. The LEDs are OFF.

Klaus
 

Hi,

...and the LED duty cycle influence each other.
means: that the brightness of a LED is different when the other switch is ON or OFF.

Klaus
 

Hi,

...and the LED duty cycle influence each other.
means: that the brightness of a LED is different when the other switch is ON or OFF.

Klaus

Please look at following screen shot
 

Attachments

  • diagram2.jpg
    diagram2.jpg
    154.1 KB · Views: 170
  • diagram1.jpg
    diagram1.jpg
    158.4 KB · Views: 148
  • diagram 2 both sw open.jpg
    diagram 2 both sw open.jpg
    147 KB · Views: 160

Hi,

* impossible to validate brightness or duty cycle
* impossible to see the fourth state (important) when both switches are ON.

Klaus
 

Hi,

* impossible to validate brightness or duty cycle
* impossible to see the fourth state (important) when both switches are ON.

Klaus

Sorry what do you mean by britness. I just removed delay in while loop to show condition when both switches are ON

See another image where you can see red and green LED.
 

Attachments

  • both switch closed.jpg
    both switch closed.jpg
    159.7 KB · Views: 157

Hi,

brightness of a LED. I think this is well defined.
****

If just the red LED is ON:
The red LED duty cycle is 2000/2000 (ON/OFF)

If both LEDs are ON:
The red LED duty cycle is 2000/10000 (ON/OFF)

Klaus
 
  • Like
Reactions: vead

    vead

    Points: 2
    Helpful Answer Positive Rating
Hi,

Klaus

How to replace switch with push button ?

I want to have a LED turn on when I push a button and go off when I push the button a second time.

Does this read the status of push button ?
Code:
      if (Switch ==  switch_pressed)

        {
                 debounce (40 );   // 40 ms

                 if (Switch ==  switch_pressed)
        }
 
Last edited:

Status
Not open for further replies.

Similar threads

Part and Inventory Search

Welcome to EDABoard.com

Sponsor

Back
Top