Using TIME=0 does not reset internal timer to zero (08M2)

Dave E

Senior Member
Hello all,
I have been trying to program an 08M2 (4.A) to wait an interval of time then do a task. During my testing, I noticed that if I reset the TIME variable to zero (TIME=0), the first one second interval varies. It is always less than a second and the interval varies.

I use a "DO WHILE button is pressed" loop to set the TIME variable to zero and I update the time on an OLED display after every increment of the TIME variable (each second). I also have a "how many times through the main program loop" counter (W1). It takes 247 loops through the program for the TIME variable to change. However, when I press and release the button which should reset the timer to zero, the number of times through the program loop varies anywhere from 51 to 226 for the first increment of the timer. I have also used the DISABLETIME/ENABLETIME commands inside the reset loop but found no changes.

This leads me to believe that just because the TIME variable is set to zero, the internal timer is not reset to the start of its count.
Does anyone have any info on this?

Dave E


Code:
#PICAXE 08M2
#NO_DATA
SETFREQ M4


SYMBOL	SECONDS	=	B4	'W2
SYMBOL	MINUTES	=	B5	'W2
SYMBOL	HOURS		=	W3
SYMBOL	TIME2		=	W4


PAUSE 1000
SEROUT C.4, N2400_4, (254, 1)
PAUSE 100

SEROUT C.4, N2400_4, (254, 192, #HOURS, ":", #MINUTES, ":", #SECONDS)

TIME = 0
TIME2 = 0

MAIN:
DO
	DO WHILE PINC.3 = 1	'RESET TIME/CLEAR SCREEN WHILE BUTTON IS PRESSED
		TIME = 0
		TIME2 = 0
		SECONDS = 0
		MINUTES = 0
		HOURS = 0
		W1 = 0
		SEROUT C.4, N2400_4, (254, 1)
	LOOP

	
	TOGGLE C.1	'LED
	INC W1		'COUNT NUMBER OF RUNS THROUGH PROGRAM


	IF TIME = 1 THEN	'ONE SECOND HAS ELAPSED
		TIME = 0
		INC TIME2
		SEROUT C.4, N2400_4, (254, 128, #TIME2, 254, 135, #W1)
		W1 = 0
		INC SECONDS
		IF SECONDS = 60 THEN
			SECONDS = 0
			SEROUT C.4, N2400_4, (254, 192, #HOURS, ":", #MINUTES, ":0 ")
			INC MINUTES
			IF MINUTES = 60 THEN
				INC HOURS
				MINUTES = 0
				SEROUT C.4, N2400_4, (254, 192, #HOURS, ":0 ", ":0  ")
			ENDIF
		ENDIF
	SEROUT C.4, N2400_4, (254, 192, #HOURS, ":", #MINUTES, ":", #SECONDS)	
	ENDIF
	
LOOP
 

Haku

Senior Member
I don't know about how the internal TIME variable is incremented but try adding this into your code whenever you want to reset the TIME variable whilst trying to keep better accuracy:


'reset TIME whilst maintaining more accuracy
TEMP=TIME
DO WHILST TEMP=TIME
LOOP
TIME=0


If you simply reset the TIME variable whenever it reaches 1 you will certainly introduce timing errors due to how long it takes for a Picaxe to execute commands. The better way to keep track of how long an event has been going on for in h/m/s, so long as it's under 18.2 hours, is to leave the TIME variable running and extrapolate the h/m/s from that:

SECONDS=TIME//60
MINUTES=TIME/60//60
HOURS=TIME/3600



EDIT: Weirdly topics that I have been replying to have been automatically rated with stars, I never clicked on the rating thing during replying so I think it must've been something to do with NoScript blocking javascript on this website, I have unblocked it so will see if that was the cause when I next reply to a topic.
 

AllyCat

Senior Member
Hi,

No doubt Hippy will be able to provide a much more comprehensive answer, but I think your problem is that Time just counts "ticks" from the PIC's Timer1 hardware, and resetting the counter doesn't affect the ticks from the hardware. You probably need to reset Timer1 using a PICaxe pokesfr command (but be careful what might also be affected). Take a look at Hippy's post #6 in this thread which is actually concerned with using low clock frequencies but reveals several key issues concerning Timer1.

But is this really significant if you're using the internal 4MHz clock, which is only specified to be accurate to within 1%? In practice it might be significantly smaller, but I doubt if it's stable to better than 0.1% (depending on supply voltage variations and temperature, etc.). So any "accuracy" of setting the "phase" of the clock will drift out of "calibration" within a few minutes anyway.

Cheers, Alan.
 

hippy

Ex-Staff (retired)
No doubt Hippy will be able to provide a much more comprehensive answer, but I think your problem is that Time just counts "ticks" from the PIC's Timer1 hardware, and resetting the counter doesn't affect the ticks from the hardware.
That was going to be my guess also, though probably a counter variable in the firmware rather then the hardware timer itself - To absolutely reset to 'zero' all would need to be zeroed.

"time=0" is probably like moving a clock's minute hand back to zero, but not moving the seconds hand back as well.
 

Dave E

Senior Member
Thanks everyone.
Haku and AlleyCat, I will give that code a try.
hippy, that is what I suspected was happening. TIME=0 zeroed the counter but not the clock.

DAVE E
 
Top