ano-ano ang mga precautions (or special attention) na dapat inote sa paggamit ng WDT?
Basic precaution would include not using long software delay in the code. If using software delay function is unavoidable, it should be very short like a few hundred microseconds maximum. Never include other blocking functions like those that manage buttons, keypads, etc..
dapat meron ng timing sis. normally, linalagay ko 'yun sa Timer na nagha-handle ng flow ng program.
I agree, dapat deterministic ang total timing ng main system loop if reliable use of WDT is desired.
This is basically achieve by using state machines implementation for each tasks inside the main loop, and a hardware timer to regulate the number of passes to the main loop. For example, we can use TMR0 to overflow 1000x per second and also make the main loop to execute 1000x per second by blocking the end of the main loop while TMR0 has not overflowed yet.
Here is a basic code.
__CONFIG(WDTEN & HS);
void interrupt isr(void)
GIE = 0;
if(T0IE && T0IF)
TMR0 = TMR0_PRESET_VALUE; //TMR0 overflow period = 1 ms
tmr0_rollover_flag = TRUE;
T0IF = 0;
GIE = 1;
InitSystem(); //include initializeing TMR0 as interrupt source
//do what your system should do
//all tasks should execute within 1 ms
doTask1(); //ex. serial com-related task
doTask2(); //ex. adc-related task
.. //other tasks
//just in case your system went nuts, recover!
if(hard_sys_error_flag == TRUE)
Systimer(); //manage system timing
//manage other timing related tasks here
CLRWDT(); //clear the WDT
while (tmr0_rollover_flag != TRUE); //wait for TMR0 to overflow, 1 ms
tmr0_rollover_flag = FALSE;
//do some initial recovery tasks here like, resetting variables and state machines
//The sys_error_recovery_flag should be set somewhere here
//lastly, if error recovery failed, reset PIC!!
if(sys_error_recovery_flag == FALSE)
while(1); //blocking code, WDT not cleared, will cause PIC to reset after 7 ms
All the tasks in the loop should execute within 1 ms. An error recovery task is also included to cure any system errors. This is a must if you are building a commercial product. Note that resetting the PIC using WDT is a last recourse and is placed last in the error recovery function. At the end of the main loop, the system timing is managed by the SystemTimer().
Again, in this example the main loop is passed 1000x per second, which also means that the WDT is cleared every 1 ms. The WDT overflow period is 7 ms if not using prescaler, so a reliable functional firmware should clear the WDT every less than 7 ms.
If the system has a major error (i.e. the program is still running but crazy), you can make the mcu reset itself by making the WDT overflows. If somehow the program freezes (i.e. a power glitch hangs up the mcu), the WDT will still overflow and reset the MCU.