Using Picaxe M2 elapsed time counter

jims

Senior Member
I have never used the M2 elapsed time counter. The following code is my attempt to understand and use this M2 feature. I believe that the code will consistently yield a 10 second timer. However there will be some additional amount of time (probably u seconds) while the chip recognizes that time is no longer <10 (when code on line 18 ends the loop); until time is set to zero (code line 19). Probably will be minute variations from chip to chip. Not sure how to calculate or measure this additional time. Will appreciate comments to help me understand this. Thank you, JimS
Code:
'***********************************************
'* 20m2 uses the internal 1 second "time" counter.
'* Sequence repeats every 10 seconds.
'*********************************************** 
symbol OLED = B.4
symbol RLED = B.6
symbol GLED = C.2
#picaxe 20m2
pause 1000	'Initialize system.
enabletime	'Enable & start the elapsed time counter.
time=0	'Reset "time counter" to zero.
main:
	do
	 call do_stuff
	 serout OLED,n2400,(254,1):pause 30  'Clear OLED
	 do	
	  pause 1
	 loop while time < 11 'wait until 10 seconds have elapsed. 'This is line 18.
 	time=0	'Reset "elapsed time counter" to zero.            'This is line 19.
	loop
	
'* Simulates code doing something.	
'* Bicolor RG/LED is red and OLED message
'* shows while doing "stuff".	
do_stuff:	
	 high B.6:low C.2	'Turn RED LED ON.
	 serout OLED,n2400,(254,128,"Doing STUFF")
	 pause 4000		'Waste 4 seconds here.
	 low B.6:low C.2	'Turn RED LED OFF.
	 return
 
Last edited:

bpowell

Senior Member
I love the elapsed time counter...I use it as a poor-man's "Watchdog"...I have code reset it from time to time...and code also monitors the counter...if it's > say 10, then I'll take steps to reset, or fix the issue.

However, it's not a precise timer...as you noted, there are some us of delay while you test the time...that's one factor...but the bigger factor is the Time timer itself.

The Time variable is likely incremented as a result of multiple interrupts on a different timer...

Let's just say, it's a 100ms timer...and PICAXE says, "Every 10 interrupts on that 100ms timer, we'll increment Time by 1"...

When you set "Time = 0" you're only setting that Variable to 0, you're not resetting that counter that causes time to increment...it could already have interrupted 9 times...and when you set 'Time = 0" it will change to Time=1 in 100ms. (not 1s).

The only way to work around that (if you want) is to discard that first second...so...

Time = 0
Wait until Time = 1
Then do something...using "1" as your beginning.
 

AllyCat

Senior Member
Hi,

The Time variable is likely incremented as a result of multiple interrupts on a different timer...
Yes indeed. It's actually the PIC's "Timer 1" which drives a x50 "prescaler" (i.e. it rolls over and interrupts every 20 ms). It is also used for the servo pulse option (so Timer 1 is not stopped by a DISABLETIME).

If you PEEKSFR Timer 1 (a 16-bit word) you should find that it always has values between 45,536 and 65,535, i.e. it is dividing a 1us clock by 20,000, Sadly, there doesn't seem to be any way to read or reset the (software) divide-by-50 prescaler. But you may be able to wait until "time" rolls over, then issue a DISABLETIME until you initiate the output of an integer time period (if you want to achieve better than one-second accuracy).

Cheers, Alan.

.
 

nekomatic

Member
In my experience so far, serial I/O messes up any attempt at accurate timing using TIME.

I say I/O, it may only be the I that's the problem i.e. waiting at a serin command for an external device to send data. This was on a 20M2.
 

lbenson

Senior Member
Rather than resetting to 0 after each 10 seconds, you may gain marginally more average accuracy if you use modulo 10 and only reset the timer when it exceeds, say, 60,000 or 65,000.
Code:
b25 = timer // 10
if b25 = 0 then
  if timer > 65000 then : timer = 0 : endif
' now make sure you don't trigger on the same timer value more than once
' ...
' now do what you want to do after 10 seconds
' ...
endif
The number of milliseconds or microseconds after 10 seconds for your trigger will vary, but in the long run you will average close to 10 seconds.
 
Top