Help me using wdt in interrupt mode while the mc is sleeping

#include <DigitalToggle.h>
#include<avr/sleep.h>
#include <stdint.h>
#include <avr/interrupt.h>
#include <avr/io.h>
#include <avr/wdt.h>

ISR(WDT_vect)
{
  digitalToggle(12);
}

class power_and_serial
{
public:
  power_and_serial();
  void sleep_for_mins(int x);
} power;

void power_and_serial::sleep_for_mins(int x)
{
 cli();          // stop interrupts
 WDTCSR  = (1<<WDCE | 0<<WDE | 1<<WDIE);     // watchdog change enable
 WDTCSR  = 1<<WDIE | (1<<WDP3)|(0<<WDP2) | (0<<WDP1)|(1<<WDP0); // set  prescaler to 1 second
 sei(); 
 sleep_enable();
 set_sleep_mode(SLEEP_MODE_PWR_DOWN);
 x = x*60.0/8;
 for(int i=0;i<x;i++)
 {
   sleep_mode();
 }
 cli();
 WDTCSR = (1<<WDCE|0<<WDE|0<<WDIE);
 sei();
 sleep_disable();
}

power_and_serial::power_and_serial()
{

}


void setup()
{
  pinMode(13,OUTPUT);
  pinMode(12,OUTPUT);
   power.sleep_for_mins(1);
}

void loop()
{
  for(int i=0;i<20;i++)
  {
    delay(500);
    digitalToggle(13);  
  }
  power.sleep_for_mins(1);
}

I dont see any errors here, it works well if using

WDTCSR = (1<<WDCE | 0<<WDE ); // watchdog change enable
but not when i use
WDTCSR = (1<<WDCE | 0<<WDE | 1<<WDIE); // watchdog change enable
, in the second case it produces interrupt as well as other code executes..

Sorry, I don't understand the question.

What are you trying to achieve? How are you trying to achieve it? What's the problem with your solution?

i wanted to use wdt timer for setting sleep times above 10 minutes and so (the function takes value in minutes as parameter),
its not working i donno why.

or can u please post a code to use wdt to sleep timer for times above 8sec using interrpt mode, so that i can resume my normal code after sleep mode.

When you run the code you posted, what happens? From your original post it sounds as if 'it works well' but I'm not sure what that means - what exactly is the problem?

While you're looking at that I suggest you put parenthesis around your bitwise operations to make it clear what order you want them to happen. I'm too lazy to look up the precedence but in any case I never like to rely on precedence. Make it explicit, then there's no doubt that it's being done correctly.

ry these routines on your code, write a simple sketch that calls these routines and give it a try, it is what I use to get the system to sleep:

ISR(WDT_vect) 
{
   f_wdt=1;  // set global flag
}


/*!
    @function
    @abstract   Sets the system to sleep
    @discussion Configures the system sleep mode and sets the system to sleep.
    This routine must be called after configuring a mechanism to wake up the
    system. Otherwise, it will never wakeup.
    @param      sleepMode[in] systel sleep mode: SLEEP_MODE_IDLE, SLEEP_MODE_ADC,
    SLEEP_MODE_PWR_DOWN, SLEEP_MODE_PWR_SAVE, SLEEP_MODE_STANDBY,
    SLEEP_MODE_EXT_STANDBY
    @result     None.
*/
static void system_sleep( int sleepMode ) 
{
   
   set_sleep_mode ( sleepMode ); // sleep mode is set here
   sleep_enable ( );
   sleep_mode( );                     // System sleeps here
   sleep_disable( );                   // System continues execution here when watchdog 
                                            // timed out 
}


/*!
    @function
    @abstract   Configure the watchdog timer time out and enable the watchdog
    interrupt.
    @discussion Configure watchdog timre to expire every wdTime. The routine
    writes into the WD timer registers and enablethe watchdog interrupt
    @param      wdTime[in] watchdog time out:
    0=16ms, 1=32ms, 2=64ms, 3=128ms, 4=250ms, 5=500ms, 6=1 sec,7=2 sec, 
    8=4 sec, 9= 8sec
    @result     None
*/
static void setup_watchdog(int wdTime) {
   
   uint8_t bMask;
   
   // Check for ranges and restrict to valid range
   // --------------------------------------------
   if (wdTime > 9 )
   {
      wdTime=9;
   }
   
   // Configure the first set of bits for the WD timeout
   bMask = wdTime & 0x0F;
   
   // Set the upper bits for the watchdog timeout
   // --------------------------------------------
   if (wdTime > 7)
   {
      bMask |= ( 1 << 5 );
   }
   
   MCUSR &= ~(1 << WDRF);
   
   // start programming sequence of the watchdog timer need to assert WDCE bit
   WDTCSR |= (1 << WDCE ) | ( 1 << WDE );

   // For programming pre-scaler WDCE need to be high
   bMask |= ( 1 << WDCE );
   
   // set new watchdog timeout value
   WDTCSR = bMask;
   WDTCSR |= _BV(WDIE);    // enable watchdog interrupt, not reset
}



/*!
 @function
 @abstract   configure sleep mode to power down and configure WD to wake up.
 @discussion Configure power down mode and configure the WD timer to wake
 the system up.
 @param      sleepTime[in] configure the watchdog timer to wake up:
 0=16ms, 1=32ms, 2=64ms, 3=128ms, 4=250ms, 5=500ms, 6=1 sec,7=2 sec, 
 8=4 sec, 9= 8sec
 @result     None
 */
static void setupSleepMode ( int sleepTime )
{
   // Sleep mode enable and power down mode 
   SMCR &= ~( ( 1 << SE ) | ( 1 << SM0 ) | ( 1 << SM2 ) );
   SMCR |= ( 1 << SM1 );
   
   setup_watchdog(sleepTime); // setup watchdog timer (not the best place to put it)
}

Now you will need to initialize it and use it.

   setupSleepMode ( 6 );

This can be done during setup, but it must be called before setting the system to sleep.

Then you would just call the:

system_sleep(SLEEP_MODE_PWR_DOWN);

In this example, it is configured to wake up every second, what you will need to do is a small routine that counts the number of times you have woken up and set it to sleep again if the amount of time hasn't expired.

Hope it helps.