简体   繁体   English

为什么按下 MSP430 上的按钮不点亮 LED

[英]Why Button Press on MSP430 Doesn't Light LEDs

Pin 1.3step counter: Modify the code to have the LEDs step one binary order each time the button has been pushed.引脚 1.3 步进计数器:修改代码以在每次按下按钮时让 LED 步进一个二进制顺序。

I'm relatively new with programming on a Micro Controller.我对 Micro Controller 编程比较陌生。 So what is above is what I'm attempting to do.所以上面是我正在尝试做的事情。 However while trying to get it to step in binary order, the button has seemed to stop working and thus none of the LEDs flash.然而,当试图让它以二进制顺序执行时,按钮似乎停止工作,因此没有一个 LED flash。 I'm uncertain what changed since previously it ran well without the button push.我不确定发生了什么变化,因为以前它在没有按钮按下的情况下运行良好。 Comparing with friends it looks the same so stumped on what went wrong.与朋友比较,它看起来一样,所以很难理解出了什么问题。

This is the code I have.这是我的代码。

#include <msp430.h> 


/*
 * main.c
 */
int main(void)
{
  WDTCTL = WDTPW + WDTHOLD;                 // Stop watchdog timer

  volatile unsigned int i;
  volatile unsigned int j;

  P1DIR |= 0x41;                            // Set P1.0 to output direction
  P1DIR &=~(BIT3);                          // Ensure P1.3 is an input
  P1REN |= BIT3;                            // Set pulling Resistor for P1.3
  P1OUT |= BIT3;                            // Make the pulling resistor for P1.3 a pull-UP resistor
  j=0;                                      // Button presses set to 0
  P1OUT &=~0x01;

  while (1)                                 // Test P1.3
  {
    if ((BIT3 & P1IN)) {                    // if P1.3 set, set P1.0 turning on the LED
        if (j == 0)
            P1OUT &= ~0x41;
        if (j == 1) {
            P1OUT |= 0x01;
            P1OUT &= ~0x40;
        }
        if (j == 2) {
            P1OUT &= ~0x01;
            P1OUT |= 0x40;
        }
       if (j == 3)
           P1OUT |= 0x41;
    else {
        for (i=3000; i>0; i--);
        if (j == 3)
            j++;
        else
            j = 0;
        }
       while ((BIT3 & P1IN));

  }
}
}

You only increase j if j == 3 .如果j == 3 ,您只会增加j So j always stays at 0, which means all LEDs off.所以 j 始终保持为 0,这意味着所有 LED 都关闭。 You also missed a closing brace before the else.您还错过了 else 之前的右大括号。 Formatting the code with correct indentation helps finding such errors.使用正确的缩进格式化代码有助于发现此类错误。

  while (1)
  {
    if ((BIT3 & P1IN)) 
    {
      if (j == 0)
      {
        P1OUT &= ~0x41;
      } 

      if (j == 1) 
      {
        P1OUT |= 0x01;
        P1OUT &= ~0x40;
      }

      if (j == 2) 
      {
         P1OUT &= ~0x01;
         P1OUT |= 0x40;
      }

      if (j == 3)
      {
         P1OUT |= 0x41;
      }
    }
    else 
    {
       for (i=3000; i>0; i--);

       // here I exchanged the == with <
       if ( j < 3 ) 
       {
         j++;
       }
       else
       {
         j = 0;
       }
    }
    while ((BIT3 & P1IN));
 }

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

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