| ??? 12/03/02 16:52 Read: times |
#33679 - RE: switch debouncing |
If you want the absolute best response time to a bouncing switch, then debounce it in hardware. That's much faster and requires NO software overhead.
However, if you are debouncing in software, there are actually 3 states the switch can be in: 1. PRESSED (0) 2. RELEASED (1) 3. BOUNCING (?) Most embedded systems I've worked on had more than 1 switch (as few as 2 as many as...well a whole bunch). I use a periodic interrupt to read the switch latch or port and AND the results (to get the released keys) and OR the results (to get the pressed keys). For example:
static unsigned char keys[3];
static unsigned char keys_index = 0;
unsigned char pressed_keys = 0;
unsigned char released_keys = 0;
void interrupt (void)
{
keys[key_index] = P1; // Read the KEYS
key_index = (key_index + 1) % 3;
pressed_keys = keys[0] | keys[1] | keys[2];
released_keys = keys[0] & keys[1] & keys[2];
}
I'm sure you could use the previous pressed and released state of a key to determine what the transition will be when the key is bouncing. However, in the system's I've worked on, most people could not press and release the key in 30ms. Another thing to consider is that human beings adapt very easily to situations that require anticipation. We are accustomed to things not being instantaneous. The best illustration of this is inexpensive digital cameras. You press the button and a second or so later it takes the picture. Jon |



