简体   繁体   English

MSP430FR6989 按钮和 LED 拨动开关

[英]MSP430FR6989 button and led toggle

Working on a school project and I am stuck on my last bit of code.在学校项目上工作,我被困在我的最后一点代码上。 Let's say S1 is held down and the red LED is on.假设按住 S1 并且红色 LED 亮起。 If, in the meanwhile, S2 is pushed, the green LED remains off and the red LED continues to be on.如果同时按下 S2,则绿色 LED 保持关闭,红色 LED 继续点亮。 This state persists until S1 is released.这种状态一直持续到 S1 被释放。 Now, S2 has the chance to turn on the green LED.现在,S2 有机会打开绿色 LED。 And, likewise, if S2 is held down with the green LED on, S1 is ignored when pushed, until S2 is released.同样,如果在绿色 LED 亮起的情况下按住 S2,则按下时 S1 将被忽略,直到 S2 被释放。

I am currently stuck in the forever loop.我目前陷入了永远的循环。 I cannot get my code to do as what is described in the above paragraph.我不能让我的代码按照上一段中的描述来做。 Once both BUT1 and BUT2 are held down both green and red led lights turn off.一旦同时按住 BUT1 和 BUT2,绿色和红色 LED 灯就会关闭。

#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(;;) {

        if((P1IN & (BUT1|BUT2))==BUT2){
                 P1OUT |= redLED;   // Turn red LED on
        }

        if((P1IN & (BUT1|BUT2))==BUT1){
                 P9OUT |= greenLED;     // Turn green LED on
        }
       if (P1IN & (BUT1 | BUT2) == (BUT1|BUT2))
                   P1OUT &= ~redLED;
                   P9OUT &= ~greenLED;

    }
}

You've got a bit inconsistent with your brackets and braces.你的括号和大括号有点不一致。 Try this for the last section:在最后一节试试这个:

if ((P1IN & (BUT1 | BUT2)) == (BUT1|BUT2)) {
    P1OUT &= ~redLED;
    P9OUT &= ~greenLED;
}

Notice the brackets round P1IN & (BUT1 | BUT2) so that it's evaluated first.注意P1IN & (BUT1 | BUT2)周围的括号,以便它首先被评估。 Not also the braces round the two lines so they are both dependant on the condition, rather than just the first line.也不是围绕两条线的大括号,因此它们都取决于条件,而不仅仅是第一行。 Essentially it now matches you first two.基本上它现在匹配你的前两个。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM