trap_test_handled.c - contains an ISR which handles the math error (divide by zero)ΒΆ
Test installing an ISR for the MathError trap. Then, execute a divide by 0 and see if the trap silently handles the call or not (it does). If the trap is not handled, then _DefaultInterrupt will catch it and report an error. See the trap_test.c file for an example of an unhandled math error trap.
 
#include "pic24_all.h"
 
Interrupt Service Routine for MathError. Do not anything, just clear the error and continue
void _ISR _MathError(void) {
Clear the _MATHERR flag to signal trap is handled.
  _MATHERR = 0;
Clear RCOUNT to stop DO loop in a divide.
  RCOUNT = 0;
}
int main(void) {
If not volatile, then the compiler will optimize out the 1/u8_zero code, NOT producing a divide by zero!
  volatile uint8_t u8_zero;
  configBasic(HELLO_MSG);
  /** start **/
  while (1) {
    outString("Hit a key to start divide by zero test...");
    inChar();
    outString("OK. Now dividing by zero.\n"
              "The _MathError ISR should prevent the chip\n"
              "from resetting or reporting this error.\n\n");
    u8_zero = 0;
    u8_zero = 1/u8_zero;
    doHeartbeat();
  }
}