Reset Bits after WDT Setup

Go To Last Post
5 posts / 0 new
Author
Message
#1
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Hi there,

I'm making my Arduino periodically wake up using the WDT and 'SLEEP_MODE_PWR_DOWN" and I'm using this bit of code to set up the WDT bits:

 

void WDT_setup() {
  MCUSR &= ~(1<<WDRF); // Clear the reset flag
  // In order to change WDE or prescaler, we need to set WDCE
  // (this will allow updates for 4 clock cycles).
  WDTCSR |= (1<<WDCE) | (1<<WDE);
  WDTCSR = 1<<WDP0 | 1<<WDP3; // Set watchdog timeout prescaler value, 8s
  WDTCSR |= _BV(WDIE); // Enable the WD interrupt (note no reset)
}

 

However, my question is, if while the code is running, I wanted to sleep the Arduino indefinitely instead of periodically waking up, how do I clear those WDT settings back to default so that these commands

 

set_sleep_mode(SLEEP_MODE_PWR_DOWN);
sleep_enable();
sleep_mode();

 

make it sleep indefinitely without waking up until the RESET button is pressed?

 

The datasheet talks about the MCUSR and WDTCSR registers on page 54, but I don't know exactly what sequence of bits to clear, etc.

Thanks!

Last Edited: Fri. Sep 22, 2017 - 02:02 AM
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0
#include <avr/wdt.h>
.
.
.
    wdt_disable();

 

"Experience is what enables you to recognise a mistake the second time you make it."

"Good judgement comes from experience.  Experience comes from bad judgement."

"When you hear hoofbeats, think horses, not unicorns."

"Fast.  Cheap.  Good.  Pick two."

"Read a lot.  Write a lot."

"We see a lot of arses on handlebars around here." - [J Ekdahl]

 

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Hah, it's that easy? I was also thinking 

 

WDTCSR = 0x00; // Turn off WDT

but wasn't sure if there was more to it. Thanks!

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

The example at the bottom of page 52 shows something like this:

 

void disable_WDT() {
  MCUSR &= ~(1<<WDRF); // Clear the reset flag
  WDTCSR |= (1<<WDCE) | (1<<WDE);
  WDTCSR = 0x00;
}
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

AVR Libc provides a library with functions that will work across all AVR targets, in all circumstances.  Why not use it?

 

One thing it does lack are functions to employ the interrupt mode, and interrupt/reset mode of the WDT.  That is a bit annoying.  However, it's fairly easy to examine the library yourself and derive your own interrupt mode versions of their static inline functions.
 

"Experience is what enables you to recognise a mistake the second time you make it."

"Good judgement comes from experience.  Experience comes from bad judgement."

"When you hear hoofbeats, think horses, not unicorns."

"Fast.  Cheap.  Good.  Pick two."

"Read a lot.  Write a lot."

"We see a lot of arses on handlebars around here." - [J Ekdahl]