ВУЗ: Не указан

Категория: Не указан

Дисциплина: Не указана

Добавлен: 02.06.2025

Просмотров: 3634

Скачиваний: 0

ВНИМАНИЕ! Если данный файл нарушает Ваши авторские права, то обязательно сообщите нам.

• Bit 3 – WDE: Watchdog Enable

When the WDE is written to logic one, the Watchdog Timer is enabled, and if the WDE is written to logic zero, the Watchdog Timer function is disabled. WDE can only be cleared if the WDCE bit has logic level one. To disable an enabled Watchdog Timer, the following procedure must be followed:

1.In the same operation, write a logic one to WDCE and WDE. A logic one must be written to WDE even though it is set to one before the disable operation starts.

2.Within the next four clock cycles, write a logic 0 to WDE. This disables the Watchdog.

• Bits 2..0 – WDP2, WDP1, WDP0: Watchdog Timer Prescaler 2, 1, and 0

The WDP2, WDP1, and WDP0 bits determine the Watchdog Timer prescaling when the

Watchdog Timer is enabled. The different prescaling values and their corresponding

Timeout Periods are shown in Table 17.

Table 17. Watchdog Timer Prescale Select

Number of WDT

Typical Time-out

Typical Time-out

WDP2

WDP1

WDP0

Oscillator Cycles

at VCC = 3.0V

at VCC = 5.0V

0

0

0

16K

(16,384)

17.1 ms

16.3 ms

0

0

1

32K

(32,768)

34.3 ms

32.5 ms

0

1

0

64K

(65,536)

68.5 ms

65 ms

0

1

1

128K

(131,072)

0.14 s

0.13 s

1

0

0

256K

(262,144)

0.27 s

0.26 s

1

0

1

512K

(524,288)

0.55 s

0.52 s

1

1

0

1,024K

(1,048,576)

1.1 s

1.0 s

1

1

1

2,048K

(2,097,152)

2.2 s

2.1 s

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

42 ATmega8(L)

2486O–AVR–10/04


Timed Sequences for Changing the Configuration of the Watchdog Timer

Safety Level 1 (WDTON Fuse

Unprogrammed)

Safety Level 2 (WDTON Fuse

Programmed)

2486O–AVR–10/04

ATmega8(L)

The sequence for changing the Watchdog Timer configuration differs slightly between the safety levels. Separate procedures are described for each level.

Assembly Code Example

WDT_off:

; reset WDT

WDR

; Write logical one to WDCE and WDE in r16, WDTCR

ori r16, (1<<WDCE)|(1<<WDE) out WDTCR, r16

; Turn off WDT ldi r16, (0<<WDE) out WDTCR, r16 ret

C Code Example

void WDT_off(void)

{

/* reset WDT */ _WDR();

/* Write logical one to WDCE and WDE */ WDTCR |= (1<<WDCE) | (1<<WDE);

/* Turn off WDT */ WDTCR = 0x00;

}

In this mode, the Watchdog Timer is initially disabled, but can be enabled by writing the WDE bit to 1 without any restriction. A timed sequence is needed when changing the Watchdog Time-out period or disabling an enabled Watchdog Timer. To disable an enabled Watchdog Timer and/or changing the Watchdog Time-out, the following procedure must be followed:

1.In the same operation, write a logic one to 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, in the same operation, write the WDE and WDP bits as desired, but with the WDCE bit cleared.

In this mode, the Watchdog Timer is always enabled, and the WDE bit will always read as one. A timed sequence is needed when changing the Watchdog Time-out period. To change the Watchdog Time-out, the following procedure must be followed:

1.In the same operation, write a logical one to WDCE and WDE. Even though the WDE always is set, the WDE must be written to one to start the timed sequence.

Within the next four clock cycles, in the same operation, write the WDP bits as desired, but with the WDCE bit cleared. The value written to the WDE bit is irrelevant.

43


Interrupts

Interrupt Vectors in

ATmega8

This section describes the specifics of the interrupt handling performed by the ATmega8. For a general explanation of the AVR interrupt handling, refer to “Reset and Interrupt Handling” on page 12.

Table 18. Reset and Interrupt Vectors

Program

Vector No.

Address(2)

Source

Interrupt Definition

1

0x000(1)

RESET

External Pin, Power-on Reset, Brown-out

Reset, and Watchdog Reset

2

0x001

INT0

External Interrupt Request 0

3

0x002

INT1

External Interrupt Request 1

4

0x003

TIMER2 COMP

Timer/Counter2 Compare Match

5

0x004

TIMER2 OVF

Timer/Counter2 Overflow

6

0x005

TIMER1 CAPT

Timer/Counter1 Capture Event

7

0x006

TIMER1 COMPA

Timer/Counter1 Compare Match A

8

0x007

TIMER1 COMPB

Timer/Counter1 Compare Match B

9

0x008

TIMER1 OVF

Timer/Counter1 Overflow

10

0x009

TIMER0 OVF

Timer/Counter0 Overflow

11

0x00A

SPI, STC

Serial Transfer Complete

12

0x00B

USART, RXC

USART, Rx Complete

13

0x00C

USART, UDRE

USART Data Register Empty

14

0x00D

USART, TXC

USART, Tx Complete

15

0x00E

ADC

ADC Conversion Complete

16

0x00F

EE_RDY

EEPROM Ready

17

0x010

ANA_COMP

Analog Comparator

18

0x011

TWI

Two-wire Serial Interface

19

0x012

SPM_RDY

Store Program Memory Ready

Notes: 1. When the BOOTRST Fuse is programmed, the device will jump to the Boot Loader address at reset, see “Boot Loader Support – Read-While-Write Self-Programming” on page 206.

2.When the IVSEL bit in GICR is set, Interrupt Vectors will be moved to the start of the boot Flash section. The address of each Interrupt Vector will then be the address in this table added to the start address of the boot Flash section.

Table 19 shows reset and Interrupt Vectors placement for the various combinations of BOOTRST and IVSEL settings. If the program never enables an interrupt source, the Interrupt Vectors are not used, and regular program code can be placed at these locations. This is also the case if the Reset Vector is in the Application section while the Interrupt Vectors are in the boot section or vice versa.

44 ATmega8(L)

2486O–AVR–10/04


ATmega8(L)

Table 19. Reset and Interrupt Vectors Placement

BOOTRST(1)

IVSEL

Reset Address

Interrupt Vectors Start Address

1

0

0x000

0x001

1

1

0x000

Boot Reset Address + 0x001

0

0

Boot Reset Address

0x001

0

1

Boot Reset Address

Boot Reset Address + 0x001

Note:

1. The Boot Reset Address is shown in Table 82 on page 217. For the BOOTRST Fuse

“1” means unprogrammed while “0” means programmed.

The most typical and general program setup for the Reset and Interrupt Vector

Addresses in ATmega8 is:

addressLabels Code

Comments

$000

rjmp

RESET

; Reset Handler

$001

rjmp

EXT_INT0

; IRQ0 Handler

$002

rjmp

EXT_INT1

; IRQ1 Handler

$003

rjmp

TIM2_COMP

; Timer2 Compare Handler

$004

rjmp

TIM2_OVF

; Timer2 Overflow Handler

$005

rjmp

TIM1_CAPT

; Timer1 Capture Handler

$006

rjmp

TIM1_COMPA

; Timer1 CompareA Handler

$007

rjmp

TIM1_COMPB

; Timer1 CompareB Handler

$008

rjmp

TIM1_OVF

; Timer1 Overflow Handler

$009

rjmp

TIM0_OVF

; Timer0 Overflow Handler

$00a

rjmp

SPI_STC

; SPI Transfer Complete Handler

$00b

rjmp

USART_RXC

; USART RX Complete Handler

$00c

rjmp

USART_UDRE

; UDR Empty Handler

$00d

rjmp

USART_TXC

; USART TX Complete Handler

$00e

rjmp

ADC

; ADC Conversion Complete Handler

$00f

rjmp

EE_RDY

; EEPROM Ready Handler

$010

rjmp

ANA_COMP

; Analog Comparator Handler

$011

rjmp

TWSI

; Two-wire Serial Interface

Handler

$012

rjmp

SPM_RDY

; Store Program Memory Ready

Handler

;

$013

RESET: ldi

r16,high(RAMEND); Main program start

$014

out

SPH,r16

; Set Stack Pointer to top of RAM

$015

ldi

r16,low(RAMEND)

$016

out

SPL,r16

$017

sei

; Enable interrupts

$018

<instr> xxx

...

...

...

45

2486O–AVR–10/04