The Watchdog Timer (WDT_A) is actually a special timer that can be used as a watchdog or a timer.
The so-called watchdog function is to monitor whether the program runs away due to some interference or error. The principle is that when the time of the fault meets the specified timing, 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 timer interrupt can prevent the program from running away.
Of course, it can also be used as a general timing function.
But in fact, because 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;
⑥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 period is 32ms/32.768KHZ (that is, when the watchdog clock frequency is 32.768KHZ, the program will restart every 32ms if it is not cleared by the software); the clock source used is SMCLK (the actual frequency is not 32.768KHZ, which will be mentioned later).
2.2 WDT_A registers and operations
Note: All registers have word operation and byte operation modes. For example, directly assigning a value to the WDTCTL register is a word operation, and you can also assign a value to a byte through WDTCTL_L (low byte register) and WDTCTL_H.
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 of this register is defined as follows: (bit operation is supported)
WDTPW: Bits 15-8, WDT Password, written as 05Ah, read as 069h.
WDTHOLD: Bit7, WDT HOLD,
0: turn on the watchdog timer;
1: Off
WDTSSEL: Bits6-5, WDT Clock Source Select clock source selection
WDTTMSEL: Bit4, working 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 Timer Counter Register WDTCNT (Watchdog Timer Counter)
This is a 32-bit up counter, but it cannot be directly assigned values by software. You can only select the time 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
The WDT uses 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 the program will restart.
Timer mode: The general interrupt GIE and the watchdog interrupt WDTIE must be enabled. In addition, after the interrupt service routine is executed, the flag WDTIFG will be automatically cleared.
2.3 Common Operations
/*turn off watchdog*/
WDTCTL=WDTPW+WDTHOLD;
/*Feed the watchdog, that is, clear the watchdog in the valid state*/
/* If the timer is full and the dog is not fed, the program will restart*/
/*WDTIS2 means WDTIS=100, the time interval is set to 1S, and the frequency is assumed to be 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 is 101*/
WDTCTL=WDTPW+WDTCNTCL+WDTTMSEL+WDTIS2+WDTIS0
Summary of the routine:
/*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 the interrupt*/
/*Note that the default clock source here is not 32KHZ, but the internal DCO-SMCLK (will be discussed later) 1.045MHZ. So the time defined by WDTIS above should be reduced by about 1045/32=32 times*/
#include <msp430.h>
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 the general interrupt
SFRIE1|=WDTIE; //Enable 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 routine*/
#pragma vector=WDT_VECTOR
__interrupt void WatchTimer(void)
{
WDTCTL=WDTPW+WDTCNTCL+WDTIS1; //Watchdog mode, time setting is about 8S
}
|