View previous topic :: View next topic |
Author |
Message |
art
Joined: 21 May 2015 Posts: 181
|
Debounce switch problem |
Posted: Tue Sep 08, 2015 12:28 am |
|
|
Hi, i'm trying to make a simple debounce switch Function. But the problem is, sometimes it works but sometimes it's not working. What's wrong with my code:
Code: |
#include <18F452.h>
#fuses HS,NOWDT,NOPROTECT,NOLVP
#use delay(c20000000000)
#use rs232(baud=9600, xmit=PIN_C6, rcv=PIN_C7)
#include <stdio.h>
#include <string.h>
void main()
{
while(true)
{
If(!input(PIN_B4)
{ output_high(PIN_D0);
If(!input(PIN_B4))
{ output_low(PIN_D0); }
}
}
}
|
|
|
![](templates/subSilver/images/spacer.gif) |
temtronic
Joined: 01 Jul 2010 Posts: 9174 Location: Greensville,Ontario
|
|
Posted: Tue Sep 08, 2015 5:19 am |
|
|
this.....
#use delay(c20000000000)
isn't nice...
I suspect you didn't cut and paste your whole program here, rather typed it in ?
Also what is the value for the pullup resistor on PIN_B4 ?
And, any capacitor across the switch contacts ?
Finally, you should add 'ERRORS' in #USE RS232(....options...). Though not used now, it'll keep the HW UART from 'locking' on you .
Jay |
|
![](templates/subSilver/images/spacer.gif) |
art
Joined: 21 May 2015 Posts: 181
|
|
Posted: Tue Sep 08, 2015 5:31 am |
|
|
Ok, i change my code. My pullup resistor is 10k and no capacitor across switch.what should i do next?
Code: |
#include <18F452.h>
#fuses HS,NOWDT,NOPROTECT,NOLVP
#use delay(clock=20M)
#use rs232(baud=9600, xmit=PIN_C6, rcv=PIN_C7,UART1,ERRORS)
#include <stdio.h>
#include <string.h>
void main()
{
while(true)
{
If(!input(PIN_B4)
{ output_high(PIN_D0);
If(!input(PIN_B4))
{ output_low(PIN_D0); }
}
}
} |
|
|
![](templates/subSilver/images/spacer.gif) |
Ttelmah
Joined: 11 Mar 2010 Posts: 19369
|
|
Posted: Tue Sep 08, 2015 6:32 am |
|
|
I have to ask where you think the debounce is?.
Bounce takes _time_. Typically switches oscillate at a few KHz, over several mSec. You are reading the switch twice, but within only a handful of instructions of one another. As such almost no effect on bounce....
Have a look in the code library. There is a set of routines there for scanning a keypad, including debounce. |
|
![](templates/subSilver/images/spacer.gif) |
art
Joined: 21 May 2015 Posts: 181
|
|
Posted: Tue Sep 08, 2015 7:43 am |
|
|
Dear ttelmah,
Is there is any example that i can used for referrence? Where can i get the library code? |
|
![](templates/subSilver/images/spacer.gif) |
asmboy
Joined: 20 Nov 2007 Posts: 2128 Location: albany ny
|
|
![](templates/subSilver/images/spacer.gif) |
Ttelmah
Joined: 11 Mar 2010 Posts: 19369
|
|
Posted: Tue Sep 08, 2015 9:05 am |
|
|
Also the 'code library', is from the forum index here. Has tens of thousands of routines, some of which are the 'bread and butter' of coding for the PIC with CCS. |
|
![](templates/subSilver/images/spacer.gif) |
art
Joined: 21 May 2015 Posts: 181
|
|
Posted: Tue Sep 08, 2015 11:28 pm |
|
|
Hi ,
I just want to make clear of my problem. Actually i want to make output D0 will remain high when i push button switch B4. And then output D0 will become low when i push button switch B4 once again. Maybe 'debounce problem' title is not suitable here. Please correct me. |
|
![](templates/subSilver/images/spacer.gif) |
jeremiah
Joined: 20 Jul 2010 Posts: 1329
|
|
Posted: Wed Sep 09, 2015 7:35 am |
|
|
Debounce is still 100% applicable here. You need to get your debounce right before you can handle the logic of toggling your output the way you want. |
|
![](templates/subSilver/images/spacer.gif) |
Sam_40
Joined: 07 Jan 2015 Posts: 127
|
|
Posted: Wed Sep 09, 2015 8:18 am |
|
|
art,
Try this, FYI; no debounce in your code or the one below:
Code: |
#include <18F452.h>
#fuses NOWDT,NOPROTECT,NOLVP
#use delay(clock=20M)
void main()
{
while(true)
{
If(!input(PIN_B4)
{
output_high(PIN_D0);
delay_ms(250);
}
If(!input(PIN_B4))
{
output_low(PIN_D0);
delay_ms(250);
}
}
}
|
Last edited by Sam_40 on Wed Sep 09, 2015 5:36 pm; edited 2 times in total |
|
![](templates/subSilver/images/spacer.gif) |
asmboy
Joined: 20 Nov 2007 Posts: 2128 Location: albany ny
|
|
Posted: Wed Sep 09, 2015 5:05 pm |
|
|
IMHO: The code above should not be used in a program
any larger than what is posted.
An un-committed timer, internally clocked,
pre-scaled down and polled on its interrupt bit
is another suggested way for generating delays.
A search for "state machine" on the forum shows clever ways to
deal with de-bouncing multiple buttons at a time -
a problem not considered in the thread(yet).
I'm also concerned that not every soul posting agrees
on the meaning of "debounce". |
|
![](templates/subSilver/images/spacer.gif) |
dyeatman
Joined: 06 Sep 2003 Posts: 1924 Location: Norman, OK
|
|
Posted: Thu Sep 10, 2015 5:12 am |
|
|
Here is an excellent paper by Jack Ganssle on debounce and the various
ways to handle it, both hardware and software.
http://www.eng.utah.edu/~cs5780/debouncing.pdf _________________ Google and Forum Search are some of your best tools!!!! |
|
![](templates/subSilver/images/spacer.gif) |
asmboy
Joined: 20 Nov 2007 Posts: 2128 Location: albany ny
|
|
Posted: Fri Sep 11, 2015 2:03 pm |
|
|
At the risk of sounding "me too",
the paper referenced in the above post is 1st rate !!
One other thing that makes debouncing simpler are
switches with "mechanical hysteresis". These are usually
found in more high end industrial applications and are
of an essential nature when the switch is gas pressure or hydraulic activated.
Readers are urged to consider that, even though this forum is full
of student questions pertaining to a single push-button - debounce is an issue
for all mechanical switches. not just the ones being pressed by a user finger.
And one approach to eliminating it logically, may not fit all needs. |
|
![](templates/subSilver/images/spacer.gif) |
temtronic
Joined: 01 Jul 2010 Posts: 9174 Location: Greensville,Ontario
|
|
Posted: Fri Sep 11, 2015 3:15 pm |
|
|
Yup, the posted article is a good read !
this chunk of inline code is crude but works for the 'simple' applications
while (!input(LT) ) ; //wait if low
delay_ms(20); //debounce delay
while (input(LT) ) ; //wait if hi
delay_ms(20); //debounce delay
I know 20ms is like 'forever' but I wasn't in a hurry ! LT is actually the 'Left Turn signal switch for my truck that goes to the trailer wiring. Used an LM7805LP as the 'logic level' translator to convert the 12V signal to 5V for the PIC.It's actually cheaper than R+ zener, R + R. One part, 3 holes, and who doesn't keep 100 of them on the shelf !
Jay |
|
![](templates/subSilver/images/spacer.gif) |
|