PIC24 Support Libraries
 All Data Structures Files Functions Variables Typedefs Enumerations Enumerator Macros Groups Pages
trap_test_handled.c
Go to the documentation of this file.
1 /*
2  * "Copyright (c) 2008 Robert B. Reese, Bryan A. Jones, J. W. Bruce ("AUTHORS")"
3  * All rights reserved.
4  * (R. Reese, reese_AT_ece.msstate.edu, Mississippi State University)
5  * (B. A. Jones, bjones_AT_ece.msstate.edu, Mississippi State University)
6  * (J. W. Bruce, jwbruce_AT_ece.msstate.edu, Mississippi State University)
7  *
8  * Permission to use, copy, modify, and distribute this software and its
9  * documentation for any purpose, without fee, and without written agreement is
10  * hereby granted, provided that the above copyright notice, the following
11  * two paragraphs and the authors appear in all copies of this software.
12  *
13  * IN NO EVENT SHALL THE "AUTHORS" BE LIABLE TO ANY PARTY FOR
14  * DIRECT, INDIRECT, SPECIAL, INCIDENTAL, OR CONSEQUENTIAL DAMAGES ARISING OUT
15  * OF THE USE OF THIS SOFTWARE AND ITS DOCUMENTATION, EVEN IF THE "AUTHORS"
16  * HAS BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
17  *
18  * THE "AUTHORS" SPECIFICALLY DISCLAIMS ANY WARRANTIES,
19  * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY
20  * AND FITNESS FOR A PARTICULAR PURPOSE. THE SOFTWARE PROVIDED HEREUNDER IS
21  * ON AN "AS IS" BASIS, AND THE "AUTHORS" HAS NO OBLIGATION TO
22  * PROVIDE MAINTENANCE, SUPPORT, UPDATES, ENHANCEMENTS, OR MODIFICATIONS."
23  *
24  * Please maintain this header in its entirety when copying/modifying
25  * these files.
26  *
27  *
28  */
29 
30 /** \file
31  * Test installing an ISR for the MathError trap.
32  * Then, execute a divide by 0 and see if the trap
33  * silently handles the call or not (it does). If the trap
34  * is not handled, then \ref _DefaultInterrupt will
35  * catch it and report an error. See the
36  * \ref trap_test.c file for an example of an unhandled
37  * math error trap.
38  */
39 
40 #include "pic24_all.h"
41 
42 //Interrupt Service Routine for MathError
43 void _ISRFAST _MathError (void) {
44  //do not anything, just clear the error and continue
45  _MATHERR = 0; //clear the _MATHERR flag to signal trap is handled
46  RCOUNT = 0; //clear the RCOUNT to break repeat loop
47 }
48 
49 int main (void) {
50  // If not volatile, then the compiler will optimize out
51  // the 1/u8_zero code, NOT producing a divide by zero!
52  volatile uint8_t u8_zero;
53 
54  configBasic(HELLO_MSG);
55  /** start **/
56  while (1) {
57  outString("Hit a key to start divide by zero test...");
58  inChar();
59  outString("OK. Now dividing by zero.\n"
60  "The _MathError ISR should prevent the chip\n"
61  "from resetting or reporting this error.\n\n");
62  u8_zero = 0;
63  u8_zero = 1/u8_zero;
64  doHeartbeat();
65  }// end while (1)
66 }