The AVR watchdog is a hardware unit that acts as a safety mechanism. If the program "flies" off course due to an error, it will automatically reset the system. Think of it like a puppy watching the door — if the owner doesn't return in time, the puppy might get restless and cause a disturbance.
// Watchdog Initialize
// prescale: 2048K
void watchdog_init(void)
{
WDR(); // this prevents a timeout on enabling
WDTCR = 0x0F; // WATCHDOG ENABLED - don't forget to issue WDRs
}
The above code is generated by ICC's App Builder. However, it does not fully initialize the watchdog correctly. To fix this, you should add `WDTCR = 0x1F` before setting `WDTCR = 0x0F`. The final line reminds the programmer to "feed the dog" regularly. If you fail to do so, the watchdog will trigger a reset, just like a puppy biting when left alone too long.
The watchdog operates as a relatively independent counter that automatically restarts the microcontroller if it is not cleared within a certain period. When the counter reaches its maximum value and overflows, the microcontroller resets. Therefore, it’s crucial to include proper watchdog instructions in your program. If your MCU gets stuck or crashes, the watchdog will restart it, assuming the problem is temporary. However, for persistent issues like long-term interference or software/hardware bugs, the watchdog may not be effective.
In my understanding, the watchdog acts as a security monitor for the program's execution. As long as the program runs normally, it must send a reset signal within a specific time window. If the program goes off track and fails to send this signal, the watchdog will trigger a system reset.
The AVR watchdog can function as both a soft and hard watchdog. If the fuse is not set, it behaves as a soft watchdog, meaning it can be enabled or disabled through software. However, once the fuse is set, it becomes a hard watchdog, which cannot be turned off — only cleared.
Is the `WDR()` function used to "feed the dog"? Feeding the dog seems to involve timing. How often should you feed the dog after each function call?
If the cycle time of your program is shorter than the watchdog's timeout, feeding the dog once per loop is sufficient.
Here’s an example of an AVR watchdog program that demonstrates the reset process. Under normal conditions, the program falls into an infinite loop, but with the watchdog enabled, the microcontroller will reset periodically. You’ll see the LED flashing continuously, similar to other sample programs.
There are two key points: first, initialize the watchdog, then "feed the dog" before it triggers a reset. This ensures the watchdog doesn’t activate prematurely.

// Watchdog reset demo program
#include
#include
void port_init(void)
{
PORTA = 0x03; // set to output
DDRA = 0x03; // high level, both LEDs are off
PORTB = 0x00;
DDRB = 0x00;
PORTC = 0x00;
DDRC = 0x00;
PORTD = 0x00;
DDRD = 0x00;
}
// Watchdog Initialize
// prescale: 2048K (the larger the prescaler, the longer the timing)
// about 2.1s reset (according to data sheet, 2048K, 5V typical value)
void watchdog_init(void)
{
WDR(); // this prevents a timeout on enabling
WDTCR = 0x1F; // special attention: not generated by ICC, added later
WDTCR = 0x0F; // WATCHDOG ENABLED - don't 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 = 0x00;
GICR = 0x00;
TIMSK = 0x00; // 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 = 0x02; // 1 pin is high, 0 pin is low, 0 pin is lit
Delay(); // delay
PORTA = 0x01; // 0 is high, 1 is low, 1 is lit
Delay(); // delay
}
while(1) // under normal circumstances, the program will fall into this loop
{
// The watchdog can reset the microcontroller, the program re-runs, and we see the LED flashing.
// If you add WDR() here, the microcontroller will not reset.
}
}
**Supplement, Special Notes:**
Enabling the watchdog must be done via direct assignment (`=`), not using bitwise OR (`|=`). Also, according to the datasheet:
- Changing the timer overflow time and disabling (already enabled) the watchdog timer requires a specific sequence:
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 write WDCE to "0".
So before `WDTCR = 0x0E;`, add `WDTCR = 0x1F;`.
void watchdog(void)
{
WDR(); // watchdog count clear
}Indoor Rental LED Display
Indoor Rental LED Display is hot selling product in the led screen market. We usually use Nova MSD300 sending card and MRV328 receving card, other controll system also can be accepted, like Linsin,colorlight and so on.....About the led lamp, we use kinglight led lamp, IC is ICN2038S, refresh rate is 1920hz. We also provide other option if the client need higher quality, like Nationstar led lamp and refresh rate can make 3840hz. This 500x500mm LED display panel can also can make curved led display, ±15° flexible curved option.
Application:
* Business Organizations:
Supermarket, large-scale shopping malls, star-rated hotels, travel agencies
* Financial Organizations:
Banks, insurance companies, post offices, hospital, schools
* Public Places:
Subway, airports, stations, parks, exhibition halls, stadiums, museums, commercial buildings, meeting rooms
* Entertainments:
Movie theaters, clubs, stages.
Indoor Rental LED Display,Advertising Led Display,Indoor Led Display,Rental Led Display
Guangzhou Chengwen Photoelectric Technology co.,ltd , https://www.cwleddisplay.com