This post is older than 2 years and might not be relevant anymore
More Info: Consider searching for newer posts
This discussion has been locked.
You can no longer post new replies to this discussion. If you have a question you can start a new discussion

How to use timer to get button pushed time?

Hello, I want to get the millisecond of pushing a button, but I am confused about how to use the timer in S110. Is there any example? thank you!

  • Hi

    I have attached an example that uses application timer to detect a button press. In the event handler for the button press, it is detected if the button is pressed or released. When the button_2 on the development board is pressed a timestamp is recorded and another timestamp is recorded when the button is released. You can see how this is done in the example, look at the button_event_handler in the main file. The app_timer library is used to get timestamps. The timestamp represents the counter of the RTC1, which is what the app_timer uses in the background. So if the difference between the two timestamps is 32768, then the button has been pressed for 1 second, because the tick frequency of the RTC1 is 32768Hz.

    ble_app_bps_test_button_push_duration.zip

    The attached code example is based on the ble_app_bps example in the nRF51 SDK. To see exactly what I have added to the code, you can run a diff on the main file in the example and in the official ble_app_bps in the SDK.

    Note that the timing generated by the event handler of the app_button library is not very accurate. Usually, the accuracy should be a few microseconds, but the softdevice might delay execution of the button handler by 1ms - 6ms. More on limitations for the app_button library on this thread.

    Hopefully you will find the above method adequeate for you scenario. Otherwise, in order to have a more accurate timing for a duration of a button press, you would have to enable a TIMER peripheral (TIMER1 or TIMER2, TIMER0 is blocked by the softdevice if you have the softdevice enabled). Enabling a TIMER perhipheral does however increase current consumption of the chip substantially, while measuring a button press duration with the app_timer does not add any current consumption.

Related