– 130 Powerful Instructions - Most Single Clock Cycle Execution
– 32 x 8 General Purpose Working Registers
– Fully Static Operation
– Up to 8 MIPS Throughput at 8 MHz
– On-chip 2-cycle Multiplier
• Program and Data Memories
– 16K Bytes of Nonvolatile In-System Programmable Flash
Endurance: 1,000 Write/Erase Cycles
– Optional Boot Code Memory with Independent Lock Bits
– Two 8-bit Timer/Counters with Separate Prescaler and PWM
– Expanded 16-bit Timer/Counter System with Separate Prescaler, Compare,
Capture Modes and Dual 8-, 9- or 10-bit PWM
– Dual Programmable Serial UARTs
– Master/Slave SPI Serial Interface
– Real Time Counter with Separate Oscillator
– Programmable Watchdog Timer with Separate On-chip Oscillator
– On-chip Analog Comparator
• Special Microcontroller Features
– Power-on Reset and Programmable Brown-out Detection
– External and Internal Interrupt Sources
– Three Sleep Modes: Idle, Power Save and Power-down
* NC = Do not connect
(Can be used in future devices)
(A10) PC2
(A11) PC3
(A12) PC4
Description
The ATmega161 is a low-power CMOS 8-bit microcontroller based on the AVR RISC architec ture. B y execu ting po werful
instructions in a single clock cycl e, the ATme ga161 achi eves throu ghputs ap proach ing 1 MIP S per MHz allo wing the system designer to op timi ze p ower c ons umption ve rsu s pro cessi ng s peed .The A VR
32 general purpose workin g registers. All the 32 registers are directl y connected to the Arithm etic Logic Unit (ALU),
allowing two independent registers to be accessed in one single instruction executed in one clock cycle. The resulting
architecture is more code efficient while achieving throughputs up to ten times faster than conventional CISC
microcontrollers.
The ATmega161 provides the followi ng features: 16K bytes of In-System- or Self-programmable Flash, 512 bytes
EEPROM, 1K bytes SR AM , 35 general purpose I/O l ine s, 32 g ener a l pu rpos e wor king registers, Real Time Cou nter , th re e
flexible timer/counters with compare modes, internal and external interrupts, two programmable serial UARTs, programmable Watchdog Timer with internal oscillator, an SPI serial port and three software selectable power saving modes. The Idle
mode stops the CPU while a llow ing t he S RAM, timer /cou nters, SPI port and interr upt s ystem to c ontin ue fu ncti oning. T he
power-down mode s aves the registe r an d SRA M co ntents but freez es the osci llato r, dis ablin g all othe r ch ip fu nctio ns unti l
the next external interrupt or hardware reset. In Power Save mode, the timer oscillator continues to run, allowing the user to
maintain a timer base while the rest of the device is sleeping.
The device is manufac tured using At mel’s h igh density nonv olati le memor y t echno logy. T he o n-chip Flas h pro gram memory can be reprogrammed using the self-programming capability through the bootblock, using an ISP through the SPI-port,
or by using a conventional nonvolatile memory programmer. By combining an enhanced RISC 8-bit CPU with In-System
Programmable Flash on a monolithic chip, the Atmel ATmega161 is a powerful microcontroller that provides a highly
flexible and cost effective solution to many embedded control applications.
The ATmega161 AVR
macro assemblers, program debugger/simulators, in-circuit emulators, and evaluation kits.
2
core combines a rich instruction set with
is supported with a full suite of program and system development tools including: C compilers,
ATmega161(L)
Block Diagram
Figure 1. The ATmega161 Block Diagram
ATmega161(L)
VCC
GND
PORTA DRIVERS
DATA REGISTER
PORTA
PROGRAM
COUNTER
PROGRAM
FLASH
INSTRUCTION
REGISTER
INSTRUCTION
DECODER
PA0-PA7
DATA DIR.
REG. PORTA
STACK
POINTER
SRAM
GENERAL
PURPOSE
REGISTERS
X
Y
Z
DATA REGISTER
8-BIT DATA BUS
PORTC DRIVERS
PORTC
INTERNAL
OSCILLATOR
WATCHDOG
TIMER
MCU CONTROL
REGISTER
TIMER/
COUNTERS
INTERRUPT
UNIT
PC0-PC7
DATA DIR.
REG. PORTC
OSCILLATOR
TIMING AND
CONTROL
XTAL1
XTAL2
RESET
ARATOR
ANALOG
COMP
-
+
CONTROL
LINES
PROGRAMMING
LOGIC
DATA REGISTER
PORTB
STATUS
REGISTER
DATA DIR.
REG. PORTB
PORTB DRIVERS
PB0 - PB7
ALU
SPI
DATA REGISTER
PORTD
PORTD DRIVERS
EEPROM
UARTS
REG. PORTD
PD0 - PD7
DATA DIR.
DATA REG.
PORTE
PORTE DRIVERS
PE0 - PE2
DATA DIR
REG. PORTE
3
Pin Descriptions
VCC
Supply voltage
GND
Ground
Port A (PA7..PA0)
Port A is an 8-bit bidirectional I/O port. Port pins can provide internal pull-up resistors (selected for each bit). The Port A
output buffers ca n sink 20 mA and c an drive LED disp lays dir ectly. When pi ns PA0 to PA7 ar e used as inputs and are
externally pulled low, they will source current if the internal pull- up resistors are activated. The Port A pins are tri-stated
when a reset condition becomes active, even if the clock is not running.
Port A serves as Multiplexed Address/Data port when using external memory interface.
Port B (PB7..PB0)
Port B is an 8 -bit bi dir ectional I/O por t with inte rnal pu ll- up res istors . The Port B o utput buffer s can sink 20 mA. As inp uts,
Port B pins that are externally pulled low will source current if the pull-up resistors are activated. The Port B pins are
tri-stated when a reset condition becomes active, even if the clock is not running.
Port B also serves the functions of various special features of the ATmega161 as listed on page 80.
Port C (PC7..PC0)
Port C is an 8-bit bidire ctiona l I/O po rt wit h inter nal pull-up resi stors. The Port C ou tpu t buffers can si nk 20 mA. A s inputs ,
Port C pins that are externally pulled low will source current if the pull-up resistors are activated. The Port C pins are
tri-stated when a reset condition becomes active, even if the clock is not running.
Port C also serves as Address high output when using external memory interface.
Port D (PD7..PD0)
Port D is an 8-bit bidire ctiona l I/O po rt wit h inter nal pull-up resi stors. The Port D ou tpu t buffers can si nk 20 mA. A s inputs ,
Port D pins that are externally pulled low will source current if the pull-up resistors are activated. The Port D pins are
tri-stated when a reset condition becomes active, even if the clock is not running.
Port D also serves the functions of various special features of the ATmega161 as listed on page 87.
Port E (PE2..PE0)
Port E is a 3-bit bidirectional I/O port with internal pull-up resistors. The Port E output buffers can sink 20 mA. As inputs,
Port E pins that are externally pulled low will source current if the pull-up resistors are activated. The Port E pins are
tri-stated when a reset condition becomes active, even if the clock is not running.
Port E also serves the functions of various special features of the ATmega161 as listed on page 93.
RESET
Reset input. A low level on thi s pin for more than 500 ns will generate a res et, even if the clock i s not running. Sho rter
pulses are not guaranteed to generate a reset.
XTAL1
Input to the inverting oscillator amplifier and input to the internal clock operating circuit.
XTAL2
Output from the inverting oscillator amplifier
4
ATmega161(L)
ATmega161(L)
Crystal Oscillator
XTAL1 and XTAL2 are input and output, respectively, of an inverting amplifier which can be configured for use as an
on-chip oscillator, as shown in Figure 2. Either a quartz crystal or a ceramic resonator may be used. To drive the device
from an external clock source, XTAL2 should be left unconnected while XTAL1 is driven as shown in Figure 3.
Figure 2. Oscillator Connections
MAX 1 HC BUFFER
HC
C2
C1
Note:When using the MCU Oscillator as a clock for an external device, an HC buffer should be connected as indicated in the figure.
Figure 3. External Clock Drive Configuration
XTAL2
XTAL1
GND
5
Architectural Overview
The fast-access register file concept contains 32 x 8-bit general purpose working registers with a single clock cycle access
time. This means th at d urin g on e s in gl e cloc k cyc le, one Arithmetic Logic Uni t ( ALU) op er ation i s ex ecute d. T wo ope ra nds
are output from the register file, the operation is executed, and the result is stored back in the register file – in one clock
cycle.
Six of the 32 registers can be used as three 16-bits indirect address register pointers for Data Space addressing – enabling
efficient address cal cula tions. One of the three addre ss po inters i s also used a s the ad dres s pointer for the const ant tabl e
look up function. These added function registers are the 16-bits X-register, Y-register and Z-register.
Figure 4. The ATmega161 AVR
RISC Architecture
AVR
8K x 16
Program
Memory
Instruction
Register
Instruction
Decoder
Control Lines
ATmega161 Architecture
Program
Counter
Direct Addressing
Indirect Addressing
Data Bus 8-bit
Status
and Control
32 x 8
General
Purpose
Registers
ALU
1024 x 8
Data
SRAM
Interrupt
Unit
SPI
Unit
Serial
UART0
Serial
UART1
8-bit
Timer/Counter
with PWM
and RTC
16-bit
Timer/Counter
with PWM
8-bit
Timer/Counter
with PWM
512 x 8
EEPROM
32
I/O Lines
6
ATmega161(L)
Watchdog
Timer
Analog
Comparator
ATmega161(L)
The ALU supports arithmetic and logic functions between registers or between a constant and a register. Single register
operations are also executed in the ALU. Figure 4 shows the ATmega161 AVR
RISC microcontroller architecture.
In addition to the register operation, the conventional memory addressing modes can be used on the register file as well.
This is enabled by the fa ct th at the re giste r file is assig ned t he 32 lowe rmost Dat a Spa ce add resses ( $00 - $ 1F), allo wing
them to be accessed as though they were ordinary memory locations.
The I/O memory spac e contai ns 64 a ddresses for CPU peripheral function s as Contr ol Regis ters, Tim er/Counters , and
other I/O functions. The I/O M emo ry c an be a cc ess ed d ir ectly , o r a s the D ata S pa ce loc at ion s f oll owing thos e o f the register file, $20 - $5F.
The AVR
uses a Harvard architecture concept – with separate memories and buses for program and data. The program
memory is executed with a two stage pipeline. While one instruction is being executed, the next instruction is pre-fetched
from the program memory. This conc ept enable s instru ctio ns to be executed in every cl ock cyc le. The program memory is
Self-programmable Flash memory.
With the jump and call in struc tions , the whol e 8K word ad dress sp ace is directl y acce ssed. Mo st AVR
instructio ns hav e a
single 16-bit word format. Every program memory address contains a 16- or 32-bit instruction.
During interrupts and su broutine call s, the r etur n addres s pr ogram co unte r (PC) is stored on the sta ck. The stac k is effe c-
tively allocate d in the ge neral data SR AM, an d consequen tly the st ack size is only limi ted by the total SRAM size and th e
usage of the SRAM. All us er progra ms must initialize the S P (Stack P ointer) in the res et routin e (befor e subroutin es or
interrupts are executed). The 16-bit stack pointer is read/write accessible in the I/O space.
The 1K bytes data SRAM can be easi ly accessed through the five different addressing modes s upported in the AVR
architecture.
The memory spaces in the AVR
architecture are all linear and regular memory maps.
7
Figure 5. Memory Maps
Program Memory
Data Memory
Program Flash
(8K x 16)
$000
$1FFF
32 Gen. Purpose
Working Registers
64 I/O Registers
Internal SRAM
(1024 x 8)
External SRAM
(0-63K x 8)
$0000
$001F
$0020
$005F
$0060
$045F
$0460
$FFFF
A flexible interrupt module has its control registers in the I/O space with an additional global interrupt enable bit in the status
register. All the different interrupts have a separate interrupt vector in the interrupt vector table at the beginning of the
program me mory. The di fferen t interr upts hav e prior ity in acco rdance with th eir inte rrupt v ector p ositi on. The lower the
interrupt vector address, the higher the priority.
8
ATmega161(L)
General Purpose Register File
Figure 6 shows the structure of the 32 general purpose working registers in the CPU.
Figure 6. AVR
CPU General Purpose Working Registers
70Addr.
R0 $00
R1$01
R2$02
…
R13$0D
GeneralR14$0E
PurposeR15$0F
WorkingR16$10
RegistersR17$11
…
R26$1AX-register low byte
R27$1BX-register high byte
R28$1CY-register low byte
R29$1DY-register high byte
R30$1EZ-register low byte
R31$1FZ-register high byte
ATmega161(L)
All the register operat ing instructi ons in the instruction s et have dir ect and s ingle cycle acc ess to all r egisters. T he only
exceptions are the five constant arithmetic and logic instructions SBCI, SUBI, CPI, ANDI, and ORI between a constant and
a register, and the LDI ins truc ti on for l oad i mm edi ate c on stant da ta. T he se instr uct io ns appl y to the secon d hal f of th e r egisters in the regis ter file – R1 6..R31 . The gen eral SB C, SUB, CP, AN D, and OR, a nd all ot her op erations between two
registers or on a single register apply to the entire register file.
As shown in Figu re 6, each regi ster is als o as signe d a da ta mem ory add ress , map ping t hem direct ly i nto the first 32 loc ations of the user Data Space. Although not being physicall y implemented as SRAM locati ons, this memory organiza tion
provides great flexibility in access of the registers, as the X, Y and Z-registers can be set to index any register in the file.
X-register, Y-register, and Z-register
The registers R2 6..R31 have som e add ed fun ction s to their gener al p urpose usa ge. The se r egister s are addr ess p ointe rs
for indirect addressing of the Data Space. The three indirect address registers X, Y and Z are defined as:
Figure 7. X, Y and Z-registers
150
X-register707 0
R27 ($1B)R26 ($1A)
150
Y-register7 07 0
R29 ($1D)R28 ($1C)
150
Z-register7 07 0
R31 ($1F)R30 ($1E)
In the different address ing mod es , thes e ad dres s r egi ster s h ave func ti ons as f ixed di s pla ce men t, aut oma tic inc r em ent an d
decrement (see the descriptions for the different instructions).
9
ALU – Arithmetic Logic Unit
The high-performance AVR ALU operates in direct connection with all the 32 general purpose working registers. Within a
single clock cy cl e, AL U ope r atio ns be tween r egi ster s in the re gis te r f ile ar e ex ec uted . The ALU operations are div i ded in to
three main catego ries – arithmetic, logica l, an d bit- func tions. A Tme ga161 does also p rovide a po werful mul tipli er sup porting both signed/unsigned multiplication and fractional format. See Instruction Set section for a detailed description.
Self-programmable Flash Program Memory
The ATmega161 contains 16K bytes on-chip Self- and In-System programmable Flash memory for program storage. Since
all instructions a re 16- or 32 -bit words, the Flash is or ga nized as 8K x 16. The Flas h me mory has an end ur ance of at least
1000 write/erase cycles. The ATmega161 Program Counter ( PC) is 13 bi ts wide, thus addressing the 819 2 program
memory locations.
See page 95 for a detailed description on Flash data downloading.
See page 11 for the different program memory addressing modes.
EEPROM Data Memory
The ATmega161 contains 512 bytes of data EEPROM memory. It is organized as a separate data space, in which single
bytes can be read and written. The EEPROM has an endurance of at least 100,000 write/erase cycles per location. The
interface between the EEPROM and the CPU is described on page 54 specifying the EEPROM address registers, the
EEPROM data register, and the EEPROM control register.
For the SPI data downloading, see page 109 for a detailed description.
Figure 8. SRAM Organization
Register FileData Address Space
R0$0000
R1$0001
R2$0002
ºº
R29$001D
R30$001E
R31$001F
I/O Registers
$00$0020
$01$0021
$02$0022
……
$3D$005D
$3E$005E
$3F$005F
Internal SRAM
$0060
$0061
º
$045E
$045F
10
ATmega161(L)
ATmega161(L)
SRAM Data Memory
Figure 8 shows how the ATmega161 SRAM Memory is organized.
The lower 1120 Data Memory locations address the Register file, the I/O Memory and the internal data SRAM. The first 96
locations address the Register File and I/O Memory, and the next 1K loc ations address the internal data SRAM. An
optional external data memory device can be placed in the same SRAM memory space. This memory device will occupy
the locations following the internal SRAM and up to as much as 64K - 1, depending on external memory size.
When the addresses acc essing the da ta memory sp ace exc eeds the int ernal data SRA M locati ons, the memor y device is
accessed using the same instructions as for the internal data SRAM access. When the internal data space is accessed, the
read and write strobe pins (RD
by setting the SRE bit in the MCUCR register. See “Interface to external memory” on page 72 for details.
Accessing external memory takes one additional clock cycle per byte compared to access of the internal SRAM. This
means that the commands LD, ST, LDS, STS, PUSH and PO P take one a dditional clock cycle. If the s tack is plac ed in
external memory, interr upts, subroutine call s and returns take two clock c ycles extra because the two-by te program
counter is pushed and popped. When external memory interface is used with wait state, two additional clock cycles is used
per byte. This ha s t he foll owin g effe ct : Dat a tr an sfe r in struc ti ons ta ke two ex tra c lo ck c y cl es, whereas interrupt, subr ou tin e
calls and returns will need four clock cycles more than specified in the instruction set manual.
The five different addressing modes for the data memory cover: Direct, Indirect with Displacement, Indirect, Indirect with
Pre-Decrement, and Indire ct with Pos t-Incre ment. In th e regist er file, regi sters R2 6 to R31 feat ure the indi rect add ressing
pointer registers.
The direct addressing reaches the entire data space.
The Indirect with Displacement mode features a 63 address locations reach from the base address given by the Y or
Z-register.
When using register indirect addressing modes with automatic pre-decrement and post-increment, the address registers X,
Y and Z are decremented and incremented.
The 32 general purpose wo rk in g regi st er s, 6 4 I/O regi ste rs an d the 1K bytes of internal data SRA M in the A T meg a161 ar e
all accessible through all these addressing modes.
See the next section for a detailed description of the different addressing modes.
and WR) are inactive during the whole access cycle. External memory operation is enabled
Program and Data Addressing Modes
The ATmega161 AVR RISC microcontroller supports powerful and efficient addressing modes for access to the program
memory (Flash) and data mem or y (SRA M, Regis ter F ile , and I/O Mem or y). T his se cti on de scr ibe s the diffe re nt add ress in g
modes supported by the AVR
simplify, not all figures show the exact location of the addressing bits.
architecture. In the figures, OP means the operation code part of the instruction word. T o
11
Register Direct, Single Register Rd
Figure 9. Direct Single Register Addressing
The operand is contained in register d (Rd).
Register Direct, Two Registers Rd and Rr
Figure 10. Direct Register Addressing, Two Registers
Operands are contained in register r (Rr) and d (Rd). The result is stored in register d (Rd).
12
ATmega161(L)
ATmega161(L)
I/O Direct
Figure 11. I/O Direct Addressing
Operand address is contained in 6 bits of the instruction word. n is the destination or source register address.
Data Direct
Figure 12. Direct Data Addressing
Data Space
$0000
$FFFF
16 LSBs
20 19
16
31
OPRr/Rd
150
A 16-bit Data Address is contained in the 1 6 LSBs of a two-word instructio n. Rd/Rr specify the destination or sourc e
register.
13
Data Indirect with Displacement
Figure 13. Data Indirect with Displacement
15
Y OR Z - REGISTER
Data Space
$0000
0
15
OPan
05610
$FFFF
Operand address is the result of the Y or Z-register contents added to the address contained in 6 bits of the
instruction word.
Data Indirect
Figure 14. Data Indirect Addressing
Data Space
015
X, Y, OR Z - REGISTER
$0000
$FFFF
Operand address is the contents of the X, Y, or the Z-register.
14
ATmega161(L)
ATmega161(L)
Data Indirect with Pre-Decrement
Figure 15. Data Indirect Addressing with Pre-Decrement
Data Space
015
X, Y, OR Z - REGISTER
-1
The X, Y, or the Z-register is decremented before the operation. Operand address is the decremented contents of the X, Y,
or the Z-register.
$0000
$FFFF
Data Indirect with Post-Increment
Figure 16. Data Indirect Addressing with Post-Increment
Data Space
015
X, Y, OR Z - REGISTER
1
$0000
$FFFF
The X, Y, or the Z-register is inc r em ente d afte r th e ope ratio n. O per an d add ress is the con tent of the X , Y, o r th e Z-re g ister
prior to incrementing.
15
Constant Addressing Using the LPM Instruction
Figure 17. Code Memory Constant Addressing
PROGRAM MEMORY
$000
$1FFF
Constant byte addr ess is s pe cif ie d b y the Z-r eg ister c on tents. The 15 MSBs se le ct wor d add re ss ( 0 - 8K ), the LS B se le cts
low byte if cleared (LSB = 0) or high byte if set (LSB = 1).
Indirect Program Addressing, IJMP and ICALL
Figure 18. Indirect Program Memory Addressing
PROGRAM MEMORY
$000
$1FFF
Program execution continues at address contained by the Z-register (i.e. the PC is loaded with the contents of the
Z-register).
16
ATmega161(L)
ATmega161(L)
Relative Program Addressing, RJMP and RCALL
Figure 19. Relative Program Memory Addressing
PROGRAM MEMORY
Program execution continues at address PC + k + 1. The relative address k is -2048 to 2047.
Direct Program Addressing, JMP and CALL
$000
$1FFF
Figure 20. Direct Program Addressing
PROGRAM MEMORY
31
OP
150
21 20
16 LSBs
16
Program execution continues at the address immediate in the instruction words.
$0000
$1FFF
17
Memory Access Times and Instruction Execution Timing
This section describes the general access timing concepts for instruction execution and internal memory access.
The AVR
clock division is used.
Figure 21 shows the parallel instruction fetches and instructio n executions enabled by the Harvard architecture and the
fast-access register file concept. This is the basic pipelining concept to obtain up to 1 MIPS per MHz with the corresponding
unique results for functions per cost, functions per clocks, and functions per power-unit.
Figure 21. The Parallel Instruction Fetches and Instruction Executions
Figure 22 shows the internal timing concept for the register file. In a single clock cycle an ALU operation using two register
operands is executed, and the result is stored back to the destination register.
CPU is driven by the System Clock Ø, directly generated from the external clock crys tal for the chip. No in ternal
T1T2T3T4
System Clock Ø
1st Instruction Fetch
1st Instruction Execute
2nd Instruction Fetch
2nd Instruction Execute
3rd Instruction Fetch
3rd Instruction Execute
4th Instruction Fetch
Figure 22. Single Cycle ALU Operation
T1T2T3T4
System Clock Ø
Total Execution Time
Register Operands Fetch
ALU Operation Execute
Result Write Back
The internal data SRAM access is performed in two System Clock cycles as described in Figure 23.
Figure 23. On-chip Data SRAM Access Cycles
T1T2T3T4
System Clock Ø
Address
Data
WR
Prev. Address
Address
Write
18
Data
RD
ATmega161(L)
Read
I/O Memory
The I/O space definition of the ATmega161 is shown in the following table:
Table 1. ATmega161 I/O Space
I/O Address (SRAM Address)NameFunction
$3F($5F)SREGStatus REGister
$3E ($5E)SPHStack Pointer High
$3D ($5D)SPLStack Pointer Low
$3B ($5B)GIMSKGeneral Interrupt MaSK register
$3A ($5A)GIFRGeneral Interrupt Flag Register
$39 ($59)TIMSKTimer/Counter Interrupt MaSK Register
$38 ($58)TIFRTimer/Counter Interrupt Flag Register
$37 ($57)SPMCRStore Program Memory Control Register
$36 ($56)EMCUCRExtended MCU general Control Register
$35 ($55)MCUCRMCU general Control Register
$34 ($54)MCUSRMCU general Status Register
ATmega161(L)
$33 ($53)TCCR0Timer/Counter0 Control Register
$32 ($52)TCNT0Timer/Counter0 (8-bit)
$31 ($51)OCR0Timer/Counter0 Output Compare Register
$30 ($50)SFIORSpecial Function IO Register
$2F ($4F)TCCR1ATimer/Counter1 Control Register A
$2E ($4E)TCCR1BTimer/Counter1 Control Register B
$2D ($4D)TCNT1HTimer/Counter1 High Byte
$2C ($4C)TCNT1LTimer/Counter1 Low Byte
$2B ($4B)OCR1AHTimer/Counter1 Output Compare RegisterA High Byte
$2A ($4A)OCR1ALTimer/Counter1 Output Compare RegisterA Low Byte
$29 ($49)OCR1BHTimer/Counter1 Output Compare RegisterB High Byte
$1F ($3F)EEARHEEPROM Address Register High
$1E ($3E)EEARLEEPROM Address Register Low
$1D ($3D)EEDREEPROM Data Register
19
Table 1. ATmega161 I/O Space (Continued)
I/O Address (SRAM Address)NameFunction
$1C ($3C)EECREEPROM Control Register
$1B($3B)PORTAData Register, Port A
$1A ($3A)DDRAData Direction Register, Port A
$19 ($39)PINAInput Pins, Port A
$18 ($38)PORTBData Register, Port B
$17 ($37)DDRBData Direction Register, Port B
$16 ($36)PINBInput Pins, Port B
$15 ($35)PORTCData Register, Port C
$14 ($34)DDRCData Direction Register, Port C
$13 ($33)PINCInput Pins, Port C
$12 ($32)PORTDData Register, Port D
$11 ($31)DDRDData Direction Register, Port D
$10 ($30)PINDInput Pins, Port D
$0F ($2F)SPDRSPI I/O Data Register
$0E ($2E)SPSRSPI Status Register
$0D ($2D)SPCRSPI Control Register
$0C ($2C)UDR0UART0 I/O Data Register
$0B ($2B)UCSR0AUART0 Control and Status Register
$0A ($2A)UCSR0BUART0 Control and Status Register
$09 ($29)UBRR0UART0 Baud Rate Register
$08 ($28)ACSRAnalog Comparator Control and Status Register
$07 ($27)PORTEData Register, Port E
$06 ($26)DDREData Direction Register, Port E
$05 ($25)PINEInput Pins, Port E
$03 ($23)UDR1UART1 I/O Data Register
$02 ($22)UCSR1AUART1 Control and Status Register
$01 ($21)UCSR1BUART1 Control and Status Register
$00 ($20)UBRR1UART1 Baud Rate Register
Note:Reserved and unused locations are not shown in the table.
All ATmega161 I/Os and peripherals are placed in the I/O space. The I/O locations are accessed by the IN and OUT
instructions transfer ring data be tween the 32 gen eral purpose wor king regis ters an d the I/O space . I/O regist ers wit hin the
address range $00 - $1F are directl y bit-acces sible us ing the SBI and CBI instruct ions. In these re giste rs, the value of single bits can be check ed by using the SBIS and SB IC instructi ons. Refer to th e instructi on set chap ter for more details.
When using the I/O specific commands IN, OUT the I/O addresses $00 - $3F must be used. When addressing I/O registers
as SRAM, $20 must be added to this address. All I/O regi ster addresses throughout this document are shown with the
SRAM address in parentheses.
For compatibilit y with fut ur e d evi ce s, r es erve d b its s hou ld be wr itte n t o z er o if a cces s ed. Rese rve d I/ O mem or y addresses
should never be written.
20
ATmega161(L)
ATmega161(L)
Some of the status flags are cleared by writing a logical one to them. Note that the CBI and SBI instructions will operate on
all bits in the I/O regi ster , writing a on e back in to an y flag r ead as set, th us clea ring t he flag . The C BI and SB I instr ucti ons
work with registers $00 to $1F only.
The I/O and peripherals control registers are explained in the following sections.
Status Register – SREG
The AVR status register – SREG – at I/O space location $3F ($5F) is defined as:
The global interrup t enable bit must be set (one) for the interrup ts to be enable d. The indivi dual inter rupt enab le control is
then performed in separate control registers. If the global interrupt enable bit is cleared (zero), none of the interrupts are
enabled independent of the individual interrupt enable settings. The I-bit is cleared by hardware after an interrupt has
occurred, and is set by the RETI instruction to enable subsequent interrupts.
Bit 6 - T: Bit Copy Storage
•
The bit copy instructions BLD (Bit LoaD) and BST (Bit STore) use the T bit as source and destination for the operated bit. A
bit from a register in the regi ste r fi le can be copi ed i nto T by the B ST i ns tru ct ion , and a bit i n T can be c opied i nto a bit i n a
register in the register file by the BLD instruction.
Bit 5 - H: Half Carry Flag
•
The half carry flag H indicates a half carry in some arithmetic operations. See the Instruction Set Description for detailed
information.
Bit 4 - S: Sign Bit, S = N ⊕ V
•
The S-bit is always an exclusive or between the negative flag N and the two’s complement overflow flag V. See the Instruction Set Description for detailed information.
•
Bit 3 - V: Two’s Complement Overflow Flag
The two’s comp lement overflow flag V s upports two’s complement arithmetics. See the Instruction Set Description for
detailed information.
Bit 2 - N: Negative Flag
•
The negative flag N indicates a negative result after the different arithmetic and logic operations. See the Instruction Set
Description for detailed information.
Bit 1 - Z: Zero Flag
•
The zero flag Z indicates a zero result after the different arithmetic and logic operations. See the Instruction Set Description
for detailed information.
•
Bit 0 - C: Carry Flag
The carry flag C indicates a c arry in an arithmetic or logic operation. S ee the Instruction S et Description for detail ed
information.
Note that the status register is not automatically stored when entering an interrupt routine and restored when returning from
an interrupt routine. This must be handled by software.
21
Stack Pointer – SP
The ATmega161 Stac k Point er is impl emented as two 8-bit r egist ers in th e I/O sp ace lo ca tions $3E ($5E ) and $3D ($5D) .
As the ATmega161 supports up to 64KB memory, all 16 bits are used.
The Stack Poin ter po ints to th e da ta SRA M s tack area whe re the Subr outi ne an d Inte rru pt St acks are loca ted. T his Sta ck
space in the d ata SRAM m ust be defi ned by the program before any subroutine calls ar e executed or interrupts are
enabled. The stack pointer must be set to point above $60. The Stack Pointer is decremented by one when data is pushed
onto the Stack with the PUSH instruction, and it is decremented by two when an address is pushed onto the Stack with
subroutine calls and interrupts. The Stack Pointer is incremented by one when data is popped from the Stack with the POP
instruction, and it is incremen ted by two when an addres s is popped from the Stack with return from subroutine RET or
return from interrupt RETI.
Reset and Interrupt Handling
The ATmega161 pr ovide s 20 d iff er ent int er rupt s ou rces . T h es e in terr up ts and the se par at e r ese t ve cto r, each hav e a s eparate program vector in the program memory space. All interrupts are assigned individual enable bits which must be set
(one) together with the I-bit in the status register in order to enable the interrupt.
The lowest addresses i n the pro gram memo ry space are automatica lly defined a s the Rese t and Inter rupt vector s. The
complete list of vectors is shown in Table 2. The list also determines the priority levels of the different interrupts. The lower
the address the higher is the priority level. RESET has the highest priority , and next is INT0 – the External Interrupt
Request 0 etc.
1$000RESETExternal Pin, Power-on Reset, Brown-out Reset and Watchd og Re se t
2$002INT0External Interrupt Request 0
3$004INT1External Interrupt Request 1
4$006INT2External Interrupt Request 2
5$008TIMER2 COMPTimer/Counter2 Compare Match
6$00aTIMER2 OVFTimer/Counter2 Overflow
7$00cTIMER1 CAPTTimer/Counter1 Capture Event
8$00eTIMER1 COMPATimer/Counter1 Compare Match A
9$010TIMER1 COMPBTimer/Counter1 Compare Match B
10$012TIMER1 OVFTimer/Counter1 Overflow
11$014TIMER0 COMPTimer/Counter0 Compare Match
12$016TIMER0 OVFTimer/Counter0 Overflow
13$018SPI, STCSerial Transfer Complete
14$01aUART0, RX UART0, Rx Complete
15$01cUART1, RX UART1, Rx Complete
• Power-on Reset. The MCU is reset when the supply voltage is below the power-on reset threshold (V
POT
).
• External Reset. The MCU is reset when a low level is present on the RESET pin for more than 500 ns.
• Watchdog Reset. The MCU is reset when the Watchdog timer period expires and the Watchdog is enabled.
• Brown-out Reset. The MCU is reset when the supply voltage V
falls below a certain voltage.
CC
During reset, all I/O regist ers are the n set to their ini tial valu es, and the pro gram starts execution from address $000. The
instruction placed in address $000 must be an JMP – relative jump – instruction to the reset handling routine. If the program
never enables an interrupt source, the interrupt vectors are not used, and regular program code can be placed at thes e
locations. The cir cuit diagram in Figure 24 shows the reset logic. Ta ble 3 and Table 4 define s the timing and elec trical
parameters of the reset circuitry
Figure 24. Reset Logic
DATA BUS
MCU Status
Register (MCUSR)
BORF
WDRF
PORF
EXTRF
BODEN
BODLEVEL
Brown-Out
Reset Circuit
CK
CKSEL[2:0]
Delay Counters
Full
24
ATmega161(L)
ATmega161(L)
Table 3. Reset Characteristics (V
= 5.0V)
CC
SymbolParameterConditionMinTypMaxUnits
BOD disabled1.01.41.8V
Power-on Reset Threshold Voltage (rising)
BOD enabled1.72.22.7V
V
POT
BOD disabled0.40.60.8V
Power-on Reset Threshold Voltage (falling)
BOD enabled1.72.22.7V
V
RST
RESET Pin Threshold Voltage--0.85 V
CC
(BODLEVEL = 1)2.6 2.7 2.8
V
BOT
Note:The Power-on Reset will not work unless the supply voltage has been below V
‘
Table 4. Reset Delay Selections
CKSEL
[2:0]
Brown-out R eset Threshold Voltage
Start-up Time, VCC = 2.7V,
BODLEVEL Unprogrammed
(BODLEVEL = 0)3.8 4.0 4.2
(falling).
POT
Start-up Time, VCC = 4.0V,
BODLEVEL ProgrammedRecommended Usage
(1)
0004.2 ms + 6 CK5.8 ms + 6 CKExternal Clock, fast rising power
00130 µs + 6 CK10 µs + 6 CKExternal Clock, BOD enabled
(2)(3)
01067 ms + 16K CK92 ms + 16K CKCrystal Oscillator, slowly rising power
0114.2 ms + 16K CK5.8 ms + 16K CKCrystal Oscillator, fast rising power
10030 µs + 16K CK10 µs + 16K CKCrystal Oscillator, BOD enabled
(2)(3)
10167 ms + 1K CK92 ms + 1K CKCeramic Resonator/External clock, Slowly rising power
V
V
1104.2 ms + 1K CK5.8 ms + 1K CKCeramic Resonator, fast rising power
11130 µs + 1K CK10 µs + 1K CKCeramic Resonator, BOD enabled
(2)(3)
Notes: 1. Th e CKSEL fuses con trol only the start-up ti me. The os cillator is the same fo r all sele ctions. On powe r-up, the rea l-ti me part
of the start-up time is increased with typ. 0.6ms.
2. Or external power-on reset.
3. When BOD is enabled, there will be a real-time part = 50 µs (typ.)
Table 4 shows th e start-up time s from reset . From slee p, only the clo ck counting part of the sta rt-up time is used. The
watchdog oscillator is used for timing the real-time part of the start-up time. The number WDT oscillator cycles used for
each time-out is shown in Table 5.
Table 5. Number of Watchdog Oscillator Cycles
BODLEVELTime-outNumber of cycles
Unprogrammed4.2 ms (at Vcc=2.7V)1K
Unprogrammed67 ms (at V
Programmed5.8 ms (at V
=2.7V)16K
cc
=4.0V)4K
cc
Programmed92 ms (at Vcc=4.0V)64K
Note:The bod-level fus e ca n b e u se d to s elect start-up times even if the Brown-o ut det ection is disabled (by le av ing th e BO DE N fuse
unprogrammed).
The frequency of the watchdog oscillator is voltage dependent as shown in the Electrical Characteristics section. The
device is shipped with CKSEL = 010.
25
Power-on Reset
A Power-on Reset (POR) pulse is generated by an on-chip detection circuit. The detection level is nominally 1.4V (rising
VCC). The POR is activated whenever V
is below the detection level. The POR circuit can be used to trigger the start-up
CC
reset, as well as detect a failure in supply voltage.
A Power-on Reset (POR) circuit ensures that the device is reset from power-on. Reaching the power-on reset threshold
voltage invokes a delay cou nter, which determi nes the delay, for which the devi ce is kept in RE SET after V
rise. The
CC
time-out period of the delay counter can be defined by the user through the CKSEL fuses. The eight different selections for
the delay period are presented in Table 4. The RESET signal is activated again, without any delay, when the V
An external reset is generated by a l ow level on the RESET
pin. Reset pulses longer than 500 ns will generate a reset,
even if the clock is not running. Shorter pulses are not guaranteed to generate a reset. When the applied signal reaches the
Reset Threshold Voltage – V
on its positive edge, the delay timer starts the MCU after the Time-out period t
RST
TOUT
has
expired.
26
ATmega161(L)
ATmega161(L)
Figure 27. External Reset During Operation
Brown-out Detection
ATmega161 has an o n-chip brown -out dete ction (BOD) circuit for m onitori ng the V
circuit can be enabled/disabled by the fuse BODEN. When BODEN is enabled (BODEN programmed), and V
to a value bel ow t he t rigg er le vel , the b row n-ou t re set is i mmedi ate ly ac tiva te d. Wh en V
the brown-out reset is deactivated after a delay. The delay is defined by the user in the same way as the delay of POR signal, in Table 4. The trig ger level for the BOD can be sel ected by the fuse BODLEV EL to be 2.7V (BODLEVEL
unprogrammed), or 4.0V ((BODLEVEL programmed). The trigger level has a hysteresis of 50 mV to ensure spike free
brown-out detection.
The BOD circuit will only detect a drop in V
if the voltage stays below the trigger level for longer than 9 µs for trigger level
CC
4.0V, 21 µs for trig ger level 2.7V (typical values).
level during the operati on. T he BOD
CC
increases above the trigger level,
CC
decreases
CC
Figure 28. Brown-out Reset During Operation
VCC
RESET
TIME-OUT
INTERNAL
RESET
V
BOT-
V
BOT+
t
TOUT
Watchdog Reset
When the Watchdo g tim es out, it will genera te a sh ort rese t pulse o f 1 XT AL cyc le dur ation. On the fal ling edge of th is
pulse, the delay timer starts counting the Time-out period t
. Refer to Page page 52 for details on operation of the
TOUT
Watchdog.
27
Figure 29. Watchdog Reset During Operation
MCU Status Register – MCUSR
The MCU Status Register provides information on which reset source caused an MCU reset.
Bit76543210
$34 ($54)----WDRFBORFEXTRFPORFMCUSR
Read/WriteRRRRR/WR/WR/WR/W
Initial value0000See bit description
Bits 7..4 - Res: Reserved Bits
•
These bits are reserved bits in the ATmega161 and always read as zero.
Bit 3 - WDRF: Watchdog Reset Flag
•
This bit is set if a watchdog reset occurs. The bit is cleared by a power-on reset, or by writing a logic zero to the flag.
•
Bit 2 - BORF: Brown-out Reset Flag
This bit is set if a brown-out reset occurs. The bit is cleared by a power-on reset, or by writing a logic zero to the flag.
Bit 1 - EXTRF: External Reset Flag
•
This bit is set if an external reset occurs. The bit is cleared by a power-on reset, or by writing a logic zero to the flag.
Bit 0 - PORF: Power-on Reset Flag
•
This bit is set if a power-on reset occurs. The bit is cleared only by writing a logic zero to the flag.
To make use of the reset flags to identify a reset condition, the user should read and then clear the MCUSR as early as
possible in the program. If th e regist er is clear ed before ano ther reset occurs, th e source of th e reset can be found by
examining the reset flags.
Interrupt Handling
The ATmega161 has two 8-bit Interrupt Mask control registe rs; GIMSK – Genera l Interrupt Ma sk register and TIM SK –
Timer/Counter Interrupt Mask register.
When an interrupt occurs, the Global Interrupt Enable I-bit is cleared (zero) and all interrupts are disabled. The user software can set (one) the I-bit to enabl e nested interrupts. The I-bit is set (one) when a Return from Interrupt instruction –
RETI – is executed.
When the Program Counter is vectored to the actual interrupt vector in order to execute the interrupt handling routine, hardware clears the corresponding flag that generated the interrupt. Some of the interrupt flags can also be cleared by writing a
logic one to the flag bit position(s) to be cleared.
If an interrupt condition occurs when the corresponding interrupt enable bit is cleared (zero), the interrupt flag will be set
and remembered until the interrupt is enabled, or the flag is cleared by software.
28
ATmega161(L)
ATmega161(L)
If one or more interrupt condi tions occu r when the global interrupt ena ble bit is clea red (zero), th e correspondi ng interrup t
flag(s) will be set and remembered until the global interrupt enable bit is set (one), and will be executed by order of priority.
Note that external level interrupt does not have a flag, and will only be remembered for as long as the interrupt condition is
present.
Note that the status register is not automatically stored when entering an interrupt routine and restored when returning from
an interrupt routine. This must be handled by software.
Interrupt Response Time
The interrupt execut ion respons e for all th e enabled A VR in terrupts is 4 clock cycles mi nimum. Afte r 4 clock cycles th e
program vec tor addre ss for the actual interru pt handl ing ro utine is execute d. Duri ng this 4 cl ock cyc le perio d, the Pr ogra m
Counter (13 bits) is pushed onto the Stack. The vector is normally a jump to the interrupt routine, and this jump takes
3 clock cycles. If an interrupt occurs during execution of a multi-cycle instruction, this instruction is completed before the
interrupt is served. If an interrupt occurs when the MCU is in sleep mode, the interrupt execution response time is
increased by 4 clock cycles.
A return from an interrupt handling routine takes 4 clock cycles. During these 4 clock cycles, the Program Counter (2 bytes)
is popped back from th e St ack , th e St ac k P oi nter is i nc remented by 2, and the I flag in S R E G is set. When AVR exits from
an interrupt, it wi ll always return to the main progr am and ex ecute one more in struction before a ny pendi ng interr upt is
served.
When the INT1 bit is set (one) and the I-bit in the Status Register (SREG) is set (one), the external pin interrupt is enabled.
The Interrupt Sense Control1 bits 1/0 (ISC11 and ISC10) in the MCU general Control Register (MCUCR) define whether
the external interrupt is ac tivated on rising and/or falli ng edge of the INT1 pin or level sense d. Acti vi ty on the pin will ca use
an interrupt request ev en if INT 1 i s co nfi gur ed as an output. The corresponding interrupt of Exte r nal Inte rrup t Re ques t 1 is
executed from program memory address $004. See also “External Interrupts”.
Bit 6 - INT0: External Interrupt Request 0 Enable
•
When the INT0 bit is set (one) and the I-bit in the Status Register (SREG) is set (one), the external pin interrupt is enabled.
The Interrupt Sense Control0 bits 1/0 (ISC01 and ISC00) in the MCU general Control Register (MCUCR) defines whet her
the external interrupt is ac tivated on rising and/or falli ng edge of the INT0 pin or level sense d. Acti vi ty on the pin will ca use
an interrupt request ev en if INT 0 i s co nfi gur ed as an output. The corresponding interrupt of Exte r nal Inte rrup t Re ques t 0 is
executed from program memory address $002. See also “External Interrupts.”
Bit 5- INT2: External Interrupt Request 2 Enable
•
When the INT2 bit is set (one) and the I-bit in the Status Register (SREG) is set (one), the external pin interrupt is activated.
The Interrupt Sense Control2 bit (ISC02 in the Extended MCU Control Register (EMCUCR) defines whether the external
interrupt is activated on rising or falling edge of the INT2 pin. Activity on the pin will cause an interrupt request even if INT2
is configured as an outp ut. The co rrespo nding inter rupt of Externa l Interru pt Reques t 2 is execute d from program m emory
address $006. See also “External Interrupts.”
Bits 4..0 - Res: Reserved bits
•
These bits are reserved bits in the ATmega161 and always read as zero.
When an event on the INT1 pin triggers an interrupt request, INTF1 becomes set (one). If the I-bit in SREG and the INT1 bit
in GIMSK are set (one), the MCU will jump to the interrupt vector at address $004. The flag is cleared when the interrupt
routine is executed. Alternatively, the flag can be cleared by writing a logical one to it.
Bit 6 - INTF0: External Interrupt Flag0
•
When an event on the INT0 pin triggers an interrupt request, INTF0 becomes set (one). If the I-bit in SREG and the INT0 bit
in GIMSK are set (one), the MCU will jump to the interrupt vector at address $002. The flag is cleared when the interrupt
routine is executed. Alternatively, the flag can be cleared by writing a logical one to it.
Bit 5 - INTF2: External Interrupt Flag2
•
When an event on the INT2 pin triggers an interrupt request, INTF2 becomes set (one). If the I-bit in SREG and the INT2 bit
in GIMSK are set (one), the MCU will jump to the interrupt vector at address $006. The flag is cleared when the interrupt
routine is executed. Alternatively, the flag can be cleared by writing a logical one to it.
Bits 4..0 - Res: Reserved bits
•
These bits are reserved bits in the ATmega161 and always read as zero.
Bit 7 - TOIE1: Timer/Counter1 Overflow Interrupt Enable
•
When the TOIE1 bit is set (one) and the I-bit in the Status Register is set (one), the Timer/Counter1 Overflow interrupt is
enabled. The corresponding interrupt (at vector $012) is executed if an overflow in Timer/Counter1 occurs, i.e., when the
TOV1 bit is set in the Timer/Counter Interrupt Flag Register – TIFR.
Bit 6 - OCE1A:Timer/Counter1 Output CompareA Match Interrupt Enable
•
When the OCIE1A bit is set (one) and the I-bit in the Status Register is set (one), the Timer/Counter1 CompareA Match
interrupt is enabled. The corresponding interrupt (at vector $00e) is executed if a CompareA match in Timer/Counter1
occurs, i.e., when the OCF1A bit is set in the Timer/Counter Interrupt Flag Register – TIFR.
Bit 5 - OCIE1B:Timer/Counter1 Output CompareB Match Interrupt Enable
•
When the OCIE1B bit is set (one) and the I-bit in the Status Register is set (one), the Timer/Counter1 CompareB Match
interrupt is enabled. The corresponding interrupt (at vector $010) is executed if a CompareB match in Timer/Counter1
occurs, i.e., when the OCF1B bit is set in the Timer/Counter Interrupt Flag Register – TIFR.
Bit 4 - TOIE2: Timer/Counter2 Overflow Interrupt Enable
•
When the TOIE2 bit is set (one) and the I-bit in the Status Register is set (one), the Timer/Counter2 Overflow interrupt is
enabled. The corresponding interrupt (at vector $00a) is executed if an overflow in Timer/Counter2 occurs, i.e., when the
TOV2 bit is set in the Timer/Counter Interrupt Flag Register – TIFR.
Bit 3 - TICIE1: Timer/Counter1 Input Capture Interrupt Enable
•
When the TICIE1 bi t is set (o ne) and the I- bit in the Status Re gister is set (one), th e Time r/Counter 1 Input Capture Ev ent
Interrupt is enabled. The corresponding interrupt (at vector $00C) is executed if a capture-triggering event occurs on pin 31,
ICP, i.e., when the ICF1 bit is set in the Timer/Counter Interrupt Flag Register – TIFR.
Bit 2 - OCIE2:Timer/Counter2 Output Compare Match Interrupt Enable
•
When the OCIE2 bit is set (one) and the I- bit in t he Status Reg is ter is s et (one ), th e Tim er /Cou nter2 Co mpa re Matc h inte rrupt is enabled. T he corre sponding interru pt (at vect or $008) is execu ted if a Comp are2 ma tch in Tim er/Cou nter2 occ urs,
i.e., when the OCF2 bit is set in the Timer/Counter Interrupt Flag Register – TIFR.
30
ATmega161(L)
Loading...
+ 104 hidden pages
You need points to download manuals.
1 point = 1 manual.
You can buy points or you can get point for every manual you upload.