title: PIC24F32KA302 Sleep mode
body: <p>I want to configure PIC24F32KA302 to go to Sleep mode and wake up by RTC interrupt. However, after waking up, my program runs again from the beginning. The datasheet mentions that:
  - If the assigned priority for the interrupt is less than or equal to the current CPU priority, the device will wake-up and continue code execution from the instruction following the PWRSAV instruction that initiated Sleep mode.
  - If the assigned priority level for the interrupt source is greater than the current CPU priority, the device will wake-up and the CPU exception process will begin. Code execution will continue from the first instruction of the ISR.
  I tried both configurations but the results are the same.
  My code is below:</p>
  
  <pre><code>int main(void) {
  SYS_Init();
  while(1){
      __delay_ms(400);
      Sleep();
  }
  return 0;}
  
  void __attribute__ ( (interrupt, no_auto_psv) )  _RTCCInterrupt(void) {
  IFS3bits.RTCIF = 0;
  //To do:
  Total_Pulse += TMR1;
  TMR1 = 0;
  LED = ~LED;}
  
  void InterruptPriority_Init(void) {
  INTCON1bits.NSTDIS = 1;
  INTCON2bits.ALTIVT = 0;
  SRbits.IPL = 1;
  IPC15bits.RTCIP = 6;//6
  _U2RXIP = 5;
  _T1IP = 4;
  _U1RXIP = 2;
  _HLVDIP = 3;}
  </code></pre>
  
  <p>The function SYS_Init() initializes interrupt, RTC and other peripheral modules. This function always runs after device wakes up from Sleep mode.
  Do you have any idea or advice for me? Thank you</p>