#define OFF 0
#define ON 1
volatile char status=ON; //the first time i'll plug vcc it will arise!
ISR(external_interrupt){
if(status==OFF)
status=ON;
else
status=OFF;
}
void isr_init(void){
//... fill with your external interrupt initialization
}
void power_down(){ //must call this routine all the times...
if (status==OFF){ //must turn off
//... enable interrupts.
//prepare to shut down...
//like disabling peripheral, slowing the clock, etc.
Sleep(); //turned off..
nop(); //or something dummy... mostly this will or will not operate
//when the interrupt happens,
//if enabled, will run the isr and
//continues in this same place
//... so here comes... the...
//power up!
// retake your tasks...
// clock up, reanble peripheral and so...!
}
}
void main(void){
main_init();
isr_init();
//enable interrupt some where!
enable_interrupt(); //or something...
//most the time, your code is an eternal loop something like
for(;;){
power_down(); //if status is ON it will not do anything... if status is OFF it will turn off (and resume appropriately)...
...
...
... // your actual program here..
}
}