The new AVR series (for example, ATMEGA168, etc.) uses an enhanced watchdog timer. Compared with the original watchdog, it has the same reset function and an interrupt function. The interrupt function of the watchdog can wake up the CPU from the Power Down state. In addition, it can use the interrupt and reset functions at the same time. When an error occurs in the system, it can first enter the interrupt state, save key data to the EEPROM, and then reset the system. Compared with the old watchdog (for example, ATMEGA16, etc.), the new AVR watchdog has added new functions and its temperament has also changed a lot. You should understand its temperament when designing to prevent being bitten by the dog. The following is a simple comparison between ATMEGA16 and ATMEGA168 to briefly explain the key points: Watchdog Timer Control Register – WDTCR: Atmega16:
1. Since A1tmega168 has interrupt function, WDTCR adds two extra bits WDIF and WDIE. 2. WDP3 is added, and the overflow period can be up to 8 seconds. 3. When the new watchdog is initialized, the value of WDT is no longer necessarily 0. As long as WDRF on the MCUSR register is set, WDT will also be forced to set. At this time, if WDE is to be cleared, WDRF must be cleared first. 4. Even if WDT is not used during design, it may be enabled unintentionally, such as in the runaway pointer and power-on condition. Therefore, even if WDT is not used, the reset flag must be checked in the initial code, and processing steps must be taken if a WDT system reset occurs. I recently redesigned a project, updating from the original ATMEGA16 to ATMEGA168. After converting the original code, the test results were abnormal. After more than a day of testing, I found that the new watchdog was the cause of the problem. In the past, when using ATMEGA16, the watchdog was fed after initialization, but with the new AVR series (such as ATMEGA168, etc.), when a system reset occurs (except for power-on reset), the WDT may continue to run and use the minimum overflow period (about 15mS), causing the system to reset continuously. According to the datasheet, everything returns to normal after turning off the watchdog at the beginning of the program: #include
#include
uint8_t mcusr_mirror; void get_mcusr(void) \ __attribute__((naked)) \ __attribute__((section(".init3"))); void get_mcusr(void) { mcusr_mirror = MCUSR; MCUSR = 0; wdt_disable(); } 5. When using the watchdog interrupt, when WDIF and WDIE are set, an interrupt will be generated. After entering the corresponding interrupt program, the system automatically clears WDIF and WDIE. The next time you want to use the watchdog interrupt, you must set WDIE again.