Button/switch debouncing


Closed Thread
Results 1 to 16 of 16
  1. #1
    xnihilo's Avatar
    xnihilo Guest

    Smile Button/switch debouncing

    Hi guys,

    I was wondering what delay I should use in debouncing a switch.
    I'm using 100ms debouncing, but that seems like overkill.

    Would 1ms be enough? Or would 10ms be enough.
    I guess it depends on the switch/button I use but I have no mean of knowing the bouncing of a given switch.

    Thanks for any answer.

    Regards

  2. #2
    Join Date
    Jul 2003
    Location
    Colorado Springs
    Posts
    4,959


    Did you find this post helpful? Yes | No

    Default

    Some interesting (although Long) reading.

    A Guide to Debouncing
    http://www.ganssle.com/debouncing.pdf

    Pretend it's a Playboy magazine, and just look at the pictures.
    <br>
    DT

  3. #3
    xnihilo's Avatar
    xnihilo Guest


    Did you find this post helpful? Yes | No

    Smile

    Yes, I've just finished to read that. Interesting. 10ms should be enough.

    Is this piece of code right? (a lever microswitch connects ground and RA0 of a PIC16F684 (input with WPU enabled for this pin)

    start:

    while trigger = 1
    pause 10
    wend

    Thanks

  4. #4
    Join Date
    Jul 2003
    Location
    Colorado Springs
    Posts
    4,959


    Did you find this post helpful? Yes | No

    Default

    NO. It'll take more than that.

    You need to test it again after the pause to make sure it's the same, and you should debounce both edges.
    Or a single pulse of any length, could re-trigger another keypress.
    <br>
    DT

  5. #5
    Join Date
    Feb 2005
    Location
    Essex, UK
    Posts
    94


    Did you find this post helpful? Yes | No

    Default

    Hi xnihilo,

    in the past I have used something like:

    Code:
    Start:
    
    If Trigger = Pressed then
    For CountByte = 0 to 9
    Pause 1
    If Trigger = NotPressed then Start
    Next CountByte
    ' Trigger is still pressed so do code here........
    Endif
    
    End
    This approach works well for me, checking the button 10 times at 1ms intervals, and will give you a starting point.

    Of course, there is always the PBP Button command...

    Hope this helps

    Rob

  6. #6
    Join Date
    Jul 2003
    Posts
    2,358


    Did you find this post helpful? Yes | No

    Default

    I think in the simplest form it's overkill...

    Look, you're not trying to ignore static from a localised lightning strike, just trying to detect that a Button has been pressed... so simply...
    Code:
    	If UserButton=0 then
    		Pause 10
    		Goto ButtonPressed
    		endif
    The IF statement checks if your Button has been pressed... The PAUSE simply ensures that you don't check for the press again within a set time period (which should be longer than your worst-case anticipated debounce time). Now, for user entry, I like to have a 100mS pause, this gives a nice 10cps repitition rate if you keep your finger on the button. If you only want ONE keypress, then simply check that it's been released first before doing anything else...
    Code:
    	While UserButton=0:Wend
    If you have a scenario where you have problematic static, then check for a Button Press once, pause a while, and check it's still pressed some time later. Static is momentary, so you will eradicate an awful lot of false triggering with something like...
    Code:
    	If UserButton=0 then
    		Pause 10
    		If UserButton=0 then Goto ButtonPressed
    		endif

  7. #7
    Join Date
    Jul 2008
    Posts
    3


    Did you find this post helpful? Yes | No

    Default coding for button by using PIC18F4620

    Hi everyone..

    I'm new in this forum and I hope u guys can help me to solve my problem. I'm try to do a coding for button by using PICF4620 but there is an error.I also not sure whether my program is right or not because this is the first time i'm using Pbasic.

    Here the program:

    define osc 25

    define INIT_ADCON2=0
    define INIT_TRISA.2=0

    DEFINE INIT_ADCON0=1
    DEFINE INIT_TRISB.0=1

    DEFINE INIT_ADCON1=1
    DEFINE INIT_TRISB.1=1

    LED VAR porta.2

    BUTTON0 VAR portb.0
    BUTTON1 var portb.1

    main:

    high BUTTON0=1
    high LED ;led is ON
    pause 600

    low LED ;led is OFF
    pause 600

    high BUTTON1=1
    high LED ;led is ON
    pause 600

    low LED ;led is OFF
    pause 600


    goto main

  8. #8
    Join Date
    Jul 2003
    Posts
    2,358


    Did you find this post helpful? Yes | No

    Default

    This is INCORRECT...

    high BUTTON0=1

    You've aliased BUTTON0 as a pin on your pic... now are you using it as an INPUT in which case you need an IF statement, or are you using it as an OUTPUT in which case you can use the HIGH statement...

    Let's assume you're using it as an INPUT and checking is it has been pressed... Buttons are usually pulled LOW when pressed because they are connected between the PIC pin and 0v. There is usually a pull-up Resistor on the pin, but since you are using PORTB then you will find you can probably enable internal pull-up's to do that job... so in this case your statement would be...

    If BUTTON0=0 then
    HIGH LED
    Pause 600
    endif

    Also, your DEFINE statements look awfully strange to me if you are using MeLabs PICBasic (which this forum is intended for)... if you are using anyone elses BASIC product, you might not get much help here...

  9. #9
    xnihilo's Avatar
    xnihilo Guest


    Did you find this post helpful? Yes | No

    Default

    Melanie, Darrel and Rob, thank you for your answers.
    It helps.

    Regards.

  10. #10
    Join Date
    Jul 2008
    Posts
    3


    Did you find this post helpful? Yes | No

    Smile

    I forgot to mention that i use the button for INPUT..anyway thank you Melanie.
    It helps.

  11. #11


    Did you find this post helpful? Yes | No

    Default

    Quote Originally Posted by Rob View Post
    Hi xnihilo,

    Of course, there is always the PBP Button command...

    Rob
    I have noticed quite a few Picbasic Pro programs which use a button de-bounce routine instead of the button command. If it's not a silly question, when and where should I not use the Button command?

    Thanks,
    Pat. Pending

  12. #12
    Join Date
    Jul 2003
    Posts
    2,358


    Did you find this post helpful? Yes | No

    Default

    Run with whatever turns you on (or in this case debounces your thing - which may very well need debouncing if you're running with it!!!)...

    In programming, there is no single correct way of doing anything - there's a zillion different ways of doing everything. Some more efficient, some less so, some you're comfortable with and work for you, so you stick with them...

    You don't HAVE to use any of PICBasics commands... ADCIN, HSERIN, HSEROUT, HPWM, BUTTON are just a few that immediately spring to mind. If you know how to do basic Port I/O and know how to access the PICs internal Registers (if you don't then it's time you did), then there's no reason why you can't and shouldn't do so if you're that way inclined.

  13. #13


    Did you find this post helpful? Yes | No

    Default

    Hi Melanie, thanks.

    I understand what you are saying, but don't understand whether you answered my question

    Quote Originally Posted by Melanie View Post
    You don't HAVE to use any of PICBasics commands...
    - Clearly, but that would leave me with, ...with, ..................ASSEMBLER! :0
    Pat. Pending

  14. #14
    Join Date
    Jul 2003
    Posts
    2,358


    Did you find this post helpful? Yes | No

    Default

    Clearly, but that would leave me with, ...with, ..................ASSEMBLER!
    No it wouldn't... take this thread topic as an example...

    First here's a couple of defines...
    Code:
    	MyButton var PortB.0
    	ButtonTemp var Byte
    Now check this code...
    Code:
    	ButtonTemp=0
    	BUTTON MyButton,0,255,0,ButtonTemp,1,ExecuteButton
    Look at the above code... it's almost the same as...
    Code:
    	IF MyButton=0 then
    		Pause 10
    		Goto ExecuteButton
    		Endif
    The salient difference is that the example with the IF statement doesn't require a dummy variable for it's exclusive use.

    Compile both examples... tell me which one uses more codespace? Both exclusively use PBP, neither uses any Assembler.

    When I say "Learn how to access and manipulate PICs Registers", - you're probably already doing it. To disable Comparators on may PICs you need to set...
    Code:
    	CMCON=7
    Well, that statement has preset the CMCON register with a value of 7 (ie CMCON=%00000111). So now, what's so difficult about dropping a byte into the appropriate USART Register instead of using HSEROUT?

  15. #15


    Did you find this post helpful? Yes | No

    Default

    Err... the assembler bit was supposed to be a joke. Maybe, I should have used emphasis -

    Quote Originally Posted by Melanie View Post
    You don't HAVE to use <u><b>ANY</b></u> of PICBasics commands...

    OK, I'm going to take your code and bounce the hell out of some buttons.

    Thanks,
    Pat. Pending

  16. #16
    Join Date
    Mar 2016
    Posts
    4


    Did you find this post helpful? Yes | No

    Default Re: Button/switch debouncing

    Quote Originally Posted by Melanie View Post
    Run with whatever turns you on (or in this case debounces your thing - which may very well need debouncing if you're running with it!!!)...

    In programming, there is no single correct way of doing anything - there's a zillion different ways of doing everything. Some more efficient, some less so, some you're comfortable with and work for you, so you stick with them...

    You don't HAVE to use any of PICBasics commands... ADCIN, HSERIN, HSEROUT, HPWM, BUTTON are just a few that immediately spring to mind. If you know how to do basic Port I/O and know how to access the PICs internal Registers (if you don't then it's time you did), then there's no reason why you can't and shouldn't do so if you're that way inclined.
    thx for ur advice it is the best i've got
    John

Similar Threads

  1. What debouncing technicques do you know?
    By Humbleworker in forum General
    Replies: 4
    Last Post: - 30th March 2007, 15:55
  2. Debouncing
    By Demon in forum Documentation
    Replies: 0
    Last Post: - 24th October 2005, 18:40

Members who have read this thread : 2

You do not have permission to view the list of names.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts