AW: [En-Nut-Discussion] The Watchdog timer
Oliver Schulz
Oliver.Schulz at bong.de
Tue Oct 5 17:29:03 CEST 2004
Hi Sigurd,
> Hey!
> I'm trying to program the watchdog timer in my application. I
> have red the datasheat for at Atmega128. When i delay the
Please read the datasheet more carefully! Did you omit the chapter describing the timed sequence to change the watchdog timeout?
I assume, that you don't operate themega128 in 103 compatibility mode and you didn't program the fuse WDTON (which is the default for atmega128, I think)
To set the watchdog timeout or disable the watchdog, you have to follow this sequence:
1. Set the bits WDCE and WDE in WDTCR in one instruction.
2. Set the desired bits for timeout or disabling in one instruction that follows step 1 immediately.
Beware, step 1 also enables the watchdog.
To be sure, the these instructions are not interrupted, I would surround them with calls to NutEnterCritical() and NutExitCritical() (or cli() and sei()).
So you code must read like this:
for(;;) {
NutEnterCritical();
WDTCR = 0x18;
WDTCR = 0x0F;
NutExitCritical();
NutSleep(100);
NutEnterCritical();
WDTCR = 0x18;
WDTCR = 0;
NutExitCritical();
}
Usually, if the watchdog is enabled, you can reset the watchdog timer by using the WDR assembler instruction (asm("wdr");).
For more simplier life, just use the macros defined in avr/wdt.h from avr-libc.
Hope that helps...
Oliver.
More information about the En-Nut-Discussion
mailing list