Forum Replies Created

Viewing 1 post (of 1 total)
  • Author
    Posts
  • in reply to: WDT Reset with LowPower.h? #8577
    adam.g
    Participant

    Thanks for your reply!

    After I made this post, I spent the next 48 hours studying the Watchdog Timer and the AVR wdt.h, sleep.h, power.h and interrupt.h libraries to get a better understanding of how everything worked. I was able to implement a simple solution with the AVR libraries that uses the WDT as an interrupt timer with a loop in the ISR that will check a flag to determine if the program is actually asleep. If it detects the program has frozen, it will enable the Watchdog System Reset Enable bit in the WDTCSR register. It’s working well so far and I now see how I could use the LowPower.h library to simplify the sleep commands.

    Cheers,
    Adam

    
    ISR(WDT_vect)
    {
      wdtIsrWasCalled = true;
      // Check if system reset is required
      if (sleeping == true)
      {
        wdt_reset();
        sleeping = false;
      }
      else
      {
        // Enable WDT System Reset Mode
        MCUSR = 0;                          // Clear Reset Flags of MCU Status Register (MCUSR)
        WDTCSR |= (1 << WDCE) | (1 << WDE); // Start timed sequence allowing changes to Watchdog Timer Control Register (WDTCSR)
        // Clear Watchdog Interrupt Enable (WDIE), set Watchdog System Reset Enable (WDE) and clear Watchdog Timer Prescalers WDP3, WDP2, WDP1 and WDP0 to select a 16 ms timeout period
        WDTCSR = (0 << WDIE) | (1 << WDE) | (0 << WDP3) | (0 << WDP2) | (0 << WDP1) | (0 << WDP0); 
        while (1);                          // System reset will occur after 16 ms
      }
    }
    
Viewing 1 post (of 1 total)