View Full Version : Looking for a timer or clock object

01-07-2009, 06:01 AM
Has anyone done a timer in spin or assembly? I'm looking for something that keeps track of at least seconds and minutes (maybe milliseconds) and that doesn't reset or loose time when the system clock recycles. I suppose I could use an external rtc but it should be possible to just keep a memory location up to date with elapsed time converted to seconds. I'm measuring real long times (hours) for a battery charger so I need to keep track of much longer time than the system clock can do.

Mike Green
01-07-2009, 06:46 AM
It's easy enough to do. Just launch a cog that repeatedly waits for a second using WAITCNT and increments a long. You'd have a Spin interface routine that simply converts the seconds count from the long into days, hours, minutes, and seconds.

Shane De Catania
01-07-2009, 07:57 AM
Hi mcstar, have you seen date_time from Bob Belleville in the object exchange - that may be what you are looking for. Not sure what section its in, just look under view all objects.

News Flash!... Energizer Bunny arrested and charged with battery.

01-07-2009, 10:24 AM
got this from someone in forums not in assembly but its a good one.

Pub Main | timer1, timer2

timer1 := cnt
timer2 := cnt

IF (MainClock(timer1)=> 1_000)
timer1 := cnt
{{do something after 1 sec}}
IF (MainClock(timer2)=> 500)
timer2 := cnt
{{do something after 1/2 sec}}

PUB MAINCLOCK(p_TimeVar) | RightNow, ClockTicks

RightNow := cnt
IF RightNow < p_TimeVar
ClockTicks := ||($FFFFFFFF - RightNow + p_TimeVar)
ClockTicks := ||(RightNow - p_TimeVar)
result := ClockTicks / (clkfreq / 1_000)

Best timer there is as far as i am concerned.

01-07-2009, 02:54 PM

I would like to add, this timer works properly for measuring times less than 53 seconds.
(After 53 seconds there is a rollover of the systemcounter from max to zero)

For longer times (like hours and days) you can use

timer-object (http://obex.parallax.com/objects/45/)

from the obex

best regards