PDA

View Full Version : How do you time an operation?



Hylan
- 17th May 2010, 15:29
I want to monitor an input for a given period of time, let's say 5 minutes. If it stays high for 5 minutes I then want to turn on another input until the original is turned off.

Can someone help point me in the right direction.

I'm using a 18F6520 with a 4mhz crystal.

thank you,
Hylan

mackrackit
- 17th May 2010, 16:08
Idea.

Set up a timer interrupt to check the pin every second. After 300 seconds if the pin is still high then..

Bruce
- 17th May 2010, 16:55
You can also do this without interrupts by just monitoring a timer overflow flag bit.

Here's one example;
http://www.picbasic.co.uk/forum/showthread.php?t=6957

Ioannis
- 18th May 2010, 08:32
Hi Bruce. At the link above, you posted a timer code. I do not get it why you are adding the timer value to the T1LO/T1HI values instead off just loading the timer with the timout value.

Sorry I seem like Goofy right now....

Ioannis

HenrikOlsson
- 18th May 2010, 09:48
Hi Ioannis,
I think doing it that way accounts for any variance in latency etc. Since the code just polls TMR1 interrupt flag rather than actually cause an interrupt the timer may have counted "a while" before the code actually gets to reloading it. If you then simply reload it the time already passed since it overflowed will get lost. By adding the preload value to the actual value already accumulated in the timer you won't loose any time.

Does that make any sense?

/Henrik.

Ioannis
- 18th May 2010, 10:41
Ohh yeah! I see the light!

Thanks Henrik.

Ioannis