int0_bounce.c - use the INT0 interrupt to detect switch bounce.ΒΆ

#include "pic24_all.h"

//Interrupt Service Routine for INT0
volatile uint8_t u8_bcnt;
void _ISRFAST _INT0Interrupt (void) {
  _INT0IF = 0;    //clear the interrupt bit
  u8_bcnt++;       //increment the bounce count
}

#define SW1             _RB7            //switch state
#define SW1_PRESSED()   (SW1==0)        //switch test
#define SW1_RELEASED()  (SW1==1)        //switch test
/// Switch1 configuration, RB7 shares INT0 pin on PIC24HGP202
inline void CONFIG_SW1()  {
  CONFIG_RB7_AS_DIG_INPUT();     //use RB7 for switch input
  ENABLE_RB7_PULLUP();           //enable the pullup
}


int main (void) {
  uint8_t u8_cnt;
  configBasic(HELLO_MSG);
  /** Configure the switch ***********/
  CONFIG_SW1();  //enables individual CN interrupt also
  DELAY_US(1);
  /** Configure INT0 interrupt  */
  _INT0IF = 0;   //Clear the interrupt flag
  _INT0IP = 2;   //Choose a priority
  _INT0EP = 1;   //negative edge triggerred
  _INT0IE = 1;   //enable INT0 interrupt
  while (1) {
    u8_bcnt = 0;
    outString("Press & release switch... ");
    while (SW1_RELEASED());
    DELAY_MS(DEBOUNCE_DLY);
    while (SW1_PRESSED());
    DELAY_MS(DEBOUNCE_DLY);
    u8_cnt = u8_bcnt;  //copy variable so will not change
    if (u8_cnt != 1) outString("..bounced: ");
    else outString("..no bounce: ");
    outUint8(u8_cnt);
    outString("\n");
  }
}