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.
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-
Debouncing a switch can be a tricky thing. I recommend approach called 'vertical counter':
The code is very short and effective. For detecting press / depress events look for last paragraph "A refined debouncing routine".
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.
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).
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.
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.
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.