Mitigating Double Interrupt Using Mechanical Switch and IRQ / KBI

cancel
Showing results for 
Show  only  | Search instead for 
Did you mean: 

Mitigating Double Interrupt Using Mechanical Switch and IRQ / KBI

Jump to solution
1,947 Views
PatrickW
Contributor III
A problem which I'm sure others have had, but which is difficult to find mentioned in past discussion:

Using a mechanical (tactile SPST momentary) switch to trigger interrupts using the IRQ or KBI modules, I often get "double clicks."  I am using falling-edge-only detection, so with a perfect switch, this should not happen.  But no mechanical switch is perfect, and the typical 6mm x 6mm tactile switch PCB-mount switch ubiquitous in consumer electronics seems to routinely switch not-so-cleanly, causing multiple pulses instead of a single edge, particularly as pressure is removed from the switch as it returns to "open" position.

Is there a pre-existing primer on dealing with this issue in in the MCU realm?  I'm new.

So far I've implemented a timer that simply skips the interrupt routine if it is called within 250 ms of a previous interrupt.  This works sometimes, but sometimes the user holds the button down longer than 250 ms, and increasing to 500 ms is bad for usability, because people want to click faster than that.

Thanks,
Patrick
Labels (1)
0 Kudos
1 Solution
517 Views
PatrickW
Contributor III
Peg and Mac:

Thanks very much for your help.  I knew it had to be an old question, but my searches weren't turning up anything.  Now I know the words you folks use, like "debounce," and I can phrase a decent search.  Your explanations are full and clear enough that I don't need to, however.

Thanks,
Patrick

View solution in original post

0 Kudos
3 Replies
517 Views
bigmac
Specialist III
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.
 
  1. 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.
  2. Monitor the timeout condition, by means of polling, or maybe a timer interrupt.  When timeout is detected, test whether the switch is still closed.
  3. 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
 
0 Kudos
517 Views
peg
Senior Contributor IV
Hi Patrick,

First of all you need to ask yourself if you really need to use interrupts just to detect a finger operated switch.
No one will notice if its polled or interrupt.
Then the basic idea is.
Detect switch is closed, then mark as maybe closed.
Re-test after a debounce time.
If its closed mark as really closed, if open clear the maybe closed.
Debounce time should be from 15 to 50ms. If you need longer than 50ms get a better switch.
Simple as that!

0 Kudos
518 Views
PatrickW
Contributor III
Peg and Mac:

Thanks very much for your help.  I knew it had to be an old question, but my searches weren't turning up anything.  Now I know the words you folks use, like "debounce," and I can phrase a decent search.  Your explanations are full and clear enough that I don't need to, however.

Thanks,
Patrick
0 Kudos