CE009 - Math Error Traps for Robust Operation

by Microchip Technology

CE009 - Math Error Traps for Robust Operation

The attached code example is valid for dsPIC30F devices in the GP, Sensor and Motor-Control families.

About this Code Example

Microchip's 16-bit dsPIC® Digital Signal Controllers feature an on-chip mechanism to detect software errors and take corrective action. Specifically, the ability to detect arithmetic (math) errors is provided by means of automatic Math Error Trap detection. Math errors may be caused by one of the following:
a. Divide by Zero
b. Accumulator A overflow (bit 31 destroyed)
c. Accumulator B Overflow (bit 31 destroyed)
d. Catastrophic overflow of Accumulator A (bit 39 destroyed)
e. Catastrophic overflow of Accumulator B (bit 39 destroyed)
Accumulator Error traps need to be enabled in software by the user, while divide by zero errors are automatically enabled in hardware.

If the application defines an Math Error Trap service routine (trap handler), the processor will vector to the trap handler when it detects a math error.

The attached code example is valid for dsPIC30F devices in the GP, Sensor and Motor-Control families.

The attached code example contains one C file and one Assembler file - "main.c" and "math_error_trap_30F_GP_MC_Sensor.s" .
- The code example in the "math_error_trap_30F_GP_MC_Sensor.s" file demonstrates a simple means by which the user may detect what kind of math error was caused -whether "Divide-by-Zero" or an "Accumulator Overflow" trap.  You may add this file to your project to ease debugging during development of your application.
NOTE: This routine also estimates the instruction that caused the math error trap to occur by examining the PC value that is stored in the stack prior to entering the Math Error Trap. Since the instruction that causes divide by zero error to occur is not executed, the stacked PC points to the offending instruction. However, the instructions that cause accumulator overflows to occur will be executed prior to the trap being caused. So the stacked PC will point to the instruction after the offending instruction. Thus, the estimation routines differ slightly for the Divide by Zero error and the Accumulator Overflow errors. It should also be noted that since this trap routine is written in C the estimation of the stacked PC will depend on the compiler optimization level set up for this file. In the trap routine presented here, a comiler optimization level of 0 is assumed for this file.

- The code in the "main.c" file simply performs an illegal math operations so as to cause a Math Error Trap. If the symbol DIVIDEBYZERO is not defined, the code in the main.c file will cuse an Accumulator A overflow error trap to occur. To remove the DIVIDEBYZERO symbol simply comment out the line of code as follows:
        //#define DIVIDEBYZERO

  • Current Version: 1.0.0
  • Updated: Jun 23, 2012
  • Downloads: 621
  • Rating: 0/5 (0 votes cast)
  • Status: Released
    • MPLAB Version: V8.xx or above
    • C Compiler: MCC18 v3.30 or above
    • Development Tools: dsPICDEM™ 1.1 Development Board
    • Supported Devices: dsPIC30F6014A


  • There are no reviews for this Code Example.

    Write your review

    Release History

    CE009 - Math Error Traps for Robust Operation  (current version) June 22, 2012
    First Release to ECS