PIC24 Support Libraries
 All Data Structures Files Functions Variables Typedefs Enumerations Enumerator Macros Groups Pages
reset.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 #include "pic24_all.h"
31 
32 
33 /** \file
34 Program that tests some of the ways that a PIC24 can
35 be reset, as well as sleep mode, idle mode, and the watchdog timer.
36 Intended to be used in a laboratory exercise in which the current
37 draw of the processor is monitored before/after the power down
38 modes.
39 */
40 
41 uint8_t printMenuGetChoice() {
42  uint8_t u8_c;
43  outString("'1' enable watchdog timer\n");
44  outString("'2' enter sleep mode\n");
45  outString("'3' enter idle mode\n");
46  outString("'4' enable watchdog timer and enter sleep mode\n");
47  outString("'5' doze = divide by 2\n");
48  outString("'6' doze = divide by 128\n");
49  outString("'7' execute reset instruction\n");
50  outString("Choice: ");
51  u8_c = inChar();
52  outChar(u8_c); //echo character
53  outString("\n"); //newline
54  return(u8_c);
55 }
56 
57 //persistent variables are not touched at reset
58 _PERSISTENT uint8_t u8_resetCount;
59 
60 
61 int main(void) {
62 
63  configClock(); //clock configuration
64  configPinsForLowPower(); //config pins for low power since we are measuring current
65  configHeartbeat(); //heartbeat LED
66  configDefaultUART(DEFAULT_BAUDRATE); //serial port config
67  outString(HELLO_MSG); //say Hello!
68 
69  if (_POR) {
70  u8_resetCount = 0; // if power on reset, init the reset count variable
71  } else {
72  u8_resetCount++; //keep track of the number of non-power on resets
73  }
74  if (_WDTO) {
75  _SWDTEN = 0; //If Watchdog timeout, disable WDT.
76  }
77  printResetCause(); //print statement about what caused reset
78  //print the reset count
79  outString("The reset count is ");
80  outUint8(u8_resetCount);
81  outString("\n");
82 
83  while (1) {
84  uint8_t u8_c;
85  u8_c = printMenuGetChoice();
86  DELAY_MS(1); //let characters clear the UART before executing choice
87  switch (u8_c) {
88  case '1': //enable watchdog timer
89  _SWDTEN = 1; //WDT ENable bit = 1
90  break;
91  case '2': //sleep mode
92  asm("pwrsav #0"); //sleep
93  outString("after sleep\n"); //never executed.
94  break;
95  case '3': //idle mode
96  asm("pwrsav #1"); //idle
97  outString("after idle\n"); //never executed.
98  break;
99  case '4':
100  _SWDTEN = 1; //WDT ENable bit = 1
101  asm("pwrsav #0"); //sleep
102  outString("after WDT enable, sleep.\n"); //executed on wakeup
103  break;
104  case '5':
105  _DOZE = 1; //chose divide by 2
106  _DOZEN = 1; //enable doze mode
107  break;
108  case '6':
109  _DOZE = 7; //chose divide by 128
110  _DOZEN = 1; //enable doze mode
111  break;
112  case '7':
113  asm("reset"); //reset myself
114  break;
115 
116  default:
117  break; //ignore
118  }
119 
120  } // end while (1)
121  return 0;
122 }