启用或禁用 mps430 uC 时,上拉电阻会影响按钮吗?

Does pullup resistor affect the push button when enabled or disabled for a mps430 uC?

代码的工作原理如下: 按下按钮:绿色亮起,红色熄灭 未按下按钮:绿色关闭,红色开启

我从网上看到的所有代码示例都在使用按钮在 LED 之间切换时启用了上拉电阻。这甚至是代码工作所必需的吗?

我在我的代码中注释掉了这一行,我注意到的唯一变化是它变成了一个重置​​按钮。这是它的目的吗?

#include <msp430g2553.h>

#define LED0      BIT0
#define LED1      BIT6 
#define BUTTON    BIT3

int main(void)
{
    // Stop watchdog timer to prevent time out reset
    WDTCTL = WDTPW + WDTHOLD;
    P1DIR |= 0x40;
    P1OUT &= ~0x40;

    P1DIR |= 0x01;
    P1OUT &= ~0x01;

    P1DIR &= ~0x08;
    P1SEL &= ~0x08;
    //P1REN |=  0x08; 

    while (1) {
        if ((P1IN & 0x08) == 0) {
            P1OUT &= ~0x01;
            P1OUT |= 0x40;      
        } else {
            P1OUT |= 0x01;
            P1OUT &= ~0x40;
        }
    }

    return 0;
}

代码似乎按预期工作,但我想知道这个上拉电阻的实际用途是什么?

复位线已经外接电阻上拉,不需要再上拉内部电阻。复位引脚也不是 GPIO(端口)引脚,因此您基本上无法为其启用内部上拉。至于 P1.3 按钮:一些 Launchpad 板修订版为该按钮焊接了外部上拉电阻,有些则没有。所以你应该为 P1.3 启用内部上拉,以防万一。当 P1.3 按钮未按下时,电路是开路的,您不希望 P1.3 引脚悬空。因此需要上拉电阻。

我找到了一些我的旧代码,可能对你有帮助。特别注意评论。

/* Program demonstrates interacting with buttons (GPIO Inputs) */

#include <msp430.h>

#define LED_RED_BIT     BIT0
#define LED_GREEN_BIT   BIT6
#define BUTTON_BIT      BIT3

enum led_state {
    RED_LIGHT   = 0,
    NO_LIGHT_R  = 1,    /* no light after red led turned off */
    GREEN_LIGHT = 2,
    NO_LIGHT_G  = 3     /* no light after green led turned off */
};

enum led_state state;   /* current state of LED state machine */
static unsigned char is_button_down;

static void init_leds(void)
{
    P1DIR |= LED_GREEN_BIT | LED_RED_BIT;
    P1OUT &= ~LED_GREEN_BIT;
    P1OUT |= LED_RED_BIT;
    state = RED_LIGHT;
}

static void init_button(void)
{
    /* Since R34 is not installed -- turn on internal pull up for P1.3.
     * Details: according to LaunchPad User's Guide, section 1.3:
     * "Pullup resistor R34 and capacitor C24 on P1.3 removed to reduce
     * the current consumption"
     */
    P1OUT |= BUTTON_BIT; /* the pin is pulled up */
    P1REN |= BUTTON_BIT; /* enable pull-up resistor */
    is_button_down = 0;
}

/* Switches to next state of LED state machine */
static void trigger_led_sm(void)
{
    switch (state) {
    case RED_LIGHT:
        state = NO_LIGHT_R;
        P1OUT &= ~LED_RED_BIT;
        break;
    case NO_LIGHT_R:
        state = GREEN_LIGHT;
        P1OUT |= LED_GREEN_BIT;
        break;
    case GREEN_LIGHT:
        state = NO_LIGHT_G;
        P1OUT &= ~LED_GREEN_BIT;
        break;
    case NO_LIGHT_G:
        state = RED_LIGHT;
        P1OUT |= LED_RED_BIT;
        break;
    default:
        /* Reset state machine */
        init_leds();
        break;
    }
}

static void init(void)
{
    init_leds();
    init_button();
}

static void loop(void)
{
    /* Button polling */
    if (P1IN & BUTTON_BIT) { /* button is in released state */
        if (is_button_down) {
            is_button_down = 0;
            /* event: button up */
        }
    } else { /* button is in pressed state */
        if (!is_button_down) {
            is_button_down = 1;
            /* event: button down */
            trigger_led_sm();
        }
    }
}

int main(void)
{
    init();
    for (;;)
        loop();

    return 0;
}

更新

在下面的示意图中,您可以看到 "RST" 引脚和 "P1.3" 引脚都有专用的外部上拉电阻:R27 用于 RST,R34 用于 P1.3。但是如果你有新改版的板--R34没有焊在那里,所以你必须使用内部上拉。

您可以找到 Launchpad 的完整原理图 here