Go Down

Topic: Wake up Leonardo via the watchdog timer (Read 647 times) previous topic - next topic

harima

I am trying to use the SLEEP_MODE_PWR_DOWN sleeping mode with a Leonardo board, and to wake up the board after about 2 min with the watchdog timer.
Since the maximum time of the watchdog is 8 sec, I am calling it 15 times in a for-loop.
However with the code below, the LED toggles every ~5 sec, instead of 2 min!
I have also tried the same code with an arduino Uno and it worked fine.

What do I need to change to make it work with a Leonardo?

Code: [Select]

#include <avr/sleep.h>
#include <avr/power.h>
#include <avr/wdt.h>

#define LED_PIN (13)

volatile int f_wdt=1;

ISR(WDT_vect){
  if(f_wdt == 0){
    f_wdt=1;
  }
}

void enterSleep(void)
{
  set_sleep_mode(SLEEP_MODE_PWR_DOWN);
  sleep_enable();
 
  /* Now enter sleep mode. */
  sleep_mode();
 
  /* The program will continue from here after the WDT timeout*/
  sleep_disable(); /* First thing to do is disable sleep. */
 
  /* Re-enable the peripherals. */
  power_all_enable();
}


void setup()
{
  /*** Setup the WDT ***/
 
  /* Clear the reset flag. */
  MCUSR &= ~(1<<WDRF);
 
  /* In order to change WDE or the prescaler, we need to
   * set WDCE (This will allow updates for 4 clock cycles).
   */
  WDTCSR |= (1<<WDCE) | (1<<WDE);

  /* set new watchdog timeout prescaler value */
  WDTCSR = 1<<WDP0 | 1<<WDP3; /* 8.0 seconds */
 
  /* Enable the WD interrupt (note no reset). */
  WDTCSR |= _BV(WDIE);
}


void loop()
{
  for (int c = 1; c < 15; c++) {
    if(f_wdt == 1){
      /* Don't forget to clear the flag. */
      f_wdt = 0;
    }
    /* Re-enter sleep mode. */
    enterSleep();
  }
  /* Toggle the LED */
  digitalWrite(LED_PIN, !digitalRead(LED_PIN));
}

Go Up