pwede rin na gamitin ang WatchDog timer ng pic para i-reset sya..or.. i-tie ang reset pin sa isang output pin (like RA0, since magkatabi lang sila ni MCLR),then iki-clear (ilo-low) yung bit ng RA0.. hehe
void pogi(void){ printf("gwapo mo!"); delay(); counter2++;}void marcelino (void){ printf ("marcelino"); delay(); counter1++;}void main(){ while(1)// forever loop { marcelino(); pogi(); clearWDT(); //clear ako dito para di na magreset gamit ang WDT }}
ano-ano ang mga precautions (or special attention) na dapat inote sa paggamit ng WDT?
dapat meron ng timing sis. normally, linalagay ko 'yun sa Timer na nagha-handle ng flow ng program.
#include <pic.h>__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;}void main(){ InitSystem(); //include initializeing TMR0 as interrupt source while(1) { //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 .. .. doTaskn(); //just in case your system went nuts, recover! if(hard_sys_error_flag == TRUE) doErrorRecoverTask(); Systimer(); //manage system timing } }void SystemTimer(void){ //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; return; }void doErrorRecoverTask(void){ //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 }