beautypg.com

Atmega128rfa1 – Rainbow Electronics ATmega128RFA1 User Manual

Page 181

background image

181


8266A-MCU Wireless-12/09

ATmega128RFA1

program security, alterations to the Watchdog set-up must follow timed sequences. The
sequence for clearing WDE and changing time-out configuration is as follows:

1. In the same operation, write a logic one to the Watchdog change enable bit (WDCE)

and WDE. A logic one must be written to WDE regardless of the previous value of
the WDE bit.

2. Within the next four clock cycles, write the WDE and Watchdog prescaler bits (WDP)

as desired, but with the WDCE bit cleared. This must be done in one operation.

The following code example shows one assembly and one C function for turning off the
Watchdog Timer. The example assumes that interrupts are controlled (e.g. by disabling
interrupts globally) so that no interrupts will occur during the execution of these
functions.

Assembly Code Example

(1)

WDT_off:

; Turn off global interrupt

cli

; Reset Watchdog Timer

wdr

; Clear WDRF in MCUSR

in r16, MCUSR

andi r16, (0xff & (0<

out MCUSR, r16

; Write logical one to WDCE and WDE

; Keep old prescaler setting to prevent unintentional time-out

ldi r16, WDTCSR

ori r16, (1<

sts WDTCSR, r16

; Turn off WDT

ldi r16, (0<

sts WDTCSR, r16

; Turn on global interrupt

sei

ret

C Code Example

(1)

void WDT_off(void)

{

disable_interrupt();

watchdog_reset();

/* Clear WDRF in MCUSR*/

MCUSR &= ~(1<

/* Write logical one to WDCE and WDE */

/* Keep old prescaler setting to prevent unintentional time-out */

WDTCSR |= (1<

/* Turn off WDT */

WDTCSR = 0x00;

__enable_interrupt();

}