Question

2.3 Using Two Buttons with Exclusive Access Modify the code from the previous part so that the red LED is on while S1 is pushed and the green LED is on while S2 is pushed. However, the two LEDs should not be lit simultaneously. Imagine that they correspond to physical phenomena and a hazard occurs if both LEDs are lit at the same time. Your software should ensure this doesnt happen. Accordingly, the button that is pushed first has precedence. Lets say S1 is held down and the red LED is on. If, in the meanwhile, S2 is pushed, the green LED remains off and the red LED continues to be on. This state persists until S1 is released. Now, S2 has the chance to turn on the green LED. And, likewise, if S2 is held down with the green LED on, S1 is ignored when pushed, until S2 is released.

C CODE LANGUAGE. MSP430.

#include <msp430fr6989.h>

#define redLED BIT0 // Red LED at P1.0

#define greenLED BIT7 // Green LED at P9.7

#define BUT1 BIT1 // Button S1 at P1.1

#define BUT2 BIT2 // Button S2 at P1.2

void main(void) {

WDTCTL = WDTPW | WDTHOLD; // Stop the Watchdog timer

PM5CTL0 &= ~LOCKLPM5; // Enable the GPIO pins

// Configure and initialize LEDs

P1DIR |= redLED; // Direct pin as output

P9DIR |= greenLED; // Direct pin as output

P1OUT &= ~redLED; // Turn LED Off

P9OUT &= ~greenLED; // Turn LED Off

// Configure buttons1

P1DIR &= ~(BUT1 | BUT2); // Direct pin as input

P1REN |= (BUT1 | BUT2); // Enable built-in resistor

P1OUT |= (BUT1 | BUT2); // Set resistor as pull-up

// Polling the button in an infinite loop

for(;;) {

// Fill the if-statement below...

if((P1IN & BUT1)==0)

P1OUT |= redLED; // Turn red LED on

else P1OUT &= ~redLED; // Turn red LED off

if((P1IN & BUT2)==0)

P9OUT |= greenLED; // Turn green LED on

else P9OUT &= ~greenLED; //Turn green LED off

}

}

0 0
Add a comment Improve this question Transcribed image text
Answer #1

C CODE LANGUAGE. MSP430.

#include <msp430fr6989.h>

#define redLED BIT0 // Red LED at P1.0

#define greenLED BIT7 // Green LED at P9.7

#define BUT1 BIT1 // Button S1 at P1.1

#define BUT2 BIT2 // Button S2 at P1.2

void main(void) {

WDTCTL = WDTPW | WDTHOLD; // Stop the Watchdog timer

PM5CTL0 &= ~LOCKLPM5; // Enable the GPIO pins

// Configure and initialize LEDs

P1DIR |= redLED; // Direct pin as output

P9DIR |= greenLED; // Direct pin as output

P1OUT &= ~redLED; // Turn LED Off

P9OUT &= ~greenLED; // Turn LED Off

// Configure buttons1

P1DIR &= ~(BUT1 | BUT2); // Direct pin as input

P1REN |= (BUT1 | BUT2); // Enable built-in resistor

P1OUT |= (BUT1 | BUT2); // Set resistor as pull-up

// Polling the button in an infinite loop

for(;;) {

// Fill the if-statement below...

if((P1IN & BUT1)==1 && BUT2==0)

P1OUT |= redLED; // Turn red LED on

else if (BUT2==1)

P9OUT |= greenLED; // Turn green LED on

BUT1=0

else P1OUT &= ~redLED; // Turn red LED off

if((P1IN & BUT2)==1 && BUT1==0)

P9OUT |= greenLED; // Turn green LED on

else if (BUT1==1)

P1OUT |= redLED; // Turn red LED on

BUT2=0

else P9OUT &= ~greenLED; //Turn green LED off

}

}

Add a comment
Know the answer?
Add Answer to:
C CODE LANGUAGE. MSP430. #include <msp430fr6989.h> #define redLED BIT0 // Red LED at P1.0 #define greenLED...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • Help modify below code using the timer Timer_A to toggle two LEDs in periodic time intervals...

    Help modify below code using the timer Timer_A to toggle two LEDs in periodic time intervals of A)10 sec B)30 sec C)1 sec the original code toggled red LED when an overflow occured: #include <ms430.h> #define RedLED BIT0 #define RedLEDToggle (P1OUT ^= RedLED) void main (void) { WDTCTL = WDTPW|WDTHOLD; P1DIR = RedLED; P1OUT = RedLED; TECTL = TASSEL_2|ID_3|MC_3|TAIE; TACCR0 = 62500; _enable_interrupts(); LPM1;       //enter low power mode } #pragma vector=TIMER_A1_VECTOR __interrupt void Timer_A(void) { switch(TAIV) { case 0x02:...

  • I need help with doing these tasks for code composer Lab 3 - Branching, Push Button...

    I need help with doing these tasks for code composer Lab 3 - Branching, Push Button and LEDs-Reading Assignment in this lab, we are going to control a LED via a push button- using general purpose digital 10 for both input (button) and output (LED) on port 1 - using Code Composer Studio. Furthermore, we are going to use a branch instruction to create an IF-ELSE structure in assembly to determine if the LED should be lit up based on...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT