00001 /* 00002 * "Copyright (c) 2008 Robert B. Reese, Bryan A. Jones, J. W. Bruce ("AUTHORS")" 00003 * All rights reserved. 00004 * (R. Reese, reese_AT_ece.msstate.edu, Mississippi State University) 00005 * (B. A. Jones, bjones_AT_ece.msstate.edu, Mississippi State University) 00006 * (J. W. Bruce, jwbruce_AT_ece.msstate.edu, Mississippi State University) 00007 * 00008 * Permission to use, copy, modify, and distribute this software and its 00009 * documentation for any purpose, without fee, and without written agreement is 00010 * hereby granted, provided that the above copyright notice, the following 00011 * two paragraphs and the authors appear in all copies of this software. 00012 * 00013 * IN NO EVENT SHALL THE "AUTHORS" BE LIABLE TO ANY PARTY FOR 00014 * DIRECT, INDIRECT, SPECIAL, INCIDENTAL, OR CONSEQUENTIAL DAMAGES ARISING OUT 00015 * OF THE USE OF THIS SOFTWARE AND ITS DOCUMENTATION, EVEN IF THE "AUTHORS" 00016 * HAS BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 00017 * 00018 * THE "AUTHORS" SPECIFICALLY DISCLAIMS ANY WARRANTIES, 00019 * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY 00020 * AND FITNESS FOR A PARTICULAR PURPOSE. THE SOFTWARE PROVIDED HEREUNDER IS 00021 * ON AN "AS IS" BASIS, AND THE "AUTHORS" HAS NO OBLIGATION TO 00022 * PROVIDE MAINTENANCE, SUPPORT, UPDATES, ENHANCEMENTS, OR MODIFICATIONS." 00023 * 00024 * Please maintain this header in its entirety when copying/modifying 00025 * these files. 00026 * 00027 * 00028 */ 00029 00030 #include "pic24_all.h" 00031 00032 00041 uint8 printMenuGetChoice() { 00042 uint8 u8_c; 00043 outString("'1' enable watchdog timer\n"); 00044 outString("'2' enter sleep mode\n"); 00045 outString("'3' enter idle mode\n"); 00046 outString("'4' enable watchdog timer and enter sleep mode\n"); 00047 outString("'5' doze = divide by 2\n"); 00048 outString("'6' doze = divide by 128\n"); 00049 outString("'7' execute reset instruction\n"); 00050 outString("Choice: "); 00051 u8_c = inChar(); 00052 outChar(u8_c); //echo character 00053 outString("\n"); //newline 00054 return(u8_c); 00055 } 00056 00057 //persistent variables are not touched at reset 00058 _PERSISTENT uint8 u8_resetCount; 00059 00060 00061 int main(void) { 00062 00063 configClock(); //clock configuration 00064 configPinsForLowPower(); //config pins for low power since we are measuring current 00065 configHeartbeat(); //heartbeat LED 00066 configDefaultUART(DEFAULT_BAUDRATE); //serial port config 00067 outString(HELLO_MSG); //say Hello! 00068 00069 if (_POR) { 00070 u8_resetCount = 0; // if power on reset, init the reset count variable 00071 } else { 00072 u8_resetCount++; //keep track of the number of non-power on resets 00073 } 00074 if (_WDTO) { 00075 _SWDTEN = 0; //If Watchdog timeout, disable WDT. 00076 } 00077 printResetCause(); //print statement about what caused reset 00078 //print the reset count 00079 outString("The reset count is "); 00080 outUint8(u8_resetCount); 00081 outString("\n"); 00082 00083 while (1) { 00084 uint8 u8_c; 00085 u8_c = printMenuGetChoice(); 00086 DELAY_MS(1); //let characters clear the UART before executing choice 00087 switch (u8_c) { 00088 case '1': //enable watchdog timer 00089 _SWDTEN = 1; //WDT ENable bit = 1 00090 break; 00091 case '2': //sleep mode 00092 asm("pwrsav #0"); //sleep 00093 outString("after sleep\n"); //never executed. 00094 break; 00095 case '3': //idle mode 00096 asm("pwrsav #1"); //idle 00097 outString("after idle\n"); //never executed. 00098 break; 00099 case '4': 00100 _SWDTEN = 1; //WDT ENable bit = 1 00101 asm("pwrsav #0"); //sleep 00102 outString("after WDT enable, sleep.\n"); //executed on wakeup 00103 break; 00104 case '5': 00105 _DOZE = 1; //chose divide by 2 00106 _DOZEN = 1; //enable doze mode 00107 break; 00108 case '6': 00109 _DOZE = 7; //chose divide by 128 00110 _DOZEN = 1; //enable doze mode 00111 break; 00112 case '7': 00113 asm("reset"); //reset myself 00114 break; 00115 00116 default: 00117 break; //ignore 00118 } 00119 00120 } // end while (1) 00121 return 0; 00122 }