PDA

View Full Version : for/loop for a timer?



Scott
- 2nd May 2005, 01:09
Is it possible to use a for loop to create a timer in software? It does not need to be very accurate. I cannot use timer0, timer1 or timer2 for I am already using them for user interface. See below of an example of what I am trying to achieve. It works but why will it not work correctly?

rx:
for i = 0 to 60000
timer = timer + i
pause 1
next i

if (timer == 60000)then
trigger = trigger + 1
timer = 0
endif

if (trigger == 1)then
trigger = 0
goto a2d
else
goto rx
endif

I am using a PIC16F876A @ 20Mhz. I know the instruction cycle is around 200ns and the pause is for 1ms. It takes 1000ms to equal 1sec thus 60000.

Thanks in advance,
Scott

Melanie
- 2nd May 2005, 02:19
What are you trying to achieve?... doing an AD conversion once per minute, otherwise keeping time in 1mS Steps?

CounterA var word

Loop:
For CounterA=0 to 60000
Pause 1
Next CounterA
Gosub A2D
Goto Loop

Scott
- 2nd May 2005, 02:35
I am sending data out over the GPRS network to a webserver. One minute is the minimum for the timer! I would like to be able to increment trigger every time my loop reaches 60000 with trigger = trigger + 1. My thought is that I can set "if (trigger == X)" trigger with X = 1, 2 and so on... depending on the cellular sevice provided or customers request.

Dwayne
- 2nd May 2005, 15:27
Hello Scott,

Scott>>I am sending data out over the GPRS network to a webserver. One minute is the minimum for the timer! I would like to be able to increment trigger every time my loop reaches 60000 with trigger = trigger + 1. My thought is that I can set "if (trigger == X)" trigger with X = 1, 2 and so on... depending on the cellular sevice provided or customers request.<<

Use Melanies Code, Unless you *must* use a interupt. Melanies code will give you 1 minute delay, it is much shorter.

Or you can use a another kind of loop:

Loop:
Pause 60000
Gosub A2D
Goto Loop

If you use a interupt, this is a poor way how to do it. PBP will not call the interupt until the Pause is over with. Unless you use a assembly interupt.

Dwayne