3. Write a program to repeat the following tasks continuously. - Turn all four L
ID: 1996066 • Letter: 3
Question
3. Write a program to repeat the following tasks continuously.
- Turn all four LED lights OFF. - Wait in a loop until one of the switches is pressed.
- Use the C-language “switch statement” to select one of the following:
Turn ON LED1 and LED2 if SW1 is pressed;
Turn ON LED2 and LED3 if SW2 is pressed;
Turn ON LED3 and LED4 if SW3 is pressed;
and Turn ON LED4 and LED1 if SW4 is pressed.
- Keep the values ON for three seconds.
A. Cut and paste the source code.
B. Press the Reset button on the Tower board and run the code.What are the LED values when you press SW4? Describe what you observe when you press a different switch when the light is ON.
Explanation / Answer
#include<reg51.h>
#define SW P2
sbit LED1 = P1^1
sbit LED2 = P1^2
sbit LED3 = P1^3
sbit LED4 = P1^4
sbit SW1 = P2^1
sbit SW2 = P2^2
sbit SW3 = P2^3
sbit SW4 = P2^4
void delay (int);
void main (void)
{
while(1)
{
LED1 = LED2 = LED3 = LED4 = 0;
switch(SW)
{
case(01): LED1 = 1;
LED2 = 1
delay(3000);
break;
case(02): LED2 = 1;
LED3 = 1
delay(3000);
break;
case(04): LED3 = 1;
LED4 = 1
delay(3000);
break;
case(08): LED4 = 1;
LED1 = 1
delay(3000);
break;
default :
LED1 = LED2 = LED3 = LED4 = 0;
}
}
}
void delay (int a)
{
for(;a>0;a--);
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.