October 22, 2024
Chicago 12, Melborne City, USA
C#

Storing context prior to sleep (PIC)


Wonder if anybody knows the correct C syntax to store context (in my case just one int) before a PIC 24F sleeps and then recover it when woken.

The example code (Microchip app note), which is supposed to store context in DSGPRO, set the DSEN bit and call sleep is –

asm("MOV #0x8000, w2");
    asm("MOV w2, DSCON");
    asm("MOV w2, DSCON");
    asm("PWRAV #0")

My (probably poor) equivalent in C (to store the int in led_step) is

int *a = (int*) 0x8000;
    *a = led_step;
    
    IFS1bits.INT2IF = 0;        //clear interrupt flag.
    INTCON2bits.INT2EP = 1;     //Interrupt iNT2E is on the falling edge
    IPC7bits.INT2IP = 7;        //set highest interrupt priority to INT2
    IEC1bits.INT2IE = 1;        //Enable INT2IE interruption
    CTMUCONbits.CTMUEN = 0;     //Disable CTMU module
    Sleep();                    // sleep();

But this just results in an output toggling whilst in sleep. The sleep / wake functions do work, there’s something wrong with variable storage / recovery.

Thoughts appreciated.



You need to sign in to view this answers

Leave feedback about this

  • Quality
  • Price
  • Service

PROS

+
Add Field

CONS

+
Add Field
Choose Image
Choose Video