Introduction: The watchdog in hardware is not the gatekeeper's idea, but a very fierce dog! If you don't feed it on time, it's going to restart the system! This is the function we want ~
1. Watchdog overview
The watchdog timer (Wdt,watch dog timer) is an integral part of the microcontroller, it is actually a counter, generally give the watchdog a number, the program starts to run after the watchdog starts to pour count. If the program is working properly, over a period of time the CPU should issue a command to reset the watchdog and start counting again. If the watchdog is reduced to 0, the program does not work properly, forcing the entire system to reset. The watchdog can therefore be used to prevent the program from returning to normal mode while running.
The watchdog can be used for applications that are affected by electrical noise, power failure, electrostatic discharge, or require a high reliability environment. If a watchdog function is not required for an application, the watchdog timer can be configured as an interval timer, which can be used to generate interrupts at selected intervals.
The features of the Watchdog Timer is as follows:
Four selectable timer intervals
Watchdog mode
Timer mode
Interrupt request generated in timer mode
WDT can be configured as a watchdog timer or as a universal timer. The operation of the WDT module is controlled by the WDCTL register. The watchdog timer includes a 15-bit counter, which is specified by the 32kHz clock source. Note The user cannot get the contents of the 15-bit counter. Under all power modes, the contents of the 15-bit counter remain, and when you re-enter active mode, the watchdog timer continues to count.
2. Watchdog mode
When the system restarts, the watchdog timer loses energy . To-start the WDT in watchdog mode, the Wdctl. MODE[1:0] Bits must is set to ten. The Watchdog Timer counter then starts incrementing from 0. When the timer was enabled in watchdog mode, it was not possible to disable the timer. Therefore, writing or Wdctl. MODE[1:0] have no effect if the WDT is already operating in Watchdog mode.
WDT operates on a watchdog timer clock at a frequency of 32.768 khz (when using a khz Xosc). The time-out period for this clock frequency is equal to 1.9ms,15.625 ms,0.25 s and 1s, respectively, corresponding to the 64,512,8192 and 32768 count value settings.
When the counter reaches the set count value, the watchdog generates a reset signal to the system, and if the watchdog clear sequence is executed before the counter reaches the setpoint, the value of the counter is reset to 0 and continues to increment. Look at the dog's clear sequence contains:writing 0xA to Wdctl. CLR[3:0], followed by writing 0x5 to the same register bits within one watchdog clock period. If the complete sequence cannot be completed before the end of the watch dog period, the watchdog generates a system reset signal.
When the WDT had been enabled in watchdog mode, it was not possible to change the mode by writing to the Wdctl. MODE[1:0] bits and the timer interval value can not be changed.
3. Timer mode
To-start the WDT in timer mode, the WDCTL. MODE[1:0] Bits must bes set to 11. The timer is started and the counter starts incrementing from 0. When the counter reaches the selected interval value, the timer produces an interrupt request (IRCON2. Wdtif/ien2. Wdtie).
In timer mode, it's possible to clear the timer contents by writing a 1 to wdctl. Clr[0]. When the timer is cleared, the content of the counter are set to 0. Writing or to Wdctl. MODE[1:0] Stops the timer and clears it to 0.
The timer interval is set by the wdctl.int[1:0] bits. The interval cannot be changed during timer operation, and should are set when the timer is started. In timer mode, a reset is not produced when the timer interval have been reached.
Note: If the watchdog mode is selected, then the timer mode can only be selected after the chip reset.
4. Project Demo
Main code:
1 /****************************************************************************2 * File name: main.c3 * Description: After you open the watchdog, you have to remember to feed the dog, or the system will be constantly reset4 ****************************************************************************/5#include <ioCC2530.h>6 7typedef unsignedCharUchar;8typedef unsignedint UINT;9 Ten #defineLED1 P1_0//P1.0 Port Control LED1 One #defineLED2 p1_1//P1.1 Port Control LED2 A - - /**************************************************************************** the * Name: delayms () - * Function: Time delay 16M in milliseconds is about 535, system clock does not modify default to 16M - * Entry parameters: msec delay parameter, the higher the value, the longer the delay - * Export parameters: None + ****************************************************************************/ - voidDelayms (UINTmsec) + { A UINTi,j; at - for(i=0; i<msec; i++) - for(j=0; j<535; J + +); - } - - /**************************************************************************** in * Name: initled () - * Function: Set the corresponding IO port of the LED lamp to * Entry parameters: None + * Export parameters: None - ****************************************************************************/ the voidInitled (void) * { $P1dir |=0x03;//P1.0, P1.1 defined as outputPanax NotoginsengLED1 =1;//default LED light is off -LED2 =1; the } + A init_watchdog void (void) , wdctl = 0x0 0; // open Idle To set the watchdog wdctl |= 0x08; // timer interval selection, interval of one second + } - feetdog void (void)//Feed dog { wdctl = 0xa0< /c9>; // clears the timer. When 0xA follows 0x5 write to these bits, the timer is cleared by wdctl = 0x50; LED2 = 0; // system no longer LED2 light + } Wu - /**************************************************************************** About * Program Entry function $ ****************************************************************************/ - voidMainvoid) - { -Initled ();//Call initialization function A Init_watchdog (); + the while(1) - { $LED1 = ~led1;//indicates only the function. theDelayms ( -); theLed2=0; the the //Feetdog (); //Feed the dog system will no longer active reset, LED2 long bright - //Note Feetdog function when the system is constantly reset, LED2 lights flashing in } the}
Where the 42~46 line is used for watchdog initialization:
Init_watchdog void (void{ wdctl = 0x00; Open Idle to set the watchdog wdctl |= 0x08; }
44th Merriness Wdctl is 0 because the following table Wdctl about mode introduction--mode[1:0] is used for mode selection, which two bits are used to start the watchdog timer in watchdog mode or timer mode. One of the ideas is that "if you want to switch to watchdog mode in timer mode, the first step is to stop WDT before you can start WDT in watchdog mode." When in watchdog mode, writing data to these bits is invalid. " So write 00 to mode so that the watchdog is in idle mode (stop timer), then the WDT can be activated.
The 45th line is set int, select the extended time limit is 1s (that is, set int[1:0]=00)
Line 48th to 53rd is the function to feed the dog, where the 50th, 51 lines set Wdctl to 0xa0, and then set to 0x50, so that a clear sequence is used to clear the Timer (section 2nd of the Red font is introduced)
void Feetdog (void{ wdctl = 0xa0; Clears the timer. When 0xA follows 0x5 write to these bits, the timer is cleared wdctl = 0x50; LED2 = 0; }
Therefore, the whole project means: Initialize the watchdog and set the 1s dog feeding period, if the whole project is performed over 1s without feeding the dog, the watchdog timer will generate a system reset signal to let the system reset. Therefore, 69 lines in the code if the comment out feed the dog, the result is the system constantly restart LED2 flashing, if the dog feed LED2 solid.
ZigBee Series Articles:
[ZigBee] 1, ZigBee introduction
[ZigBee] 2, ZigBee development environment construction
[ZigBee] 3, ZigBee basic experiment--gpio Output control experiment-control LED light off
[ZigBee] 4, ZigBee basic experiment--interrupt
[ZigBee] 5, ZigBee basic Experiment--graphics and code detailed timer 1 (16-bit timer) (long text)
[ZigBee] 6, ZigBee basic Experiment--timer 3 and Timer 4 (8-bit timer)
[ZigBee] 7, ZigBee analysis of the UART (only serial transmission)
[ZigBee] 8, ZigBee of the UART analysis • Two (serial transceiver)
[ZigBee] 9, ZigBee AD Analysis--ad collection CC2530 temperature serial display
[ZigBee] 10, ZigBee sleep Timer
[ZigBee] 11, ZigBee Sleep Timer two
PS: If you feel good, click a praise, let more people benefit ~
@beautifulzzzz 2016-07-20 continue~
e-mail:[email protected]
Sina:http://weibo.com/beautifulzzzz?is_all=1
[ZigBee] 12, ZigBee watchdog timer--Bite the good dog when hungry