debouncing switches with a timer interrupt?


Results 1 to 28 of 28

Threaded View

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


    Did you find this post helpful? Yes | No

    Default

    Consider using a switch state latch and relatively simple logic to sample your switches and detect the "new press" state while ignoring the other switch states. Here are the switch states;

    Code:
    swnew swold  switch state
      1     0    a "new press"
      1     1    still pressed
      0     1    a "new release"
      0     0    still released
    Sample the switches at a decent debounce interval (16 to 25 msecs) either in your main program loop or in a timer based interrupt service routine. You only need a few lines of code to implement the switch state logic for multiple switches (perhaps one of the PBP gurus can interpret the C code? Sorry!);

    Code:
    //  swnew  ____---____-----_____   new switch sample
    //  swold  _____---____-----____   switch state latch
    //  delta  ____-__-___-____-____   changes, press or release
    //  newhi  ____-______-_________   filter out release bits
    //
    
    while(1)                  //
    {                         //
      delay_ms(24)            // 24-msec debounce interval
      swnew = ~porta;         // sample active-lo switches
      swnew &= 0x0F;          // on RA3..RA0 pins
      swnew ^= swold;         // changes, press or release
      swold ^= swnew;         // update switch state latch
      swnew &= swold;         // filter out "new release" bits
      if(swnew)               // if any "new press" bits
      { beep();               // task a "new press" beep
      }
      
      if(swnew.0)             // if RB0 "new press"
      {                       //
      }                       //
    
      if(swnew.1)             // if RB1 "new press"
      {                       //
      }                       //
    }
    If you implement the method in an ISR you'll need to inclusive-or "swnew" with a "flags" register for your main program.

    Kind regards, Mike
    Last edited by Mike, K8LH; - 31st July 2010 at 02:57.

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