watchdog timer thingy
science_geek
Posts: 247
i am wondering how would you implement some sort of watchdog timer that would just keep track of time. i know how to implement one that takes up a cog, but say i was reading in sensors and at some point i wanted to know (in milliseconds) how long a program had been running, or when the timer hit certain time to activate some sensor, i know how to use waitcnt, but i dont necessarilly want to stop the processing, i just want to keep track and then every so often check the time and do updates accordingly. i know that you can use "cnt" to read the system clock, but how do i translate a millisecond change into a known value at start up.
Comments
waitcnt and the system counter cnt have a problem - the counter is 32 bits and is increased with each cycle. So in a little bit more than 50 seconds it wraps around. If your program guarantees to ask for the time within this timeframe you can use cnt for calculating the runtime:
start_time:=cnt
repeat
.... do something ....
end_time:=cnt
if (end_time-start_time)>clkfreq*10
do something periodically
If your periodical task should run each 10 minutes you need another counter to be increased when the cnt is wrapped around. It should be possible to do that with a counter of a COG.
The calculation of wait-times uses the clkfreq-variable. This is a variable pre-defined in SPIN. It's defined by the clock and PLL settings. In a 5MHz system with PLL 16x it's 80_000_000.
So, if you want to wait for 1ms (=1/1000s) you simly say waitcnt( clkfreq/1000 + cnt )
You should be aware of the upper and lower boundaries for tha value you add to cnt:
You can't wait longer than 2^32-1 and you can't wait shorter than something around 400 because SPIN needs this time to execute the waitcnt instruction. So, when the SPIN COG actually executes the PASM waitcnt the time to wait for has already passed and it will wait ~50s.
Hope that helped a bit.
http://forums.parallax.com/showthread.php?p=820890
I thought I had uploaded it to Obex, but could not find it, so put it here: obex.parallax.com/objects/560/
Post Edited (matb) : 1/28/2010 9:17:54 AM GMT