uart_wakeup.c - Demonstrates wakeup from sleep using the UART

 
#include "pic24_all.h"
#include <stdio.h>

volatile uint8_t u8_sleepFlag;

void _ISR _U1RXInterrupt (void) {
  if (u8_sleepFlag) {
    _U1RXIE = 0;              //disable the interrupt
    _U1RXIF = 0;              //ignore this garbled character
    u8_sleepFlag = 0;
  }

}

int main (void) {
  uint8_t u8_c;

  configBasic(HELLO_MSG);
  _U1RXIP = 1;  //choose a priority
  while (1) {
    outString("Entering Sleep mode. Enter character to wake\n");

Finish sending characters before sleeping

    WAIT_UNTIL_TRANSMIT_COMPLETE_UART1();
    _U1RXIF = 0;              //clear the flag
    u8_sleepFlag = 1;
    _U1RXIE = 1;              //enable the interrupt
    U1MODEbits.WAKE = 1;
    SLEEP();         //macro for asm("pwrsav #0")
    DELAY_MS(10);    //delay long enough for wake character to clear uart
    U1MODEbits.UARTEN = 0;  //disable UART and clear internal buffers
    configUART1(DEFAULT_BAUDRATE); //reconfigure it
    outString ("Awake! Now waiting for another character\n");
    u8_c = inChar();       //get  the character, this is garbage...
    printf ("First character is: %c (%x) \n", u8_c,u8_c);
    u8_c = inChar();       //get  the character, this is garbage...
    printf ("Second character is: %c (%x) \n", u8_c,u8_c);
    doHeartbeat();
  }
}