AVR Watchdog interrupt mode and reset mode

avrctimerwatchdog

I am going through datasheet of AVR2560V. In page 65 , table 12-1, it says that AVR2560 has 3 mode for watchdog

  1. Interrupt mode - WDE = 0, WDIE = 1
  2. Reset Mode - WDE = 1, WDIE = 0
  3. Both - WDE = 1, WDIE = 1

This means if we want to use watchdog timer as source of Interrupt, then i should disable WDE. Am i right? Because when i disabling WDE in code, Watchdog timer is getting disabled.I had look at this link also and even he is doing the same. WDE make sense that it need to be enabled, just that table in the datasheet is confusing me. Can anyone please help me in clearing out this doubt.

Below is the Watchdog_init function which i am using.

 Watchdog_init(){
        MCUSR &= ~(1<<WDRF);

        WDTCSR |= (1<<WDCE) | (1<<WDE); // as per datasheet WDE should be 0?

        // timeout in 8 second
        WDTCSR = 1<<WDP0 | 1<<WDP3;

        // watchdog interrupt enabler
        WDTCSR |= _BV(WDIE);
    }

Best Answer

You have multiple writes to WDTCSR:

  1. Set WDCE and WDE, leaving the rest garbage
  2. Set WDP0 and WDP3, and clear the rest
  3. Set some other bits according to the _BV macro, leaving the rest as they were

So your step 1 is getting wiped out by step 2.

I'd suggest combining all three into a single assignment.