WDT/sleep

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

I'm trying to use the WDT to wake from powerdown mode, and as normal system reset. The problem is instead of waking from sleep, it resets the micro.

#include 

wdt_disable();
_WDT_Init();
wdt_reset();
set_sleep_mode(1);   //power down mode
sei();
 _delay_ms(200);
  RTS=0;
 wdt_reset();
_nop_();

while(1)
	{
	wdt_reset();
        WDTCSR |= (1<<WDIE);   
	RTS=1;
	_delay_ms(200);
	wdt_reset();
	WDTCSR|=(1<<WDIF);
	_delay_ms(200);
	wdt_reset();
	RTS=0;
WDTCSR |= (1<<WDIE);
	wdt_reset();
	RTS=1;
	_delay_ms(200);
	RTS=0;
	_delay_ms(200);
	wdt_reset();
void _WDT_Init(void)
{
	cli();
	wdt_reset();
	WDTCSR |= (1<<WDCE) | (1<<WDE);		// enable WDT
	WDTCSR = 0x4E;	     // ~1s timeout,interrupt and reset mode
	sei();
}

RTS toggles a pin. I'm thinking it's something with the WDT interrupt not getting enabled. ATmega88V.

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

If you only want a wakeup interrupt from the watchdog timer then you have to change the initialization.

void _WDT_Init(void) 
{ 
  cli(); 
  wdt_reset();
  MCUCR &= ~(1<<WDRF);	// WDRF must be cleared before WDE can be cleared
  WDTCSR |= (1<<WDCE);	// enable WDT 4 cycle timer operation bit
  WDTCSR = (1<<WDIE) | (1<<WDP2) | (1<<WDP1);	// ~1s timeout, interrupt mode 
  sei(); 
}

Then you need an interrupt handler for the watch dog at interrupt vector 7 WDT address 0x0006. Branching to an interrupt without an interrupt handler can sometimes appear to be the same as a reset after it crashes the program. If you only want a wakeup then your WDT interrupt response routine can be a wdt_reset() and return from interrupt. Executing the WDT interrupt vector will automatically clear the WDIE bit and WDIF bit.

I have not played with the new enhanced WDT yet, so the above is my interpretation from the data sheet.

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

yeah, I didn't have the ISR right. Seems to be working now. Thanks.