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.

Do if button pressed, then wait query...

Other Parts Discussed in Thread: MSP-EXP430FR5969

Hi,

Just starting using C language. Fundamentally I would like to read an input with a momentary push button, do some logic and stop until the same button is pressed. I do not want the program to continue going round and round. Is it possible to do the following in a nutshell:

If button pressed,

Do X

Else Wait. (for button press and do nothing)

Thanks in advance!

  • clueless clueless said:

    Hi,

    Just starting using C language. Fundamentally I would like to read an input with a momentary push button, do some logic and stop until the same button is pressed. I do not want the program to continue going round and round. Is it possible to do the following in a nutshell:

    If button pressed,

    Do X

    Else Wait. (for button press and do nothing)

    Thanks in advance!

    Port interrupt can be used, and than when port pin input value is changed (button pressed) interrupt is executed (Do X). Device can be off / low power mode, doing nothing, waiting for button press. Check TI open source code examples (port interrupt)  for MSP430 target device that you are using. 

  • clueless clueless, 

    The easiest way to do this would be to use a port interrupt on the port pin that is used for the button.

    Usually, this is done by pulling up the port pin and then grounding the other end of the button (so that the port pin will be low when the button is pushed). You can trigger the falling edge of this pin to cause the interrupt and in the ISR for that port pin, set a flag.

    Then your main loop would only need to look like the below:

    if(flag) {
      // do stuff
    }
    else {
      __bis_SR_register(LPM4_bits | GIE);
    }

    The code examples for your specific MSP will help out with the basics, but buttons have been implemented quite a bit in LP code examples. For instance, the FR5969 LP (http://www.ti.com/tool/MSP-EXP430FR5969) uses both of the buttons as inputs. You may want to refer to this code to get a feel for how to implement your solution.

  • #include  <msp430g2553.h>
    
    void main(void)
    {
      WDTCTL = WDTPW + WDTHOLD;                 // Stop watchdog timer
      //for time being consider toggling LED on P1.0 is "X" from your post
      P1DIR = 0x01;                             // P1.0 output, else input // LED
      //SET up BUTTOn
      P1OUT =  0x10;                            // P1.4 set, else reset
      P1REN |= 0x10;                            // P1.4 pullup
      P1IE |= 0x10;                             // P1.4 interrupt enabled
      P1IES |= 0x10;                            // P1.4 Hi/lo edge
      P1IFG &= ~0x10;                           // P1.4 IFG cleared
      //UNless button is pressed, SLEEP.
      _BIS_SR(LPM4_bits + GIE);                 // Enter LPM4 w/interrupt
    }
    
    // Port 1 interrupt service routine
    #pragma vector=PORT1_VECTOR
    __interrupt void Port_1(void)
    {
      // Reads INTERRUPTS and DOES "X"
      P1OUT ^= 0x01;                            // P1.0 = toggle
      P1IFG &= ~0x10;                           // P1.4 IFG cleared
    }

  • This interrupt has worked great for me Tyler!

    I do have one other query. I have entered an interrupt and based on the logic within the interrupt I need to exit this interrupt early to go back to the main program. Is this possible as I do not wish to do further logic within the interrupt.

    Thanks in advance.

  • For most of us, the difficulty we encounter in writing software (or give any directives) is often not because we are not fluent in the computer language (or other language) we use. But rather we do not think thoroughly and give unwise directives.

    clueless clueless said:
    ...
    If button pressed,
    Do X
    Else Wait. (for button press and do nothing) ...


    Let me paraphrase that with the simple substitutions:
    “button pressed” ==> “the phone ring”
    “X” ==> “answer the phone”

    I end up with the directives:

    If the phone ringed,
    Do answer the phone
    Else Wait. (for the phone ring and do nothing) ...

    If one follows the above directives precisely, one end up waiting for the phone to ring and do nothing most of the time. And this has nothing to do with c or English!

    -- OCY

  • clueless,

    From what you have written, this my understanding of what you want to do:

    • Stay sleeping, wait for button press
    • When button is pressed, wake up and run program

    If you take what Sri had posted, but instead of doing any processing in the ISR itself, just set a flag (RAM variable). You can then use the __bic_SR_register_on_exit(LPM4_bits) command to wake the processor up from LPM.

    Then when the ISR exits, it will continue running from the main while loop and be able to do processing and actions from there.

    For example, the pseudo code below would allow you to do all your processing in the main loop and the button push would just enable this processing to happen.

    void main(void) {
      init();                       // set things up
      while(1) {             // main program loop
        if(flag) {              // if button pressed
          do(x);                // do X
          flag = 0;           // clear flag
        }
       else {                 // if button not pressed
         __bis_SR_register(LPM4_bits | GIE);  // go to LPM
         __no_operation(); // for debugger
        }
      }
    }
    
    // ISR
    #pragma vector=PORT1_VECTOR
    __interrupt void PORT1_ISR(void) {
      flag = 1;
      P1IFG &= ~BIT3;  // Assume button on P1.3
      __bic_SR_register(LPM4_bits);
    }
    

    Let me know if this answers your question or not.

  • You can find an example of using a push-button to trigger an interrupt in the MSP430 Workshop. Check out the interrupts chapter...

    http://processors.wiki.ti.com/index.php/Getting_Started_with_the_MSP430_LaunchPad_Workshop

    -sgs

  • Hi Tyler,

    Yes I've used the principle of your pseudo code and kept the interrupt as short as possible and it is working great! 

    Thanks again,

    Clueless

**Attention** This is a public forum