int1_bounce.c - use the INT1 interrupt to detect switch bounce.ΒΆ

#include "pic24_all.h"

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

#define SW1             _RB13           //switch state
#define SW1_PRESSED()   (SW1==0)        //switch test
#define SW1_RELEASED()  (SW1==1)        //switch test
/// Switch1 configuration
inline void CONFIG_SW1()  {
  CONFIG_RB13_AS_DIG_INPUT();     //use RB13 for switch input
  ENABLE_RB13_PULLUP();           //enable the pullup
}


int main (void) {
  uint8_t u8_cnt;
  configBasic(HELLO_MSG);
  /** Configure the switch ***********/
  CONFIG_SW1();
  /** Configure INT1 interrupt  */
#if (defined(__dsPIC33E__) || defined(__PIC24E__))
  CONFIG_INT1_TO_RP(45);   //RPI45 shares RB13 pin on these families
#else
  CONFIG_INT1_TO_RP(13);   //RP13 shares RB13 pin
#endif
  DELAY_US(1);
  _INT1IF = 0;   //Clear the interrupt flag
  _INT1IP = 2;   //Choose a priority
  _INT1EP = 1;   //negative edge triggerred
  _INT1IE = 1;   //enable INT1 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");
  }
}