Use of the WatchDog Timer


' WatchDog.Bas (BX24)
'
' Illustrates use of the on-board watch dog timer.
'
' On boot, routine flashes Green LED one time and then continually flashes
' the Red LED.  
'
' Note that the watch dog timer is enabled for a nominal two second timeout.
' If there is no call to WatchDog() in the flash routine, the processor will 
' reset (GreenLED will wink) after a few flashes of the Red LED.  However, if
' there is a call to WatchDog() at time intervals of less than the timeout,
' the routine will perform normally.
'
' Copyright, Peter H. Anderson, Baltimore, MD, Nov, '99

Const RedLED as Byte = 25
Const GreenLED as Byte = 26


Sub Main()

   Call FlashLED(GreenLed, 1, 0.5, 0.5)	' flash Green LED to indicate
					' processor has booted

   Call OpenWatchDog(7)	' 2 sec timeout
			' timeout = 16 * 2^N ms
			' in this case 16 * 2^7 = 2048 ms
   Do          
     Call FlashLED(RedLED, 10, 0.5, 0.25)
   Loop
End Sub
         
Sub FlashLED(ByVal Pin as Byte,  ByVal NumTimes as Integer, _
             ByVal OnTime as Single, ByVal OffTime as Single)
   Dim N as Integer

   For N = 1 to NumTimes
      Call PutPin(Pin, 0)	
      Call Sleep(OnTime)
'     Call WatchDog()	' without call to watch dog, processor will reboot
      Call PutPin(Pin, 1)	' uncomment the Call WatchDog() for normal 
				' operation
      Call Sleep(OffTime)
   Next

End Sub