View previous topic :: View next topic |
Author |
Message |
alm1006
Joined: 12 Jul 2005 Posts: 5
|
Debounce routine - |
Posted: Tue Mar 07, 2006 11:52 am |
|
|
I would like to know if someone has a routine capable of debouncing any switch.
I've enclosed my jungle version of a debounce routine that's clearly not working. The routine && a 1 with FF then shifts << debounce until it equals 0. Your thoughts would be greatly appreciated.
Regards,
Albert
void TIMER2_isr(void)
{
cnt++; // cnt every 10ms
if (!input(pin_a4))
{
if (test_a4 == 0) //initialized in main
{
set_a4 = 1; // set_bita4 = input(pin_a4)
debounce = (set_a4 && 0xFF); // AND input(pin_a4) && 0x01
test_a4 = 1;
}
if (!input(pin_a4))
{
if (test_a4 == 1)
{
if (debounce >= 0x01)
{
1<<debounce;
output_high(pin_b2); // test bit
delay_cycles(2);
output_low(pin_b2);
}
else
{
a4_debounced = 1;
test_a4 = 0;
}
}
}
//---------------------------------------------------------------------------//
} |
|
|
PCM programmer
Joined: 06 Sep 2003 Posts: 21708
|
|
|
John P
Joined: 17 Sep 2003 Posts: 331
|
|
Posted: Thu Mar 09, 2006 1:08 am |
|
|
One method of switch debouncing simply involves reading the switch at a slow enough rate that any bouncing will be over and done with between tests. If you happen to check the switch during bouncing, it either will or won't be found in the new state, but this doesn't matter: if you catch it in the new state, it'll be steady by the time it's next checked and you see the new state a second time. The same is true if you fail to catch it; it'll be steady in the new state by the time the next check is done, so the only effect is to delay recognition of the new state by one cycle. There are times where you may need to have an exact timestamp for when the first transition in the switch state occurred, but if it's just a question of reading a pushbutton operated by a human, there's no need to work hard to get the data. If timing is crucial, use an optical or magnetic switch instead! |
|
|
|