PIC24 Support Libraries
 All Data Structures Files Functions Variables Typedefs Enumerations Enumerator Macros Groups Pages
uart_wakeup.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 #include <stdio.h>
32 
33 /** \file
34  * Demonstrates wakeup from sleep using the UART.
35 */
36 
37 volatile uint8_t u8_sleepFlag;
38 
39 void _ISR _U1RXInterrupt (void) {
40  if (u8_sleepFlag) {
41  _U1RXIE = 0; //disable the interrupt
42  _U1RXIF = 0; //ignore this garbled character
43  u8_sleepFlag = 0;
44  }
45 
46 }
47 
48 int main (void) {
49  uint8_t u8_c;
50 
51  configBasic(HELLO_MSG);
52  _U1RXIP = 1; //choose a priority
53  while (1) {
54  outString("Entering Sleep mode. Enter character to wake\n");
55  // Finish sending characters before sleeping
57  _U1RXIF = 0; //clear the flag
58  u8_sleepFlag = 1;
59  _U1RXIE = 1; //enable the interrupt
60  U1MODEbits.WAKE = 1;
61  SLEEP(); //macro for asm("pwrsav #0")
62  DELAY_MS(10); //delay long enough for wake character to clear uart
63  U1MODEbits.UARTEN = 0; //disable UART and clear internal buffers
64  configUART1(DEFAULT_BAUDRATE); //reconfigure it
65  outString ("Awake! Now waiting for another character\n");
66  u8_c = inChar(); //get the character, this is garbage...
67  printf ("First character is: %c (%x) \n", u8_c,u8_c);
68  u8_c = inChar(); //get the character, this is garbage...
69  printf ("Second character is: %c (%x) \n", u8_c,u8_c);
70  doHeartbeat();
71  }
72 }