Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

1. Write a program in C to modify the state of LED1 and LED2 depending upon whether SW1 or SW2 is pressed. At program startup,

1. Write a program in C to modify the state of LED1 and LED2 depending upon whether SW1 or SW2 is pressed. At program startup, LED1 should be on and LED2 should be off. As long as SW1 is pressed, both LEDs should be turned on and as long as SW2 is pressed, LED1 and LED2 should blink at 4Hz alternately.

Summary: LED1 LED2

No Switch Pressed (LED1-On, LED2-Off)

SW1 Pressed (LED1-On, LED2-On)

SW2 Pressed (LED1-4Hz, LED2-4Hz) (alternately)

After Releasing SW1/SW2 (LED1-Off, LED2-Off)

2. Bonus (up to 10 pts): If both switches are pressed, LED1 and LED2 should blink at the frequency of 2Hz and 4Hz respectively. LED1 and LED2 should be turned off when both switches are released.

Note: a. Implementation of bonus question needs to be as an extension of original assignment instead of as a separate program. b. The statement If both switches are pressed implies that switches can be pressed any order and they does not have to be pressed at the same time.

I need to implement the bonus. My code is below and the part I need to implement is the else if((S1) == 0 && (S2) == 0)

#include

#define S1 P2IN&BIT1 #define S2 P1IN&BIT1 #define REDLED 0x01 // Mask for BIT0 = 0000_0001b #define GREENLED 0x80 // Mask for BIT7 = 1000_0000b

void main(void) { WDTCTL = WDTPW + WDTHOLD; // Stop watchdog timer unsigned int i = 0;

P1DIR |= REDLED; // Set P1.0 to output direction P4DIR |= GREENLED; // Set P4.7 to output direction P1OUT |= REDLED; // Set P1OUT to 0000_0001b (LED1 is ON) P4OUT &= ~GREENLED; // Set P4OUT to 1000_0000b (LED1 is ON) P2DIR &= ~BIT1; // Set P2.1 as input for S1 input P2REN |= BIT1; // Enable the pull-up resistor at P2.1 P2OUT |= BIT1; P1DIR &= ~BIT1; // Set P1.1 as input for S1 input P1REN |= BIT1; // Enable the pull-up resistor at P1.1 P1OUT |= BIT1;

for(;;) // Infinite loop, LED1 ON AND LED2 OFF WORKS { // SWITCH 1 WORKS for (i = 2000; i > 0; i--); // Debounce ~20 ms if ((S1) == 0) // If S1 is pressed { for(;;) { P1OUT |= REDLED; // Turn LED1 on P4OUT |= GREENLED; // Turn LED2 on if((S1) != 0) { break; } } P1OUT &= ~REDLED; // Turn LED1 off P4OUT &= ~GREENLED; // Turn LED2 off } // SWITCH 2 WORKS else if ((S2) == 0) { P1OUT |= REDLED; // Turn LED1 on P4OUT |= ~GREENLED; // Turn LED2 on for(;;) { for (i = 0; i < 12500; i++); P1OUT ^= REDLED; // Toggle LED1 P4OUT ^= GREENLED; // Toggle LED2 if((S2) != 0) { break; } } P1OUT &= ~REDLED; // Turn LED1 off P4OUT &= ~GREENLED; // Turn LED2 off } else if((S1) == 0 && (S2) == 0) { P1OUT |= REDLED; // Turn LED1 on P4OUT |= ~GREENLED; // Turn LED2 on for(;;) { for (i = 0; i < 6250; i++) { P1OUT ^= REDLED; // Toggle LED1 //P4OUT ^= GREENLED; // Toggle LED2 } for (i = 0; i < 12500; i++) { P4OUT ^= GREENLED; // Toggle LED2 } if((S1) != 0 && (S2) != 0) { break; } } P1OUT &= ~REDLED; // Turn LED1 off P4OUT &= ~GREENLED; // Turn LED1 off } } }

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Graph Databases New Opportunities For Connected Data

Authors: Ian Robinson, Jim Webber, Emil Eifrem

2nd Edition

1491930896, 978-1491930892

Students also viewed these Databases questions

Question

How many products are formed when compound B is decarboxylated? , -

Answered: 1 week ago

Question

Please help me evaluate this integral. 8 2 2 v - v

Answered: 1 week ago