trap_test.c - Code which causes a math error (divide by zero) trapΒΆ
Perform a divide by 0. Because there is no handler defined for the math error
trap, the _DefaultInterrupt
will catch it and report an error. Code in
trap_test_handled.c gives an example of an ISR
that handles this trap.
#include "pic24_all.h"
int main(void) {
If u8_zero is not declared as 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 math errror trap is not handled, so\n"
"the _DefaultInterrupt handler should be\n"
"called, causing the chip to reset.\n\n");
u8_zero = 0;
u8_zero = 1/u8_zero;
doHeartbeat();
}
}