Small knowledge, big challenge! This article is participating in the creation activity of “Essential Tips for Programmers”.

I. Introduction of the watchdog

Watchdog can be used in normally designed products. It is mainly used to solve problems caused by program running, abnormal crash and so on. It can automatically reset and restart the program.

The watchdog itself is a timer, much like a regular timer. Set a fixed timeout period. Normally, the program will reset the time before the watchdog expires (this is called feeding the dog). When the program works properly, there is no timeout because the dog is fed every time. If the program is unable to feed the dog within the specified time due to logic problems, the watchdog timer will time out, and the software will be reset and the program will restart, so that the product can avoid downtime. This kind of restart usually resolves some of the bugs that happen. Just like our common computers, if they get stuck or have some weird problem, rebooting almost always solves it.

Here is the introduction of CC2530 watchdog program configuration, the specific configuration of direct screenshots out. In the manual, the initial introduction has been very detailed. As long as you understand the function and meaning of the watchdog, you can directly look at the code in Chapter 3 to practice, and analyze the configuration of the code to read the manual again.

Two, watchdog configuration

Three, sample code

#include <ioCC2530.h>

#define uint unsigned int

#define RLED  P1_0
#define GLED  P1_1

void CC2530_InitLEDIO(void)
{
	P1DIR |= 0x03;  //P10, P11 are defined as outputs
	RLED = 1;
	GLED = 1;
	// The LED is initialized to off
}

void CC2530_Init_Watchdog(void)
{
	WDCTL = 0x00;
	// Time interval one second, watchdog mode
	WDCTL |= 0x08;
	// Start the watchdog
}

void  CC2530_SET_MAIN_CLOCK(source) 
{                                  
	if(source) 
	{                            
		CLKCONCMD |= 0x40;          /*RC*/               
		while(! (CLKCONSTA &0X40));  / * to * /       
	}                                       
	else
	{                       
		CLKCONCMD &= ~0x47;        / * * / crystal vibration             
		while((CLKCONSTA &0X40));  / * to * /}}void CC2530_FeetDog(void)
{
	WDCTL = 0xa0;
	WDCTL = 0x50;
}
void CC2530_Delay(uint n)
{
	uint i;
	for(i=0; i<n; i++);for(i=0; i<n; i++);for(i=0; i<n; i++);for(i=0; i<n; i++);for(i=0; i<n; i++); }void main(void)
{
	CC2530_SET_MAIN_CLOCK(0);CC2530_InitLEDIO(a);CC2530_Init_Watchdog(a);CC2530_Delay(10000);
        
	RLED=0;	       
	GLED=0;       
	while(1)
	{
		CC2530_FeetDog(a);// Implement the watchdog test by masking the command
	}	// Feed the dog command (after joining the system is no longer in position, the small light does not blink)
}
Copy the code