Basically, you can use the same debounce code twice but you need separate and unique counter variables and flags for each switch. Perhaps something like this (untested) variation;
Code:
/******************************************************************
* *
* Project: 12F683 Debounce Test *
* Source: 12F683_Debounce_Test.c *
* Author: Mike McLaren, K8LH *
* (C)2012: Micro Application Consultants *
* Date: 05-Jun-2014, revised 05-Dec-2015 *
* *
* 12F683 Debounce Test Code (AAC Forum) *
* 12F683 Debounce Test For Two Switches (AAC Forum) *
* *
* *
* IDE: MPLAB 8.92 (tabs = 4) *
* Lang: Sourceboost BoostC v7.05, Lite/Free version *
* *
******************************************************************/
#include <system.h>
#pragma DATA _CONFIG, _MCLRE_OFF & _WDT_OFF & _INTOSCIO
#pragma CLOCK_FREQ 1000000 // 1-MHz INTOSC
/******************************************************************
* constants *
******************************************************************/
#define LED1 gpio.2 // LED attached to pin 5
#define LED2 gpio.1 // LED attached to pin 6
#define SW1 gpio.3 // active lo switch on GP3 (pin 4)
#define SW2 gpio.0 // active lo switch on GP0 (pin 7)
#define deb_peroid 30 // 30ms - time for the switch to be considered debounced
#define smpl_rate 1 // 1ms - per sample @ 1MHZ CPU clock /4 /TMR0 (sample rate)
#define db_count (deb_peroid/smpl_rate) // iterations needed for test
/* ****** Global Variables ****** */
volatile bit sw1_state; // sw1 state latch
volatile bit sw1_changed; // sw1 state change flag
volatile bit sw2_state; // sw2 state latch
volatile bit sw2_changed; // sw2 state change flag
/******************************************************************
* *
******************************************************************/
void interrupt() // ISR - interrupt service routine
{ static unsigned char db1_cnt = 0; // sw1 debounce counter
static unsigned char db2_cnt = 0; // sw2 debounce counter
if(intcon.T0IF) // if timer0 overflows
{ intcon.T0IF = 0; // clear interupt flag
tmr0 += (256-250+3); // reset for 250 * 4 = 1000 usecs
if(SW1 == sw1_state) // if sw1 == sw1 latch (same state)
db1_cnt = 0; // reset debounce counter
else //
{ if(++db1_cnt == db_count) // if 30-msecs
{ sw1_state ^= 1; // update switch state latch
if(sw1_state == 0) // if "new press"
sw1_changed = 1; // flag "new press" for main
db1_cnt = 0; // reset debounce counter
} // end if
} // end if
if(SW2 == sw2_state) // if sw2 == sw2 latch (same state)
db2_cnt = 0; // reset debounce counter
else //
{ if(++db2_cnt == db_count) // if 30-msecs
{ sw2_state ^= 1; // update switch state latch
if(sw2_state == 0) // if "new press"
sw2_changed = 1; // flag "new press" for main
db2_cnt = 0; // reset debounce counter
} // end if
} // end if
} // end if
} // end ISR
void init() // Initialize 12F683 PIC
{ trisio = 0b00001001; // GP3 and GP0 inputs
cmcon0 = 0x07; // Disable the comparator
ansel = 0; // Disable the A/D - Make pins digital i/o
adcon0 = 0; // A/D converter off
ccp1con = 0; // compare/ capture/ PWM off
osccon = 0x41; // setup INTOSC = 1-MHz
option_reg = 0x08; // TMR0 prescaler 1 (4-us ticks)
LED1 = 0; // LED1 'off'
LED2 = 0; // LED2 'off'
intcon = 1<<GIE|1<<T0IE; // global & tmr0 interrupts enabled
}
/******************************************************************
* *
******************************************************************/
void main()
{ init(); //
sw1_state = 1; // 1 = not pressed
sw1_changed = 0; // clear "new press" flag
sw2_state = 1; //
sw2_changed = 0; //
while(1)
{ if(sw1_changed) // if sw1 "new press
{ LED1 = ~LED1; // toggle LED1 and
sw1_changed = 0; // clear sw1 flag
} // end if
if(sw2_changed) // if sw2 "new press"
{ LED2 = ~LED2; // toggle LED2 and
sw2_changed = 0; // clear sw2 flag
} // end if
} // end while
} // end main
Last edited: