
Originally Posted by
kenhorner
In the true sense of a dog with a bone, I decided to work up a simple solution not dependent on device clock rates. The code below will work for any Teensy 3x (not having a model 4 around, I couldn't test it.) It should be self-explanatory enough
for general use. Thanks all.
//================================================== ======
//==================Teensy Watchdog Timer Routine==================
void Set_WatchDog_Timer(){ // A CPU Independent WD Timer
// This code sets a 5 second timeout
noInterrupts()
WDOG_UNLOCK = WDOG_UNLOCK_SEQ1;
WDOG_UNLOCK = WDOG_UNLOCK_SEQ2;
delayMicroseconds(1); // Need to wait a bit..
WDOG_STCTRLH = 0x0001; // Set Control Register... Enable WDG using the Low Power
// 1khz clock, and reset only no interrupt triggering,
// windowing disabled, allow updates, debug off, and
// WD disabled in CPU Stop mode
WDOG_TOVALL = 1000;// The next 2 lines set the time-out value. Low and High 16 bit values
WDOG_TOVALH = 0; // This is the value that the watchdog timer compare itself to-- 1000 ticks.
// Now set the clock prescaler.
// WDOG_PRESC = 0x0000; // After reset, the default prescaler setting is 0x0400. The hardware adds
// 1 to get a divisor of 5. This gives the default tick rate of
// 1khz/5 = 200 hz. If we wanted another rate, we could set the value here.
// Uncommenting the above line would produce a 1khz clock rate.
interrupts();
}
void Reset_Watchdog_Timer(){ // Set for a delay;
// Timer cycles at 200 hz. "count" value of 1000 = 5 secs
noInterrupts();
WDOG_REFRESH = 0xA602;
WDOG_REFRESH = 0xB480;
interrupts();
}
//================================================== =============
Code:
//================================================== ======
//==================Teensy Watchdog Timer Routine==================
void Set_WatchDog_Timer() { // A CPU Independent WD Timer
// This code sets a 5 second timeout
noInterrupts()
WDOG_UNLOCK = WDOG_UNLOCK_SEQ1;
WDOG_UNLOCK = WDOG_UNLOCK_SEQ2;
delayMicroseconds(1); // Need to wait a bit..
WDOG_STCTRLH = 0x0001; // Set Control Register... Enable WDG using the Low Power
// 1khz clock, and reset only no interrupt triggering,
// windowing disabled, allow updates, debug off, and
// WD disabled in CPU Stop mode
WDOG_TOVALL = 1000; // The next 2 lines set the time-out value. Low and High 16 bit values
WDOG_TOVALH = 0; // This is the value that the watchdog timer compare itself to-- 1000 ticks.
// Now set the clock prescaler.
// WDOG_PRESC = 0x0000; // After reset, the default prescaler setting is 0x0400. The hardware adds
// 1 to get a divisor of 5. This gives the default tick rate of
// 1khz/5 = 200 hz. If we wanted another rate, we could set the value here.
// Uncommenting the above line would produce a 1khz clock rate.
interrupts();
}
void Reset_Watchdog_Timer() { // Set for a delay;
// Timer cycles at 200 hz. "count" value of 1000 = 5 secs
noInterrupts();
WDOG_REFRESH = 0xA602;
WDOG_REFRESH = 0xB480;
interrupts();
}
//================================================== =============
Do you think in future you could enclose your code between code tags.
You can do this using the # button. I think you will agree it makes the code much more readable, and therefore more understandable.