PIC 18 IO Push & Debounce!
Treating buttons' Debounce — Debut pic — Episode#03
Hi, let’s deal with debounce.
Here’s code ( ❤ git):
What is debounce?
Bouncing is the tendency of any two metal contacts in an electronic device to generate multiple signals as the contacts close or open; debouncing is any kind of hardware device or software that ensures that only a single signal will be acted upon for a single opening or closing of a contact.
Tips n Tricks
if(PORTB.RB0 == 0 && flag == 0 )
{
PORTD.RD0 = ~ LATD.RD0; // Toggle LED
flag = 1; // Force break loop when key's pressed Delay_ms(40); // Treat debounce
} if(PORTB.RB0 == 1 && flag == 1 )
{
flag = 0; // Resetting flag just when key's released Delay_ms(40); // Treat debounce
}
The secret lies in the flag’s manipulation: as we initialize the flag as zero, the first if loop is executed and the debounce is treated. Inside, modify the flag’s state so that when the user releases the button, the code’s train is forced to the second if loop that resets the flag, repeating the cycle all over again. Simple like this!
In the next lesson let’s light LEDs in a row by pressing a single button:)
And that’s it
Thanks a lot! See you in the next episode.
Bye!
Related posts:
1º Episode — IO: Debut pic — Pic 18 Hello World \o/ — Just initiating a magic journey throughout Microchip’s best seller pastille
2º Episode — IO: Debut pic — Pic 18 Push & Blink an LED — Let’s Unveil IO’s PIC18 Capabilities
3º Episode — IO: Debut pic — PIC 18 Push & Debounce! — Treating buttons’ Debounce
4º Episode — IO: Debut pic — PIC 18 Lighting LEDs In a Row! — Nice Special Effect
5º Episode — IO: Debut pic — PIC 18 Chasing LEDs — The net result is that LEDs seem to be chasing each other
6º Episode — IO: Debut pic — PIC 18 Double Chasing — The LEDs chase each other in both directions
7º Episode — IO: Debut pic — PIC 18 Ambulance Light — Algorithm for ambulance-flashing-light
8º Episode — IO: Debut pic — PIC 18 Random Flashing — Generating a random number
9º Episode — IO: Debut pic — PIC 18 internal weak pull-ups — How to programming WPUR
10º Episode — IO: Debut pic — Button Library — How to use mikroC PRO for PIC library
11º Episode — IO: Debut pic — Up Down Counter — How to use mikroC PRO for PIC library
References & Credits
GitHub: pic18f