Search code examples
cmplabpic32

How to wake Pic32 from sleep mode?


I am trying to keep PIC32 in sleep mode on boot up. And when the power button is pressed the Pic32 should exit the sleep mode and wake up.

I am able to put PIC32 in sleep mode but I am not sure how I can trigger it to wake up on button press. Is it possible if I can trigger an interrupt whenever the user presses the power button and thus wake the PIC32? I am using microchip harmony framework and am quite new to this can someone please suggest how I can achieve this?

To put PIC32 I am using the PowerEnterMode function of harmony. To wake up PIC32 I have tried using the watch dog timer following the sample project provided with microchip harmony but couldn't get it to work. I also read that I can use external interrupt to set it up but I don't know how to set it up.

I have added my code below.

void APP_Initialize ( void )
{
    DRV_ADC_Open();
    DRV_ADC_Start();
    PLIB_ADC_Enable(ADC_ID_1);

    SPIHandle = DRV_SPI_Open(DRV_SPI_INDEX_0, DRV_IO_INTENT_READWRITE );
    DelayMs(100);
    SYS_DEVCON_PowerModeEnter(SYS_POWER_MODE_IDLE);

    appData.state = APP_STATE_POWER_UP;    
}

void APP_Tasks (void)
{
    switch ( appData.state )
    {
        case APP_STATE_POWER_UP:
             {
                 uint8_t pwrButton;
                 pwrButton = PWR_BTNStateGet() ;   
                 if (npwrButton == 0)                  // If button is pressed
                 {
                     PwrButtonDebounce += 1;      // Increment the pressed timer
                     DelayMs(10);
                 } else
                 {
                     PwrButtonDebounce = 0;            // Clear the Debounce Timer  
                 }
                 if (PwrButtonDebounce == MAX_DEBOUNCE)     // Debounced....
                 {
                     // Here Wake up routine on button press 
                     appData.state = APP_STATE_INIT;
                 }
             }
    }

I expect that whenever I press the power button the Pic32 should come in the APP_STATE_POWER_UP state and on debounce go in the initialization state but It never comes in this state. Can someone suggest how I can set this up?


Solution

  • You should use Change Notification interrupt.

    Enable the CN interrupt on the pin that you have your button and it will wake your device when pressed.