AVR watchdog use example, the reset program detailed

AVR Watchdog A hardware unit that, when the program ran for a reason, "fly", it resets the program . Just like a puppy watching a door.

//Watchdog iniTIalize

// prescale: 2048K

Void watchdog_init(void)

{

WDR(); //this prevents a timout on enabling

WDTCR = 0 x0F; //WATCHDOG ENABLED - dont forget to issue WDRs

}

The above is the watchdog initialization program generated by ICC's App Builder. These statements do not achieve the purpose of initializing the watchdog. You need to add a WDTCR = 0 x1F in the middle. The last line of code reminds the dog owner, don't forget to clear the watchdog timer (feed the dog) in time, otherwise the puppy will bite.

A relatively independent count automatically restarts the hardware components of the microcontroller. If it is enabled, it will not clear its count value within a certain period of time, it will reach the highest value of the count and overflow, and then it will direct the microcontroller to restart. So to properly add the watchdog instructions in your program, once your MCU program has a problem, of course, you can't automatically clear the watchdog as your program originally set, which is often said. Run away, this time the watchdog will restart the microcontroller to try to solve the problem. Generally, it is only effective for problems caused by transient interference. If it is a long-term interference or hardware and software problem, the significance of the watchdog is not great.

My understanding is like monitoring the security of the execution of the program. When the program is executed normally, it will give him a reset signal within his beneficial time. When the program runs away, he will not receive the reset signal within the overflow time. At this time, the watchdog will generate a system reset signal within the set time!

The AVR watchdog is a soft dog and a hard dog! If the fuse is not set, it is a soft dog, because the program can be turned off, it can also be turned on. If the fuse is set, it is a hard dog, because the program can only be cleared. Can't open or close!

Is WDR() added to the program; even if you "feed the dog"? Feeding the dog seems to calculate the time? How do I feed the dog every time I perform a function?

If the cycle time of your cycle does not exceed the reset time of the watchdog, it is enough to feed the dog once.

AVR watchdog program example, the program demonstrates the reset process of the watchdog, using the first example of the newcomer to the site, under normal circumstances, the program finally falls into an infinite loop, but in this program, the watchdog resets the microcontroller. You will see the LED flashing all the time, the same effect as the first sample program.

At two points, initialize, then feed the dog, feed the dog before the watchdog bites, (feed the dog before resetting).

// Watchdog reset demo program.

#include

#include

Void port_init(void)

{

PORTA = 0 x03; // set to output

DDRA = 0 x03; //High level, both LEDs are off

PORTB = 0 x00;

DDRB = 0 x00;

PORTC = 0 x00;

DDRC = 0 x00;

PORTD = 0 x00;

DDRD = 0 x00;

}

//Watchdog initialize

// prescale: 2048K //The larger the prescaler, the longer the timing, that is, it can be used for a longer time.

// about 2.1s reset (according to data sheet, 2048K, 5V typical value)

Void watchdog_init(void)

{

WDR(); //this prevents a timout on enabling

WDTCR = 0 x1F; //Special attention to this is not generated by ICC, it is added later.

WDTCR = 0 x0F; //WATCHDOG ENABLED - dont forget to issue WDRs

}

/ / Added a dog delay program

Void Delay(void)

{

Unsigned char i,j;

For(i=200;i>0;i--)

{

For(j=200;j>0;j--)

;

}

WDR(); //feed the dog here

}

//call this routine to initialize all peripherals

Void init_devices(void)

{

//stop errant interrupts until set up

CLI(); //dISAble all interrupts

Port_init();

Watchdog_init();

MCUCR = 0 x00;

GICR = 0 x00;

TIMSK = 0 x00; //timer interrupt sources

SEI(); //re-enable interrupts

//all peripherals are now initialized

}

Void main(void)

{

Unsigned int i;

Init_devices(); //initialize

For(i=10;i>0;i--) //See the flashing of the program

{

PORTA = 0 x02; //1 pin is high, 0 pin is low, 0 pin is lit

Delay(); //delay

PORTA = 0 x01; //0 is high, 1 is low, 1 is lit

Delay(); //delay

}

While(1) // Under normal circumstances, the program will fall into this loop all the time.

//The watchdog can reset the microcontroller, the program re-runs, and we see the LED flashing.

//If you add WDR() here; feed the dog, the microcontroller will not reset.

}

Supplement, special instructions

Enable the watchdog can not use |=, must be directly assigned =.

There is also a data sheet saying:

Changing the timer overflow time and disabling (already enabled) the watchdog timer requires a specific time series:

1. Write "1" to WDCE and WDE in the same instruction, even if WDE is already "1".

2. Set WDE and WDP to the appropriate values ​​within the next 4 clock cycles, and WDCE writes "0".

So before WDTCR=0 x0E; add a WDTCR=0 x1F;

Void watchdog(void)

{

WDR (); / / watchdog count clear

WDTCR=0 x1F; //Enable watchdog, and use 2048K crossover, typical overflow time 5V 2.1S

WDTCR=0 x0F; //Enable watchdog, and use 2048K crossover, typical overflow time 5V 2.1S

}

57 Series Centronic Connectors

Current Rating:5A
Dielectric Withstanding Voltage:1000V for one minute
Insulation Resistance:1000MΩ Min.(at 500V DC)
Contact Resistance:35mΩ Max.
Temperature:-55°C to +105°C



57 Series Centronic Connectors

ShenZhen Antenk Electronics Co,Ltd , https://www.antenksocket.com

This entry was posted in on