I'm trying to use the watchdog timer to prevent the atmel from being stuck in a loop.
Right now, I have the watchdog timer in System Reset Mode, with a 8sec timer. I reset the timer in every loop while the system is active. But when it goes to sleep, it cannot clear the timer and the whole system resets. Essentially, the system sleeps > System Reset every 8 sec > goes back to sleep.
While this works, I initialize a lot of other hardware with the atmel, and doing it every 8 second seems like a waste of resources.
I want to use the watchdog interrupt somehow only to wake the system up briefly and stop the full system reset. Only if it's truly stuck, it will do a full system reboot. But i'm not sure how the ISR will work or how to reset the timer or toggle.
Here a basic rundown of what I was thinking about.
ISR(wdt_timer)
{
how to stop the full system reboot?
how to make the interrupt start again?
}
ISR(button_press)
{
gotoSleep=0;
}
void watchdog_init()
{
/* Reset the wdt. */
wdt_reset(); //from wdt.h
/* 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 both interrupt and reset). */
WDTCSR |= (1<<WDIE) | (1<<WDE);
}
void main ()
{
watchdog_init();
//and initialize all the other hardware and power supply
while (true) //no need to step out of this loop
{
reset_watchdog();
//
//main program
//
if(go_to_sleep=1) //command received through serial
gotoSleep();
//wake up here
}
}