Using TMR1 Gate to Measure Short vs. Long Button Press

This example describes how to initialize and use the TMR1 in Gate Single-Pulse mode. The timer will start counting on an falling edge. If the leading edge appears, a gate interrupt will be generated, denoting that the button was short pressed. If the timer overflows before the leading edge appears, an overflow interrupt will be generated, denoting that the button was long pressed. A GPIO pin will be configured as input and connected to a button.

Note: The polarity of the gate is based on the button logic. If the button is active-low (meaning it will provide zero logic value when pressed), the timer needs to count on negative polarity and start counting on falling edge.

In this example, the microcontroller was configured with a clock system of 1 MHz and the timer was configured with a clock source frequency of 31,250 MHz = 32 µs and is able to measure the following range of values:

To achieve the functionality described by this use case, the following actions will have to be performed: