Best Way to Use Asynchronous KBI ISR to debounce

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

Best Way to Use Asynchronous KBI ISR to debounce

776 Views
PG1
Contributor I

I need to detect key presses from tactile switches which are likely to bounce for milliseconds. I only need to detect presses (not releases) and do not have to measure how long the key is down.

 

I am looking for the easiest way to implement debouncing.

 

Here is what I came up with

 

1. Initially all 4 switch inputs to the KBI module can trigger an interrupt using edge detection only.  When one or more switches is pressed, the ISR is called.

 

2. The state of all 4 switch inputs are read, and global flags (contained in one byte) are updated to indicat which key(s)  is (are) pressed. This occurs in the ISR. Higher level routines can use this flag and reset it.

 

3. Any key that is pressed will be masked off so it can cause another interrupt. A software timer associated with that key is started. This occurs within the ISR. In this case there will be four software timers

 

4. The KBI is ack'ed and the ISR returns.

 

5. If a software timer associated with a certain key times out, that key input is un-masked  so that it can cause another KBI Int to occur.

 

 

Is there a simpler or better way?

 

Thanks.

Labels (1)
0 Kudos
2 Replies

467 Views
bigmac
Specialist III

Hello,

 

I think that one aspect needs to be addressed in more detail.


PG1 wrote:

5. If a software timer associated with a certain key times out, that key input is un-masked  so that it can cause another KBI Int to occur.


I will assume that this part of the operation is a polling process within the main loop.

 

The active input cannot be unmasked until after the switch has been released.  If an unmasked input is active, it is not possible to detect a closure at the other three inputs.  In this case the timeout period represents a delay until the testing for switch release commences.

 

To allow for further contact bounce when the switch is released, a further timout period should expire before the KBI input is unmasked.

 

For the de-bounce timing, I would tend to utilise a simple "tick" timer at say, 1 to 5 ms intervals.  Within the timer ISR, each debounce timing register would be tested, and it non-zero would be decremented.  You might allow for debounce intervals of 50 to100ms.  Therefore, timeout will have occurred when each register becomes zero - no additional timeout flags are required.  The timing uncertainty will be one tick interval.

 

Note that the debounce timing registers should be 8-bit so that the write process will be atomic.  Otherwise, interrupts would need to be globally disabled and re-enabled when the debounce timeout period was set.

 

Regards,

Mac

0 Kudos

467 Views
PG1
Contributor I

Step 3. has a typo.  It should say --- Any key that is pressed will be masked off so it cannot cause another interrupt. A software timer associated with that key is started. This occurs within the ISR. In this case there will be four software timers

0 Kudos