Clock question
I am trying to setup a simple clock so every hour I can setup an event.
Also, I want a variable which tracks total number of seconds the program has run.
I have a feeling it has to do with the cnt register rolling over after 32 bits. But there has to be some way to make it work.
It freaks outs after (I assume) cnt gets too high. So I tried to fix it with the "curclk =< (4294967295 - clkfreq)", but now it seems to just stop counting all together. I figured it would roll over and continue once that second had passed. Can someone explain what may be happening.
This code is extracted out of a program I wrote (not exactly, so I am sorry if it doesn't function as is).
Also, I want a variable which tracks total number of seconds the program has run.
I have a feeling it has to do with the cnt register rolling over after 32 bits. But there has to be some way to make it work.
It freaks outs after (I assume) cnt gets too high. So I tried to fix it with the "curclk =< (4294967295 - clkfreq)", but now it seems to just stop counting all together. I figured it would roll over and continue once that second had passed. Can someone explain what may be happening.
This code is extracted out of a program I wrote (not exactly, so I am sorry if it doesn't function as is).
CON
_CLKMODE = XTAL1 + PLL8X ' 10MHz clock
_XINFREQ = 5_000_000 ' 5MHz Crystal
VAR
long on_seconds ' total seconds since last reboot (max of 136 years -- I think)
PUB Clock | minutes, seconds, time, curclk ' main program to get, display, and adjust frequencies
time := cnt + clkfreq ' get next time marker
on_seconds := 0
seconds := 0
minutes := 0
repeat ' repeat forever
curclk := cnt
if (curclk => time) AND (curclk =< (4294967295 - clkfreq)) ' if it has been a second
on_seconds++
seconds++
time := time + clkfreq ' move marker ahead one second
if seconds => 60 ' if it has been a minute
minutes++
seconds := 0
if minutes => 60 ' if it has been an hour
minutes := 0
''EVENT

Comments
Here's the first part, that toggles a pin (PinB) every second. It also toggles PinA 10 times a second, although I don't currently use that part.
This code was derived from a Propeller application note. Also, note that this assumes a 8 MHz osc. and a PLL of 8X.
J
PUB Cycle(PinA, PinB) ctra := %00100_000 << 23 + 1 << 9 + PinA 'Establish mode and APIN (BPIN is ignored) frqa := 1366 'adjusted 6 clicks up for 1 sec slow over 5 min dira[noparse][[/noparse]PinA] := 1 'Set APIN to output dira[noparse][[/noparse]PinB] := 1 repeat repeat 10 'go thru 10 times, making for one sec waitpeq(|< PinA, |< PinA, 0) 'Wait for Pin to go high. waitpne(|< PinA, |< PinA, 0) 'Wait for Pin to go low !outa[noparse][[/noparse]PinB] 'PinB toggles every second !outa[noparse][[/noparse]PinB]VAR byte hours, minutes, seconds PUB clock : time time := cnt repeat waitcnt(time := time + clkfreq) if seconds++ == 60 seconds~ if minutes++ == 60 minutes~ if hours++ == 24 hours~pub whatever|seconds ctra:=%00100<<26 'note please check this it may be incorrect phsa:=0 frqa:=1 repeat dosomethingelse if phsa>80_000_000 seconds++ phsa-=80_000_000 pub dosomethingelserepeat dosomethingelse if testvalue - phsa < 80_000_000 seconds++ testvalue += 80_000_000▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
VAR long on_seconds ' total seconds since last reboot (max of 136 years) byte hours, minutes, seconds word days ' total days since last reboot PUB Main | time time := cnt ' get next time marker on_seconds~ seconds~ minutes~ hours~ days~ repeat ' repeat forever if (cnt - time) => clkfreq ' if it has been a second on_seconds++ seconds++ time += clkfreq if seconds => 60 ' if it has been a minute minutes++ seconds~ if minutes => 60 ' if it has been an hour hours++ minutes~ if hours => 24 ' if it has been a day days++ hours~Just set commPin to an unused pin within your cog. Call getTimer to get the seconds since you called setupTimer. This works great for things like unix timestamps, etc. In fact, I think there is an object in the OBEX that provides unix timestamp style date functions.