Interrupt+Sleep

Thread Starter

ecka333

Joined Oct 1, 2009
76
I am using PIC18F452 microcontroller. I wrote program, which basically looks like this:

void interrupt()
{
A-time counting...
if(Blackout==1){asm sleep; asm nop;}
B-some other commands
}

void main{....}

There A and B are command lines. So, i am using Timer1 module, which owerflows every 0,25seconds. Then program counter goes to interrupt routine. Here time counting routines are executed (A commands). Next program checks if mains voltage is present. If voltage is present, B commands are executed. After this main() routine is executed. But if blackout is occured, system goes to sleep after A line execution. After 0,25sec timer1 gives interrupt, microcontroller is waked-up.
So my question is: after wake-up interrupt routine will be executed from the begining (A command line) or B line will be executed?
 

ErnieM

Joined Apr 24, 2011
8,377
Wow, you waited almost a whole 6 hours to bump your thread. Your question could not be any more obfuscated.

Look at your program.

Find the line where you perform SLEEP.

Note the code line that is immediately following the SLEEP line.

There, that is what is executed after your uP wakes.
 

Markd77

Joined Sep 7, 2009
2,806
It depends if interrupts are enabled or not. Read section 19.3 of the datasheet. It will execute the instruction after sleep, but will then jump to the interrupt if enabled, otherwise it will just continue.
I don't imagine that putting the sleep command inside the interrupt is a good idea. Interrupts are disabled within the interrupt.
Unless you have a crystal or external clock connected to the timer1 pins, it will not be running so it will not wake from sleep.
 

Thread Starter

ecka333

Joined Oct 1, 2009
76
I am using Timer1 interrupts to wake-up uController from the sleep, because this module continues to function even during sleep. My goal is to reduce number of code strokes which microcontroller will execute when it is not sleeping. Code after sleep instruction is not very important, it is better not to execute it if blackout occured. So when microcontroller will wake up from the sleep, timer1 flag interrupt flag will be set. According to the datasheet microcontroller must execute code after sleep instruction. But if interrupt flag will be set, maybe microcontroller will start execute interrupt() routine frof the begining?
 

Markd77

Joined Sep 7, 2009
2,806
I am using Timer1 interrupts to wake-up uController from the sleep, because this module continues to function even during sleep. My goal is to reduce number of code strokes which microcontroller will execute when it is not sleeping. Code after sleep instruction is not very important, it is better not to execute it if blackout occured. So when microcontroller will wake up from the sleep, timer1 flag interrupt flag will be set. According to the datasheet microcontroller must execute code after sleep instruction. But if interrupt flag will be set, maybe microcontroller will start execute interrupt() routine frof the begining?
I think you will find Timer1 will stop along with the main instruction clock in sleep unless Timer1 uses an external clock or crystal.
What will happen if it ever wakes up is that the Timer1 interrupt flag will be set, but all interrupts are turned off within the interrupt. It will execute all remaining instructions in interrupt(), then immediately re-enter at the start of the interrupt because the Timer1 interrupt flag is set.
It's a much better idea to exit the interrupt before going to sleep.
 

Thread Starter

ecka333

Joined Oct 1, 2009
76
Markd77, my project has external 32768kHz oscilator and everything works fine, system wakes-up from the sleep now in two minutes intervals (with prescaler set to 1). Now i just want to inprove my code and to make it more efficient. Anyway, thank you for the answer.
 
Top