PDA

View Full Version : Sleep when inactive ... how ?



AndrewC
- 10th November 2009, 10:43
I'm sure this is simple but I can't find the answer (even with the new improved search engine) so perhaps I'm phrasing the question wrong.

I want a 16F628 to go to sleep when inactive for say 60secs.
The code I'm using is constantly looping looking for a trigger signal to go low before firing a camera but I'd like it to power down completely if nothing happens for say 60secs and wake up again and start monitoring the trigger if a PORTB pin changes state.

Thanks, Andrew

grounded
- 10th November 2009, 11:27
not sure if this is what your looking for but is what I do with my camera


ON INTERRUPT GoTo TAKEPIC 'ON PIR TRIP WAKE UP
INTCON=$90 'ENABLE .2 INTERUPT

AndrewC
- 10th November 2009, 11:42
Possibly :)

The thing is I want an LCD to display some relevant info for a short time after the shot is taken but then go to sleep.

I suppose I could use a timer overflow to trigger the SLEEP command, and reset the timer everytime something happens to keep it awake ?

Running at 4MHz can I count to 60secs with an internal timer or do I need to use the timer overflow to increment a "second counter" ?

Andrew

Jerson
- 10th November 2009, 11:52
The easiest way would be to count down 60 x 1 second delays using pause. After that time, simply put your PIC to sleep. The delay can be reset by the Interrupt occurence.



ISR:

....
....
SleepTimeOut = 60

INT_RETURN

main:
...
...
if SleepTimeOut <> 0 then
pause 1000
SleepTimeOut = SleepTimeOut-1
else
@ sleep ' use the ASM sleep command
@ nop ' some pics need these 2 nops (the datasheet will tell)
@ nop
endif
goto main

Acetronics2
- 10th November 2009, 12:24
Hi, Andrew

If your program loops ...

just count how many loops in a minute ... and go to an @sleep command if value reaches the loops per minute value ...

just need to add a counter and a test in your loop ...

Non-blocking solution ...

Alain

AndrewC
- 10th November 2009, 13:43
So Alain,

One thing that I always find difficult to answer, how can you work out how many clock cycles are used by a particular group of PBP instructions ? I could test it empirically but there must be a way of calculating it.

Thanks, Andrew

AndrewC
- 10th November 2009, 13:48
The easiest way would be to count down 60 x 1 second delays using pause. After that time, simply put your PIC to sleep. The delay can be reset by the Interrupt occurence.



ISR:

....
....
SleepTimeOut = 60

INT_RETURN

main:
...
...
if SleepTimeOut <> 0 then
pause 1000
SleepTimeOut = SleepTimeOut-1
else
@ sleep ' use the ASM sleep command
@ nop ' some pics need these 2 nops (the datasheet will tell)
@ nop
endif
goto main


Would work but then I need to rewrite my code - I don't trigger off an interrupt but instead look at PortB which holds not just the trigger signal but also a small keypad. The unit I'm making isn't just a simple "fire", there is also a small menu system to enter a few system parameters.

Not impossible to rewrite but perhaps I'll just count how many times I loop and conditionally branch ..

Thanks, Andrew