The watchdog timer (Watchdog Timer (WDT_A)) is actually a special timer, which can be used as a watchdog or a timer. The so-called watchdog function refers to the ability to monitor whether the program runs away due to some interference or errors. The principle is that when the time of the failure meets the specified timing time, a non-masking interrupt is generated to reset the system. In this way, when debugging the program or predicting that the program may have an instantaneous error somewhere (such as external circuit interference), setting a watchdog timing interrupt can prevent the program from running away. Of course, it can also be used as a general timing function. However, in fact, since the watchdog timer (when used as a watchdog) requires very strict settings (otherwise the program is prone to frequent restarts), many people will not use this function. Therefore, at the beginning of the program, add a sentence: WDTCTL=WDTPW+WDTHOLD to turn off the watchdog. 2.1 Introduction to WDT_A Features: ① There are 8 optional timing times; ② Watchdog mode; ③ Timer mode; ④ The watchdog control register is password protected; ⑤ The clock source is optional and has accidental clock source protection; ⑥ It can be terminated to save energy; ⑦ Whether used as a watchdog or a timer, the interval time cannot be set arbitrarily, and can only be selected from 8 settings. Of course, the time can be changed indirectly by changing the clock frequency; Note the default settings: the watchdog starts when the program starts; the monitoring cycle is 32ms/32.768KHZ (that is, when the watchdog clock frequency is 32.768KHZ, every 32ms, if it is not cleared by the software, the program will restart); the clock source used is SMCLK (the actual frequency is not 32.768KHZ, which will be mentioned later). 2.2 Registers and operations of WDT_A Note: All registers store word operation and byte operation modes. For example, directly assigning a value to the WDTCTL register is a word operation. You can also use WDTCTL_L (low byte register) and WDTCTL_H to perform byte operations. This type of register can only be assigned values and no logical operations such as "|=, &=" can be performed. 2.2.1 Watchdog Control Register WDTCTL (Watchdog Timer Control) This register is a 16-bit read-write register with password protection. The so-called password protection is to prevent the register from being accidentally tampered with. So how to implement password protection? In fact, the high byte of this register is used to store the password, and the low byte is the control data. The write password is 05Ah, and the read password is 069h. Any high byte operation that is different from the password will cause the system to reset. The bit function definition of this register is as follows: (supports bit operation) WDTPW: Bits15-8, WDT Password, write as 05Ah, read as 069h. WDTHOLD: Bit7, WDT HOLD, 0: Open the watchdog timer; 1: Close WDTSSEL: Bits6-5, WDT Clock Source Select Clock source selection WDTTMSEL: Bit4, Operation mode selection 0: Watchdog mode; 1: Timer mode; WDTCNTCL: Bit3, Timer clear 0: Invalid; 1: Clear the counter, that is, WDTCNT=0x0000h WDTIS: Bits2-0, WDT Interval Select, Interval time selection. Used to select the counting cycle, there are 8 times to choose from. Note: The number before the bracket is the value of the counter 2.2.2 Watchdog count value register WDTCNT (Watchdog Timer Counter) This is a 32-bit up counter, but it cannot be directly assigned by software, etc., and the time can only be selected through WDTIS in WDTCTL. Or you can select different clock sources through WDTSSEL to change the time indirectly. 2.2.3 Watchdog interrupt bit control WDT uses the two bits in the SFRS register to control the interrupt. WDT interrupt flag: WDTIFG, located at SFRIFG1.0 WDT interrupt enable bit: WDTIE, located at SFRIE1.0 Watchdog mode: If WDTCNT is not cleared or WDT is not initialized in time, WDTIFG will be set, and then the program will restart. Timer mode: The general interrupt GIE and watchdog interrupt WDTIE must be opened. In addition, after the interrupt service program is executed, the flag bit WDTIFG will be automatically cleared. 2.3 Common Operations /*Turn off the watchdog*/ WDTCTL=WDTPW+WDTHOLD; /*Feed the watchdog, that is, clear the watchdog in the valid state*/ /* If the watchdog is not fed after the time is up, the program will be restarted*/ /*WDTIS2, that is, WDTIS=100, at this time the time interval is set to 1S, assuming the frequency is 32.768KHZ*/ WDTCTL = WDTPW + WDTCNTCL+WDTSSEL0+WDTIS2; /*Set the watchdog to counter mode, count 8192 for about 250ms, assuming the frequency is 32.768KHZ*/ /* WDTIS2+WDTIS0, that is, 101*/ WDTCTL=WDTPW+WDTCNTCL+WDTTMSEL+WDTIS2+WDTIS0 Summary of routines: /*First, set WDT to the timer function. The interrupt service subroutine changes WDT to the watchdog function. In this way, the LED flashes by periodically restarting the program controlled by interrupts*/ /*Note that the default clock source here is not 32KHZ, but the internal DCO-SMCLK (which will be discussed later) 1.045MHZ. Therefore, the time defined by WDTIS above should be reduced by about 1045/32=32 times*/ #include
void main(void) { /*Clear - set as counter - time set to 010 mode, i.e. 256S/32=8S*/ WDTCTL=WDTPW+WDTCNTCL+WDTTMSEL+WDTIS1; __enable_interrupt(); //Open general interrupt SFRIE1|=WDTIE; //Open watchdog timer interrupt P1DIR=0xff; int i,j; P1OUT=0xff; for(i=0;i<30000;i++) for(j=0;j<50;j++); //Delay about 8S P1OUT=0x00; while(1); } /*Interrupt service program*/ #pragma vector=WDT_VECTOR __interrupt void WatchTimer(void) { WDTCTL=WDTPW+WDTCNTCL+WDTIS1; //Watchdog mode, time setting is about 8S }