??? 07/01/06 14:49 Modified: 07/01/06 14:57 Read: times |
#119512 - How to debounce without jumps and branch Responding to: ???'s previous message |
Suresh said:
So i could do that with these steps.,
1)wait for switch press. 2)once switch is pressed, take samples for every 10msec by storing each sample in an inividual register. say R1 to R5 (for 5 samples) 3)Then the sample in R1 should be cheked for equality with R2, R3, R4 (if not equal get back to step 1)and inequality with R5. A disadvantage of this scheme is, that you wait for a switch pressing. Then, afterwards, you take samples. This makes it impossible or at least very code consuming to debounce more than one switch at a time. You can successfully debounce multi switch arrangements by sampling the switches at a fixed sampling rate. You don't need to wait for a certian pressing of switch and then invoke the taking of samples. Do some plausibility thoughts: The user will hit the key less than five times a second. This gives you a time of more than 200msec for a complete key action, means more than 100msec for the key press and more than 100msec for the key release. If you take into account a bounce time of 50msec, which is very much and is surely never exceeded with common key switches, then you can assume, that after the moment of key pressing or key releasing it takes less than 50msec to have a stable switch reading. So, in this worst case scenario from the 100msec of key press and the 100msec of key release you have at least 50msec of stable reading, each. So, debouncing a key means the following: First, you must have stored somewhere in the RAM the former valid key status, for instance DBNCE=1 means key was released, DBNCE=0 means key was pressed. Take every 10msec a sample. Do this all the time, equally whether a switch was just pressed or not. Now, check whether the new sample is identical to DBNCE. If yes, then clear a 5 step counter. If no, then increment the counter by one step. Take a new sample (as mentioned above) and check if it's identical with DBNCE. If yes, then clear the 5 step counter. If no, then increment the counter by one step. So, you see the following: If bouncing takes place and the key changes back to the former state, then the counter is cleared all the time. Only, when all samples in row show the same reading and if this reading differs from the former key status stored in DBNCE, then the 5 step counter is incremented. So, during the bounce time the 5 step counter is resetted all the time. Only when the bouncing is over, the counter has a chance to be incremented a number of times and to reach its final value (here "5"), means to overflow. When this overflow occurs, then change the state of DBNCE, to store that the key status has changed, means that a valid key press or key release was detected. This whole algorithm can be coded without any jump or conditional branch. In most cases a 4 step counter is used, not the 5 step counter of the example. This makes no difference as bounce time is even less then 40msec, if good switch contacts are used. Now, take for this 4 step counter a vertical counter configuration and you have that, what was discussed here for many times: "Debouncing of multiple switches by the help of vertical counters". Here's a nice example: http://www.8052.com/forum/read.phtml?id=76155 Take care, with this code example the 10msec delay is missing. Kai |