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.

Button issues with Launch pad

Other Parts Discussed in Thread: MSP430G2231, MSP430G2452

Greetings,

I am fairly new to the MSP430 and i am having trouble figuring out how to accomplish what i would like to do with my program. What i would like to do is when i press a button the corresponding LED would light up and when i let go the LED would turn off. however right now all my program does is light the LED and then stay in its state and not return to the main program loop. Any help would be appreciated.

Thank you.

Pierre

// this is to test out some of the functions of the elvis model using an MSP430 micro.

#include <msp430g2231.h>

#define LED1    BIT0 //Red LED on the borad
#define LED2    BIT6 //Green LED on the board
#define HSR  BIT1 //Head Shift Right connects to Pin 2 on J47
#define HSL  BIT2 //Head Shift Left connects to Pin 3 on J47
#define BTNL BIT4 //button to move left
#define BTNR BIT5 //button to move right

/*  Global Variables  */
char i=0;                   // char values are used to preserve memory
char bcs_vals[3] = {7,9,2};
char dco_vals[3] = {3,5,6};

/*  Function Declarations  */
void delay(void);

void main(void)
{
 WDTCTL = WDTPW + WDTHOLD;
 
 P1OUT = 0x42;
 P1DIR = LED1+LED2+HSR+HSL;
 P1IES |= BIT3+BTNL+BTNR;    //high -> low is selected with IES.x = 1
 P1IFG &= ~BIT3+BTNL+BTNR;    //to prevent an immediate interrupt, clear the flag for P1.3 before enabling the interrupt.
 P1IE |= BIT3+BTNL+BTNR;     //enable interrupts for P1.3
 
 _enable_interrupt();
 
 for (;;)
 {             // Note the change in method; rather than flashing 5
  P1OUT ^= LED1|LED2;//+LED2;   // times, this program flashes the LED continuously.
  //P1OUT ^= LED2;
  //P1OUT ^= HSR;
  //P1OUT ^= HSL;
  delay();
 }
} // main

void delay(void)
{
 unsigned int n;
 for (n=0; n<60000; n++);
} // delay

#pragma vector = PORT1_VECTOR
__interrupt void P1_ISR(void)
{
 switch(P1IFG& (BIT3|BTNL|BTNR))
 {
  case BIT3:
   P1IFG &= ~BIT3;  //clear the interrupt flag
   BCSCTL1 = bcs_vals[i];
   DCOCTL = dco_vals[i];
   if (++i ==3)
    i = 0;
   return;
  case BTNL:
   P1IFG &= ~HSL;  //clear the interrupt flag
   P1OUT = HSL;
   return;
  case BTNR:
   P1IFG &= ~HSR;  //clear the interrupt flag
   P1OUT = HSR;
   return;
  default:
   P1IFG = 0;   //probley unnecessary, but if another flag occurs in p1, this will clear it.
        //no error handling is provided this way, though
   return;
 }//switch
}//P1_ISR


  • Hi Pierre,

    you need to de-bounce the buttons. As a alternative (since I don't know what you will use this functionality for) you can use a timer for periodically polling the state of your button(s) (i.e. Watchdog timer in intervall mode; i.e. with >4ms timeout).

    Rgds
    aBUGSworstnightmare

    P.S. As far as I remember there's an example in the code examples: MSP430G2xx1  

  • Are you using CCSv4?

    One of the Grace New->Project Code Examples does exactly what you mention above (and even does the debouncing with WDT).  I am using the output of the Launchpad to wake up another TI processor that is in IDLE to measure wake up latency (and my kids like pressing the button and seeing the LED toggle).

    Raw code is below:

    //*****************************************************************************
    //   Watchdog timer used in interval mode for switch debounce
    //
    //   Description: A switch on P1.3 is used to toggle the LED on Port 1.0. The
    //   switch has been internally pulled-high and is debounced by ignoring
    //   transitions for a period determined by the watchdog timer operating in
    //   interval mode.
    //
    //                MSP430G2xxx
    //             -----------------
    //         /|\|              XIN|-
    //          | |                 |
    //          --|RST          XOUT|-
    //            |                 |
    //            |             P1.0|-->RED LED
    //            |                 |
    //            |                 |    /
    //            |             P1.3|---/  ---
    //            |                 |        |
    //            |                 |       GND
    //            |                 |
    //
    //   Texas Instruments Inc.
    //*****************************************************************************

    /*
     * ======== Standard MSP430 includes ========
     */
    #include <msp430.h>

    /*
     * ======== Grace related includes ========
     */
    #include <ti/mcu/msp430/csl/CSL.h>

    /*
     * ======== globals ========
     */
    unsigned char buttonOn;

    /*
     *  ======== main ========
     */
    void main(int argc, char *argv[])
    {
        CSL_init();                       // Activate Grace-generated configuration
       
        buttonOn = 0;                     // Initialize buttonOn state and LEDs
        P1OUT &= ~BIT0;                   // Turn off LED (P1.0)
       
        __enable_interrupt();             // Set global interrupt enable

        while (1) {
            __bis_SR_register(LPM0_bits); // Enter LPM0
            __no_operation();             // this nop is only to assist debugging
        }
    }

    /*
     *  ======== button_push_isr ========
     *  GPIO Port 1 ISR
     *
     *  This ISR handles the button push and uses the WDT
     *  to efficiently manage delays for debounce.
     *
     *  This interrupt handler routine (for GPIO port 1) gets
     *  assigned in the GPIO Grace view.
     */
    void button_push_isr(void)
    {
        P1IFG &= ~BIT3;                   // Clear the port 1.3 interrupt flag
        P1IE &= ~BIT3;                    // Disable this (GPIO P1.3) interrupt 

        /* Use WDT as an interval counter for button debounce */
        IFG1 &= ~WDTIFG;                  // Clear any pre-existing WDT interrupt
        WDTCTL = (WDTCTL & 7) + WDTCNTCL  // Reset watchdog with same divider,
            + WDTPW + WDTTMSEL;           // write password, and timer mode
        IE1 |= WDTIE;                     // Enable WDT interrupts

        if (buttonOn == 1) {              // Switch LED and button state
            buttonOn = 0;
            P1OUT &= ~BIT0;
        }
        else {
            buttonOn = 1;
            P1OUT |= BIT0;
        }
    }

    /*
     *  ======== watchdog_isr ========
     *  Watchdog timer ISR
     *
     *  This ISR is triggered N ms after the button_push_isr() function
     *  is triggered, were N is a delay configured via Grace and set by
     *  CSL_init().  It clears any pending GPIO port 1 interrupt
     *  flag, and reenables the GPIO port 1 ISR allowing additional
     *  button presses to be detected.
     *
     *  This interrupt handler (for the WDT+) and the debounce delay are
     *  both configured in the WDTplus Grace view.
     */
    void watchdog_isr(void)
    {
        IE1 &= ~WDTIE;                    // Disable this (WDT) interrupt

        P1IFG &= ~BIT3;                   // Ensure button interrupt flag is clear
        P1IE |= BIT3;                     // Re-enable button interrupt
    }

  • Will this code work on board msp-exp430g2 rev. 1.5 with a microcontroller msp430g2452?

    this code does not work either:

    #include "msp430g2452.h"

    void delay_ms(unsigned int ms ){
    unsigned int i;
    for (i = 0; i <= ms; i++){
    __delay_cycles(500);
    }
    }

    void do_led( int led, int delay ) {
    P1OUT = led;
    delay_ms( delay );
    }

    void main(void) {

    WDTCTL = WDTPW + WDTHOLD; // Hold the WDT (WatchDog Timer)
    P1DIR |= BIT0|BIT6; // Setup the appropriate output bits

    while(1) {

    if ( !(P1IN & BIT3) ){ // If not (port one input AND bit3 (sw1))
    do_led( BIT6, 2000 ); // Turn on LED 2
    }else{ // Otherwise...
    do_led( BIT0, 100 ); // Turn on LED 1
    }

    }

    }


    press on the button is triggered when touching contact P1.3

     ---

    just need to initialize the button:

    P1OUT |= BIT3;
    P1REN |= BIT3;


**Attention** This is a public forum