Unlock-Doc2466 4
Unlock-Doc2466 4
Unlock-Doc2466 4
Watchdog Reset When the Watchdog times out, it will generate a short reset pulse of one CK cycle duration. On
the falling edge of this pulse, the delay timer starts counting the Time-out period tTOUT. Refer to
page 42 for details on operation of the Watchdog Timer.
CK
MCU Control and The MCU Control and Status Register provides information on which reset source caused an
Status Register – MCU Reset.
MCUCSR
Bit 7 6 5 4 3 2 1 0
JTD ISC2 – JTRF WDRF BORF EXTRF PORF MCUCSR
Read/Write R/W R/W R R/W R/W R/W R/W R/W
Initial Value 0 0 0 See Bit Description
41
2466T–AVR–07/10
ATmega16(L)
Internal Voltage ATmega16 features an internal bandgap reference. This reference is used for Brown-out Detec-
Reference tion, and it can be used as an input to the Analog Comparator or the ADC. The 2.56V reference
to the ADC is generated from the internal bandgap reference.
Voltage Reference The voltage reference has a start-up time that may influence the way it should be used. The
Enable Signals and start-up time is given in Table 16. To save power, the reference is not always turned on. The ref-
Start-up Time erence is on during the following situations:
1. When the BOD is enabled (by programming the BODEN Fuse).
2. When the bandgap reference is connected to the Analog Comparator (by setting the
ACBG bit in ACSR).
3. When the ADC is enabled.
Thus, when the BOD is not enabled, after setting the ACBG bit or enabling the ADC, the user
must always allow the reference to start up before the output from the Analog Comparator or
ADC is used. To reduce power consumption in Power-down mode, the user can avoid the three
conditions above to ensure that the reference is turned off before entering Power-down mode.
Watchdog Timer The Watchdog Timer is clocked from a separate On-chip Oscillator which runs at 1 MHz. This is
the typical value at VCC = 5V. See characterization data for typical values at other VCC levels. By
controlling the Watchdog Timer prescaler, the Watchdog Reset interval can be adjusted as
shown in Table 17 on page 43. The WDR – Watchdog Reset – instruction resets the Watchdog
Timer. The Watchdog Timer is also reset when it is disabled and when a Chip Reset occurs.
Eight different clock cycle periods can be selected to determine the reset period. If the reset
period expires without another Watchdog Reset, the ATmega16 resets and executes from the
Reset Vector. For timing details on the Watchdog Reset, refer to page 41.
To prevent unintentional disabling of the Watchdog, a special turn-off sequence must be fol-
lowed when the Watchdog is disabled. Refer to the description of the Watchdog Timer Control
Register for details.
WATCHDOG
OSCILLATOR
42
2466T–AVR–07/10
ATmega16(L)
Watchdog Timer
Control Register – Bit 7 6 5 4 3 2 1 0
WDTCR – – – WDTOE WDE WDP2 WDP1 WDP0 WDTCR
Read/Write R R R R/W R/W R/W R/W R/W
Initial Value 0 0 0 0 0 0 0 0
43
2466T–AVR–07/10
ATmega16(L)
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.
Assembly Code Example
WDT_off:
; Reset WDT
WDR
; Write logical one to WDTOE and WDE
in r16, WDTCR
ori r16, (1<<WDTOE)|(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 WDTOE and WDE */
WDTCR |= (1<<WDTOE) | (1<<WDE);
/* Turn off WDT */
WDTCR = 0x00;
}
44
2466T–AVR–07/10
ATmega16(L)
Interrupts This section describes the specifics of the interrupt handling as performed in ATmega16. For a
general explanation of the AVR interrupt handling, refer to “Reset and Interrupt Handling” on
page 13.
Interrupt Vectors
in ATmega16 Table 18. Reset and Interrupt Vectors
Program
Vector No. Address(2) Source Interrupt Definition
(1)
1 $000 RESET External Pin, Power-on Reset, Brown-out
Reset, Watchdog Reset, and JTAG AVR
Reset
2 $002 INT0 External Interrupt Request 0
3 $004 INT1 External Interrupt Request 1
4 $006 TIMER2 COMP Timer/Counter2 Compare Match
5 $008 TIMER2 OVF Timer/Counter2 Overflow
6 $00A TIMER1 CAPT Timer/Counter1 Capture Event
7 $00C TIMER1 COMPA Timer/Counter1 Compare Match A
8 $00E TIMER1 COMPB Timer/Counter1 Compare Match B
9 $010 TIMER1 OVF Timer/Counter1 Overflow
10 $012 TIMER0 OVF Timer/Counter0 Overflow
11 $014 SPI, STC Serial Transfer Complete
12 $016 USART, RXC USART, Rx Complete
13 $018 USART, UDRE USART Data Register Empty
14 $01A USART, TXC USART, Tx Complete
15 $01C ADC ADC Conversion Complete
16 $01E EE_RDY EEPROM Ready
17 $020 ANA_COMP Analog Comparator
18 $022 TWI Two-wire Serial Interface
19 $024 INT2 External Interrupt Request 2
20 $026 TIMER0 COMP Timer/Counter0 Compare Match
21 $028 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 246.
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.
45
2466T–AVR–07/10
ATmega16(L)
46
2466T–AVR–07/10
ATmega16(L)
When the BOOTRST Fuse is unprogrammed, the Boot section size set to 2 Kbytes and the
IVSEL bit in the GICR Register is set before any interrupts are enabled, the most typical and
general program setup for the Reset and Interrupt Vector Addresses is:
Address Labels Code Comments
$000 RESET: ldi r16,high(RAMEND) ; Main program start
$001 out SPH,r16 ; Set Stack Pointer to top of RAM
$002 ldi r16,low(RAMEND)
$003 out SPL,r16
$004 sei ; Enable interrupts
$005 <instr> xxx
;
.org $1C02
$1C02 jmp EXT_INT0 ; IRQ0 Handler
$1C04 jmp EXT_INT1 ; IRQ1 Handler
... .... .. ;
$1C28 jmp SPM_RDY ; Store Program Memory Ready Handler
When the BOOTRST Fuse is programmed and the Boot section size set to 2 Kbytes, the most
typical and general program setup for the Reset and Interrupt Vector Addresses is:
Address Labels Code Comments
.org $002
$002 jmp EXT_INT0 ; IRQ0 Handler
$004 jmp EXT_INT1 ; IRQ1 Handler
... .... .. ;
$028 jmp SPM_RDY ; Store Program Memory Ready Handler
;
.org $1C00
$1C00 RESET: ldi r16,high(RAMEND) ; Main program start
$1C01 out SPH,r16 ; Set Stack Pointer to top of RAM
$1C02 ldi r16,low(RAMEND)
$1C03 out SPL,r16
$1C04 sei ; Enable interrupts
$1C05 <instr> xxx
When the BOOTRST Fuse is programmed, the Boot section size set to 2 Kbytes and the IVSEL
bit in the GICR Register is set before any interrupts are enabled, the most typical and general
program setup for the Reset and Interrupt Vector Addresses is:
Address Labels Code Comments
.org $1C00
$1C00 jmp RESET ; Reset handler
$1C02 jmp EXT_INT0 ; IRQ0 Handler
$1C04 jmp EXT_INT1 ; IRQ1 Handler
... .... .. ;
$1C28 jmp SPM_RDY ; Store Program Memory Ready Handler
;
$1C2A RESET: ldi r16,high(RAMEND) ; Main program start
$1C2B out SPH,r16 ; Set Stack Pointer to top of RAM
$1C2C ldi r16,low(RAMEND)
$1C2D out SPL,r16
$1C2E sei ; Enable interrupts
$1C2F <instr> xxx
47
2466T–AVR–07/10
ATmega16(L)
Moving Interrupts The General Interrupt Control Register controls the placement of the Interrupt Vector table.
Between Application
and Boot Space
General Interrupt
Control Register – Bit 7 6 5 4 3 2 1 0
GICR INT1 INT0 INT2 – – – IVSEL IVCE GICR
Read/Write R/W R/W R/W R R R R/W R/W
Initial Value 0 0 0 0 0 0 0 0
48
2466T–AVR–07/10
ATmega16(L)
.
C Code Example
void Move_interrupts(void)
{
/* Enable change of interrupt vectors */
GICR = (1<<IVCE);
/* Move interrupts to boot Flash section */
GICR = (1<<IVSEL);
}
49
2466T–AVR–07/10
ATmega16(L)
I/O Ports
Introduction All AVR ports have true Read-Modify-Write functionality when used as general digital I/O ports.
This means that the direction of one port pin can be changed without unintentionally changing
the direction of any other pin with the SBI and CBI instructions. The same applies when chang-
ing drive value (if configured as output) or enabling/disabling of pull-up resistors (if configured as
input). Each output buffer has symmetrical drive characteristics with both high sink and source
capability. The pin driver is strong enough to drive LED displays directly. All port pins have indi-
vidually selectable pull-up resistors with a supply-voltage invariant resistance. All I/O pins have
protection diodes to both VCC and Ground as indicated in Figure 22. Refer to “Electrical Charac-
teristics” on page 291 for a complete list of parameters.
Rpu
Pxn Logic
Cpin
See Figure 23
"General Digital I/O" for
Details
All registers and bit references in this section are written in general form. A lower case “x” repre-
sents the numbering letter for the port, and a lower case “n” represents the bit number. However,
when using the register or bit defines in a program, the precise form must be used, that is,
PORTB3 for bit no. 3 in Port B, here documented generally as PORTxn. The physical I/O Regis-
ters and bit locations are listed in “Register Description for I/O Ports” on page 66.
Three I/O memory address locations are allocated for each port, one each for the Data Register
– PORTx, Data Direction Register – DDRx, and the Port Input Pins – PINx. The Port Input Pins
I/O location is read only, while the Data Register and the Data Direction Register are read/write.
In addition, the Pull-up Disable – PUD bit in SFIOR disables the pull-up function for all pins in all
ports when set.
Using the I/O port as General Digital I/O is described in “Ports as General Digital I/O” on page
50. Most port pins are multiplexed with alternate functions for the peripheral features on the
device. How each alternate function interferes with the port pin is described in “Alternate Port
Functions” on page 55. Refer to the individual module sections for a full description of the alter-
nate functions.
Note that enabling the alternate function of some of the port pins does not affect the use of the
other pins in the port as general digital I/O.
Ports as General The ports are bi-directional I/O ports with optional internal pull-ups. Figure 23 shows a functional
Digital I/O description of one I/O-port pin, here generically called Pxn.
50
2466T–AVR–07/10