[ZigBee] 12、ZigBee之看门狗定时器——饿了就咬人的GOOD DOG

 

引言:硬件中的看门狗,不是门卫的意思,而是一只很凶的狗!如果你不按时喂它,它就会让系统重启!这反而是我们想要的功能~

 

1、看门狗概述

  看门狗定时器(WDT,Watch Dog Timer)是单片机的一个组成部分,它实际上是一个计数器,一般给看门狗一个数字,程序开始运行后看门狗开始倒计数。如果程序运行正常,过一段时间CPU应发出指令让看门狗复位,重新开始倒计数。如果看门狗减到0就认为程序没有正常工作,强制整个系统复位。因此可以用看门狗防止程序在跑飞的时候回不到正常模式。

  看门狗可用于受到电气噪音、电源故障、静电放电等影响的应用,或需要高可靠性的环境。如果一个应用不需要看门狗功能,可以配置看门狗定时器为一个间隔定时器,这样可以用于在选定的时间间隔产生中断。

  The features of the Watchdog Timer are as follows:

● 四个可选的定时器间隔
● 看门狗模式
● 定时器模式
● 在定时器模式下产生中断请求

 

  WDT 可以配置为一个看门狗定时器或一个通用的定时器。WDT 模块的运行由WDCTL 寄存器控制。看门狗定时器包括一个15 位计数器,它的频率由32kHz 时钟源规定。注意用户不能获得15 位计数器的内容。在all power modes下,15 位计数器的内容保留,且当重新进入active mode,看门狗定时器继续计数。

 

2、看门狗模式

  当系统重启后看门狗定时器失能。To start the WDT in watchdog mode, the WDCTL.MODE[1:0] bits must be set to 10. The Watchdog Timer counter then starts incrementing from 0. When the timer is enabled in watchdog mode, it is not possible to disable the timer. Therefore, writing 00 or 01 to WDCTL.MODE[1:0] has no effect if the WDT is already operating in Watchdog mode.

  WDT 运行在一个频率为32.768 kHz(当使用32 kHz XOSC时)的看门狗定时器时钟上。这个时钟频率的超时期限等于1.9ms,15.625 ms,0.25 s 和1s,分别对应64,512,8192 和32768 的计数值设置。

  当计数器达到设定的计数值时,看门狗会对系统产生一个reset信号,如果在计数器到达设定值之前执行了看门狗clear序列,counter的值会被重置为0,并会继续递增。看们狗的clear序列包含:writing 0xA to WDCTL.CLR[3:0], followed by writing 0x5 to the same register bits within one watchdog clock period. 如果这个完整序列不能在watch dog时期结束前完成,看门狗就会产生一个系统reset信号。

  When the WDT has been enabled in watchdog mode, it is 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、定时器模式

  To start the WDT in timer mode, the WDCTL.MODE[1:0] bits must be 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 is possible to clear the timer contents by writing a 1 to WDCTL.CLR[0]. When the timer is cleared, the content of the counter is set to 0. Writing 00 or 01 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 be set when the timer is started. In timer mode, a reset is not produced when the timer interval has been reached.

  注意:如果看门狗模式被选择,那只能等到芯片reset之后定时器模式才能被选择。

 

4、工程DEMO

main code:

 1 /****************************************************************************
 2 * 文 件 名: main.c
 3 * 描    述: 打开看门狗后,得记得喂狗,不然系统就会不停地复位了
 4 ****************************************************************************/
 5 #include <ioCC2530.h>
 6 
 7 typedef unsigned char uchar;
 8 typedef unsigned int  uint;
 9 
10 #define LED1 P1_0       // P1.0口控制LED1
11 #define LED2 P1_1       // P1.1口控制LED2
12 
13 
14 /****************************************************************************
15 * 名    称: DelayMS()
16 * 功    能: 以毫秒为单位延时 16M时约为535,系统时钟不修改默认为16M
17 * 入口参数: msec 延时参数,值越大,延时越久
18 * 出口参数: 无
19 ****************************************************************************/
20 void DelayMS(uint msec)
21 { 
22     uint i,j;
23     
24     for (i=0; i<msec; i++)
25         for (j=0; j<535; j++);
26 }
27 
28 /****************************************************************************
29 * 名    称: InitLed()
30 * 功    能: 设置LED灯相应的IO口
31 * 入口参数: 无
32 * 出口参数: 无
33 ****************************************************************************/
34 void InitLed(void)
35 {
36     P1DIR |= 0x03;      //P1.0、P1.1定义为输出
37     LED1 = 1;           //默认LED灯为熄灭状态
38     LED2 = 1;
39 }
40 
41 
42 void Init_Watchdog(void) 
43 { 
44     WDCTL = 0x00;       //打开IDLE才能设置看门狗
45     WDCTL |= 0x08;      //定时器间隔选择,间隔一秒
46 }
47 
48 void FeetDog(void) //喂狗
49 { 
50     WDCTL = 0xa0;       //清除定时器。当0xA跟随0x5写到这些位,定时器被清除
51     WDCTL = 0x50; 
52     LED2 = 0;           //系统不复位LED2灯长亮
53 }
54 
55 /****************************************************************************
56 * 程序入口函数
57 ****************************************************************************/
58 void main(void)
59 {
60     InitLed();        //调用初始化函数
61     Init_Watchdog();
62 
63     while(1)
64     {         
65         //LED1 = ~LED1;   //仅指示作用。
66         DelayMS(300);
67         LED2=0;
68                
69         //FeetDog();     //喂狗系统将不再主动复位,LED2长亮
70                        //注释FeetDog函数时系统不断复位,LED2灯闪烁
71     }
72 }

 其中42~46行用于看门狗初始化:

42 void Init_Watchdog(void) 
43 { 
44     WDCTL = 0x00;       //打开IDLE才能设置看门狗
45     WDCTL |= 0x08;      //定时器间隔选择,间隔一秒
46 }

第44行令WDCTL为0是因为下表WDCTL中关于MODE介绍——MODE[1:0]是用来模式选择,这两位用来在Watchdog模式或Timer模式启动看门狗定时器。其中有个主意说明“如果在timer模式想切换到看门狗模式,第一步需要停止WDT,然后才能在看门狗模式启动WDT。当处在看门狗模式,向这些位写数据是无效的”。因此向MODE中写00是使让看门狗处于IDLE模式(停止timer),接着才能启动WDT。

第45行是设置INT,选择超期时限为1s(即设置INT[1:0]=00)

 

 

第48~53行是喂狗的函数,其中第50、51行设置设置WDCTL为0xa0,紧接着设置为0x50,使一个clear序列,用于清除定时器(第2节红色字体部分由介绍)

48 void FeetDog(void) 
49 { 
50     WDCTL = 0xa0;       //清除定时器。当0xA跟随0x5写到这些位,定时器被清除
51     WDCTL = 0x50; 
52     LED2 = 0;           //系统不复位LED2灯长亮
53 }

 

因此,整个工程的意思是:初始化看门狗并设置1s种的喂狗期限,如果整个工程执行过程中超过1s不喂狗,看门狗timer就会产生一个系统reset信号让系统重置。因此代码中69行如果注释掉喂狗,带来的结果就是系统不断重启LED2不断闪烁;如果喂狗则LED2常亮。

 

 

Zigbee系列文章:

[ZigBee] 1、 ZigBee简介

[ZigBee] 2、 ZigBee开发环境搭建

[ZigBee] 3、ZigBee基础实验——GPIO输出控制实验-控制Led亮灭

[ZigBee] 4、ZigBee基础实验——中断

[ZigBee] 5、ZigBee基础实验——图文与代码详解定时器1(16位定时器)(长文)

[ZigBee] 6、ZigBee基础实验——定时器3和定时器4(8 位定时器)

[ZigBee] 7、ZigBee之UART剖析(ONLY串口发送)

[ZigBee] 8、ZigBee之UART剖析·二(串口收发)

[ZigBee] 9、ZigBee之AD剖析——AD采集CC2530温度串口显示

[ZigBee] 10、ZigBee之睡眠定时器

[ZigBee] 11、ZigBee之睡眠定时器二

 

 

PS:如果您觉得还不错,点个赞,让更多人受益~

@beautifulzzzz 2016-07-20 continue~  
e-mail:beautifulzzzz@qq.com 
sina:http://weibo.com/beautifulzzzz?is_all=1

 

posted @ 2016-07-20 22:45  beautifulzzzz  阅读(3527)  评论(4编辑  收藏  举报