12F683 ISR switch debounce

Discussion in 'Embedded Systems and Microcontrollers' started by dayv3, May 23, 2014.

  1. dayv3

    Thread Starter Member

    May 22, 2014

    I am a newbie to pic’s but have survived my first few hello world/ blinking LED projects on a 12F683. I want to experiment with making one of the blinking LED projects begin as a reaction to a switch press that causes a call to an interrupt routine. I studied the 12F683 data sheet section 12.4 and figured out, I think, how to get it to work.

    My problem? I am unclear on how to debounce the switch.

    I am using Mikro C and want to use the “button” function to de-bounce the switch but am afraid, based on the Mikro C button example, that the way that it is done in a do/ while loop will obscure the interrupt pin from sensing the outside world trigger. Is there a software solution or does the switch debounce need to done in hardware external to the chip? If anyone has gone down this road already, I would appreciate any help in how you approached this problem.

    Thanks in advance,
  2. enggricha


    May 17, 2014
    Software debounce is the best way to go about it, although hardware debounce is also possible, but nobody ever does that with microcontrollers. There are many ways of software debouncing, like disable the interrupt for a certain delay after the first time the interrupt is triggered.
  3. THE_RB

    AAC Fanatic!

    Feb 11, 2008
    I don't use MikroC's button() function, it's too simplistic and limiting, especially where interrupts are concerned.

    If you learn ways of doing debouncing in your own code it is well worth it because you can get better code perfromance. For example your PIC can be doing other things while the debounce is happening.

    One of my preferred ways is to use a timer interrupt (which can also do other tasks) and check the button in the timer interrupt. If the button is pressed, you increment a counter variable. Once the counter >X you know the button was pressed for sure. Any time the button reads as not pressed you just clear the counter. So the button needs to be pressed contiguously for >X counts before you accept that it's pressed.
  4. dayv3

    Thread Starter Member

    May 22, 2014
    @The RB, and others, I spent a lot of time looking at the data sheet today but, could you please post some code to help me with this.
  5. THE_RB

    AAC Fanatic!

    Feb 11, 2008
    This is a crude simple version of debouncing in an interrupt. It should give you an idea. It can be improved in a few ways, I'll leave that up to you. :)

    Code ( (Unknown Language)):
    2. unsigned char but1deb;       // debounce count
    3. unsigned char but1pressed;   // flag
    4. #define BUT1PIN  PORTB.F0    // LO=pressed
    6. interrupt()    // timer int, happens every 1mS
    7. {
    9.   if(!BUT1PIN)  // debounce button 1
    10.   {
    11.     but1deb++;
    12.     if(but1deb >= 30)    // if pressed >30mS
    13.     {
    14.       but1pressed = 1;   // set flag, is sensed elsewhere in code
    15.       but1deb = 0;
    16.     }
    17.   }
    18.   else but1deb = 0;      // clear count whenever NOT pressed!
    20. }