PDA

View Full Version : 65 seconds.



Tomas
- 31st March 2004, 00:15
Hi everybody,
I'm storing some A2D results to my 24LC256 using I2CWRITE, it is working perfectly.
When a particular pin is set i want the program to jump to a small routine, some loop, and excute the loop for 65 seconds.
My question is how do i get roughly 65 seconds. I'm using a
@ SLEEP command in other part of my code and i don't want to enable watchdog timer because i want the PIC to wake up when an RB.0 interrupt occurs, this part is working and i want to leave it as it is.
I tried to count the firaction of seconds when TMR0 overflow interrupt occurs but for some reason, i think i'm using @ sleep, TMR0 interrupt is not working. Does Timer0 runs when the PIC is sleeping?
So how can i get 65 second without using TMR0 interrupt. The time doesn't necessarly be exact but around a minute.

Regards,
Tom.

picnaut
- 31st March 2004, 06:27
Hi,

You could try this...

FOR i = 1 to 65000
'Do stuff in loop
PAUSE 1 'ms
NEXT i

Also, you could time exactly how long it takes to do the task in your loop, subtract that from 1000us and then use PAUSEUS.
For instance, if it takes 100us to perform the code in the loop, simply use "PAUSEUS 900" for your delay.

To time the loop code, write a quick program that simply performs the loop code endlessly and toggle a pin each time through the loop. You should be able to check it out on the scope and get a rough idea of how many microseconds it takes.

Cheers.

Melanie
- 31st March 2004, 13:47
It's not going to happen the way you want. TMR0 even with the prescaler attached will not be able to give you 65 seconds. The same applies for TMR1 and TMR2. Futher, the obvious clock souces for the Timers are also halted during sleep.

But there's heaps of alternate solutions... here's one to get you thinking...

To achieve Low-Power consumption for the majority of the time, use NAP (example NAP 4 which is approximately 288mS), poll the keyboard to see if a button has been pressed, increment a timeout counter and go take another Nap...

TimeOut var Byte
.. ..

' Nap Entry Point
NapStart:
TimeOut=0
' Main Nap Loop
NapLoop:
NAP 4 ' Suspend reality for 288mS
If PortB.0=0 then goto KeyPressed
TimeOut=TimeOut+1
If TimeOut < 225 then Goto NapLoop
' Process 65 Second Timeout here

In this example, every approx quarter of a second, you poll the keyboard and check for key depression, and if less than 65 seconds have elapsed (0.288mS x 225 loop counts) then you simply just Nap again. This way, about 99.9% of the time you'll be asleep in low-power mode.

If polling the keyboard every 288mS is insufficient, then go to NAP 3, but you'll have to increase the loop-count for the 65 second timeout appropriately (if you do this then you'll have to make the TimeOut variable a WORD rather than a BYTE).

Melanie