detecting switch release.


Closed Thread
Results 1 to 8 of 8

Hybrid View

  1. #1
    Join Date
    Feb 2004
    Location
    Michigan, USA
    Posts
    305

    Default detecting switch release.

    Greetings all,

    I'm trying to get a push button switch to only increment a variable on release. I've tried a few.loops but can't seem to get it working right. Anyone have a code sample I can pick over?

    Thanks.

  2. #2
    Join Date
    Nov 2003
    Location
    Wellton, U.S.A.
    Posts
    5,924


    Did you find this post helpful? Yes | No

    Default Re: detecting switch release.

    Look up interrupt on change in you chips data sheet.
    Dave
    Always wear safety glasses while programming.

  3. #3


    Did you find this post helpful? Yes | No

    Default Re: detecting switch release.

    Try this

    input portb.0 'input with pullup resistor
    clear 'all variables = 0

    start:
    if portb.0 = 1 then start 'stay here until pushbutton pushed

    waitforrelease:
    if portb.0 =1 then pause 50 ‘debounce
    if portb.0 = 0 then waitforrelease 'stay here until button released
    let (your variable) = (your variable + 1) 'button released
    goto start 'wait for another push

  4. #4
    Join Date
    Aug 2005
    Location
    Michigan, USA
    Posts
    224


    Did you find this post helpful? Yes | No

    Default Re: detecting switch release.

    I would recommend using a switch state latch bit and simple logic to filter out all but a "new press" or "new release" state without having to wait for the opposite state. You didn't mention if you're using active-low or active-high switch signals so I'll provide (C code) examples for both using parallel switch state logic (below).

    Code:
       /*
        *  K8LH parallel switch state logic
        *
        *  sample active lo switches with a "new press" filter or
        *  sample active hi switches with a "new release" filter
        *
        *  swnew  ___---___---___---___  sample switches
        *  swold  ____---___---___---__  switch state latch
        *  swnew  ___-__-__-__-__-__-__  changes, press or release
        *  swnew  ___-_____-_____-_____  filter for desired state
        *  flags  ___------______------  toggle flag bits for main
        *
        */
        swnew = ~portb;               // sample active lo switches
        swnew ^= swold;               // changes, press or release
        swold ^= swnew;               // update switch state latch
        swnew &= swold;               // filter for desired state
        flags ^= swnew;               // toggle flag bits for main
    Code:
       /*
        *  K8LH parallel switch state logic
        *
        *  sample active hi switches with a "new press" filter or
        *  sample active lo switches with a "new release" filter
        *
        *  swnew  ___---___---___---___  sample switches
        *  swold  ____---___---___---__  switch state latch
        *  swnew  ___-__-__-__-__-__-__  changes, press or release
        *  swnew  ___-_____-_____-_____  filter for desired state
        *  flags  ___------______------  toggle flag bits for main
        *
        */
        swnew = portb;                // sample active lo switches
        swnew ^= swold;               // changes, press or release
        swold ^= swnew;               // update switch state latch
        swnew &= swold;               // filter for desired state
        flags ^= swnew;               // toggle flag bits for main
    Execute this code in an ISR or in a main program loop at some "debounce" interval, typically 10 to 20 milliseconds. Test "flags" bit 4 in the main program for a "new press" or "new release", whichever you're using, for the switch connected to RB4. Clear the flag bit after using it.

    To emulate a toggle switch, where you press a switch to toggle its flag from on-to-off or from off-to-on, simply test the flag bit for that switch without clearing the flag afterwards.

    The switch state latch variable (swold) represents the real time (plus debounce interval) debounced state of the switches on portb and can be used for selective "repeat" key operation, if desired.

    Good luck with your project and Happy Holidays to everyone.

    Cheerful regards, Mike
    Last edited by Mike, K8LH; - 27th November 2012 at 14:46.

  5. #5
    Join Date
    Feb 2004
    Location
    Michigan, USA
    Posts
    305


    Did you find this post helpful? Yes | No

    Default Re: detecting switch release.

    Thank you for the responses. I now have a functioning routine and everything is working perfectly.

    On to the next issue: how to display text on an LCD from a word or byte.

  6. #6
    Join Date
    Nov 2003
    Location
    Wellton, U.S.A.
    Posts
    5,924


    Did you find this post helpful? Yes | No

    Default Re: detecting switch release.

    Have you seen the LCDOUT command?
    Dave
    Always wear safety glasses while programming.

Similar Threads

  1. Toggle switch (latching switch) state
    By craigwb in forum General
    Replies: 4
    Last Post: - 6th November 2011, 14:18
  2. Replies: 6
    Last Post: - 9th January 2011, 15:26
  3. Detecting a pulse
    By Dennis in forum mel PIC BASIC Pro
    Replies: 17
    Last Post: - 28th February 2010, 13:12
  4. 16F628A - Stops if release power switch.
    By dene12 in forum General
    Replies: 16
    Last Post: - 14th February 2009, 07:57
  5. Detecting EOF
    By Nicmus in forum mel PIC BASIC Pro
    Replies: 2
    Last Post: - 27th January 2008, 13:26

Members who have read this thread : 0

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