How do you read digital inputs from microswitch, jumpers, dip-switches...?

Sep 20, 2018 27 Replies

Gaming might be a special case, but perhaps the way round that is to adjust the clock tick period to get the required response time ?. I doubt if 2 or 3 mS would be noticeable, but there are various other issues, such as how long a key must be down to be positively identified. A polling approach may not be good enough, depending on polling interval and or the system load elsewhere. For absolutely instant response, the only way is to add hardware to post an interrupt for any keypress, with the interrupt handler deciding what to do with it. Whatever solution, the keyprees has to get to the consumer process, either through polling ar perhaps a signal and task switch, so overall system architecture matters a lot and not later into the design when problems surface :-).

The reason I use such techniques is that a lot of the work done here is on none rtos platforms, often with inadequate hardware and running things like keyscanners in the background reduces complexity and allows more flexibility in the design of mainline code. Same applies to serial comms for example, where an upper (mainline) and lower (interrupt) layer, with queues on tx and rx to connect the two, can offload a whole wedge of functionality...

Chris

Raising the frequency can invalidate the debouncing. The key to debouncing is that after the switch makes contact, it might rebound off and bounce for a period of time. You thus need to do something to avoid seeing the multiple on-off-on transitions. The period of this bouncing is strongly a function of the type and quality of the switch, some (like mercury wetted contacts) having inherently no bounce, while some cheap leaf spring contacts can bounce for 10s of milliseconds.

Gaming is a bit special because time gets naturally quantized into frames, everything happens in multiples of the frame (so 'polling' the switch by reading it once a frame is natural, if a frame is longer than the bounce period, you can ignore bouncing, if it is shorter you need to do something to debounce (preferably without adding a frame delay). At

60 Hz, you are naturally sampling about every 16 ms, which is longer than most bounce times, but controllers tend to get mashed a lot, so sometimes you want to be a bit careful.

Doesn't matter. You don't need to resolve it with a FF. You get the old data or the new data depending on the state of the bounce. How would it matter which one?

Rick C.

But you are introducing a latency in the mode change. Time 0 seconds Machine state A and switch state A Time 1 seconds Machine state A and Reading switch during transition to B but bounce causes value to read A, so no state change. Time 2 seconds Machine state A and Reading switch during transition to A but bounce causes value to read A, so no state change. If this machine is being used to count something via transitions on that switch, you just lost a count.

IOW sampling frequency is not the only factor to consider in debouncing a switch input.

HTH, Ed

Which says if you need to count 1 second pulses you need to be sampling at a rate of at least slightly faster than once a second. Basicslly your sample time must be less than the 'solid' press time, which is roughly the physical press time minus the bounce time.

If you are debouncing solely by sampling, the max rate you can sample at is the bounce time, so it can only be used to detect button presses that exceed twice the bounce period (which normally isn't an issue), but if you do, you need to use something a bit more complicated.

And that is the key. If the state is important and not at a high speed, th en simple reads are sufficient. If you want to detect the transitions of a switch and not the bounces, then you have to add debounce hardware or soft ware. If you want to read and respond to a switch in a short amount of tim e, you need to debounce the switch signal.

Don't make this more complicated than it is.

I don't think that was what anyone did. Samping rate is the only important issue for a state detection which I believe had been indicated at some poi nt.

Rick C.

Join the Discussion

Have something to add? Share your thoughts — no account required.

Didn't find your answer?

Ask the community — no account required