Hi all,
I am given a task to create a program in PIC18F4520 which
uses 3 switches (toggle switch or push button)
to on 4 LEDs(in any sequence), which at the same time cancels out debouncing.
Hope u guys can help me asap.
Thank you.
There are 2 methods to cancel out signal bouncing on such switches.
Hardware de-bounce:
This method uses a resister, a capacitor and a schmitt-trigger. Schematic and description can be found on following link; Electronics Tips: Contact Bounce and De-Bouncing
Software de-bounce:
In uC code, when you poll switch, if it is pressed you can allow 10ms~20ms delay and check the situation of switch again; if it is still pressed you may consider switch-pressed, else neglect the situation.
You can also use polling in fixed intervals,
for example use a timer and check the buttons port every 50ms to see if a button is presses,
that way you avoid detecting a button as pressed multiple times because of bouncing.
The other solution is to add a delay
I don't think there is a need for hardware solution when mce can do this very easily and without additional expense.
For code of debouching of switch,
I give you one sample code below
/***************************
#define SwitchA PORTBbits.RB0
char KEY = 0;
if(Switch == 1) //if Switch is pressed(here 1 = 5V,
// if your pin is 0V when switch is pressed than take Switch == 0)
{
delay(5) // delay of 5 milisecond, Neglect to debouching
if(Switch == 1) //Again check Switch is till pressed
{
KEY = 1; //Switch press is OK
}
Denouncing means that you avoid reading spike when polling the switch. When you push a simple switch the signal is not clean as in step signal, it is full with spikes(up and down). The simples method is to poll the pin and validate the level of the switch after several samples.
Regarding canceling the denounce, I did not understand how do you intend to active.