防弹跳按钮


Answers:


17

I would highly recommend hooking a scope (hopefully you have one or can get your hands on one to use) up to your switch. I have seen a student's project that had a bounce on their switch that went from 5v down to -5v up to 4v down to -3v then up to 2v then back down to 0v. When we looked at the current draw on a scope there were some a very very large spike.

在他的特殊情况下,他非常需要消除硬件开关的抖动。

但是,另一方面,我看到的开关效果要小得多,可以很容易地在软件中将其删除。

您确实需要权衡您的选择。如果您的固件数量非常复杂,那么增加程序员和cpu使用量的开销可能不值得,那么最好增加一点硬件。现在,另一方面,如果您想降低成本和减小尺寸,则将希望尽可能多地删除硬件,并在固件中全部完成。


我确定了开关弹跳的范围,并将其发布在Wikipedia上:en.wikipedia.org/wiki/File
Thomas O 2010年

@Thomas O,这是一个非常有趣的弹跳画面。我只想确保@Vincent Van Den Berghe理解并非所有反弹都会像那样。该反弹限制为0-5v,它不会总是那样。
Kellenjb 2010年

2
@Thomas O, as a side note, we have created a practice of failing students who take Oscope images like that. Flash on a screen is horrible. We also take it a step further and push students to collect actual data points, using something like a CSV or labview, so that it can then easily be used in reports and data analysis.
Kellenjb

@Kellenjb, I'm getting a printer soon for my scope which supports HP-IB. Also I've discovered the "Sports" mode on my camera can take good pictures of the scope screen without a flash and with no blurring.
Thomas O

1
@Lundin In a real-time environment you depend on interrupts a lot. If you have a button press on an interrupt you don't really want your real-time system be be interrupted several times by a single button press. You also don't want to have to dedicate resources to wait for 10 ms.
Kellenjb

15

If you're a professional electronics designer chances are that your boss won't even let you do it in hardware. The reason is simple: if your production batch is large enough software is virtually free, while hardware has to be paid for for each unit you produce. And while resistors and capacitors are dirt cheap, mounting them on a PCB may cost up to 20 times their purchase price.

Whether you debounce in software or in hardware, you still have to select quality pushbuttons. The infamous 157ms button from the article is simply not fit for any application.
I usually sample the button at 32ms intervals, which is enough to bridge the debounce time of any good button. I'm quite a fan of the Alps SKQG TACT Switches.

Alps tact switch

On the few devices I tested it had an initial bounce time of less than 10ns. While it has an operating life of 100 000 cycles we tested it for 200 000 cycles and even then the 32ms debounce was sufficient. (I guess I should have measured the actual level of debounce, but our main interest at the time was the final product's behavior. Anyhow, we were using it out-of-spec.)

If you really want a hardware solution I second the SR flip-flop solution mentioned in the article as the technically best solution:

debounce circuit

The flip-flop can be constructed with a dual NAND gate, which is available in a small VSSOP8 package, for instance. The major drawback of this solution is that you need a SPDT pushbutton, where SPST is much more commonly available.


12

There are lots (and lots) of different ways to debounce buttons. Whether you do it in software or hardware is going to depend on your project requirements and switch type.

Here are some links to different methods:

http://www.ganssle.com/debouncing.htm

http://hackaday.com/2010/11/09/debounce-code-one-post-to-rule-them-all/


I was going to go get a link to ganssle when I saw the question.
Kortuk

The ganssle article was the reason I asked this question, it's linked in my question :) Thanks for the link to the debounce code snippets.
Vincent Van Den Berghe

Would you mind summing up a few use cases to illustrate when to use soft/hardware? (this is probably subjective, but I'd like some examples anyway)
Vincent Van Den Berghe

1
@Vincent, Kellenjb summed it up on his answer how to decide. I did not click your link because it had a funny name, now that I clicked it I see ganssle!
Kortuk

If that is the consensus/general rule then indeed, there is no need for more examples.
Vincent Van Den Berghe

6

That article is the "bible" on debouncing. Contact bounce can be a problem with any application.

It's generally best to debounce switches in software as it's easier to adjust for the delays for particular switches, as they differ in their amount of contact bounce. Debouncing the key release is often necessary, as well. Switch manufacturers often specify the amount of bounce for their products, it's typically around 10ms - 20ms.


Why are people voting this down?
Toby Jaffey

Good point about key release debouncing!
Vincent Van Den Berghe

1
I did not downvote it, but just saying to do it in software is an awful idea. It is a good idea to characterize.
Kortuk

I said "generally", which is the case, and gave the reason. It also minimizes BOM cost and increases reliability.
Leon Heller

it only increases reliability if the signal stays within safe bounds of your controller. This action will increase reliability if you decrease component count without increases components wear. I was not saying that I disagreed with your posts concept, but I constantly work with engineers just coming into the field and an answer like this and they will debounce everything in software. You always must characterize. I also did not downvote you, but I did not upvote either, I think a broad answer like software risks newer developers risking their hardware.
Kortuk

1

Switch bounce can go on for tens of milliseconds. If you're polling a switch from an interrupt routine that runs on a timer, bounce won't be an issue, because even if you happen to poll the switch in the middle of a bounce storm, you either get the new state right away, or at worst get the old state, and don't see the new state until the next timer based poll. Polling from a timed ISR like this constitutes a form of software debounce.

However, if you are using that switch to cause the interrupt, and you expect that interrupt service routine to run quickly, in less than say 10 milliseconds, you'll need hardware debouncing, otherwise one switch event could result in a somewhat random number of interrupts, and certainly often more than just the one expected. On the other hand, if the interrupt routine runs long enough, the switch bounce will have settled before the ISR finishes, and you'd be fine, but most well constructed ISRs don't take all that long.


Having the switch to cause the interrupt is a good idea. But once you get it, don't allow further interrupts - shut them off and instead start a timer for the specified debounce time.
Lundin

@Lundin - if timers are available, you can just use the timer to implement the polled approach, and not bother having the switch generate interrupts at all.
JustJeff

0

The best way to do anyhting is the way that suits you best. But when you already have a microcontroller you can debounce in software at only the cost of a some code effort.

The essiest way to debounce in software is to check the buttons at moments in time that are further apart than the longest bounce time. 50 ms seems to be an upper bound on the bounce time of 'normal' switches, so when you can arrange your software like this you are in the clear:

forever loop
   wait (at least) 50 ms
   check buttons
   do procesing
end loop

And use the on-chip timers on your MCU for this.
Lundin

That is of course possible, but you can write a lot of programs without.
Wouter van Ooijen

3
Professional software in quality products will always use on-chip timers. Hobbyists can get away with some "NOP" for loop or dead wait polling. But there is no reason ever to do this in real products, no matter your realtime requirements. "I don't know how the timer works and this dead loop takes 10 seconds for my lazy self to write" is not a valid argument for an engineer.
Lundin

Bullocks. Professionals must be effective, which (depending on the project at hand) can meant (amongst other things) 'use the hardware effectively as possible' or 'use the professional's time as effecive as possible'. So your comming certainly applies in some cases (probably all cases you have dealt with) it certainly does not apply in all cases.
Wouter van Ooijen

To implement the on-chip timer takes an hour of your time at most. This is hardly something complex, it is everyday bread & butter engineering. You can't even invest one hour in your project to get a significantly better solution and overall better quality? Yeah well... if you don't know much about programming, it might take you a week. But then perhaps you shouldn't be working with software in the first place... or perhaps you should work more with it, so you'll learn to implement this simple little thing in no time.
Lundin

0

One approach to debouncing which hasn't yet been mentioned is to use a double-throw switch with one throw tied to VDD and the other to ground. Feed that into a pin which (either via software or hardware) will be weakly pulled to its present state. Such an approach will provide the advantages of a double-throw switch, but will only require one I/O pin rather than two.

By using our site, you acknowledge that you have read and understand our Cookie Policy and Privacy Policy.
Licensed under cc by-sa 3.0 with attribution required.