Trying to understand the "TIME" variable

FIREMANJIM

New Member
I am trying to understand how to use the TIME variable on the Picaxe 14M2 and 20M2 chips. As I understand it it works as a true second counter that I can reset at will. So will something like this work??
Code:
     EnableTime
     if PirInput = 1 then     
      	pause 29
      	if PIRInput = 0 then
      		goto WalkTest
      	Endif
      	high LED
      	nap 6                     ' pause low power for 1.1 seconds
      	low LED
      	nap 8                     ' pause low power for 4 seconds
      	Time = 0
      endif

      if Time >= 30 then           ' approx 30 seconds 
            for Counter = 1 to 10
		high LED
		pause 200
		low LED
		pause 200
	    next Counter
            pause 1000
	    Disable Time
            goto MainProgramLoop                            ' start main program loop
      endif 
      Nap 3
      goto WalkTest
 
Last edited by a moderator:

westaust55

Moderator
I believe that will function as you desire- that is, 30 seconds after the LED flash following a PIR event.

Have you tired it in the PE Simulator?

If you place your code between code tags then the indenting and program flow will make it easier for others to follow.
Have added the code tags for you on this occasion.
 

Jeremy Harris

Senior Member
The Time variable is very useful as long as your programme has no instructions that stop it updating. For example, it gets messed up with any blocking instruction. As long as this limitation is remembered I've found it to be very useful.
 

AllyCat

Senior Member
Hi,

AFAIK the TIME variable also does not increment during a NAP or SLEEP (because they use the "Watchdog" timer, whilst TIME divides down the program instruction clock).

Also beware that the TIME prescaler is not Reset by TIME = 0, so the first period can be anything between 0 and 1 second.

Cheers, Alan.
 

FIREMANJIM

New Member
Thanks for all your answers....So Time = 0 does not clear the time counter and reset it?? And using naps and sleep will alter the time counter?? My timing does not have to be super precise. I can live with 31 or 32 seconds.....doesn't have to be exact. I am obviously new to all this and you guys are helping me a lot!!
 

AllyCat

Senior Member
Hi,

Yes TIME = 0 does "reset" the TIME variable but the complete time counter has three stages (or four if you include the initial division by 4 from the "4 MHz" clock down to the PIC's 1 MHz instruction cycles). Firstly the instruction clock is divided by 20,000 (or maybe other values with a non-4 MHz clock) to generate internal 20 ms interrupts (which are also used for the servo pulses). This divider can be read and reset using a PEEK/POKESFR of the PIC's "Timer 1". The second stage divides these 20ms interrupt "ticks" by 50 and increments the TIME variable on every 50th, but unfortunately this prescaler/counter cannot be either read or reset.

Therefore, after any "arbitrary" time has elapsed, if TIME = 0 is executed, then it might increment after anything between 0 and 999 ms later, whenever the prescaler next rolls over from 49 to 50 (and then 0). For more precise (integer) times, you can DISABLETIME immediately after the TIME variable increments and then re-start timing "integer seconds" with an ENABLETIME.

For your application it's probably best just not to use SLEEP or NAPs; The power consumption of a PICaxe is normally very low anyway and many other factors may need to be considered for ultra-low power consumption applications. Or, follow each NAP 6 with a TIME=TIME+1, or each SLEEP 1 by a TIME=TIME+2 to take into account the "stopped" time periods.

Cheers, Alan.
 
Top