Shop OBEX P1 Docs P2 Docs Learn Events
watchdog timer thingy — Parallax Forums

watchdog timer thingy

science_geekscience_geek Posts: 247
edited 2010-01-28 09:10 in Propeller 1
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

  • Mike GreenMike Green Posts: 23,101
    edited 2010-01-26 19:53
    It's easy to time things up to about 50 seconds with an 80MHz system clock. You save the value of CNT in a variable, then compute CNT - savedCnt when your task is done. The difference is the number of 12.5ns clock ticks from when you saved CNT. Look at the Simple_Serial object for one example of the use of this to establish a timeout (for serial input).
  • MagIO2MagIO2 Posts: 2,243
    edited 2010-01-26 20:10
    Actually you don't talk about a watchdog. A watchdog is a little piece of hardware (either build in mikrocontrollers or external) that has to be triggered from time to time by the mikrocontroller. If it's not triggered in time it usually means that the program hangs and the watchdog resets the controller.

    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.
  • matbmatb Posts: 39
    edited 2010-01-28 09:10
    If you want is to track time, i.e. software RTC, without wasting a COG, I posted one here:

    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
Sign In or Register to comment.