Design implement and verify an interruptbased program on the
Design, implement and verify an interrupt-based program on the PIC32 Starter Kit that allows the system to select between IDLE and SLEEP modes based on the button pressed. You will be required to submit your program software as part of your Exam. Describe your design, show your code, and clearly states the results of testing your program. See pp. 112~113 of the Di Jasio textbook for more details
Solution
OSCCONSET = 0x10; // set Power-Saving mode to Sleep
WDTCONCLR = 0x0002; // Disable WDT window mode
WDTCONSET = 0x8000; //
Enable WDT // WDT time-out period is set in the device // configuration
WDTCONSET = 0x01; // service the WDT
asm volatile( “wait” ); // put device in selected power-saving mode
// code execution will resume here after wake
// The following code fragment is at the beginning of the ‘C’ start-up code // to find whether the wake from Sleep is due to the WDT if ( RCON & 0x18 ) // The WDT caused a wake from Sleep { asm volatile( “eret” ); // return from interrupt }
SYSKEY = 0x0; // Write invalid key to force lock
SYSKEY = 0xAA996655; // Write Key1 to SYSKEY
SYSKEY = 0x556699AA; // Write Key2 to SYSKEY
OSCCONCLR = 0x10; // Set the power-saving mode to an idle mode
SYSKEY = 0x0; // Write invalid key to force lock asm volatile ( \"wait\" );
// Put device in selected power-saving mode
// Code execution will resume here after wake and
// the ISR is complete
// interrupt handler
void __ISR(_ADC_VECTOR, ipl7)
ADC_HANDLER(void) { unsigned long int result;
result = ADC1BUF0; // Read the result
IFS1CLR = 2; // Clear ADC conversion interrupt flag }
Design Description:
In sleep mode
The device can be woken up only by the following ways
Here in the code, we have used WDT to switch between the idle mode and sleep mode.
also, the controller switches between the mode after waking up. This cannot be done without waking up the controller
The system key value to put the controller to idle mode has been loaded to the register. When the value equals the stored value, the corresponding mode is switched over.
An Interrupt SubRoutine is attached in the last. This is to wake the controller when it is sleeping or in idle mode.

