Switch Debounce using a 10F chip


Closed Thread
Results 1 to 8 of 8
  1. #1
    Join Date
    Apr 2005
    Posts
    96

    Default Switch Debounce using a 10F chip

    I know this is a simple question, but I am so used to using the "Button" command I can't think of the correct method for debouncing a switch.

    The pic10F206 chip I am using does not seem to like the "Button" command. I'm guessing that is because of the reduced instruction set with the 10F.

    The price of the 10F chips makes them hard to pass up for some projects.

  2. #2
    Join Date
    Apr 2005
    Posts
    96


    Did you find this post helpful? Yes | No

    Default

    by the way right now my switch is wired in with the pin pulled down to ground through a 10k resistor. Please excuse my poor ASCII art below:



    +5v_____/ switch__________pic input
    .............................|
    .............................|_____10k resistor______GND
    Last edited by modifyit; - 20th March 2006 at 19:02.

  3. #3
    Join Date
    Dec 2003
    Location
    Wichita KS
    Posts
    511


    Did you find this post helpful? Yes | No

    Default

    Debouncing is just the matter of putting a little "Pause" in the program. This little pause will allow accidental "double pushing", "electrical connections", that are iffy, and other uncontrollable human factors to be "softened" out.

    Loop:

    if GPIO.1 = 0 Not pushed goto Loop (or use some kind of interupt)
    pauseus 1000 (Pause 100 microseconds)
    while GPIO.1=1 endwhile (Button released)
    Do your program stuff here
    goto Loop


    You can adjust your pauseus to whatever fits your need to your project.
    When you use a interput, you dont want to interupt a bunch of times, so you can "debounce" the button, so that the interput only works 1 time ever 1000 microseconds.

    Dwayne
    Ability to Fly:
    Hurling yourself towards the ground, and missing.

    Engineers that Contribute to flying:
    Both optimists and pessimists contribute to the society. The optimist invents the aeroplane, the pessimist the parachute

    Pilots that are Flying:
    Those who know their limitations, and respect the green side of the grass...

  4. #4
    Join Date
    Apr 2005
    Posts
    96


    Did you find this post helpful? Yes | No

    Default

    Thanks for the response, I am still a little confused though…does this code snippet basically do what you are suggesting? This code should toggle the state of an LED when I press the switch and not be effected if the switch is held down.

    Code:
    SW var GPIO.1
    LED1 var GPIO.2
    OnOff var bit
    Swset var bit
    
    Startup:
    Led1=0
    	OnOff=0
    GOTO Main
    
    Main:
    …….
    …..do regular code stuff
    ……
    if OnOFF=1 then 
        led1=1
    else
        led1=0
    endif
    GOSUB Debounce:
    GOTO Main
    
    Debounce:
          If SW=0 then
          	Swset=0			;used to determine if switch is held down
          	RETURN			;if switch is not pressed return to main
    	Endif
          If swset=1 then RETURN		;this returns to the main loop if SW is held down
          If SW=1 then PAUSEUS 100       ;if switch is pressed pause 100us
          IF SW=0 then RETRN		;if switch is now not pressed return to main
          IF SW=1 then
         	OnOff=OnOff+1   ;if switch is still pressed toggle OnOff
                Swset=1			;used to determine if switch is held down
          Endif
    RETURN

  5. #5
    Join Date
    Dec 2003
    Location
    Wichita KS
    Posts
    511


    Did you find this post helpful? Yes | No

    Default

    SW var GPIO.1
    1. LED1 var GPIO.2
    2. OnOff var bit
    3. Swset var bit

    4. Startup:
    5. Led1=0
    6. OnOff=0
    7. GOTO Main

    8. Main:
    9…….
    10…..do regular code stuff
    11……
    12. if OnOFF=1 then
    led1=1
    else
    led1=0
    endif
    13. GOSUB Debounce:
    14. GOTO Main

    Debounce:
    If SW=0 then
    Swset=0 ;used to determine if switch is held down
    RETURN ;if switch is not pressed return to main
    Endif
    If swset=1 then RETURN ;this returns to the main loop if SW is held down
    If SW=1 then PAUSEUS 100 ;if switch is pressed pause 100us
    IF SW=0 then RETRN ;if switch is now not pressed return to main
    IF SW=1 then
    OnOff=OnOff+1 ;if switch is still pressed toggle OnOff
    Swset=1 ;used to determine if switch is held down
    Endif
    RETURN
    Ok, Lets look at your code. The First goto Main (Line 7) is worthless... your program will automatically fall through to the beginning.

    Lets Look at something similar...

    SW var GPIO.1
    LED1 var GPIO.2
    OnOff var bit
    Swset var bit

    Startup:
    Led1=0
    OnOff=0

    Main:
    …….
    …..do regular code stuff
    ……
    if SW=1 then
    led1=1
    else
    led1=0
    endif

    Pauseus 1000
    Goto Main

    Looking at this, as long as OnOff button is pushed (On or equal to 1) your LED will stay on. (assuming you are pulling the OnOff pin to ground, and when you push the button, you are at 5 volts).

    Another way...............

    SW var GPIO.1
    LED1 var GPIO.2
    OnOff var bit
    Swset var bit

    Startup:
    Led1=0
    OnOff=0

    Main:
    …….
    …..do regular code stuff
    ……
    if SW=1 then
    Toggle Switchset
    pauseus 100 ; debounce timing
    endif

    If SwitchSet=1 then
    led=1
    else
    led=0
    endif

    Goto Main

    This one, will keep the LED on until you press the switch the second time.



    Realize, that I have not tested these programs, but it will give you an idea...
    Also, make sure you correctly assign the GPIO pins and turn off things like comparitors... else your program will not work!

    Dwayne
    Ability to Fly:
    Hurling yourself towards the ground, and missing.

    Engineers that Contribute to flying:
    Both optimists and pessimists contribute to the society. The optimist invents the aeroplane, the pessimist the parachute

    Pilots that are Flying:
    Those who know their limitations, and respect the green side of the grass...

  6. #6
    Join Date
    Apr 2005
    Posts
    96


    Did you find this post helpful? Yes | No

    Default

    I'm not sure why, but when I try your second method I do not get reliable switch debounce, or my "OnOff" variable is constantly toggling keeping the result from being reliable.

    I tried my overkill code and it works like a champ, so I'm going to go with it now.

    I appreciate the help, and I understand why your code method should work, but I can't get it reliable.

  7. #7
    Join Date
    Jul 2003
    Posts
    2,405


    Did you find this post helpful? Yes | No

    Default

    The pin you have your switch on is by default an analog comparator input on
    power up. Do you have CMCON0.3=0 in your program somewhere, and just
    forgot to show it in your example?
    Regards,

    -Bruce
    tech at rentron.com
    http://www.rentron.com

  8. #8
    Join Date
    Apr 2005
    Posts
    96


    Did you find this post helpful? Yes | No

    Default

    yeah, I've got CMCON0=%00000001 set at the top of the code.

    Thanks

Similar Threads

  1. PIC chip resetting. Very weird
    By The Master in forum Off Topic
    Replies: 0
    Last Post: - 28th October 2007, 17:07
  2. Replies: 14
    Last Post: - 26th September 2007, 05:41
  3. Newbie - 16F628A and switch latching
    By malc-c in forum mel PIC BASIC Pro
    Replies: 45
    Last Post: - 19th May 2006, 02:35
  4. Switch Sequence
    By Tissy in forum mel PIC BASIC Pro
    Replies: 11
    Last Post: - 13th February 2005, 20:36
  5. Battery Backup with PIC controlling switch
    By Dwayne in forum Schematics
    Replies: 1
    Last Post: - 27th December 2003, 13:16

Members who have read this thread : 3

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