This thread has been locked.

If you have a related question, please click the "Ask a related question" button in the top right corner. The newly created question will be automatically linked to this question.

MSP-EXP430FR4133 Button S2

Other Parts Discussed in Thread: MSP-EXP430FR4133

I bought a MSP-EXP430FR4133 and fired up CCSv6.  I wanted to play around with the buttons to see if I can toggle an LED using a button.  I found out I had to set BIT6 on the P2OUT register in order to get it to work for S2.  For S1 it wasn't necessary.  Can anyone explain why this is?

  • According to the schematic, buttons S1 and S2 are just connections between P1.2/P2.6 and ground. This means that the GPIO pins need pull-up resistors to have a defined state when the button is not pressed. In other words, the PxREN and PxOUT bits must be set.

    I don't know why it works without the pull-up for S1, but it's probably just luck. Don't rely on it.

  • Sammy,

    when using buttons, the OUT-register is used in combination with the REN-register to set a pull-up/down-resistor. Can you show your complete code? S1 is on P1.2, S2 on P2.6 - both have no external resistor, so they both need a software set one inside the microcontroller. Since both switches short the pin to GND, you will use a pull-up resistor by setting

    P1OUT |= 0x04; // P1.2 resistor pulls-up
    P1REN |= 0x04; // P1.2 enable resistor

    and

    P2OUT |= 0x40; // P2.6 resistor pulls-up
    P2REN |= 0x40; // P2.6 enable resistor

    Dennis

  • This is my code for S1:
    int main(void) {
        volatile unsigned int p2_in;
        WDTCTL = WDTPW | WDTHOLD;               // Stop watchdog timer
        PM5CTL0 &= ~LOCKLPM5;                   // Disable the GPIO power-on default high-impedance mode
                                                // to activate previously configured port settings
    
        P1DIR |= BIT0;
        P4DIR |= BIT0;
        P5DIR |= BIT0;
    
        P1REN |= BIT2;
        P1DIR &= ~BIT2;
        //P1OUT |= BIT2;
        P1SEL0 &= ~BIT2;
        for(;;) {
            volatile unsigned int i;            // volatile to prevent optimization
    
            P1OUT ^= 0x01;                      // Toggle P1.0 using exclusive-OR
            p2_in = P1IN;
            if ((P1IN & BIT2) == BIT2) {
            	P4OUT |= 0x01;
            } else {
            	P4OUT &= ~0x01;
            }
      
            i = 10000;                          // SW Delay
            do i--;
            while(i != 0);
        }
    
        return 0;
    }

    This is my code for S2:

    int main(void) {
        volatile unsigned int p2_in;
        WDTCTL = WDTPW | WDTHOLD;               // Stop watchdog timer
        PM5CTL0 &= ~LOCKLPM5;                   // Disable the GPIO power-on default high-impedance mode
                                                // to activate previously configured port settings
        P1DIR |= 0x01;                          // Set P1.0 to output direction
        P4DIR |= 0x01;
    
        // Initialize Switch 2
        P2REN |= BIT6;							// Initialize port 2.6 enabling pull up register
        P2DIR &= ~BIT6;							// Set P2.6 to be input by clearing that bit
        P2OUT |= BIT6;
        P2SEL0 &= ~BIT6;
    
        for(;;) {
            volatile unsigned int i;            // volatile to prevent optimization
    
            P1OUT ^= 0x01;                      // Toggle P1.0 using exclusive-OR
            p2_in = P2IN;
            if ((P2IN & BIT6) == BIT6) {
            	P4OUT |= 0x01;
            } else {
            	P4OUT &= ~0x01;
            }
          
            i = 10000;                          // SW Delay
            do i--;
            while(i != 0);
        }
    
        return 0;
    }
    



  • The Port Out registers are in a random undefined state at power-up and could be already ‘1’.

    It is highly recommended to adjust the port registers prior to enable them, as written in the comment.

    Sammy Lin said:
    PM5CTL0 &= ~LOCKLPM5; // Disable the GPIO power-on default high-impedance mode // to activate previously configured port settings

  • Thanks for all the help.

**Attention** This is a public forum