View Full Version : Watchdog Timer basics...ho ho ho

12-26-2009, 03:00 AM
··· Hi All,

··· It started out as a 1hr hardware project for a neighbor's kid, commonly called a
·· watchdog timer.· It took a 74hct74 FF and a LM556 dual-timer chip and a few other
·· minor parts, one push button, 2 NPNs and some R's and C's. with one push
·· I toggle the clock pin-3 of the FF; when the Q-pin-5 goes Hi; it enables pin 4
·· of the LM556 and also triggers a 30sec timer loop; after 30sec it resets
· pin 1 of the FF.··· ·BUT, if I push the button a 2nd time, Q-pin goes Lo
· and diables the timer - it pre-terminates (!)·the timing cycle in progress.

· Then I wanted to simulate this function on my Prop-Ed-Kit.

· I coded the waitcnt loop as :·· waitcnt(clkfreq * Delay + cnt)
· before the repeat loop starts I set· Delay := 30, for 30sec.
· But after waitcnt starts with that Delay value I have no control over it,
·such as, externally, redefining· Delay as· Delay := 1· for a quick timeout.

· Then I tried a decrement repeat loop:

· PUB Counter4··· 'watchdog timer ;· monitoring Runlogic == 1 (motor is active)
······················· 'Blink6: P7 Led Blinks at 1Hz while Runlogic == 0(motor is inactive)

····· repeat

········ if Runlogic == 1·· 'if motor turns in either DIR
··········· Delay := 30···· 'set delay for 30sec

··········· repeat until Delay =<1· 'to ensure loop doesn't go below 0
·············· Delay := Delay -1
·············· waitcnt(clkfreq + cnt)·· '1sec decrement steps
············································· 'when loop is done
··········· CCWlogic := 0·· 'stop motor if in CCW DIR
··········· CWlogic·· := 0·· 'stop motor if in CW DIR

······ Blink6

···· This works fine if I let the loop time out
···· I figured that I'd have more luck for··changing· Delay·· while the loop
···· was in progress,· to pre-terminate the·loop time,· ·but no luck.

··· Any ideas you "Pro Santas" out there· what Spin or ASSY code works
·· to simulate a Watchdog timer ?

·· Thanks,
·· karl i.


Mike Green
12-26-2009, 03:13 AM
You've got the right idea using WAITCNT, but it needs to run in its own cog. There's some other stuff you have to do to reset the watchdog timer because you've got two cogs talking to each other.

You can also just use the system clock like:

' When you start your operation, do:
startTime := CNT ' where startTime is any variable

' When you want to check for the timeout expiring, do:
IF (CNT - startTime) > Delay
' Here you do something if the timeout is passed
' Here you do your normal stuff

' Delay is in clock ticks. If your Delay value is in milliseconds, you could do
Delay := Delay * (CLKFREQ / 1000)

Post Edited (Mike Green) : 12/25/2009 8:18:45 PM GMT

12-26-2009, 03:21 AM

You can do this easily in spin without the use of any extra cogs.
What you need is to set a counter to do the blinking of the led at the rate you need.
The counter runs in the background without any further interference then you start a
loop where you check for a push button and at the same time check the CNT register
for a time out.
So you set the CTRA for example to NCO mode with the right FreqA register to make
the right on/off timing on a Pin.
Then you start a repeat loop having previously saved the current value of CNT.
In the loop you check for the status of another pin which has the push button to
pre-terminate the loop. Also you keep checking the current CNT register if it has
become > the previously saved value before you entered the loop and an increment
of ClkFreq*3/100 …ie. 30 ms as you stated.
Once the push button is pushed or the time is out you get out of the repeat loop and then you·
fall to the code that will disable the CTRA and thus stop the blinking.
You can then enter another loop checking the push button for another push which will then
repeat the whole process.... (you put the whole thing inside a container loop).
I could·write the code for you but you might enjoy trying to do it yourself first now
that you have the idea.

Post Edited (SamMishal) : 12/25/2009 8:27:53 PM GMT

12-26-2009, 04:42 AM
Kind thanks to you both, MIke Green and Sam Mishal, for your quick response !
I'll digest your info and see if I can make it work for my particular
start+stop motor application.
The PUB Counter4 gets called by a cog when my "Window Blinds" motor
(to control the angle of the horizontal slats) starts in either direction.
I figured that it might be safer to add this watchdog timer function in case
my UP or Down DIRection opto Limit sensors fail to turn the motor off.
If that should happen then this timer will stop the motor so that it doesn't
crunch the blinds mechansim.
Sam, thanks for the offer. If I get stuck then I'll send you an email
and maybe then you can give me another nudge in the right direction.

Best wishes for a joyful christmas day to you both !
karl i.