switch debounce

Tip / Sign in to post questions, reply, level up, and achieve exciting badges. Know more

cross mob
urchc_1533771
Level 5
Level 5
5 likes given First like received First like given

I need read switch that change status on  both edges of input. I placed interrupt but can't solve problem   with spikes.

Any of code that make debounce is appreciate.

Untitled.png

0 Likes
15 Replies
Anonymous
Not applicable

Hi,

There is a Debouncer component in some PSoC 4 devices which you can make use of.

Please refer following application note which provides more information-

http://www.cypress.com/documentation/application-notes/an60024-psoc-3-psoc-4-psoc-5lp-switch-debounc...

0 Likes

I use PROC so I can't use components

0 Likes
Anonymous
Not applicable

The application note also provides code for debouncing in firmware. Please refer section 5.

-Rajiv

0 Likes

chaplin.u,

Debouncing a switch can be a tricky thing. I recommend approach called 'vertical counter':

https://www.compuphase.com/electronics/debouncing.htm

The code is very short and effective. For detecting press / depress events look for last paragraph "A refined debouncing routine". 

/odissey1

0 Likes

I'll  try. I see many strange symbols that I don't familiar ...

0 Likes

That vertical counter is a classic , I've used in the past and it works very well.

Using an interrupt is usually a good idea only for some types of signals, latched or that don't ever change "on its will". Nowadays, for simple buttons, I usually check for button state change at periodic intervals and when I detect a change, I read the state again ~50ms after and assume that's the state.

If you insist in using interrupts, for what ever reason (power saving?), then I would set the interrupt on state change and, on interrupt, disable the pin change interrupt, set a timer to wakeup in ~50ms, read the state and then, if you want some hold-off time, set another ~50ms timer before re-enablig the interrupt. The switch bounce will typically occurr within the 1st 50ms, so the interrupt is disabled to make the CPU "blind" to the bounces.

0 Likes
Anonymous
Not applicable

An approach is to disable the state-change interrupt, setup a small timer to either poll the button state for debouncing, or to delay 50 ms until bouncing is done, and then to re-enable the interrupt on state change once the state of the button has been determined.

With my own application, I was able to leave the interrupt on state-change enabled, and merely add another timer-interrupt for polling the button. I then perform button debouncing whenever either one interrupts until a steady-state is achieved, and then use the results in the application. This allows for multiple button interrupts within the debouncing time period without missing them, and/or having to debounce for longer than necessary (saving up to a full debounce time period of activity, which is up to 50% power savings based on randomness).

0 Likes
lock attach
Attachments are accessible only for community members.
odissey1
Level 9
Level 9
First comment on KBA 1000 replies posted 750 replies posted

chaplin.u,

Attached is a demo project showing single button debouncind using vertical counters approach. The button is continuously polled at about 250 Hz, and flags are updated on button pressed an released events. This is bare code, not a component, thus should run on PRoC as well.

/odissey1

Button_SW_P4_01c.png

0 Likes

I tried all previous variants and it's was not as good as need.

Let's try this.

Thanks !

0 Likes

Did this one work?

/odissey1

0 Likes

not yet. I'm in vacation

0 Likes

I use PROC so I can't use UDB

0 Likes
Anonymous
Not applicable

You can replace the T-Flip-Flop with just a simple WDT to debounce at a fixed polling rate instead, as I believe that is the only thing using UDBs in the picture.

0 Likes

I see that it's make interrupts continuously so it can finish battery very fast.

No good for me.

0 Likes
Anonymous
Not applicable

Change it so that the button interrupts fire an ISR directly. Then, when a button interrupt occurs, you can set a 1 ms timer to check the button for 10 presses or so and determine if the button is bouncing or not. Then, once you determine whether the button is no longer bouncing, you can turn off the 1 ms timer and the device can go back into deep sleep.

0 Likes