Search code examples
cmicrocontrollermsp430

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


The code works as follows: Button pressed: green on, red off Button ot pressed: green off, red on

All the code samples ive seen from people online had the pullup resistor enabled when using a button to toggle between the LEDs. Is this even necessary for a code to work?

I commented the line out in my code and the only change i noticed was that it turned one the reset button. Is that what its intended for?

#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;
}

The code seems to work as intended but am wondering what this pullup resistor is actually intended to do?


Solution

  • Reset line is already pulled-up by external resistor, there is no need to pull it up with internal resistor. Reset pin also is not a GPIO (port) pin, so you basically can't enable internal pull-up for it. As for P1.3 button: some Launchpad board revisions have external pull-up resistor soldered for that button, some don't. So you should enable internal pullup for P1.3, just in case. When P1.3 button is not pressed, the circuit is open and you don't want P1.3 pin to be floating. Hence pull-up resistor required.

    I found some old code of mine, it may be helpful for you. Especially pay attention to comments.

    /* 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;
    }
    

    UPDATE

    On schematic below you can see that "RST" pin and "P1.3" pin both have dedicated external pull-up resister: R27 for RST and R34 for P1.3. But if you have new revision of board -- R34 is not soldered there, so you have to use internal pull-up.

    You can find complete schematics for your Launchpad here.

    Launchpad board schematic