debounced switch detect on key release

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

debounced switch detect on key release

4,872 Views
stevec
Contributor III
I have 7 switches on a port which I need to debounce but need to register the key release as well as how long the key was pressed for (could be up to 3 seconds). I believe a passing reference was made by tonyp to this in a previous thread. I shall  be using a background tick interval to sample the switches rather than the KBI (although the switches are connected to the KBI port for waking the processor up). Has anyone any method to do this?

Steve
Labels (1)
0 Kudos
7 Replies

614 Views
rocco
Senior Contributor II
Hi Steve,

I have a message-based key decoder that runs as a background task, sampling the switches when scheduled to do so by a timer interrupt. Its features are:

It currently supports 16 switches on two ports, but expandable, if needed.
Differentiates between a switch being 'clicked' and a switch being 'held'.
The time-difference between a 'clicked' switch and a 'held' switch is selectable.
Keys can be programmed auto-repeat.
Supports an unlimited amount of key rollover.
Messages are issued when any key event occurs. The key events are:
KeyPressed:   the key was pressed down
KeyClicked:   the key was pressed and released quickly
KeyHeld:   the key has been pressed long enough to be 'Held' (typically 1 second)
KeyRepeat:   the key has been down for an additional 1/4 second (time is selectable)
KeyHoldEnd:   the key was 'Held', but is now released
KeyReleased:   the key was let back up
Some actions can generate more than one message. As an example, when a key is released a "KeyReleased" message will be issued, but either a "KeyClicked" or "KeyHoldEnd" will also be issued, depending on how long the key was down. This way, the receiving task can decide which messages it cares about.

I'm not sure if this is what you want to get into, but if it is I can post the code. It is written to run as a task, not as an ISR. A timer interrupt queues it up to my nano-scheduler, which then runs it.

Oops, I forgot to ask if you needed C or assembly language. My code is written in assembly language.
0 Kudos

614 Views
BobMac
Contributor I
Hi Rocco;
 I'd like to request a copy of your keyboard decoding routine(s) in assembly format. This application sounds really interesting and I'd like to look at it.
Regards, Bob
0 Kudos

614 Views
rocco
Senior Contributor II
Hi Bob,

Sorry for the delay, I was on location for a few days.

Here is a copy of the 'KeyPad' routine, as well as my task dispatch routine and the appropriate macros. If I have left anything out, let me know.


Message Edited by rocco on 2007-07-27 06:12 PM
0 Kudos

614 Views
rocco
Senior Contributor II
Well, I did leave out something.

I forgot to mention that this code was written for McuEZ, Freescale's predecessor to CodeWarrior. I was told that it should be compatible, as CodeWarrior uses the same assembler and linker, but I have never been able to verify that.
0 Kudos

614 Views
BobMac
Contributor I
Hi Rocco;
 Thanks for the listing! I really enjoyed reading it. The reason I sent you two e-mails was I wasn't sure the first one was sent correctly.

Best regards, BobMac

PS, I'll have to ask our moderator about a spelling checker sometime. Like where to find it? I'm sure Freescale must have bought and included one somewhere ;o)

0 Kudos

614 Views
stevec
Contributor III
Sounds a very comprehensive piece of code. Probably a bit over the top for my purpose but thanks anyway.

I'm writing in C although my background is assembler.

0 Kudos

614 Views
bigmac
Specialist III
Hello Steve,
 
Where all keyswitches are contiguously allocated on a single port, I have previously used the following polling method within the main loop.  For the purpose of the discussion, I will assume that all eight inputs have switches connected.
 
I use a timer to set a "cycle flag", and when the flag is set, the processing of keyswitch closures is performed.  For simplicity, the cycle period should be suitable for key de-bounce (typically 20 - 100 ms) - I might use timer overflow interrupt for this purpose.
 
To simplify the de-bounce process, when one or more inputs changes state, it is required that the next sample of all the inputs should have the same state.  If the two states differ, transient operation is assumed.  This should be satisfactory for mechanical switches, where switch activity is not rapid.  The sample when a change is first detected, is called "phase 0", and the subsequent sample "phase 1".  This is controlled by a phase flag bit.
 
The attached code attempts to demonstrate this operation.  To determine the period that each keyswitch remains active, an array variable is used to provide a separate timer value for each switch - this is incremented each cycle.  Additional code would need to be provided for the following functions -
 
void key_on(byte key);  /* Turn-on processing for each key */
void key_off(byte key, word time);  /* Turn-offf processing for each key */
 
Regards,
Mac


Message Edited by bigmac on 2007-07-20 03:55 AM
0 Kudos