vead
Full Member level 5
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 ?
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: