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.