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.

push button S2

Other Parts Discussed in Thread: MSP430G2553

I want to find the duration for which the pushbutton is pressed continously .i know i should use timers for this ,but i dont know how to do so.

please help

thanks in advance :P

  • You should configure the microcontroller to detect a transition on an input pin (your button) and then start a timer that counts pulses (which is time if the clock is known). For detecting the transition on the pin you enable the interrupt for it. You don't say anything about the processor, but I assume you will use the G2 LaunchPad with the G2553. The button S2 shorts the pin to GND, so set the pin to input direction (default after startup), enable the pull-up-resistor for it and enable the innterrupt with an interrupt edge select of high->low. When the interrupt fires, disable the pin interrupt, change the edge select from low->high to have the next interrupt when the button is released again, clear a potentially pending IFG, enable the interrupt for the pin again and start a timer. For the timer you could use the up-mode with an interrupt for incrementing a variable each time the timer rolls over. In the ISR for releasing the button again you then stop the timer, disable the pin interrupt, take the TAR value and the the roll-over-variable and you get the time the button was pressed. Now change everything back to detect the high-low transition again, reset TAR and the variable, clear a potentially pending pin IFG, enable the pin interrupt and you can start again.

    That is a brief overview on how to do it. You may get some problems with bouncing contacts on the button, so you should think about re-enabling the interrupt capability after a defined time like 10ms, otherwise the result is not reliable.

    If that now sounds too difficult to understand, then you may start in smaller steps having a look at the different functions separately. To do this, go through the code examples for the MSP430G2553 which can be found here:

    Dennis

  • Thank you Mr Dennis ,
    ya ,i use the msp430g2553 ,and embedded c for writing the code .
    if you can give me a c code for this it will be very helpful
  • I would recommend you start with

    • msp430g2xx3_P1_04.c                        P1 Interrupt from LPM4 with Internal Pull-up

    from the mentioned code-examples. This shows how to use the interrupt on an input pin. Then look at

    • msp430g2xx3_ta_03.c                        Timer_A, Toggle P1.0, Overflow ISR, DCO SMCLK

    which uses the timer's overflow interrupt in continuous mode. You can also use that.

    Dennis

  • in the first explanation you provided,,Mr Dennis, i was able to understand till the portion in which you enable the pin for the next interrupt ,and start a timer .can you explain in detail from that part alone .
  • You want to detect the press on the button to start the timer, that means you want to detect the signal on the pin from high to low level. Then you want to detect the release of the button again, so you want to detect the change from low to high level. That is why you have to change the edge sensitivity for the interrupt.

    Look into the user's guide

    Page 327 - Digital I/O

    Dennis

  • Um .. i am talking about inside the iSR
  • i understood your first explanantion completely now except the part in which you "take the TAR value and roll over variable".
    can you explain with code that part alone.
    And what is a roll over variable ?
  • If lowest possible power consumption is not required, then polling approach is much more easy to implement and understand:

    1. Enable P1.3 for input wit pull-up enabled

    2. Set periodic timer interrupts, for example WDT in periodic interupt mode, WDT_MDLY_32.

    3. In WDT ISR check pin state and count ISR calls measuring for how long pin input is low, such way you will know time of button press:

    volatile unsigned int s2counter = 0, flag_s2pressed = 0;
    
    #pragma vector=WDT_VECTOR
    __interrupt void watchdog_timer(void)
    {
      if (!(P1IN & BIT3)) {
        if (s2counter < 65535)
          if ( ++s2counter > 8 ) // debounce (1/4 sec in case of 32ms tics)
            flag_s2pressed = 1;
      } else {
        s2counter = 0;
        flag_s2pressed = 0;
      }
    }
    

    This will allow you to count button press time up-to 2097 seconds.

    [disclaimer] Code is not checked, use at your own risk

  • Did it work for you?
  • The timer is 16 bits, so it can count to a maximum of 65535 and then "rolls over" to 0 again. This roll-over can generate an interrupt where you can increment a variable that counts these roll-overs for you. For a long button press the timer might roll over many times. Something must keep track on this. You could also set an easier to calculate value for the timer. If you clock the timer with 1MHz, for example and configure the timer to generate an interrupt every 50,000 clock cycles, then this would mean every 50ms.

    Let's say you had 20 interrupts during your button press and the roll-over-variable therefore holds the value 20. Furthermore TAR is 30,000 now. Then your time would be

    20 * 50ms = 1s
    +
    (1 / 1,000,000 Hz) * 30,000 = 30ms
    =
    1s and 30ms

    Dennis

  • Thanks both of you 

    It kinda worked but it still has timer issues which i hope the previous post will solve

    thanks for your help once again

**Attention** This is a public forum