Hello Patrick,
There should be numerous discussions on this forum - perhaps try the forum search engine at the bottom of this page.
It may be possible to use polling of the pin to which the pushbutton is connected. However, assuming that you use the KBI module, perhaps to provide wake-up from STOP mode, you might consider the following sequence of events.
- Within the KBI ISR code:
Check which switch is active (if more than one), and start a debounce timeout period for that switch, and initiate the action required for the keypress. Disable further interrupts for that input, clear the interrupt flag, and exit the ISR. Incidently, a debounce period of 50 ms will probably be adequate for most tactile switches. - Monitor the timeout condition, by means of polling, or maybe a timer interrupt. When timeout is detected, test whether the switch is still closed.
- If not released, commence another timeout period, and continue. If the switch is now open, re-enable the KBI interrupt for the switch input.
A very simple way to generate the required delay (which does not have to be particularly accurate), is to utilise the overflow condition of the TIM or TPM timer module. The overflow period needs to be short compared with the timeout period, so that multiple overflow events can be counted to determine the timeout. If you have more than one switch, a separate counter might be used for each switch.
Within the timer overflow ISR, each counter could be handled in the following manner -
unsigned char count1, count2; // Global counter variables
// Within the ISR code -
if (count1)
count1--;
if (count2)
count2--;
// etc.
To start a timeout period, write the requred timeout value to the counter. When timeout has occurred, the counter will become zero, and will remain at zero.
Regards,
Mac