PDA

View Full Version : Question about "@ SLEEP"



kevj
- 8th January 2008, 04:59
Finding Darrel's very helpful post here.....
http://www.picbasic.co.uk/forum/showpost.php?p=41144&postcount=13&highlight=sleep

and reading the data sheet about sleeping (PIC 16F616), I've got a few questions....

I just want the pic to sleep forever until a high input is found on the RA2/INT pin, then resume operating at the line of code just following the @SLEEP command.

1. I understand an "interrupt" is generated when in this case, the RA2/INT pin goes high (button press) as per the "Wake from sleep" section... but where does the program execution actually continue in the code? Does it always execute the line following the @ SLEEP command?

2. Fig 12-9 notes "GIE = 1" is assumed, processor jumps to "0004h", if GIE=0, execution will continue in-line. Can I write to the GIE bit? It looks like I'd set GIE=0 just before the sleep to insure execution picks up with the instruction following.

3. If I have the WDT running (as a fail safe if the external resonator goes out) - will the pic wake from sleep after each WDT cycle? Is there anything special I need to do with the WDT to make sure it doesn't wake the pic?

4. Section 12.4.1 in the datasheet talks about this interrupt on RA2/INT. It notes the INTF bit of INTCON is set when the valid edge appears, the interrupt can be disabled by clearing the INTE bit of INTCON (which I don't think I want in this case). It then says the INTF bit must be cleared before re-enabling the interrupt, and it sounds like I'd need to re-set the INTE bit prior to the next sleep cycle also.

That's too many things that start with "INT" in one paragraph to make any sense out of them. lol. I'm confused.

Can anyone extrapolate a list of the flags I need to set or clear before executing @SLEEP so that pic will goto sleep, and stay sleeping forever (days, months, years, etc) until that button is pressed driving RA2 high again? And to make the program resume right after the @SLEEP without jumping to some other place as a result of the interrupt?

Much appreciated.


Sidenote: I've also considered just naping in a loop, waking, checking the switch, and naping again about a second appart - which is kind of a dirty way of doing it, but it gets the job done. Just uses more juice over time. I'm sure all those little 2 uS wake / sleep cycles do add up after a while.

paul borgmeier
- 8th January 2008, 07:18
>> but where does the program execution actually continue in the code? Does it always execute the line following the @ SLEEP command?

If wake up by WDT or RA2 then it will always execute the line following the sleep command

>> Can I write to the GIE bit?

whether high or low, it will always execute the line after the sleep command. The difference is if it is low, the program will continue on after the line after the sleep command. If GIE is high and the INTE bit is set, the program will go to the interrupt vector after completing the line after the sleep command. You can set the GIE bit like this

INTCON.7 = 1 ' GIE = 1

You can set the INTE bit like this

INTCON.4 = 1

>> If I have the WDT running (as a fail safe if the external resonator goes out) - will the pic wake from sleep after each WDT cycle?

YES

>> Is there anything special I need to do with the WDT to make sure it doesn't wake the pic?

TURN IT OFF

>> Section 12.4.1 in the datasheet talks about this interrupt on RA2/INT. It notes the INTF bit of INTCON is set when the valid edge appears, the interrupt can be disabled by clearing the INTE bit of INTCON (which I don't think I want in this case). It then says the INTF bit must be cleared before re-enabling the interrupt, and it sounds like I'd need to re-set the INTE bit prior to the next sleep cycle also.

YES (you need to clear it, not re-set it)



Can anyone extrapolate a list of the flags I need to set or clear before executing @SLEEP so that pic will goto sleep, and stay sleeping forever (days, months, years, etc) until that button is pressed driving RA2 high again? And to make the program resume right after the @SLEEP without jumping to some other place as a result of the interrupt?


(Configure WDT off)
PORTA = 0 ; PortA low
ANSEL = 0 ; PortA digital
TRISA = 4 ; RA2 = input
INTCON.7 = 0 ; turn off global interrupts
OPTION_REG.6 = 1 ; wake on rising edge

Code Here

INTCON.1 = 0 ; clear RA2 interrupt flag in case set (right before @ Sleep command)
@ SLEEP
continues here when RA2 goes high (unless goto or branch, etc)
and then here (unless goto or branch etc)
and then here (unless goto or branch etc)

EDIT:

if you have your heart set on using the WDT, you can test bit 4 of the OPTION register to see what woke the PIC from sleep. If this bit is zero, you can put the PIC immediately back to sleep. The danger is you might miss the RA2 high while testing the Option bit (if RA2's high duration is short).

kevj
- 8th January 2008, 17:45
Outstanding! Thank you so much Paul. That's everything I needed. Makes much sense now.