This article is a mirror article of machine translation, please click here to jump to the original article.

View: 9306|Reply: 0

Zigbee Journey (8): Several important CC2430 basic experiments - watchdogs

[Copy link]
Posted on 10/30/2014 11:23:37 PM | | | |
1. Connecting the upper and lower levels

No matter how good the operating system, whether it is the current Win7 or the future Win8 and Win9, there will always be a BlueScreen, not to mention a small microcontroller~ Unpredictable reasons such as electrical noise, power failure, electrostatic discharge, etc., may cause abnormal operation of the embedded system.

The Watch Dog, to be precise, should be a watchdog timer, which is a circuit structure specially used to monitor the running status of the microcontroller program. The basic principle is: after starting the watchdog timer, it will start counting from 0, if the program does not clear it in time within the specified time interval, the watchdog timer will reset the system (equivalent to restarting the computer), as shown in the figure below (drawn in word, the drawing is more eggache~):

Let's introduce a simple guard dog application method: How to let a dog go? How to feed? What will happen if you don't feed it?

2. The story of the watchdog(1) Introduction to the experiment

If the dog is fed, the system is running normally; If you don't feed the dog, the system keeps restarting.

(2) Program flow chart

(3) Experimental source code and analysis/*
    Experiment description: In the watchdog experiment, if the dog is not fed, the system keeps restarting.
*/

#include

#define led1 P1_0         
#define led2 P1_1         
#define led3 P1_2         
#define led4 P1_3   

/*系统时钟初始化
-------------------------------------------------------*/
voidxtal_init(void)
{
  SLEEP &= ~0x04;            //都上电
  while(! (SLEEP &0x40));     //晶体振荡器开启且稳定
  CLKCON &= ~0x47;            Choose a 32MHz crystal oscillator
  SLEEP |=0x04;
}

/*LED初始化
-------------------------------------------------------*/
voidled_init(void)
{
  P1SEL  =0x00;              P1 is the normal I/O port
  P1DIR |=0x0F;              P1.0 P1.1 P1.2 P1.3 output
  
  led1 =1;                  //关闭所有LED
  led2 =1;
  led3 =1;
  led4 =1;
}

/*看门狗初始化
-------------------------------------------------------*/
voidwatchdog_Init(void)   
{
  WDCTL =0x00;              //看门狗模式,时间间隔一秒
  WDCTL |=0x08;            //启动看门狗
}

/*喂狗程序
-------------------------------------------------------*/
voidFeetDog(void)   
{
  WDCTL =0xa0;
  WDCTL =0x50;
}

/*延时函数(小于1秒。读者可以想一下,若大于1秒,会出现什么情况)
-------------------------------------------------------*/
voidDelay(void)
{
  unsigned intn;
  for(n=50000; n>0; n--);
  for(n=50000; n>0; n--);
  for(n=50000; n>0; n--);
  for(n=50000; n>0; n--);
  for(n=50000; n>0; n--);
  for(n=50000; n>0; n--);
  for(n=50000; n>0; n--);
}

/*主函数
-------------------------------------------------------*/
voidmain(void)
{
  xtal_init();  
  led_init();
  watchdog_Init();

  Delay();
  led1 =0;         //点亮led1
  
  while(1)
  {
    FeetDog();      Dog feeding command (the system does not reset after joining, and the small light does not flash; If annotated, the system will continue to reset, and the small light will flash every 1s)
  }
}

As you can see from the source code above, the operation method of Watch Dog is very simple, and the whole process involves only a new SFR, namelyWDCTL。The following is a specific description of CC2430 in the Chinese manual:

The use of watchdogs can be summarized as:Select Mode → Select Timer Interval → Put the Dog → Feed the Dog

(1) Select mode:

The watchdog timer has two modes, namely "watchdog mode" and "timer" mode.

In timer mode, it is equivalent to a normal timer, and when the timer interval is reached, it will produce an interrupt (you can find it in the ioCC2430.h file with an interrupt vector of WDT_VECTOR); In watchdog mode, when a scheduled interval is reached, there is no interruption, instead a reset signal is sent to the system.

In this experiment, it passedWDCTL. MODE=0to select the gatekeeper mode.

  (2) Select the scheduled interval:

As shown in the figure above, there are four clock periods to choose from, and for testing convenience, we choose the time interval of 1s (i.e., order).WDCTL.INT=00)。

  (3) Release the dog:

OrderWDCTL.EN=1to start the watchdog timer.

  (4) Feed the dog:

Once the timer starts, it starts counting from 0. Before its count reaches 32768 (i.e. <1s), if we feed the dog with the following code:

  WDCTL =0xa0;
  WDCTL =0x50;  

Then the count value of the timer will be cleared to 0, and then it will start counting from 0x0000 again, which prevents it from sending a reset signal, which is manifested on the development board: LED1 will always be on and will not flash;

If we don't feed the dog (i.e., comment out this code), then when the timer count reaches 32768, a reset signal will be issued, and the program will run from scratch, which is manifested on the development board: LED1 is constantly flashing, and the flashing interval is 1s. (Note: The dog feeding program must be strictly consistent with the above code, and the order is reversed/wrong/written less will not play a role in clearing 0.) )

CC2430 Tips
Two additional notes:
(1) InWatch dog modeIf the watchdog timer has been enabled, setting 0 to WDCTL.EN is invalid (i.e., this bit cannot play the role of stopping the timer);
(2) InTimer mode, you can write 1 to WDCTL.CLR[0] to clear the timer; Writing 0 to the enable bit WDCTL.EN will stop the timer, while writing 1 to the enable bit will restart the timer from 0x0000 on.


(4) Experimental results:

If you add the FeedDog function, run the code, and find that LED1 is always on (the system does not reset);

If you comment out the FeedDog function, run the code and find that LED1 flashes at 1s intervals (the system resets every 1s).

3. Conclusion

This section describes the principle and usage of watchdog timing circuits. In practical applications, if high reliability is required, the watchdog can be used in the system. When the system goes down for some reason (it can't feed the dog anymore), the dog that no one feeds will bark: "Master, there is an anomaly, there is an anomaly! ”

For a wireless sensor network, its runtime power consumption is a critical aspect of performance evaluation. In the next section, we will discuss CC2430 system sleep and its interrupted wake-up call.






Previous:Zigbee Journey (7): Several important CC2430 basic experiments - DMA transmission
Next:Zigbee Journey (9): Several important basic CC2430 experiments - systematic sleep and interrupted wakefulness
Disclaimer:
All software, programming materials or articles published by Code Farmer Network are only for learning and research purposes; The above content shall not be used for commercial or illegal purposes, otherwise, users shall bear all consequences. The information on this site comes from the Internet, and copyright disputes have nothing to do with this site. You must completely delete the above content from your computer within 24 hours of downloading. If you like the program, please support genuine software, purchase registration, and get better genuine services. If there is any infringement, please contact us by email.

Mail To:help@itsvse.com