bigmac wrote:
I agree that the sampling method is potentially simpler and will provide integral de-bounce, but for the rotary encoder decoding it may not be adequate, depending on the type of encoder. In particular, there would be no protection against "creep", as previously discussed in the thread.
Actually, the sampling method that I use has no creep and misses no transitions. But my debounce algorithm is not that simple.
First of all, I sample faster than the debounce period of my encoders and switches (the same routine does them all). My slowest switch is specified at 5 milliseconds, so I sample every 500 microseconds. My scheduler calls the debounce task when requested by a timer interrupt. I maintain a debounce counter, the last read state and the last reported state for each pin being debounced. That adds up to 16 bytes of ram for 16 input pins, plus a byte for an image of each i/o port.
It goes like this:
At each sample, I read the two ports, and XOR it with the value of the two ports saved from the previous sample. For any bit that has changed, I set its debounce counter to the debounce constant (for 5 milliseconds at 500 uSeconds-per-sample, I use 10).
I then decrement any debounce counter that is not already zero. When an input has been stable for the full 5 milliseconds, its debounce counter becomes zero, and I process the transition.
It's possible that the transition was just noise or jitter. So I then compare the new debounced state with the last-reported state, and discard the transition if they are the same. If they are not the same, I send a switch-event message to the routine that processes them.
A disadvantage of this approach is that there is no easy way to extract velocity data (that my meager brain can come up with on a Sunday).
Some advantages of this approach:
1) The overhead is low: about 30 microseconds every 500 microseconds on an 8MHz HC08.
2) If the jitter occurs in under 5 milliseconds, both edges are discarded. If the jitter is over 5 milliseconds, both edges are counted. So no counts are lost and there is no creep.
3) All encoder edges are counted, giving you x4 quadrature decode.
Additional advantages, not related to the encoder:
1) Other switches and encoders are debounced at the same time.
2) The routine also detects when a button has been held down for a predefined period (I use 1 second).
3) The routine can auto-repeat the switch after that hold-down time, at a predefined rate (I use 8-per-second).
4) It also reports when a switch has been released.
I will try to post the code later today, but it is written for HC08 (sorry).
Message Edited by rocco on 03-12-200603:14 PM