change_test.c - demos a change notification interrupt using an input pushbuttonΒΆ

A simple test of the change notification feature. A momentary pushbutton is connected to RB13, with the change notication interrupt for that port is enabled. A message is printed each time the change notification interrupt occurs.

 
#include "pic24_all.h"


volatile uint8_t u8_cFlag = 0;

//Interrupt Service Routine for Change Notification
void _ISRFAST _CNInterrupt (void) {
  _CNIF = 0;    //clear the change notification interrupt bit
  u8_cFlag = 1;
}

/// Switch1 configuration
inline void CONFIG_SW1()  {
  CONFIG_RB13_AS_DIG_INPUT();     //use RB13 for switch input
  ENABLE_RB13_PULLUP();           //enable the pullup
  ENABLE_RB13_CN_INTERRUPT();     //CN13IE = 1
}

int main (void) {
  configBasic(HELLO_MSG);
  /** Configure the switch ***********/
  CONFIG_SW1();  //enables individual CN interrupt also
  DELAY_US(1);   //delay for pullup
  /** Configure Change Notification general interrupt  */
  _CNIF = 0;         //Clear the interrupt flag
  _CNIP = 2;         //Choose a priority
  _CNIE = 1;         //enable the Change Notification general interrupt
  while (1) {
    if (u8_cFlag) {
      DELAY_MS(15);  //wait for switch bounce to settle
      u8_cFlag = 0;
      outString("Change notification interrupt occurred\n");
    }
    doHeartbeat();
  }
}