Clock question
Bobb Fwed
Posts: 1,119
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
▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
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.