Datasheet ATtiny24, ATtiny44, ATtiny84 Datasheet (ATMEL)

Page 1
Features
High Performance, Low Power AVR
Advanced RISC Architecture
– 120 Powerful Instructions – Most Single Clock Cycle Execution – 32 x 8 General Purpose Working Registers – Fully Static Operation
– 2/4/8K Byte of In-System Programmable Program Memory Flash (ATtiny24/44/84)
Endurance: 10,000 Write/Erase Cycles
– 128/256/512 Bytes In-System Programmable EEPROM (ATtiny24/44/84)
Endurance: 100,000 Write/Erase Cycles – 128/256/512 Bytes Internal SRAM (ATtiny24/44/84) – Programming Lock for Self-Programming Flash Program and EEPROM Data
Security
Peripheral Features
– Two Timer/Counters, 8- and 16-bit counters with two PWM Channels on both – 10-bit ADC
8 single-ended channels
12 differential ADC channel pairs with programmable gain (1x, 20x)
Temperature Measurement – Programmable Watchdog Timer with Separate On-chip Oscillator – On-chip Analog Comparator – Universal Serial Interface
Special Microcontroller Features
– debugWIRE On-chip Debug System – In-System Programmable via SPI Port – External and Internal Interrupt Sources – Pin Change Interrupt on 12 pins – Low Power Idle, ADC Noise Reduction, Standby and Power-down Modes – Enhanced Power-on Reset Circuit – Programmable Brown-out Detection Circuit – Internal Calibrated Oscillator – On-chip Temperature Sensor
I/O and Packages
– 14-pin SOIC, PDIP and 20-pin QFN/MLF: Twelve Programmable I/O Lines
Operating Voltage:
– 1.8 - 5.5V for ATtiny24V/44V/84V – 2.7 - 5.5V for ATtiny24/44/84
Speed Grade
– ATtiny24V/44V/84V: 0 - 4 MHz @ 1.8 - 5.5V, 0 - 10 MHz @ 2.7 - 5.5V – ATtiny24/44/84: 0 - 10 MHz @ 2.7 - 5.5V, 0 - 20 MHz @ 4.5 - 5.5V
Industrial Temperature Range
Low Power Consumption
– Active Mode:
1 MHz, 1.8V: 380 µA – Power-down Mode:
1.8V: 100 nA
®
8-Bit Microcontroller
8-bit
Microcontroller with 2/4/8K Bytes In-System Programmable Flash
ATtiny24/44/84
Preliminary
Rev. 8006F–AVR–02/07
Page 2

1. Pin Configurations

Figure 1-1. Pinout ATtiny24/44/84
PDIP/SOIC
VCC
(PCINT8/XTAL1/CLKI) PB0
(PCINT9/XTAL2) PB1
(PCINT11/RESET/dW) PB3
(PCINT10/INT0/OC0A/CKOUT) PB2
(PCINT7/ICP/OC0B/ADC7) PA7
(PCINT6/OC1A/SDA/MOSI/ADC6) PA6
(ADC4/USCK/SCL/T1/PCINT4) PA4
(ADC3/T0/PCINT3) PA3 (ADC2/AIN1/PCINT2) PA2 (ADC1/AIN0/PCINT1) PA1
(ADC0/AREF/PCINT0) PA0
NOTE Bottom pad should be soldered to ground. DNC: Do Not Connect
1 2 3 4 5 6 7
QFN/MLF
PA 5
201918
1 2 3 4 5
6
DNC
DNC
7
DNC
14 13 12 11 10
DNC
8
GND
GND PA0 (ADC0/AREF/PCINT0) PA1 (ADC1/AIN0/PCINT1) PA2 (ADC2/AIN1/PCINT2) PA3 (ADC3/T0/PCINT3)
9
PA4 (ADC4/USCK/SCL/T1/PCINT4)
8
PA5 (ADC5/DO/MISO/OC1B/PCINT5)
Pin 16: PA6 (PCINT6/OC1A/SDA/MOSI/ADC6) Pin 20: PA5 (ADC5/DO/MISO/OC1B/PCINT5)
DNC
PA 6
17
16 15
PA7 (PCINT7/ICP/OC0B/ADC7)
14
PB2 (PCINT10/INT0/OC0A/CKOUT)
13
PB3 (PCINT11/RESET/dW)
12
PB1 (PCINT9/XTAL2)
11
PB0 (PCINT8/XTAL1/CLKI)
9
10
VCC
DNC

1.1 Disclaimer

Typical values contained in this data sheet are based on simulations and characterization of other AVR microcontrollers manufactured on the same process technology. Min and Max values will be available after the device is characterized.
2
ATtiny24/44/84
8006F–AVR–02/07
Page 3

2. Overview

2.1 Block Diagram

ATtiny24/44/84
The ATtiny24/44/84 is a low-power CMOS 8-bit microcontroller based on the AVR enhanced RISC architecture. By executing powerful instructions in a single clock cycle, the ATtiny24/44/84 achieves throughputs approaching 1 MIPS per MHz allowing the system designer to optimize power consumption versus processing speed.
Figure 2-1. Block Diagram
VCC
GND
PROGRAM COUNTER
PROGRAM
FLASH
INSTRUCTION
REGISTER
INSTRUCTION
DECODER
CONTROL
LINES
PROGRAMMING
LOGIC
STACK
POINTER
SRAM
GENERAL PURPOSE
REGISTERS
X Y Z
ALU
STATUS
REGISTER
ISP INTERFACE
8-BIT DATABUS
INTERNAL
OSCILLATOR
WATCHDOG
TIMER
MCU CONTROL
REGISTER
MCU STATUS
REGISTER
TIMER/
COUNTER0
TIMER/
COUNTER1
INTERRUPT
UNIT
EEPROM
INTERNAL CALIBRATED OSCILLATOR
TIMING AND
CONTROL
OSCILLATORS
8006F–AVR–02/07
DATA REGISTER
+
-
PORT A
ANALOG
COMPARATOR
PORT A DRIVERS
PA7-PA0
DATA DIR.
REG.PORT A
ADC
DATA REGISTER
PORT B
PORT B DRIVERS
PB3-PB0
DATA DIR.
REG.PORT B
The AVR core combines a rich instruction set with 32 general purpose working registers. All the 32 registers are directly connected to the Arithmetic Logic Unit (ALU), allowing two independent
3
Page 4
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 con­ventional CISC microcontrollers.
The ATtiny24/44/84 provides the following features: 2/4/8K byte of In-System Programmable Flash, 128/256/512 bytes EEPROM, 128/256/512 bytes SRAM, 12 general purpose I/O lines, 32 general purpose working registers, a 8-bit Timer/Counter with two PWM channels, a 16-bit timer/counter with two PWM channels, Internal and External Interrupts, a 8-channel 10-bit ADC, programmable gain stage (1x, 20x) for 12 differential ADC channel pairs, a programmable Watchdog Timer with internal Oscillator, internal calibrated oscillator, and three software select­able power saving modes. The Idle mode stops the CPU while allowing the SRAM, Timer/Counter, ADC, Analog Comparator, and Interrupt system to continue functioning. The Power-down mode saves the register contents, disabling all chip functions until the next Inter­rupt or Hardware Reset. The ADC Noise Reduction mode stops the CPU and all I/O modules except ADC, to minimize switching noise during ADC conversions. In Standby mode, the crys­tal/resonator Oscillator is running while the rest of the device is sleeping. This allows very fast start-up combined with low power consumption.
The device is manufactured ng Atmel’s high density non-volatile memory technology. The On­chip ISP Flash allows the Program memory to be re-programmed In-System through an SPI serial interface, by a conventional non-volatile memory programmer or by an On-chip boot code running on the AVR core.
The ATtiny24/44/84 AVR is supported with a full suite of program and system development tools including: C Compilers, Macro Assemblers, Program Debugger/Simulators, In-Circuit Emulators, and Evaluation kits.
4
ATtiny24/44/84
8006F–AVR–02/07
Page 5

2.2 Pin Descriptions

2.2.1 VCC

Supply voltage.

2.2.2 GND

Ground.

2.2.3 Port B (PB3...PB0)

Port B is a 4-bit bi-directional I/O port with internal pull-up resistors (selected for each bit). The Port B output buffers have symmetrical drive characteristics with both high sink and source capability except PB3 which has the RESET RESET pin, program (‘0’) RSTDISBL fuse. As inputs, 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 ATtiny24/44/84 as listed on
Section 12.3 ”Alternate Port Functions” on page 61.

2.2.4 RESET

ATtiny24/44/84
capability. To use pin PB3 as an I/O pin, instead of
Reset input. A low level on this pin for longer than the minimum pulse length will generate a reset, even if the clock is not running. The minimum pulse length is given in Table 22-3 on page
182. Shorter pulses are not guaranteed to generate a reset.

2.2.5 Port A (PA7...PA0)

Port A is a 8-bit bi-directional I/O port with internal pull-up resistors (selected for each bit). The Port A output buffers have symmetrical drive characteristics with both high sink and source capability. As inputs, Port A pins that are externally pulled low will source current if the 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 has an alternate functions as analog inputs for the ADC, analog comparator, timer/counter, SPI and pin change interrupt as described in ”Alternate Port Functions” on page
61
8006F–AVR–02/07
5
Page 6

3. Resources

A comprehensive set of development tools, drivers and application notes, and datasheets are available for download on http://www.atmel.com/avr.
6
ATtiny24/44/84
8006F–AVR–02/07
Page 7

4. About Code Examples

This documentation contains simple code examples that briefly show how to use various parts of the device. These code examples assume that the part specific header file is included before compilation. Be aware that not all C compiler vendors include bit definitions in the header files and interrupt handling in C is compiler dependent. Please confirm with the C compiler documen­tation for more details.
For I/O Registers located in extended I/O map, “IN”, “OUT”, “SBIS”, “SBIC”, “CBI”, and “SBI” instructions must be replaced with instructions that allow access to extended I/O. Typically “LDS” and “STS” combined with “SBRS”, “SBRC”, “SBR”, and “CBR”.
ATtiny24/44/84
8006F–AVR–02/07
7
Page 8

5. CPU Core

5.1 Overview

This section discusses the AVR core architecture in general. The main function of the CPU core is to ensure correct program execution. The CPU must therefore be able to access memories, perform calculations, control peripherals, and handle interrupts.

5.2 Architectural Overview

Figure 5-1. Block Diagram of the AVR Architecture
Data Bus 8-bit
Flash
Program
Memory
Instruction
Register
Instruction
Decoder
Control Lines
Program
Counter
Direct Addressing
Status
and Control
32 x 8
General
Purpose
Registrers
ALU
Indirect Addressing
Data
SRAM
EEPROM
Interrupt
Unit
Watchdog
Timer
Analog
Comparator
Timer/Counter 0
Timer/Counter 1
Universal
Serial Interface
I/O Lines
In order to maximize performance and parallelism, the AVR uses a Harvard architecture – with separate memories and buses for program and data. Instructions in the Program memory are executed with a single level pipelining. While one instruction is being executed, the next instruc­tion is pre-fetched from the Program memory. This concept enables instructions to be executed in every clock cycle. The Program memory is In-System Reprogrammable Flash memory.
8
ATtiny24/44/84
8006F–AVR–02/07
Page 9
ATtiny24/44/84
The fast-access Register File contains 32 x 8-bit general purpose working registers with a single clock cycle access time. This allows single-cycle Arithmetic Logic Unit (ALU) operation. In a typ­ical ALU operation, two operands 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-bit indirect address register pointers for Data Space addressing – enabling efficient address calculations. One of the these address pointers can also be used as an address pointer for look up tables in Flash Program memory. These added function registers are the 16-bit X-, Y-, and Z-register, described later in this section.
The ALU supports arithmetic and logic operations between registers or between a constant and a register. Single register operations can also be executed in the ALU. After an arithmetic opera­tion, the Status Register is updated to reflect information about the result of the operation.
Program flow is provided by conditional and unconditional jump and call instructions, able to directly address the whole address space. Most AVR instructions have a single 16-bit word for­mat. Every Program memory address contains a 16- or 32-bit instruction.
During interrupts and subroutine calls, the return address Program Counter (PC) is stored on the Stack. The Stack is effectively allocated in the general data SRAM, and consequently the Stack size is only limited by the total SRAM size and the usage of the SRAM. All user programs must initialize the SP in the Reset routine (before subroutines or interrupts are executed). The Stack Pointer (SP) is read/write accessible in the I/O space. The data SRAM can easily be accessed through the five different addressing modes supported in the AVR architecture.
The memory spaces in the AVR architecture are all linear and regular memory maps.
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 interrupts have a separate Interrupt Vector in the Interrupt Vector table. The interrupts have priority in accordance with their Interrupt Vector posi­tion. The lower the Interrupt Vector address, the higher the priority.
The I/O memory space contains 64 addresses for CPU peripheral functions as Control Regis­ters, SPI, and other I/O functions. The I/O memory can be accessed directly, or as the Data Space locations following those of the Register File, 0x20 - 0x5F.
5.3 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 cycle, arithmetic operations between general purpose registers or between a register and an immediate are executed. The ALU operations are divided into three main categories – arithmetic, logical, and bit-functions. Some implementations of the architecture also provide a powerful multiplier supporting both signed/unsigned multiplication and fractional format. See the “Instruction Set” section for a detailed description.

5.4 Status Register

The Status Register contains information about the result of the most recently executed arith­metic instruction. This information can be used for altering program flow in order to perform conditional operations. Note that the Status Register is updated after all ALU operations, as specified in the Instruction Set Reference. This will in many cases remove the need for using the dedicated compare instructions, resulting in faster and more compact code.
8006F–AVR–02/07
The Status Register is not automatically stored when entering an interrupt routine and restored when returning from an interrupt. This must be handled by software.
9
Page 10
5.4.1 SREG – AVR Status Register
Bit 76543210
0x3F (0x5F) I T H S V N Z C SREG
Read/Write R/W R/W R/W R/W R/W R/W R/W R/W
Initial Value00000000
• Bit 7 – I: Global Interrupt Enable
The Global Interrupt Enable bit must be set for the interrupts to be enabled. The individual inter­rupt enable control is then performed in separate control registers. If the Global Interrupt Enable Register is cleared, 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. The I-bit can also be set and cleared by the application with the SEI and CLI instructions, as described in the instruction set reference.
• Bit 6 – T: Bit Copy Storage
The Bit Copy instructions BLD (Bit LoaD) and BST (Bit STore) use the T-bit as source or desti­nation for the operated bit. A bit from a register in the Register File can be copied into T by the BST instruction, and a bit in T can be copied into a bit in 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. Half Carry is useful in BCD arithmetic. 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 Complement Overflow Flag V supports 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 in an arithmetic or logic operation. See the “Instruction Set Description” for detailed information.
• Bit 1 – Z: Zero Flag
The Zero Flag Z indicates a zero result in an arithmetic or logic operation. See the “Instruction Set Description” for detailed information.
• Bit 0 – C: Carry Flag
The Carry Flag C indicates a carry in an arithmetic or logic operation. See the “Instruction Set Description” for detailed information.
10
ATtiny24/44/84
8006F–AVR–02/07
Page 11

5.5 General Purpose Register File

The Register File is optimized for the AVR Enhanced RISC instruction set. In order to achieve the required performance and flexibility, the following input/output schemes are supported by the Register File:
• One 8-bit output operand and one 8-bit result input
• Two 8-bit output operands and one 8-bit result input
• Two 8-bit output operands and one 16-bit result input
• One 16-bit output operand and one 16-bit result input
Figure 5-2 on page 11 shows the structure of the 32 general purpose working registers in the
CPU.
Figure 5-2. AVR CPU General Purpose Working Registers
General R14 0x0E
Purpose R15 0x0F
Working R16 0x10
Registers R17 0x11
ATtiny24/44/84
7 0 Addr.
R0 0x00
R1 0x01
R2 0x02
R13 0x0D
R26 0x1A X-register Low Byte
R27 0x1B X-register High Byte
R28 0x1C Y-register Low Byte
R29 0x1D Y-register High Byte
R30 0x1E Z-register Low Byte
R31 0x1F Z-register High Byte
Most of the instructions operating on the Register File have direct access to all registers, and most of them are single cycle instructions.
As shown in Figure 5-2, each register is also assigned a Data memory address, mapping them directly into the first 32 locations of the user Data Space. Although not being physically imple­mented as SRAM locations, this memory organization provides great flexibility in access of the registers, as the X-, Y- and Z-pointer registers can be set to index any register in the file.

5.5.1 The X-register, Y-register, and Z-register

The registers R26..R31 have some added functions to their general purpose usage. These reg­isters are 16-bit address pointers for indirect addressing of the data space. The three indirect address registers X, Y, and Z are defined as described in Figure 5-3 on page 12.
8006F–AVR–02/07
11
Page 12

5.6 Stack Pointer

Figure 5-3. The X-, Y-, and Z-registers
15 XH XL 0
X-register 707 0
R27 (0x1B) R26 (0x1A)
15 YH YL 0
Y-register 707 0
R29 (0x1D) R28 (0x1C)
15 ZH ZL 0
Z-register 7070
R31 (0x1F) R30 (0x1E)
In the different addressing modes these address registers have functions as fixed displacement, automatic increment, and automatic decrement (see the instruction set reference for details).
The Stack is mainly used for storing temporary data, for storing local variables and for storing return addresses after interrupts and subroutine calls. The Stack Pointer Register always points to the top of the Stack. Note that the Stack is implemented as growing from higher memory loca­tions to lower memory locations. This implies that a Stack PUSH command decreases the Stack Pointer.
The Stack Pointer points to the data SRAM Stack area where the Subroutine and Interrupt Stacks are located. This Stack space in the data SRAM must be defined by the program before any subroutine calls are executed or interrupts are enabled. The Stack Pointer must be set to point above 0x60. 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 the return address is pushed onto the Stack with subroutine call or interrupt. The Stack Pointer is incremented by one when data is popped from the Stack with the POP instruction, and it is incremented by two when data is popped from the Stack with return from subroutine RET or return from interrupt RETI.
The AVR Stack Pointer is implemented as two 8-bit registers in the I/O space. The number of bits actually used is implementation dependent. Note that the data space in some implementa­tions of the AVR architecture is so small that only SPL is needed. In this case, the SPH Register will not be present.
5.6.1 SPH and SPL – Stack Pointer High and Low
Bit 151413121110 9 8
0x3E (0x5E) SP15 SP14 SP13 SP12 SP11 SP10 SP9 SP8 SPH
0x3D (0x5D) SP7 SP6 SP5 SP4 SP3 SP2 SP1 SP0 SPL
76543210
Read/Write R/W R/W R/W R/W R/W R/W R/W R/W
R/W R/W R/W R/W R/W R/W R/W R/W
Initial Value 00000000
00000000
12
ATtiny24/44/84
8006F–AVR–02/07
Page 13

5.7 Instruction Execution Timing

This section describes the general access timing concepts for instruction execution. The AVR CPU is driven by the CPU clock clk chip. No internal clock division is used.
Figure 5-4 on page 13 shows the parallel instruction fetches and instruction executions enabled
by the Harvard architecture and the fast access Register File concept. This is the basic pipelin­ing 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 5-4. The Parallel Instruction Fetches and Instruction Executions
1st Instruction Fetch
1st Instruction Execute
2nd Instruction Fetch
2nd Instruction Execute
3rd Instruction Fetch
3rd Instruction Execute
4th Instruction Fetch
clk
CPU
ATtiny24/44/84
, directly generated from the selected clock source for the
CPU
T1 T2 T3 T4
Figure 5-5 on page 13 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.
Figure 5-5. Single Cycle ALU Operation
Total Execution Time
Register Operands Fetch
ALU Operation Execute
Result Write Back

5.8 Reset and Interrupt Handling

The AVR provides several different interrupt sources. These interrupts and the separate Reset Vector each have a separate Program Vector in the Program memory space. All interrupts are assigned individual enable bits which must be written logic one together with the Global Interrupt Enable bit in the Status Register in order to enable the interrupt.
The lowest addresses in the Program memory space are by default defined as the Reset and Interrupt Vectors. The complete list of vectors is shown in ”Interrupts” on page 50. 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.
clk
T1 T2 T3 T4
CPU
8006F–AVR–02/07
13
Page 14
When an interrupt occurs, the Global Interrupt Enable I-bit is cleared and all interrupts are dis­abled. The user software can write logic one to the I-bit to enable nested interrupts. All enabled interrupts can then interrupt the current interrupt routine. The I-bit is automatically set when a Return from Interrupt instruction – RETI – is executed.
There are basically two types of interrupts. The first type is triggered by an event that sets the Interrupt Flag. For these interrupts, the Program Counter is vectored to the actual Interrupt Vec­tor in order to execute the interrupt handling routine, and hardware clears the corresponding Interrupt Flag. 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 while the corresponding interrupt enable bit is cleared, the Interrupt Flag will be set and remembered until the interrupt is enabled, or the flag is cleared by software. Similarly, if one or more interrupt conditions occur while the Global Interrupt Enable bit is cleared, the corresponding Interrupt Flag(s) will be set and remembered until the Global Interrupt Enable bit is set, and will then be executed by order of priority.
The second type of interrupts will trigger as long as the interrupt condition is present. These interrupts do not necessarily have Interrupt Flags. If the interrupt condition disappears before the interrupt is enabled, the interrupt will not be triggered.
When the AVR exits from an interrupt, it will always return to the main program and execute one more instruction before any pending interrupt is served.
Note that the Status Register is not automatically stored when entering an interrupt routine, nor restored when returning from an interrupt routine. This must be handled by software.
When using the CLI instruction to disable interrupts, the interrupts will be immediately disabled. No interrupt will be executed after the CLI instruction, even if it occurs simultaneously with the
CLI instruction. The following example shows how this can be used to avoid interrupts during the timed EEPROM write sequence..
Assembly Code Example
in r16, SREG ; store SREG value
cli ; disable interrupts during timed sequence
sbi EECR, EEMPE ; start EEPROM write
sbi EECR, EEPE
out SREG, r16 ; restore SREG value (I-bit)
C Code Example
char cSREG;
cSREG = SREG; /* store SREG value */
/* disable interrupts during timed sequence */
_CLI();
EECR |= (1<<EEMPE); /* start EEPROM write */
EECR |= (1<<EEPE);
SREG = cSREG; /* restore SREG value (I-bit) */
14
ATtiny24/44/84
8006F–AVR–02/07
Page 15
When using the SEI instruction to enable interrupts, the instruction following SEI will be exe­cuted before any pending interrupts, as shown in this example.
Assembly Code Example
sei ; set Global Interrupt Enable
sleep; enter sleep, waiting for interrupt
; note: will enter sleep before any pending
; interrupt(s)
C Code Example
_SEI(); /* set Global Interrupt Enable */
_SLEEP(); /* enter sleep, waiting for interrupt */
/* note: will enter sleep before any pending interrupt(s) */

5.8.1 Interrupt Response Time

The interrupt execution response for all the enabled AVR interrupts is four clock cycles mini­mum. After four clock cycles the Program Vector address for the actual interrupt handling routine is executed. During this four clock cycle period, the Program Counter is pushed onto the Stack. The vector is normally a jump to the interrupt routine, and this jump takes three 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 four clock cycles. This increase comes in addition to the start-up time from the selected sleep mode.
ATtiny24/44/84
A return from an interrupt handling routine takes four clock cycles. During these four clock cycles, the Program Counter (two bytes) is popped back from the Stack, the Stack Pointer is incremented by two, and the I-bit in SREG is set.
8006F–AVR–02/07
15
Page 16

6. Memories

This section describes the different memories in the ATtiny24/44/84. The AVR architecture has two main memory spaces, the Data memory and the Program memory space. In addition, the ATtiny24/44/84 features an EEPROM Memory for data storage. All three memory spaces are lin­ear and regular.

6.1 In-System Re-programmable Flash Program Memory

The ATtiny24/44/84 contains 2/4/8K byte On-chip In-System Reprogrammable Flash memory for program storage. Since all AVR instructions are 16 or 32 bits wide, the Flash is organized as 1024/2048/4096 x 16.
The Flash memory has an endurance of at least 10,000 write/erase cycles. The ATtiny24/44/84 Program Counter (PC) is 10/11/12 bits wide, thus addressing the 1024/2048/4096 Program memory locations. ”Memory Programming” on page 164 contains a detailed description on Flash data serial downloading using the SPI pins.
Constant tables can be allocated within the entire Program memory address space (see the LPM – Load Program memory instruction description).
Timing diagrams for instruction fetch and execution are presented in ”Instruction Execution Tim-
ing” on page 13.
Figure 6-1. Program Memory Map

6.2 SRAM Data Memory

Figure 6-2 on page 17 shows how the ATtiny24/44/84 SRAM Memory is organized.
The lower 160 Data memory locations address both the Register File, the I/O memory and the internal data SRAM. The first 32 locations address the Register File, the next 64 locations the standard I/O memory, and the last 128/256/512 locations address the internal data SRAM.
The five different addressing modes for the Data memory cover: Direct, Indirect with Displace­ment, Indirect, Indirect with Pre-decrement, and Indirect with Post-increment. In the Register File, registers R26 to R31 feature the indirect addressing pointer registers.
The direct addressing reaches the entire data space.
Program Memory
0x0000
0x03FF/0x07FF/0xFFF
16
The Indirect with Displacement mode reaches 63 address locations from the base address given by the Y- or Z-register.
ATtiny24/44/84
8006F–AVR–02/07
Page 17
ATtiny24/44/84
When using register indirect addressing modes with automatic pre-decrement and post-incre­ment, the address registers X, Y, and Z are decremented or incremented.
The 32 general purpose working registers, 64 I/O Registers, and the 128/256/512 bytes of inter­nal data SRAM in the ATtiny24/44/84 are all accessible through all these addressing modes. The Register File is described in ”General Purpose Register File” on page 11.
Figure 6-2. Data Memory Map
Data Memory

6.2.1 Data Memory Access Times

This section describes the general access timing concepts for internal memory access. The internal data SRAM access is performed in two clk
17.
Figure 6-3. On-chip Data SRAM Access Cycles
clk
CPU
Address
Data
WR
Data
RD
32 Registers
64 I/O Registers
Internal SRAM
(128/256/512 x 8)
T1 T2 T3
Compute Address
0x0000 - 0x001F 0x0020 - 0x005F 0x0060
0x0DF/0x015F/0x025F
cycles as described in Figure 6-3 on page
CPU
Address valid
Write
Read
8006F–AVR–02/07
Memory Access Instruction
Next Instruction
17
Page 18

6.3 EEPROM Data Memory

The ATtiny24/44/84 contains 128/256/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. The access between the EEPROM and the CPU is described in the following, specifying the EEPROM Address Registers, the EEPROM Data Register, and the EEPROM Control Register. For a detailed description of Serial data downloading to the EEPROM, see ”Serial Downloading” on page 168.

6.3.1 EEPROM Read/Write Access

The EEPROM Access Registers are accessible in the I/O space.
The write access times for the EEPROM are given in Table 6-1 on page 24. A self-timing func­tion, however, lets the user software detect when the next byte can be written. If the user code contains instructions that write the EEPROM, some precautions must be taken. In heavily fil­tered power supplies, V device for some period of time to run at a voltage lower than specified as minimum for the clock frequency used. See ”Preventing EEPROM Corruption” on page 21 for details on how to avoid problems in these situations.
In order to prevent unintentional EEPROM writes, a specific write procedure must be followed. See ”Atomic Byte Programming” on page 18 and ”Split Byte Programming” on page 18 for details on this.
When the EEPROM is read, the CPU is halted for four clock cycles before the next instruction is executed. When the EEPROM is written, the CPU is halted for two clock cycles before the next instruction is executed.
is likely to rise or fall slowly on Power-up/down. This causes the
CC

6.3.2 Atomic Byte Programming

Using Atomic Byte Programming is the simplest mode. When writing a byte to the EEPROM, the user must write the address into the EEARL Register and data into EEDR Register. If the EEPMn bits are zero, writing EEPE (within four cycles after EEMPE is written) will trigger the erase/write operation. Both the erase and write cycle are done in one operation and the total programming time is given in Table 1. The EEPE bit remains set until the erase and write opera­tions are completed. While the device is busy with programming, it is not possible to do any other EEPROM operations.

6.3.3 Split Byte Programming

It is possible to split the erase and write cycle in two different operations. This may be useful if the system requires short access time for some limited period of time (typically if the power sup­ply voltage falls). In order to take advantage of this method, it is required that the locations to be written have been erased before the write operation. But since the erase and write operations are split, it is possible to do the erase operations when the system allows doing time-critical operations (typically after Power-up).

6.3.4 Erase

To erase a byte, the address must be written to EEAR. If the EEPMn bits are 0b01, writing the EEPE (within four cycles after EEMPE is written) will trigger the erase operation only (program­ming time is given in Table 1). The EEPE bit remains set until the erase operation completes. While the device is busy programming, it is not possible to do any other EEPROM operations.
18
ATtiny24/44/84
8006F–AVR–02/07
Page 19

6.3.5 Write

ATtiny24/44/84
To write a location, the user must write the address into EEAR and the data into EEDR. If the EEPMn bits are 0b10, writing the EEPE (within four cycles after EEMPE is written) will trigger the write operation only (programming time is given in Table 1). The EEPE bit remains set until the write operation completes. If the location to be written has not been erased before write, the data that is stored must be considered as lost. While the device is busy with programming, it is not possible to do any other EEPROM operations.
The calibrated Oscillator is used to time the EEPROM accesses. Make sure the Oscillator fre­quency is within the requirements described in ”Oscillator Calibration Register – OSCCAL” on
page 33.
The following code examples show one assembly and one C function for erase, write, or atomic write of the EEPROM. The examples assume that interrupts are controlled (e.g., by disabling interrupts globally) so that no interrupts will occur during execution of these functions.
8006F–AVR–02/07
19
Page 20
Assembly Code Example
EEPROM_write:
; Wait for completion of previous write
sbic EECR,EEPE
rjmp EEPROM_write
; Set Programming mode
ldi r16, (0<<EEPM1)|(0<<EEPM0)
out EECR, r16
; Set up address (r17) in address register
out EEARL, r17
; Write data (r16) to data register
out EEDR,r16
; Write logical one to EEMPE
sbi EECR,EEMPE
; Start eeprom write by setting EEPE
sbi EECR,EEPE
ret
C Code Example
void EEPROM_write(unsigned char ucAddress, unsigned char ucData)
{
/* Wait for completion of previous write */
while(EECR & (1<<EEPE))
;
/* Set Programming mode */
EECR = (0<<EEPM1)|(0>>EEPM0)
/* Set up address and data registers */
EEARL = ucAddress;
EEDR = ucData;
/* Write logical one to EEMPE */
EECR |= (1<<EEMPE);
/* Start eeprom write by setting EEPE */
EECR |= (1<<EEPE);
}
20
Note: The code examples are only valid for ATtiny24 and ATtiny44, using 8-bit addressing mode.
ATtiny24/44/84
8006F–AVR–02/07
Page 21
ATtiny24/44/84
The next code examples show assembly and C functions for reading the EEPROM. The exam­ples assume that interrupts are controlled so that no interrupts will occur during execution of these functions.
Assembly Code Example
EEPROM_read:
; Wait for completion of previous write
sbic EECR,EEPE
rjmp EEPROM_read
; Set up address (r17) in address register
out EEARL, r17
; Start eeprom read by writing EERE
sbi EECR,EERE
; Read data from data register
in r16,EEDR
ret
C Code Example
unsigned char EEPROM_read(unsigned char ucAddress)
{
/* Wait for completion of previous write */
while(EECR & (1<<EEPE))
;
/* Set up address register */
EEARL = ucAddress;
/* Start eeprom read by writing EERE */
EECR |= (1<<EERE);
/* Return data from data register */
return EEDR;
}
Note: The code examples are only valid for ATtiny24 and ATtiny44, using 8-bit addressing mode.

6.3.6 Preventing EEPROM Corruption

During periods of low V too low for the CPU and the EEPROM to operate properly. These issues are the same as for board level systems using EEPROM, and the same design solutions should be applied.
An EEPROM data corruption can be caused by two situations when the voltage is too low. First, a regular write sequence to the EEPROM requires a minimum voltage to operate correctly. Sec­ondly, the CPU itself can execute instructions incorrectly, if the supply voltage is too low.
EEPROM data corruption can easily be avoided by following this design recommendation:
Keep the AVR RESET active (low) during periods of insufficient power supply voltage. This can be done by enabling the internal Brown-out Detector (BOD). If the detection level of the internal BOD does not match the needed detection level, an external low V be used. If a reset occurs while a write operation is in progress, the write operation will be com­pleted provided that the power supply voltage is sufficient.
8006F–AVR–02/07
, the EEPROM data can be corrupted because the supply voltage is
CC
reset protection circuit can
CC
21
Page 22

6.4 I/O Memory

The I/O space definition of the ATtiny24/44/84 is shown in ”Register Summary” on page 212.
All ATtiny24/44/84 I/Os and peripherals are placed in the I/O space. All I/O locations may be accessed by the LD/LDS/LDD and ST/STS/STD instructions, transferring data between the 32 general purpose working registers and the I/O space. I/O Registers within the address range 0x00 - 0x1F are directly bit-accessible using the SBI and CBI instructions. In these registers, the value of single bits can be checked by using the SBIS and SBIC instructions. See the instruction set section for more details. When using the I/O specific commands IN and OUT, the I/O addresses 0x00 - 0x3F must be used. When addressing I/O Registers as data space using LD and ST instructions, 0x20 must be added to these addresses.
For compatibility with future devices, reserved bits should be written to zero if accessed. Reserved I/O memory addresses should never be written.
Some of the Status Flags are cleared by writing a logical one to them. Note that, unlike most other AVRs, the CBI and SBI instructions will only operate on the specified bit, and can therefore be used on registers containing such Status Flags. The CBI and SBI instructions work with reg­isters 0x00 to 0x1F only.
The I/O and Peripherals Control Registers are explained in later sections.

6.4.1 General Purpose I/O Registers

The ATtiny24/44/84 contains three General Purpose I/O Registers. These registers can be used for storing any information, and they are particularly useful for storing global variables and status flags. General Purpose I/O Registers within the address range 0x00 - 0x1F are directly bit­accessible using the SBI, CBI, SBIS, and SBIC instructions.
22
ATtiny24/44/84
8006F–AVR–02/07
Page 23

6.5 Register Description

6.5.1 EEARH – EEPROM Address Register
Bit 76543210
0x1F (0x3F) –––––––EEAR8EEARH
Read/Write RRRRRRRR/W
Initial Value 0 0 0 0 0 0 0 X
• Bits 7..1 – Res: Reserved Bits
These bits are reserved bits in the ATtiny24/44/84 and will always read as zero.
• Bit 0 – EEAR8: EEPROM Address
The EEPROM Address Register – EEARH – specifies the most significant bit for EEPROM address in the 512 bytes EEPROM space for Tiny84. This bit is reserved bit in the ATtiny24/44 and will always read as zero. The initial value of EEAR is undefined. A proper value must be written before the EEPROM may be accessed.
6.5.2 EEARL – EEPROM Address Register
Bit 76543210
0x1E (0x3E) EEAR7 EEAR6 EEAR5 EEAR4 EEAR3 EEAR2 EEAR1 EEAR0 EEARL
Read/Write R/W R/W R/W R/W R/W R/W R/W R/W
Initial Value X X X X X X X X
ATtiny24/44/84
• Bits 7..0 – EEAR7..0: EEPROM Address
The EEPROM Address Register – EEARL – specifies the EEPROM address. In the 128 bytes EEPROM space in ATiny24 bit 7 is reserved and always read as zero. The EEPROM data bytes are addressed linearly between 0 and 128/256/512. The initial value of EEAR is undefined. A proper value must be written before the EEPROM may be accessed.
6.5.3 EEDR – EEPROM Data Register
Bit 76543210
0x1D (0x3D) EEDR7 EEDR6 EEDR5 EEDR4 EEDR3 EEDR2 EEDR1 EEDR0 EEDR
Read/Write R/W R/W R/W R/W R/W R/W R/W R/W
Initial Value 0 0 0 0 0 0 0 0
• Bits 7..0 – EEDR7..0: EEPROM Data
For the EEPROM write operation the EEDR Register contains the data to be written to the EEPROM in the address given by the EEAR Register. For the EEPROM read operation, the EEDR contains the data read out from the EEPROM at the address given by EEAR.
6.5.4 EECR – EEPROM Control Register
Bit 76543210
0x1C (0x3C) EEPM1 EEPM0 EERIE EEMPE EEPE EERE EECR
Read/Write R R R/W R/W R/W R/W R/W R/W
Initial Value 0 0 X X 0 0 X 0
8006F–AVR–02/07
23
Page 24
• Bit 7 – Res: Reserved Bit
This bit is reserved for future use and will always read as 0 in ATtiny24/44/84. For compatibility with future AVR devices, always write this bit to zero. After reading, mask out this bit.
• Bit 6 – Res: Reserved Bit
This bit is reserved in the ATtiny24/44/84 and will always read as zero.
• Bits 5, 4 – EEPM1 and EEPM0: EEPROM
Mode Bits
The EEPROM Programming mode bits setting defines which programming action that will be triggered when writing EEPE. It is possible to program data in one atomic operation (erase the old value and program the new value) or to split the Erase and Write operations in two different operations. The Programming times for the different modes are shown in Table 6-1. While EEPE is set, any write to EEPMn will be ignored. During reset, the EEPMn bits will be reset to 0b00 unless the EEPROM is busy programming.
Table 6-1. EEPROM Mode Bits
Programming
EEPM1 EEPM0
0 0 3.4 ms Erase and Write in one operation (Atomic Operation)
0 1 1.8 ms Erase Only
Time Operation
1 0 1.8 ms Write Only
1 1 Reserved for future use
• Bit 3 – EERIE: EEPROM Ready Interrupt Enable
Writing EERIE to one enables the EEPROM Ready Interrupt if the I-bit in SREG is set. Writing EERIE to zero disables the interrupt. The EEPROM Ready Interrupt generates a constant inter­rupt when Non-volatile memory is ready for programming.
• Bit 2 – EEMPE: EEPROM Master Program Enable
The EEMPE bit determines whether writing EEPE to one will have effect or not.
When EEMPE is set, setting EEPE within four clock cycles will program the EEPROM at the selected address. If EEMPE is zero, setting EEPE will have no effect. When EEMPE has been written to one by software, hardware clears the bit to zero after four clock cycles.
• Bit 1 – EEPE: EEPROM Program Enable
The EEPROM Program Enable Signal EEPE is the programming enable signal to the EEPROM. When EEPE is written, the EEPROM will be programmed according to the EEPMn bits setting. The EEMPE bit must be written to one before a logical one is written to EEPE, otherwise no EEPROM write takes place. When the write access time has elapsed, the EEPE bit is cleared by hardware. When EEPE has been set, the CPU is halted for two cycles before the next instruction is executed.
24
• Bit 0 – EERE: EEPROM Read Enable
The EEPROM Read Enable Signal – EERE – is the read strobe to the EEPROM. When the cor­rect address is set up in the EEAR Register, the EERE bit must be written to one to trigger the
ATtiny24/44/84
8006F–AVR–02/07
Page 25
EEPROM read. The EEPROM read access takes one instruction, and the requested data is available immediately. When the EEPROM is read, the CPU is halted for four cycles before the next instruction is executed. The user should poll the EEPE bit before starting the read opera­tion. If a write operation is in progress, it is neither possible to read the EEPROM, nor to change the EEAR Register.
6.5.5 GPIOR2 – General Purpose I/O Register 2
Bit 76543210
0x15 (0x35) MSB LSB GPIOR2
Read/Write R/W R/W R/W R/W R/W R/W R/W R/W
Initial Value 0 0 0 0 0 0 0 0
6.5.6 GPIOR1 – General Purpose I/O Register 1
Bit 76543210
0x14 (0x34) MSB LSB GPIOR1
Read/Write R/W R/W R/W R/W R/W R/W R/W R/W
Initial Value 0 0 0 0 0 0 0 0
6.5.7 GPIOR0 – General Purpose I/O Register 0
Bit 76543210
0x13 (0x33) MSB LSB GPIOR0
Read/Write R/W R/W R/W R/W R/W R/W R/W R/W
Initial Value 0 0 0 0 0 0 0 0
ATtiny24/44/84
8006F–AVR–02/07
25
Page 26

7. System Clock and Clock Options

7.1 Clock Systems and their Distribution

Figure 7-1 on page 26 presents the principal clock systems in the AVR and their distribution. All
of the clocks need not be active at a given time. In order to reduce power consumption, the clocks to modules not being used can be halted by using different sleep modes, as described in
”Power Management and Sleep Modes” on page 35. The clock systems are detailed below.
Figure 7-1. Clock Distribution
ADC
General I/O
Modules
CPU Core RAM
Flash and EEPROM
7.1.1 CPU Clock – clk
7.1.2 I/O Clock – clk
Reset Logic
clk
CPU
clk
FLASH
Low-Frequency
Crystal Oscillator
Watchdog Timer
Watchdog clock
Watchdog
Oscillator
Calibrated RC
Oscillator
CPU
clk
I/O
clk
ADC
Source clock
External Clock
AVR Clock
Control Unit
System Clock
Prescaler
Clock
Multiplexer
Calibrated RC
Crystal
Oscillator
Oscillator
The CPU clock is routed to parts of the system concerned with operation of the AVR core. Examples of such modules are the General Purpose Register File, the Status Register and the Data memory holding the Stack Pointer. Halting the CPU clock inhibits the core from performing general operations and calculations.
I/O
The I/O clock is used by the majority of the I/O modules, like Timer/Counter. The I/O clock is also used by the External Interrupt module, but note that some external interrupts are detected by asynchronous logic, allowing such interrupts to be detected even if the I/O clock is halted.
7.1.3 Flash Clock – clk
The Flash clock controls operation of the Flash interface. The Flash clock is usually active simul­taneously with the CPU clock.
7.1.4 ADC Clock – clk
The ADC is provided with a dedicated clock domain. This allows halting the CPU and I/O clocks in order to reduce noise generated by digital circuitry. This gives more accurate ADC conversion results.
26
ATtiny24/44/84
FLASH
ADC
8006F–AVR–02/07
Page 27

7.2 Clock Sources

ATtiny24/44/84
The device has the following clock source options, selectable by Flash Fuse bits as shown below. The clock from the selected source is input to the AVR clock generator, and routed to the appropriate modules.
Table 7-1. Device Clocking Options Select
Device Clocking Option CKSEL3..0
External Clock 0000
Calibrated Internal RC Oscillator 8.0 MHz 0010
Watchdog Oscillator 128 kHz 0100
External Low-frequency Oscillator 0110
External Crystal/Ceramic Resonator 1000-1111
Reserved 0101, 0111, 0011,0001
Note: 1. For all fuses “1” means unprogrammed while “0” means programmed.
(1)
The various choices for each clocking option is given in the following sections. When the CPU wakes up from Power-down or Power-save, the selected clock source is used to time the start­up, ensuring stable Oscillator operation before instruction execution starts. When the CPU starts from reset, there is an additional delay allowing the power to reach a stable level before com­mencing normal operation. The Watchdog Oscillator is used for timing this real-time part of the start-up time. The number of WDT Oscillator cycles used for each time-out is shown in Table 7-2
on page 27.
Table 7-2. Number of Watchdog Oscillator Cycles
Typ Time-out Number of Cycles

7.3 Default Clock Source

The device is shipped with CKSEL = “0010”, SUT = “10”, and CKDIV8 programmed. The default clock source setting is therefore the Internal RC Oscillator running at 8.0 MHz with longest start­up time and an initial system clock prescaling of 8, resulting in 1.0 MHz system clock. This default setting ensures that all users can make their desired clock source setting using an In­System or High-voltage Programmer.

7.4 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 7-2. Either a quartz
crystal or a ceramic resonator may be used.
C1 and C2 should always be equal for both crystals and resonators. The optimal value of the capacitors depends on the crystal or resonator in use, the amount of stray capac-itance, and the electromagnetic noise of the environment. Some initial guidelines for choosing capacitors for use with crystals are given in Table 7-3 on page 28. For ceramic resonators, the capacitor val­ues given by the manufacturer should be used.
4 ms 512
64 ms 8K (8,192)
8006F–AVR–02/07
27
Page 28
Figure 7-2. Crystal Oscillator Connections
C2
C1
XTAL2
XTAL1
GND
The Oscillator can operate in three different modes, each optimized for a specific frequency range. The operating mode is selected by the fuses CKSEL3..1 as shown in Table 7-3 on page
28.
Table 7-3. Crystal Oscillator Operating Modes
Recommended Range for Capacitors C1 and
CKSEL3..1 Frequency Range (MHz)
(1)
100
101 0.9 - 3.0 12 - 22
110 3.0 - 8.0 12 - 22
111 8.0 - 12 - 22
0.4 - 0.9
C2 for Use with Crystals (pF)
Notes: 1. This option should not be used with crystals, only with ceramic resonators.
The CKSEL0 Fuse together with the SUT1..0 Fuses select the start-up times as shown in Table
7-4 on page 29.
28
ATtiny24/44/84
8006F–AVR–02/07
Page 29
ATtiny24/44/84
Table 7-4. Start-up Times for the Crystal Oscillator Clock Selection
Start-up Time from
Power-down and
CKSEL0 SUT1..0
0 00 258 CK
0 01 258 CK
010 1K CK
011 1K CK
100 1K CK
Power-save
(1)
(1)
(2)
(2)
(2)
1 01 16K CK 14CK
1 10 16K CK 14CK + 4.1 ms
1 11 16K CK 14CK + 65 ms
Additional Delay
from Reset
(VCC = 5.0V) Recommended Usage
14CK + 4.1 ms
14CK + 65 ms
14CK
14CK + 4.1 ms
14CK + 65 ms
Ceramic resonator, fast rising power
Ceramic resonator, slowly rising power
Ceramic resonator, BOD enabled
Ceramic resonator, fast rising power
Ceramic resonator, slowly rising power
Crystal Oscillator, BOD enabled
Crystal Oscillator, fast rising power
Crystal Oscillator, slowly rising power
Notes: 1. These options should only be used when not operating close to the maximum frequency of the
device, and only if frequency stability at start-up is not important for the application. These options are not suitable for crystals.
2. These options are intended for use with ceramic resonators and will ensure frequency stability at start-up. They can also be used with crystals when not operating close to the maximum fre­quency of the device, and if frequency stability at start-up is not important for the application.

7.5 Low-frequency Crystal Oscillator

To use a 32.768 kHz watch crystal as the clock source for the device, the low-frequency crystal oscillator must be selected by setting CKSEL fuses to ‘0110’. The crystal should be connected as shown in Figure 7-2. See the 32 kHz Crystal Oscillator Application Note for details on oscilla­tor operation and how to choose appropriate values for C1 and C2.
When this oscillator is selected, start-up times are determined by the SUT fuses as shown in
Table 7-5.
Table 7-5. Start-up Times for the Low Frequency Crystal Oscillator Clock Selection
Start-up Time from
Power Down and Power
SUT1..0
00 1K CK
01 1K CK
10 32K CK 64 ms Stable frequency at start-up
11 Reserved
Notes: 1. These options should only be used if frequency stability at start-up is not important for the
application.
Save
Additional Delay from
Reset (VCC = 5.0V) Recommended usage
(1)
(1)
4 ms
64 ms Slowly rising power
Fast rising power or BOD enabled
8006F–AVR–02/07
29
Page 30

7.6 Calibrated Internal RC Oscillator

By default, the Internal RC Oscillator provides an approximate 8 MHz clock. Though voltage and temperature dependent, this clock can be very accurately calibrated by the the user. See Table
22-1 on page 181 and ”Internal Oscillator Speed” on page 205 for more details. The device is
shipped with the CKDIV8 Fuse programmed. See ”System Clock Prescaler” on page 32 for more details.
This clock may be selected as the system clock by programming the CKSEL Fuses as shown in
Table 7-6. If selected, it will operate with no external components. During reset, hardware loads
the pre-programmed calibration value into the OSCCAL Register and thereby automatically cal­ibrates the RC Oscillator. The accuracy of this calibration is shown as Factory calibration in
Table 22-1 on page 181.
By changing the OSCCAL register from SW, see ”Oscillator Calibration Register – OSCCAL” on
page 33, it is possible to get a higher calibration accuracy than by using the factory calibration.
The accuracy of this calibration is shown as User calibration in Table 22-1 on page 181.
When this Oscillator is used as the chip clock, the Watchdog Oscillator will still be used for the Watchdog Timer and for the Reset Time-out. For more information on the pre-programmed cali-
bration value, see the section ”Calibration Byte” on page 166.
Table 7-6. Internal Calibrated RC Oscillator Operating Modes
CKSEL3..0 Nominal Frequency
(1)
0010
8.0 MHz

7.7 External Clock

Note: 1. The device is shipped with this option selected.
When this Oscillator is selected, start-up times are determined by the SUT Fuses as shown in
Table 7-7 on page 30..
Table 7-7. Start-up Times for the Internal Calibrated RC Oscillator Clock Selection
Start-up Time
SUT1..0
00 6 CK 14CK BOD enabled
01 6 CK 14CK + 4 ms Fast rising power
(1)
10
11 Reserved
Note: 1. The device is shipped with this option selected.
from Power-down
6 CK 14CK + 64 ms Slowly rising power
Additional Delay from
Reset (VCC = 5.0V) Recommended Usage
To drive the device from an external clock source, CLKI should be driven as shown in Figure 7-3
on page 31. To run the device on an external clock, the CKSEL Fuses must be programmed to
“0000”.
30
ATtiny24/44/84
8006F–AVR–02/07
Page 31
Figure 7-3. External Clock Drive Configuration
ATtiny24/44/84
EXTERNAL
CLOCK
SIGNAL
CLKI
GND
When this clock source is selected, start-up times are determined by the SUT Fuses as shown in
Table 7-8 on page 31.
Table 7-8. Start-up Times for the External Clock Selection
Start-up Time from Power-
SUT1..0
00 6 CK 14CK BOD enabled
01 6 CK 14CK + 4 ms Fast rising power
10 6 CK 14CK + 64 ms Slowly rising power
11 Reserved
down and Power-save
Additional Delay from
Reset Recommended Usage
When applying an external clock, it is required to avoid sudden changes in the applied clock fre­quency to ensure stable operation of the MCU. A variation in frequency of more than 2% from one clock cycle to the next can lead to unpredictable behavior. It is required to ensure that the MCU is kept in Reset during such changes in the clock frequency.
Note that the System Clock Prescaler can be used to implement run-time changes of the internal clock frequency while still ensuring stable operation. See to ”System Clock Prescaler” on page
32 for details.
8006F–AVR–02/07
31
Page 32

7.8 128 kHz Internal Oscillator

The 128 kHz internal Oscillator is a low power Oscillator providing a clock of 128 kHz. The fre­quency is nominal at 3V and 25°C. This clock may be select as the system clock by programming the CKSEL Fuses to “0100”.
When this clock source is selected, start-up times are determined by the SUT Fuses as shown in
Table 7-9 on page 32.
Table 7-9. Start-up Times for the 128 kHz Internal Oscillator
SUT1..0
00 6 CK 14CK BOD enabled
01 6 CK 14CK + 4 ms Fast rising power
10 6 CK 14CK + 64 ms Slowly rising power
11 Reserved

7.9 System Clock Prescaler

The ATtiny24/44/84 system clock can be divided by setting the Clock Prescale Register – CLKPR. This feature can be used to decrease power consumption when the requirement for processing power is low. This can be used with all clock source options, and it will affect the clock frequency of the CPU and all synchronous peripherals. clk are divided by a factor as shown in Table 7-10 on page 34.

7.9.1 Switching Time

When switching between prescaler settings, the System Clock Prescaler ensures that no glitches occur in the clock system and that no intermediate frequency is higher than neither the clock frequency corresponding to the previous setting, nor the clock frequency corresponding to the new setting.
The ripple counter that implements the prescaler runs at the frequency of the undivided clock, which may be faster than the CPU’s clock frequency. Hence, it is not possible to determine the state of the prescaler – even if it were readable, and the exact time it takes to switch from one clock division to another cannot be exactly predicted.
Start-up Time from Power-
down and Power-save
Additional Delay from
Reset Recommended Usage
I/O
, clk
ADC
, clk
CPU
, and clk
FLASH
32
From the time the CLKPS values are written, it takes between T1 + T2 and T1 + 2*T2 before the new clock frequency is active. In this interval, 2 active clock edges are produced. Here, T1 is the previous clock period, and T2 is the period corresponding to the new prescaler setting.
ATtiny24/44/84
8006F–AVR–02/07
Page 33

7.10 Register Description

7.10.1 Oscillator Calibration Register – OSCCAL
Bit 76543210
0x31 (0x51) CAL7 CAL6 CAL5 CAL4 CAL3 CAL2 CAL1 CAL0 OSCCAL
Read/Write R/W R/W R/W R/W R/W R/W R/W R/W
Initial Value Device Specific Calibration Value
The Oscillator Calibration Register is used to trim the Calibrated Internal RC Oscillator to remove process variations from the oscillator frequency. A pre-programmed calibration value is automatically written to this register during chip reset, giving the Factory calibrated frequency as specified in Table 22-1 on page 181. The application software can write this register to change the oscillator frequency. The oscillator can be calibrated to frequencies as specified in Table 22-
1 on page 181. Calibration outside that range is not guaranteed.
Note that this oscillator is used to time EEPROM and Flash write accesses, and these write times will be affected accordingly. If the EEPROM or Flash are written, do not calibrate to more than 8.8 MHz. Otherwise, the EEPROM or Flash write may fail.
The CAL7 bit determines the range of operation for the oscillator. Setting this bit to 0 gives the lowest frequency range, setting this bit to 1 gives the highest frequency range. The two fre­quency ranges are overlapping, in other words a setting of OSCCAL = 0x7F gives a higher frequency than OSCCAL = 0x80.
ATtiny24/44/84
The CAL6..0 bits are used to tune the frequency within the selected range. A setting of 0x00 gives the lowest frequency in that range, and a setting of 0x7F gives the highest frequency in the range.
7.10.2 Clock Prescale Register – CLKPR
Bit 76543210
0x26 (0x46)
Read/Write R/W RRRR/W R/W R/W R/W
Initial Value 0 0 0 0 See Bit Description
CLKPCE CLKPS3 CLKPS2 CLKPS1 CLKPS0 CLKPR
• Bit 7 – CLKPCE: Clock Prescaler Change Enable
The CLKPCE bit must be written to logic one to enable change of the CLKPS bits. The CLKPCE bit is only updated when the other bits in CLKPR are simultaniosly written to zero. CLKPCE is cleared by hardware four cycles after it is written or when the CLKPS bits are written. Rewriting the CLKPCE bit within this time-out period does neither extend the time-out period, nor clear the CLKPCE bit.
• Bits 6..4 – Res: Reserved Bits
These bits are reserved bits in the ATtiny24/44/84 and will always read as zero.
• Bits 3..0 – CLKPS3..0: Clock Prescaler Select Bits 3 - 0
These bits define the division factor between the selected clock source and the internal system clock. These bits can be written run-time to vary the clock frequency to suit the application requirements. As the divider divides the master clock input to the MCU, the speed of all synchro­nous peripherals is reduced when a division factor is used. The division factors are given in
Table 7-10 on page 34.
8006F–AVR–02/07
33
Page 34
To avoid unintentional changes of clock frequency, a special write procedure must be followed to change the CLKPS bits:
1. Write the Clock Prescaler Change Enable (CLKPCE) bit to one and all other bits in CLKPR to zero.
2. Within four cycles, write the desired value to CLKPS while writing a zero to CLKPCE.
Interrupts must be disabled when changing prescaler setting to make sure the write procedure is not interrupted.
The CKDIV8 Fuse determines the initial value of the CLKPS bits. If CKDIV8 is unprogrammed, the CLKPS bits will be reset to “0000”. If CKDIV8 is programmed, CLKPS bits are reset to “0011”, giving a division factor of eight at start up. This feature should be used if the selected clock source has a higher frequency than the maximum frequency of the device at the present operating conditions. Note that any value can be written to the CLKPS bits regardless of the CKDIV8 Fuse setting. The Application software must ensure that a sufficient division factor is chosen if the selcted clock source has a higher frequency than the maximum frequency of the device at the present operating conditions. The device is shipped with the CKDIV8 Fuse programmed.
Table 7-10. Clock Prescaler Select
CLKPS3 CLKPS2 CLKPS1 CLKPS0 Clock Division Factor
0000 1
0001 2
0010 4
0011 8
0100 16
0101 32
0110 64
0111 128
1000 256
1001 Reserved
1010 Reserved
1011 Reserved
1100 Reserved
1101 Reserved
1110 Reserved
1111 Reserved
34
ATtiny24/44/84
8006F–AVR–02/07
Page 35

8. Power Management and Sleep Modes

Sleep modes enable the application to shut down unused modules in the MCU, thereby saving power. The AVR provides various sleep modes allowing the user to tailor the power consump­tion to the application’s requirements.

8.1 Sleep Modes

Figure 7-1 on page 26 presents the different clock systems in the ATtiny24/44/84, and their dis-
tribution. The figure is helpful in selecting an appropriate sleep mode. Table 8-1 shows the different sleep modes and their wake up sources
Table 8-1. Active Clock Domains and Wake-up Sources in the Different Sleep Modes
Active Clock Domains Oscillators Wake-up Sources
ATtiny24/44/84

8.2 Idle Mode

CPU
Sleep Mode
Idle X X X X X X X X
ADC Noise Reduction
Power-down X
Stand-by
Note: 1. For INT0, only level interrupt.
(2)
2. Only recommended with external crystal or resonator selected as clock source
clk
FLASH
clk
IO
clk
ADC
clk
XXX
Main Clock
Source Enabled
INT0 and
Pin Change
SPM/
EEPROM
Ready
(1)
(1)
XX
XX X
(1)
ADC
Other I/O
Watchdog
X
Interrupt
To enter any of the three sleep modes, the SE bit in MCUCR must be written to logic one and a SLEEP instruction must be executed. The SM1..0 bits in the MCUCR Register select which sleep mode (Idle, ADC Noise Reduction, Standby or Power-down) will be activated by the SLEEP instruction. See Table 8-2 on page 38 for a summary.
If an enabled interrupt occurs while the MCU is in a sleep mode, the MCU wakes up. The MCU is then halted for four cycles in addition to the start-up time, executes the interrupt routine, and resumes execution from the instruction following SLEEP. The contents of the Register File and SRAM are unaltered when the device wakes up from sleep. If a reset occurs during sleep mode, the MCU wakes up and executes from the Reset Vector.
8006F–AVR–02/07
When the SM1..0 bits are written to 00, the SLEEP instruction makes the MCU enter Idle mode, stopping the CPU but allowing Analog Comparator, ADC, Timer/Counter, Watchdog, and the interrupt system to continue operating. This sleep mode basically halts clk
CPU
and clk
FLASH
, while
allowing the other clocks to run.
Idle mode enables the MCU to wake up from external triggered interrupts as well as internal ones like the Timer Overflow. If wake-up from the Analog Comparator interrupt is not required,
35
Page 36
the Analog Comparator can be powered down by setting the ACD bit in the Analog Comparator Control and Status Register – ACSR. This will reduce power consumption in Idle mode. If the ADC is enabled, a conversion starts automatically when this mode is entered.

8.3 ADC Noise Reduction Mode

When the SM1..0 bits are written to 01, the SLEEP instruction makes the MCU enter ADC Noise Reduction mode, stopping the CPU but allowing the ADC, the external interrupts, and the Watchdog to continue operating (if enabled). This sleep mode halts clk while allowing the other clocks to run.
This improves the noise environment for the ADC, enabling higher resolution measurements. If the ADC is enabled, a conversion starts automatically when this mode is entered. Apart form the ADC Conversion Complete interrupt, only an External Reset, a Watchdog Reset, a Brown-out Reset, an SPM/EEPROM ready interrupt, an external level interrupt on INT0 or a pin change interrupt can wake up the MCU from ADC Noise Reduction mode.

8.4 Power-down Mode

When the SM1..0 bits are written to 10, the SLEEP instruction makes the MCU enter Power­down mode. In this mode, the Oscillator is stopped, while the external interrupts, and the Watch- dog continue operating (if enabled). Only an External Reset, a Watchdog Reset, a Brown-out Reset, an external level interrupt on INT0, or a pin change interrupt can wake up the MCU. This sleep mode halts all generated clocks, allowing operation of asynchronous modules only.
I/O
, clk
, and clk
CPU
FLASH
,
Note that if a level triggered interrupt is used for wake-up from Power-down mode, the changed level must be held for some time to wake up the MCU. See ”External Interrupts” on page 52 for details

8.5 Standby Mode

When the SM1..0 bits are 11 and an external crystal/resonator clock option is selected, the SLEEP instruction makes the MCU enter Standby mode. This mode is identical to Power-down with the exception that the Oscillator is kept running. From Standby mode, the device wakes up in six clock cycles.

8.6 Power Reduction Register

The Power Reduction Register (PRR), see ”PRR – Power Reduction Register” on page 39, pro- vides a method to stop the clock to individualperipherals to reduce power consumption. The current state of the peripheral is frozenand the I/O registers can not be read or written. Resources used by the peripheral when stopping the clock will remain occupied, hence the peripheral should in most cases be disabled before stopping the clock. Waking up a module, which is done by clearing the bit in PRR, puts the module in the same state as before shutdown.
Module shutdown can be used in Idle mode and Active mode to significantly reduce the overall power consumption. See ”Power-down Supply Current” on page 194 for examples. In all other sleep modes, the clock is already stopped.
36
ATtiny24/44/84
8006F–AVR–02/07
Page 37

8.7 Minimizing Power Consumption

There are several issues to consider when trying to minimize the power consumption in an AVR controlled system. In general, sleep modes should be used as much as possible, and the sleep mode should be selected so that as few as possible of the device’s functions are operating. All functions not needed should be disabled. In particular, the following modules may need special consideration when trying to achieve the lowest possible power consumption.

8.7.1 Analog to Digital Converter

If enabled, the ADC will be enabled in all sleep modes. To save power, the ADC should be dis­abled before entering any sleep mode. When the ADC is turned off and on again, the next conversion will be an extended conversion. See ”Analog to Digital Converter” on page 138 for details on ADC operation.

8.7.2 Analog Comparator

When entering Idle mode, the Analog Comparator should be disabled if not used. When entering ADC Noise Reduction mode, the Analog Comparator should be disabled. In the other sleep modes, the Analog Comparator is automatically disabled. However, if the Analog Comparator is set up to use the Internal Voltage Reference as input, the Analog Comparator should be dis­abled in all sleep modes. Otherwise, the Internal Voltage Reference will be enabled, independent of sleep mode. See ”Analog Comparator” on page 134 for details on how to config- ure the Analog Comparator.
ATtiny24/44/84

8.7.3 Brown-out Detector

If the Brown-out Detector is not needed in the application, this module should be turned off. If the Brown-out Detector is enabled by the BODLEVEL Fuses, it will be enabled in all sleep modes, and hence, always consume power. In the deeper sleep modes, this will contribute significantly to the total current consumption. See ”Brown-out Detection” on page 43 for details on how to configure the Brown-out Detector.

8.7.4 Internal Voltage Reference

The Internal Voltage Reference will be enabled when needed by the Brown-out Detection, the Analog Comparator or the ADC. If these modules are disabled as described in the sections above, the internal voltage reference will be disabled and it will not be consuming power. When turned on again, the user must allow the reference to start up before the output is used. If the reference is kept on in sleep mode, the output can be used immediately. See ”Internal Voltage
Reference” on page 44 for details on the start-up time.

8.7.5 Watchdog Timer

If the Watchdog Timer is not needed in the application, this module should be turned off. If the Watchdog Timer is enabled, it will be enabled in all sleep modes, and hence, always consume
power. In the deeper sleep modes, this will contribute significantly to the total current consump­tion. See Watchdog Timer” on page 44 for details on how to configure the Watchdog Timer.

8.7.6 Port Pins

8006F–AVR–02/07
When entering a sleep mode, all port pins should be configured to use minimum power. The most important thing is then to ensure that no pins drive resistive loads. In sleep modes where both the I/O clock (clk will be disabled. This ensures that no power is consumed by the input logic when not needed. In
) and the ADC clock (clk
I/O
) are stopped, the input buffers of the device
ADC
37
Page 38
some cases, the input logic is needed for detecting wake-up conditions, and it will then be enabled. See the section ”Digital Input Enable and Sleep Modes” on page 60 for details on which pins are enabled. If the input buffer is enabled and the input signal is left floating or has an analog signal level close to V
For analog input pins, the digital input buffer should be disabled at all times. An analog signal level close to V
CC
input buffers can be disabled by writing to the Digital Input Disable Register (DIDR0). See
”DIDR0 – Digital Input Disable Register 0” on page 156 for details.

8.8 Register Description

8.8.1 MCUCR – MCU Control Register
The MCU Control Register contains control bits for power management.
Bit 76543210
Read/Write R R/W R/W R/W R/W RR/W R/W
Initial Value00000000
• Bit 5 – SE: Sleep Enable
The SE bit must be written to logic one to make the MCU enter the sleep mode when the SLEEP instruction is executed. To avoid the MCU entering the sleep mode unless it is the programmer’s purpose, it is recommended to write the Sleep Enable (SE) bit to one just before the execution of the SLEEP instruction and to clear it immediately after waking up.
/2, the input buffer will use excessive power.
CC
/2 on an input pin can cause significant current even in active mode. Digital
PUD SE SM1 SM0 ISC01 ISC00 MCUCR
• Bits 4, 3 – SM1..0: Sleep Mode Select Bits 2..0
These bits select between the three available sleep modes as shown in Table 8-2 on page 38.
Table 8-2. Sleep Mode Select
SM1 SM0 Sleep Mode
00Idle
0 1 ADC Noise Reduction
1 0 Power-down
1 1 Standby
Note: 1. Only recommended with external crystal or resonator selected as clock source
(1)
• Bit 2 – Res: Reserved Bit
This bit is a reserved bit in the ATtiny24/44/84 and will always read as zero.
38
ATtiny24/44/84
8006F–AVR–02/07
Page 39
8.8.2 PRR – Power Reduction Register
Bit 76543 2 10
PRTIM1 PRTIM0 PRUSI PRADC PRR
Read/Write RRRRR/W R/W R/W R/W
Initial Value 0 0 0 0 0 0 0 0
• Bits 7, 6, 5, 4- Res: Reserved Bits
These bits are reserved bits in the ATtiny24/44/84 and will always read as zero.
• Bit 3- PRTIM1: Power Reduction Timer/Counter1
Writing a logic one to this bit shuts down the Timer/Counter1 module. When the Timer/Counter1 is enabled, operation will continue like before the shutdown.
• Bit 2- PRTIM0: Power Reduction Timer/Counter0
Writing a logic one to this bit shuts down the Timer/Counter0 module. When the Timer/Counter0 is enabled, operation will continue like before the shutdown.
• Bit 1 - PRUSI: Power Reduction USI
Writing a logic one to this bit shuts down the USI by stopping the clock to the module. When waking up the USI again, the USI should be re initialized to ensure proper operation.
ATtiny24/44/84
• Bit 0 - PRADC: Power Reduction ADC
Writing a logic one to this bit shuts down the ADC. The ADC must be disabled before shut down. The analog comparator cannot use the ADC input MUX when the ADC is shut down.
8006F–AVR–02/07
39
Page 40

9. System Control and Reset

9.0.1 Resetting the AVR

During reset, all I/O Registers are set to their initial values, and the program starts execution from the Reset Vector. The instruction placed at the Reset Vector must be a RJMP – 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 these locations. The circuit diagram in Figure 9-1 on page 41 shows the reset logic. Table 22-3 on
page 182 defines the electrical parameters of the reset circuitry.
The I/O ports of the AVR are immediately reset to their initial state when a reset source goes active. This does not require any clock source to be running.
After all reset sources have gone inactive, a delay counter is invoked, stretching the internal reset. This allows the power to reach a stable level before normal operation starts. The time-out period of the delay counter is defined by the user through the SUT and CKSEL Fuses. The dif­ferent selections for the delay period are presented in ”Clock Sources” on page 27.

9.0.2 Reset Sources

The ATtiny24/44/84 has four sources of reset:
• Power-on Reset. The MCU is reset when the supply voltage is below the Power-on Reset
threshold (V
• External Reset. The MCU is reset when a low level is present on the RESET
the minimum pulse length when RESET
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
threshold (V
POT
BOT
).
function is enabled.
) and the Brown-out Detector is enabled.
pin for longer than
is below the Brown-out Reset
CC
40
ATtiny24/44/84
8006F–AVR–02/07
Page 41
Figure 9-1. Reset Logic
]
Power-on Reset
Circuit
DATA BU S
MCU Status
Register (MCUSR)
BORF
PORF
WDRF
EXTRF
ATtiny24/44/84

9.0.3 Power-on Reset

BODLEVEL [1..0]
Pull-up Resistor
SPIKE
FILTER
Brown-out
Reset Circuit
Watchdog
Oscillator
Clock
Generator
CKSEL[1:0]
SUT[1:0
CK
Delay Counters
TIMEOUT
A Power-on Reset (POR) pulse is generated by an On-chip detection circuit. The detection level is defined in ”System and Reset Characterizations” on page 182. The POR is activated when- ever V
is below the detection level. The POR circuit can be used to trigger the Start-up Reset,
CC
as well as to 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 the delay counter, which determines how long the device is kept in RESET after V when V
decreases below the detection level.
CC
rise. The RESET signal is activated again, without any delay,
CC
8006F–AVR–02/07
Figure 9-2. MCU Start-up, RESET
V
POT
V
RST
t
TOUT
V
CC
RESET
TIME-OUT
INTERNAL
RESET
Tied to V
CC
41
Page 42

9.0.4 External Reset

Figure 9-3. MCU Start-up, RESET Extended Externally
V
V
CC
RESET
TIME-OUT
INTERNAL
RESET
POT
V
RST
t
TOUT
An External Reset is generated by a low level on the RESET
pin if enabled. Reset pulses longer than the minimum pulse width (see ”System and Reset Characterizations” on page 182) 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 edge, the delay counter starts the MCU after the Time-out period – t
TOUT –
– on its positive
RST
has expired.
Figure 9-4. External Reset During Operation
CC
42
ATtiny24/44/84
8006F–AVR–02/07
Page 43

9.0.5 Brown-out Detection

ATtiny24/44/84
ATtiny24/44/84 has an On-chip Brown-out Detection (BOD) circuit for monitoring the V
CC
level during operation by comparing it to a fixed trigger level. The trigger level for the BOD can be selected by the BODLEVEL Fuses. The trigger level has a hysteresis to ensure spike free Brown-out Detection. The hysteresis on the detection level should be interpreted as V V
+ V
BOT
When the BOD is enabled, and V
9-5 on page 43), the Brown-out Reset is immediately activated. When V
trigger level (V out period t
The BOD circuit will only detect a drop in V longer than t
HYST
/2 and V
BOT+
has expired.
TOUT
given in ”System and Reset Characterizations” on page 182.
BOD
BOT-
= V
BOT
- V
CC
/2.
HYST
decreases to a value below the trigger level (V
in Figure 9-5 on page 43), the delay counter starts the MCU after the Time-
if the voltage stays below the trigger level for
CC
in Figure
BOT-
increases above the
CC
BOT+
=
Figure 9-5. Brown-out Reset During Operation
V
CC
RESET
TIME-OUT
V
BOT-
V
BOT+
t
TOUT

9.0.6 Watchdog Reset

INTERNAL
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 t
TOUT
. See
Watchdog Timer” on page 44 for details on operation of the Watchdog Timer.
Figure 9-6. Watchdog Reset During Operation
SDI (PB0), SII (PB1)
t
IVSH
t
SHIX
t
SLSH
SCI (PB3)
t
SHSL
SDO (PB2)
t
SHOV
8006F–AVR–02/07
43
Page 44

9.1 Internal Voltage Reference

ATtiny24/44/84 features an internal bandgap reference. This reference is used for Brown-out Detection, and it can be used as an input to the Analog Comparator or the ADC.

9.1.1 Voltage Reference Enable Signals and Start-up Time

The voltage reference has a start-up time that may influence the way it should be used. The start-up time is given in ”System and Reset Characterizations” on page 182. To save power, the reference is not always turned on. The reference is on during the following situations:
1. When the BOD is enabled (by programming the BODLEVEL [2..0] 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.

9.2 Watchdog Timer

The Watchdog Timer is clocked from an On-chip Oscillator which runs at 128 kHz. By controlling the Watchdog Timer prescaler, the Watchdog Reset interval can be adjusted as shown in Table
9-3 on page 48. 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. Ten different clock cycle periods can be selected to determine the reset period. If the reset period expires without another Watchdog Reset, the ATtiny24/44/84 resets and executes from the Reset Vec­tor. For timing details on the Watchdog Reset, refer to Table 9-3 on page 48.
The Wathdog Timer can also be configured to generate an interrupt instead of a reset. This can be very helpful when using the Watchdog to wake-up from Power-down.
To prevent unintentional disabling of the Watchdog or unintentional change of time-out period, two different safety levels are selected by the fuse WDTON as shown in Table 9-1. See ”Timed
Sequences for Changing the Configuration of the Watchdog Timer” on page 45 for details.
Table 9-1. WDT Configuration as a Function of the Fuse Settings of WDTON
Safety
WDTON
Unprogrammed 1 Disabled Timed sequence No limitations
Programmed 2 Enabled Always enabled Timed sequence
Level
WDT Initial State
How to Disable the WDT
How to Change Time­out
44
ATtiny24/44/84
8006F–AVR–02/07
Page 45
Figure 9-7. Watchdog Timer
ATtiny24/44/84
128 kHz
OSCILLATOR
WATCHDOG
RESET
WDP0 WDP1 WDP2 WDP3
WDE
OSC/2K
WATCHDOG
PRESCALER
OSC/4K
OSC/8K
OSC/16K
MCU RESET
OSC/32K
OSC/64K
MUX
OSC/128K
OSC/256K
OSC/512K
OSC/1024K

9.3 Timed Sequences for Changing the Configuration of the Watchdog Timer

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

9.3.1 Safety Level 1

In this mode, the Watchdog Timer is initially disabled, but can be enabled by writing the WDE bit to one without any restriction. A timed sequence is needed when disabling an enabled Watch- dog Timer. 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 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.

9.3.2 Safety Level 2

8006F–AVR–02/07
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.
2. 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.
45
Page 46

9.4 Register Description

9.4.1 MCUSR – MCU Status Register
The MCU Status Register provides information on which reset source caused an MCU Reset.
Bit 76543210
0x34 (0x54) WDRF BORF EXTRF PORF MCUSR
Read/Write RRRRR/W R/W R/W R/W
Initial Value 0 0 0 0 See Bit Description
• Bits 7..4 – Res: Reserved Bits
These bits are reserved bits in the ATtiny24/44/84 and will always read as zero.
• Bit 3 – WDRF: Watchdog Reset Flag
This bit is set if a Watchdog Reset occurs. The bit is reset 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 reset 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 reset 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 reset 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 reset the MCUSR as early as possible in the program. If the register is cleared before another reset occurs, the source of the reset can be found by examining the Reset Flags.
9.4.2 WDTCSR – Watchdog Timer Control and Status Register
Bit 76543210
0x21 (0x41) WDIF WDIE WDP3 WDCE WDE WDP2 WDP1 WDP0 WDTCSR
Read/Write R/W R/W R/W R/W R/W R/W R/W R/W
Initial Value 0 0 0 0 X 0 0 0
• Bit 7 – WDIF: Watchdog Timeout Interrupt Flag
This bit is set when a time-out occurs in the Watchdog Timer and the Watchdog Timer is config­ured for interrupt. WDIF is cleared by hardware when executing the corresponding interrupt handling vector. Alternatively, WDIF is cleared by writing a logic one to the flag. When the I-bit in SREG and WDIE are set, the Watchdog Time-out Interrupt is executed.
• Bit 6 – WDIE: Watchdog Timeout Interrupt Enable
When this bit is written to one, WDE is cleared, and the I-bit in the Status Register is set, the Watchdog Time-out Interrupt is enabled. In this mode the corresponding interrupt is executed instead of a reset if a timeout in the Watchdog Timer occurs.
46
If WDE is set, WDIE is automatically cleared by hardware when a time-out occurs. This is useful for keeping the Watchdog Reset security while using the interrupt. After the WDIE bit is cleared,
ATtiny24/44/84
8006F–AVR–02/07
Page 47
ATtiny24/44/84
the next time-out will generate a reset. To avoid the Watchdog Reset, WDIE must be set after each interrupt.
Table 9-2. Watchdog Timer Configuration
WDE WDIE Watchdog Timer State Action on Time-out
0 0 Stopped None
0 1 Running Interrupt
1 0 Running Reset
1 1 Running Interrupt
• Bit 4 – WDCE: Watchdog Change Enable
This bit must be set when the WDE bit is written to logic zero. Otherwise, the Watchdog will not be disabled. Once written to one, hardware will clear this bit after four clock cycles. See the description of the WDE bit for a Watchdog disable procedure. This bit must also be set when changing the prescaler bits. See ”Timed Sequences for Changing the Configuration of the
Watchdog Timer” on page 45.
• 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. In safety level 2, it is not possible to disable the Watchdog Timer, even with the algorithm
described above. See ”Timed Sequences for Changing the Configuration of the Watchdog
Timer” on page 45.
In safety level 1, WDE is overridden by WDRF in MCUSR. See ”MCUSR – MCU Status Regis-
ter” on page 46 for description of WDRF. This means that WDE is always set when WDRF is set.
To clear WDE, WDRF must be cleared before disabling the Watchdog with the procedure described above. This feature ensures multiple resets during conditions causing failure, and a safe start-up after the failure.
Note: If the watchdog timer is not going to be used in the application, it is important to go through a
watchdog disable procedure in the initialization of the device. If the Watchdog is accidentally enabled, for example by a runaway pointer or brown-out condition, the device will be reset, which in turn will lead to a new watchdog reset. To avoid this situation, the application software should always clear the WDRF flag and the WDE control bit in the initialization routine.
• Bits 5, 2..0 – WDP3..0: Watchdog Timer Prescaler 3, 2, 1, and 0
The WDP3..0 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 9-3 on page 48.
8006F–AVR–02/07
47
Page 48
Table 9-3. Watchdog Timer Prescale Select
Number of WDT Oscillator
WDP3 WDP2 WDP1 WDP0
0 0 0 0 2K cycles 16 ms
0 0 0 1 4K cycles 32 ms
0 0 1 0 8K cycles 64 ms
0 0 1 1 16K cycles 0.125 s
0 1 0 0 32K cycles 0.25 s
0 1 0 1 64K cycles 0.5 s
0 1 1 0 128K cycles 1.0 s
0 1 1 1 256K cycles 2.0 s
1 0 0 0 512K cycles 4.0 s
1 0 0 1 1024K cycles 8.0 s
1010
1011
1100
1101
1110
1111
Cycles
Reserved
Typical Time-out at
VCC = 5.0V
48
ATtiny24/44/84
8006F–AVR–02/07
Page 49
ATtiny24/44/84
The following code example shows one assembly and one C function for turning off the WDT. The example assumes that interrupts are controlled (e.g., by disabling interrupts globally) so that no interrupts will occur during execution of these functions.
Assembly Code Example
WDT_off:
WDR
; Clear WDRF in MCUSR
ldi r16, (0<<WDRF)
out MCUSR, r16
; Write logical one to WDCE and WDE
; Keep old prescaler setting to prevent unintentional Watchdog Reset
in r16, WDTCSR
ori r16, (1<<WDCE)|(1<<WDE)
out WDTCSR, r16
; Turn off WDT
ldi r16, (0<<WDE)
out WDTCSR, r16
ret
C Code Example
(1)
(1)
void WDT_off(void)
{
_WDR();
/* Clear WDRF in MCUSR */
MCUSR = 0x00
/* Write logical one to WDCE and WDE */
WDTCSR |= (1<<WDCE) | (1<<WDE);
/* Turn off WDT */
WDTCSR = 0x00;
}
Note: 1. See ”About Code Examples” on page 7.
8006F–AVR–02/07
49
Page 50

10. Interrupts

This section describes the specifics of the interrupt handling as performed in ATtiny24/44/84. For a general explanation of the AVR interrupt handling, see ”Reset and Interrupt Handling” on
page 13.

10.1 Interrupt Vectors

Table 10-1. Reset and Interrupt Vectors
Vector No. Program Address Source Interrupt Definition
1 0x0000 RESET
2 0x0001 EXT_INT0 External Interrupt Request 0
3 0x0002 PCINT0 Pin Change Interrupt Request 0
4 0x0003 PCINT1 Pin Change Interrupt Request 1
5 0x0004 WATCHDOG Watchdog Time-out
6 0x0005 TIM1_CAPT Timer/Counter1 Capture Event
7 0x0006 TIM1_COMPA Timer/Counter1 Compare Match A
8 0x0007 TIM1_COMPB Timer/Counter1 Compare Match B
9 0x0008 TIM1_OVF Timer/Counter1 Overflow
10 0x0009 TIM0_COMPA Timer/Counter0 Compare Match A
11 0x000A TIM0_COMPB Timer/Counter0 Compare Match B
12 0x000B TIM0_OVF Timer/Counter0 Overflow
13 0x000C ANA_COMP Analog Comparator
14 0x000D ADC ADC Conversion Complete
15 0x000E EE_RDY EEPROM Ready
16 0x000F USI_STR USI START
17 0x0010 USI_OVF USI Overflow
External Pin, Power-on Reset, Brown-out Reset, Watchdog Reset
50
If the program never enables an interrupt source, the Interrupt Vectors are not used, and regular program code can be placed at these locations. The most typical and general program setup for the Reset and Interrupt Vector Addresses in ATtiny24/44/84 is:
ATtiny24/44/84
8006F–AVR–02/07
Page 51
ATtiny24/44/84
Address Labels Code Comments
0x0000 rjmp RESET ; Reset Handler
0x0001 rjmp EXT_INT0 ; IRQ0 Handler
0x0002 rjmp PCINT0 ; PCINT0 Handler
0x0003 rjmp PCINT1 ; PCINT1 Handler
0x0004 rjmp WATCHDOG ; Watchdog Interrupt Handler
0x0005 rjmp TIM1_CAPT ; Timer1 Capture Handler
0x0006 rjmp TIM1_COMPA ; Timer1 Compare A Handler
0x0007 rjmp TIM1_COMPB ; Timer1 Compare B Handler
0x0008 rjmp TIM1_OVF ; Timer1 Overflow Handler
0x0009 rjmp TIM0_COMPA ; Timer0 Compare A Handler
0x000A rjmp TIM0_COMPB ; Timer0 Compare B Handler
0x000B rjmp TIM0_OVF ; Timer0 Overflow Handler
0x000C rjmp ANA_COMP ; Analog Comparator Handler
0x000D rjmp ADC ; ADC Conversion Handler
0x000E rjmp EE_RDY ; EEPROM Ready Handler
0x000F rjmp USI_STR ; USI STart Handler
0x0010 rjmp USI_OVF ; USI Overflow Handler
;
0x0011 RESET: ldi r16, high(RAMEND); Main program start
0x0012 out SPH,r16 ; Set Stack Pointer to top of RAM
0x0013 ldi r16, low(RAMEND)
0x0014 out SPL,r16
0x0015 sei ; Enable interrupts
0x0016 <instr> xxx
... ... ... ...
8006F–AVR–02/07
51
Page 52

11. External Interrupts

The External Interrupts are triggered by the INT0 pin or any of the PCINT11..0 pins. Observe that, if enabled, the interrupts will trigger even if the INT0 or PCINT11..0 pins are configured as outputs. This feature provides a way of generating a software interrupt. Pin change 0 interrupts PCI0 will trigger if any enabled PCINT7..0 pin toggles. Pin change 1 interrupts PCI1 will trigger if any enabled PCINT11..8 pin toggles. The PCMSK0 and PCMSK1 Registers control which pins contribute to the pin change interrupts. Pin change interrupts on PCINT11..0 are detected asyn­chronously. This implies that these interrupts can be used for waking the part also from sleep modes other than Idle mode.
The INT0 interrupts can be triggered by a falling or rising edge or a low level. This is set up as indicated in the specification for the MCU Control Register – MCUCR. When the INT0 interrupt is enabled and is configured as level triggered, the interrupt will trigger as long as the pin is held low. Note that recognition of falling or rising edge interrupts on INT0 requires the presence of an I/O clock, described in ”Clock Systems and their Distribution” on page 26. Low level interrupt on INT0 is detected asynchronously. This implies that this interrupt can be used for waking the part also from sleep modes other than Idle mode. The I/O clock is halted in all sleep modes except Idle mode.
Note that if a level triggered interrupt is used for wake-up from Power-down, the required level must be held long enough for the MCU to complete the wake-up to trigger the level interrupt. If the level disappears before the end of the Start-up Time, the MCU will still wake up, but no inter­rupt will be generated. The start-up time is defined by the SUT and CKSEL Fuses as described in ”System Clock and Clock Options” on page 26.

11.1 Pin Change Interrupt Timing

An example of timing of a pin change interrupt is shown in Figure .
Timing of pin change interrupts
PCINT(0)
pin_sync
pcint_in_(0)
pcint_syn
pcint_setflag
PCINT(0)
pin_lat
pin_lat
D Q
LE
clk
clk
pin_sync
PCINT(0) in PCMSK(x)
pcint_in_(0)
0
x
clk
pcint_syn
pcint_setflag
PCIF
52
PCIF
ATtiny24/44/84
8006F–AVR–02/07
Page 53

11.2 Register Description

11.2.1 MCUCR – MCU Control Register
The External Interrupt Control Register A contains control bits for interrupt sense control.
Bit 76543210
0x35 (0x55)
Read/Write R R/W R/W R/W R/W RR/W R/W
Initial Value 0 0 0 0 0 0 0 0
• Bits 1, 0 – ISC01, ISC00: Interrupt Sense Control 0 Bit 1 and Bit 0
The External Interrupt 0 is activated by the external pin INT0 if the SREG I-flag and the corre­sponding interrupt mask are set. The level and edges on the external INT0 pin that activate the interrupt are defined in Table 11-1 on page 53. The value on the INT0 pin is sampled before detecting edges. If edge or toggle interrupt is selected, pulses that last longer than one clock period will generate an interrupt. Shorter pulses are not guaranteed to generate an interrupt. If low level interrupt is selected, the low level must be held until the completion of the currently executing instruction to generate an interrupt.
Table 11-1. Interrupt 0 Sense Control
ATtiny24/44/84
PUD SE SM1 SM0 ISC01 ISC00 MCUCR
ISC01 ISC00 Description
0 0 The low level of INT0 generates an interrupt request.
0 1 Any logical change on INT0 generates an interrupt request.
1 0 The falling edge of INT0 generates an interrupt request.
1 1 The rising edge of INT0 generates an interrupt request.
11.2.2 GIMSK – General Interrupt Mask Register
Bit 76543210
0x3B (0x5B) INT0 PCIE1 PCIE0 GIMSK
Read/Write R R/W R/W R/wRRRR
Initial Value 0 0 0 0 0 0 0 0
• Bits 7, 3..0 – Res: Reserved Bits
These bits are reserved bits in the ATtiny24/44/84 and will always read as zero.
• 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 exter­nal pin interrupt is enabled. The Interrupt Sense Control0 bits 1/0 (ISC01 and ISC00) in the External Interrupt Control Register A (EICRA) define whether the external interrupt is activated on rising and/or falling edge of the INT0 pin or level sensed. Activity on the pin will cause an interrupt request even if INT0 is configured as an output. The corresponding interrupt of External Interrupt Request 0 is executed from the INT0 Interrupt Vector.
8006F–AVR–02/07
• Bit 5 – PCIE1: Pin Change Interrupt Enable 1
When the PCIE1 bit is set (one) and the I-bit in the Status Register (SREG) is set (one), pin change interrupt 1 is enabled. Any change on any enabled PCINT11..8 pin will cause an inter­rupt. The corresponding interrupt of Pin Change Interrupt Request is executed from the PCI1 Interrupt Vector. PCINT11..8 pins are enabled individually by the PCMSK1 Register.
53
Page 54
• Bit 4– PCIE0: Pin Change Interrupt Enable 0
When the PCIE0 bit is set (one) and the I-bit in the Status Register (SREG) is set (one), pin change interrupt 0 is enabled. Any change on any enabled PCINT7..0 pin will cause an interrupt. The corresponding interrupt of Pin Change Interrupt Request is executed from the PCI0 Inter­rupt Vector. PCINT7..0 pins are enabled individually by the PCMSK0 Register.
11.2.3 GIFR – General Interrupt Flag Register
Bit 76543210
0x3A (0x5A –INTF0PCIF1PCIF0––––GIFR
Read/Write R R/W R/W R/W RRRR
Initial Value 0 0 0 0 0 0 0 0
• Bits 7, 3..0 – Res: Reserved Bits
These bits are reserved bits in the ATtiny24/44/84 and will always read as zero.
• Bit 6 – INTF0: External Interrupt Flag 0
When an edge or logic change 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 cor­responding Interrupt Vector. The flag is cleared when the interrupt routine is executed. Alternatively, the flag can be cleared by writing a logical one to it. This flag is always cleared when INT0 is configured as a level interrupt.
• Bit 5 – PCIF1: Pin Change Interrupt Flag 1
When a logic change on any PCINT11..8 pin triggers an interrupt request, PCIF1 becomes set (one). If the I-bit in SREG and the PCIE1 bit in GIMSK are set (one), the MCU will jump to the corresponding Interrupt Vector. The flag is cleared when the interrupt routine is executed. Alter­natively, the flag can be cleared by writing a logical one to it.
• Bit 4– PCIF0: Pin Change Interrupt Flag 0
When a logic change on any PCINT7..0 pin triggers an interrupt request, PCIF becomes set (one). If the I-bit in SREG and the PCIE0 bit in GIMSK are set (one), the MCU will jump to the corresponding Interrupt Vector. The flag is cleared when the interrupt routine is executed. Alter­natively, the flag can be cleared by writing a logical one to it.
11.2.4 PCMSK1 – Pin Change Mask Register 1
Bit 7 6 5 4 3 2 1 0
0x20 (0x40) PCINT11 PCINT10 PCINT9 PCINT8 PCMSK1
Read/Write R R R R R/W R/W R/W R/W
Initial Value 0 0 0 0 0 0 0 0
• Bits 7, 4– Res: Reserved Bits
These bits are reserved bits in the ATtiny24/44/84 and will always read as zero.
• Bits 3..0 – PCINT11..8: Pin Change Enable Mask 11..8
Each PCINT11..8 bit selects whether pin change interrupt is enabled on the corresponding I/O pin. If PCINT11..8 is set and the PCIE1 bit in GIMSK is set, pin change interrupt is enabled on
54
ATtiny24/44/84
8006F–AVR–02/07
Page 55
the corresponding I/O pin. If PCINT11..8 is cleared, pin change interrupt on the corresponding I/O pin is disabled.
11.2.5 PCMSK0 – Pin Change Mask Register 0
Bit 76543210
0x12 (0x32) PCINT7 PCINT6 PCINT5 PCINT4 PCINT3 PCINT2 PCINT1 PCINT0 PCMSK0
Read/Write R/W R/W R/W R/W R/W R/W R/W R/W
Initial Value 0 0 0 0 0 0 0 0
• Bits 7..0 – PCINT7..0: Pin Change Enable Mask 7..0
Each PCINT7..0 bit selects whether pin change interrupt is enabled on the corresponding I/O pin. If PCINT7..0 is set and the PCIE0 bit in GIMSK is set, pin change interrupt is enabled on the corresponding I/O pin. If PCINT7..0 is cleared, pin change interrupt on the corresponding I/O pin is disabled.
ATtiny24/44/84
8006F–AVR–02/07
55
Page 56

12. I/O Ports

12.1 Overview

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 V
cal Characteristics” on page 179 for a complete list of parameters.
Figure 12-1. I/O Pin Equivalent Schematic
and Ground as indicated in Figure 12-1 on page 56. See ”Electri-
CC
R
pu
Pxn
C
pin
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. For example, 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 ”EXT_CLOCK = external clock is selected as system clock.” on
page 70.
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. However, writing a logic one to a bit in the PINx Register, will result in a toggle in the correspond­ing bit in the Data Register. In addition, the Pull-up Disable – PUD bit in MCUCR 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
57. 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 61. Refer to the individual module sections for a full description of the alter-
nate functions.
See Figure
"General Digital I/O" for
Logic
Details
56
ATtiny24/44/84
8006F–AVR–02/07
Page 57
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.

12.2 Ports as General Digital I/O

The ports are bi-directional I/O ports with optional internal pull-ups. Figure 12-2 shows a func­tional description of one I/O-port pin, here generically called Pxn.
ATtiny24/44/84
Figure 12-2. General Digital I/O
Pxn
PUD: PULLUP DISABLE SLEEP: SLEEP CONTROL clk
: I/O CLOCK
I/O
(1)
SLEEP
SYNCHRONIZER
DLQ
D
PINxn
Q
PUD
Q D
DDxn
Q
CLR
RESET
D
Q
PORTxn
Q
CLR
RESET
Q
Q
WDx: WRITE DDRx RDx: READ DDRx WRx: WRITE PORTx RRx: READ PORTx REGISTER RPx: READ PORTx PIN WPx: WRITE PINx REGISTER
RRx
WDx
RDx
RPx
clk
1
0
I/O
WRx
DATA BUS
WPx
Note: 1. WRx, WPx, WDx, RRx, RPx, and RDx are common to all pins within the same port. clk

12.2.1 Configuring the Pin

Each port pin consists of three register bits: DDxn, PORTxn, and PINxn. As shown in
”EXT_CLOCK = external clock is selected as system clock.” on page 70, the DDxn bits are
accessed at the DDRx I/O address, the PORTxn bits at the PORTx I/O address, and the PINxn bits at the PINx I/O address.
The DDxn bit in the DDRx Register selects the direction of this pin. If DDxn is written logic one, Pxn is configured as an output pin. If DDxn is written logic zero, Pxn is configured as an input pin.
If PORTxn is written logic one when the pin is configured as an input pin, the pull-up resistor is activated. To switch the pull-up resistor off, PORTxn has to be written logic zero or the pin has to be configured as an output pin. The port pins are tri-stated when reset condition becomes active, even if no clocks are running.
8006F–AVR–02/07
SLEEP, and PUD are common to all ports.
I/O
,
57
Page 58
If PORTxn is written logic one when the pin is configured as an output pin, the port pin is driven high (one). If PORTxn is written logic zero when the pin is configured as an output pin, the port pin is driven low (zero).

12.2.2 Toggling the Pin

Writing a logic one to PINxn toggles the value of PORTxn, independent on the value of DDRxn. Note that the SBI instruction can be used to toggle one single bit in a port.

12.2.3 Switching Between Input and Output

When switching between tri-state ({DDxn, PORTxn} = 0b00) and output high ({DDxn, PORTxn} = 0b11), an intermediate state with either pull-up enabled {DDxn, PORTxn} = 0b01) or output low ({DDxn, PORTxn} = 0b10) must occur. Normally, the pull-up enabled state is fully accept­able, as a high-impedant environment will not notice the difference between a strong high driver and a pull-up. If this is not the case, the PUD bit in the MCUCR Register can be set to disable all pull-ups in all ports.
Switching between input with pull-up and output low generates the same problem. The user must use either the tri-state ({DDxn, PORTxn} = 0b00) or the output high state ({DDxn, PORTxn} = 0b10) as an intermediate step.
Table 12-1 on page 58 summarizes the control signals for the pin value.
Table 12-1. Port Pin Configurations
DDxn PORTxn
0 0 X Input No Tri-state (Hi-Z)
0 1 0 Input Yes Pxn will source current if ext. pulled low.
PUD
(in MCUCR) I/O Pull-up Comment
0 1 1 Input No Tri-state (Hi-Z)
1 0 X Output No Output Low (Sink)
1 1 X Output No Output High (Source)

12.2.4 Reading the Pin Value

Independent of the setting of Data Direction bit DDxn, the port pin can be read through the PINxn Register bit. As shown in Figure 12-2 on page 57, the PINxn Register bit and the preced­ing latch constitute a synchronizer. This is needed to avoid metastability if the physical pin changes value near the edge of the internal clock, but it also introduces a delay. Figure 12-3 on
page 59 shows a timing diagram of the synchronization when reading an externally applied pin
value. The maximum and minimum propagation delays are denoted t respectively.
pd,max
and t
pd,min
58
ATtiny24/44/84
8006F–AVR–02/07
Page 59
ATtiny24/44/84
Figure 12-3. Synchronization when Reading an Externally Applied Pin value
SYSTEM CLK
INSTRUCTIONS
XXX in r17, PINx
XXX
SYNC LATCH
PINxn
r17
0x00 0xFF
t
pd, max
t
pd, min
Consider the clock period starting shortly after the first falling edge of the system clock. The latch is closed when the clock is low, and goes transparent when the clock is high, as indicated by the shaded region of the “SYNC LATCH” signal. The signal value is latched when the system clock goes low. It is clocked into the PINxn Register at the succeeding positive clock edge. As indi­cated by the two arrows tpd,max and tpd,min, a single signal transition on the pin will be delayed between ½ and 1½ system clock period depending upon the time of assertion.
When reading back a software assigned pin value, a nop instruction must be inserted as indi­cated in Figure 12-4 on page 59. The out instruction sets the “SYNC LATCH” signal at the positive edge of the clock. In this case, the delay tpd through the synchronizer is one system clock period.
Figure 12-4. Synchronization when Reading a Software Assigned Pin Value
SYSTEM CLK
r16
INSTRUCTIONS
out PORTx, r16 nop in r17, PINx
0xFF
SYNC LATCH
PINxn
r17
0x00 0xFF
t
pd
The following code example shows how to set port A pins 0 and 1 high, 2 and 3 low, and define the port pins from 4 to 5 as input with a pull-up assigned to port pin 4. The resulting pin values
8006F–AVR–02/07
59
Page 60
are read back again, but as previously discussed, a nop instruction is included to be able to read back the value recently assigned to some of the pins.
Assembly Code Example
...
; Define pull-ups and set outputs high
; Define directions for port pins
ldi r16,(1<<PA4)|(1<<PA1)|(1<<PA0)
ldi r17,(1<<DDA3)|(1<<DDA2)|(1<<DDA1)|(1<<DDA0)
out PORTA,r16
out DDRA,r17
; Insert nop for synchronization
nop
; Read port pins
in r16,PINA
...
(1)
C Code Example
unsigned char i;
...
/* Define pull-ups and set outputs high */
/* Define directions for port pins */
PORTA = (1<<PA4)|(1<<PA1)|(1<<PA0);
DDRA = (1<<DDA3)|(1<<DDA2)|(1<<DDA1)|(1<<DDA0);
/* Insert nop for synchronization*/
_NOP();
/* Read port pins */
i = PINA;
...
Note: 1. For the assembly program, two temporary registers are used to minimize the time from pull-
ups are set on pins 0, 1 and 4, until the direction bits are correctly set, defining bit 2 and 3 as low and redefining bits 0 and 1 as strong high drivers.

12.2.5 Digital Input Enable and Sleep Modes

As shown in Figure 12-2 on page 57, the digital input signal can be clamped to ground at the input of the schmitt-trigger. The signal denoted SLEEP in the figure, is set by the MCU Sleep Controller in Power-down mode, Power-save mode, and Standby mode to avoid high power consumption if some input signals are left floating, or have an analog signal level close to V
SLEEP is overridden for port pins enabled as external interrupt pins. If the external interrupt request is not enabled, SLEEP is active also for these pins. SLEEP is also overridden by various other alternate functions as described in ”Alternate Port Functions” on page 61.
If a logic high level (“one”) is present on an asynchronous external interrupt pin configured as “Interrupt on Rising Edge, Falling Edge, or Any Logic Change on Pin” while the external interrupt is not enabled, the corresponding External Interrupt Flag will be set when resuming from the above mentioned Sleep mode, as the clamping in these sleep mode produces the requested logic change.
60
ATtiny24/44/84
/2.
CC
8006F–AVR–02/07
Page 61

12.2.6 Unconnected Pins

If some pins are unused, it is recommended to ensure that these pins have a defined level. Even though most of the digital inputs are disabled in the deep sleep modes as described above, float­ing inputs should be avoided to reduce current consumption in all other modes where the digital inputs are enabled (Reset, Active mode and Idle mode).
The simplest method to ensure a defined level of an unused pin, is to enable the internal pull-up. In this case, the pull-up will be disabled during reset. If low power consumption during reset is important, it is recommended to use an external pull-up or pulldown. Connecting unused pins directly to V accidentally configured as an output.

12.3 Alternate Port Functions

Most port pins have alternate functions in addition to being general digital I/Os. Figure 12-5 on
page 62 shows how the port pin control signals from the simplified Figure 12-2 on page 57 can
be overridden by alternate functions. The overriding signals may not be present in all port pins, but the figure serves as a generic description applicable to all port pins in the AVR microcontrol­ler family.
ATtiny24/44/84
or GND is not recommended, since this may cause excessive currents if the pin is
CC
8006F–AVR–02/07
61
Page 62
Figure 12-5. Alternate Port Functions
1
0
1
0
(1)
PUOExn
PUOVxn
DDOExn
DDOVxn
PVOExn
PVOVxn
Q
DDxn
Q
CLR
RESET
PUD
D
WDx
RDx
Pxn
1
0
DIEOExn
1
0
DIEOVxn
SLEEP
Q
PORTxn
Q
CLR
RESET
D
SYNCHRONIZER
SET
DLQ
CLR
Q
D
PINxn
Q
Q
CLR
1
0
RRx
WRx
PTOExn
WPx
DATA BUS
RPx
clk
I/O
DIxn
AIOxn
PUOExn: Pxn PULL-UP OVERRIDE ENABLE PUOVxn: Pxn PULL-UP OVERRIDE VALUE DDOExn: Pxn DATA DIRECTION OVERRIDE ENABLE DDOVxn: Pxn DATA DIRECTION OVERRIDE VALUE PVOExn: Pxn PORT VALUE OVERRIDE ENABLE PVOVxn: Pxn PORT VALUE OVERRIDE VALUE DIEOExn: Pxn DIGITAL INPUT-ENABLE OVERRIDE ENABLE DIEOVxn: Pxn DIGITAL INPUT-ENABLE OVERRIDE VALUE SLEEP: SLEEP CONTROL PTOExn: Pxn, PORT TOGGLE OVERRIDE ENABLE
Note: 1. WRx, WPx, WDx, RRx, RPx, and RDx are common to all pins within the same port. clk
PUD: PULLUP DISABLE WDx: WRITE DDRx RDx: READ DDRx RRx: READ PORTx REGISTER WRx: WRITE PORTx RPx: READ PORTx PIN WPx: WRITE PINx clk
: I/O CLOCK
I/O
DIxn: DIGITAL INPUT PIN n ON PORTx AIOxn: ANALOG INPUT/OUTPUT PIN n ON PORTx
I/O
,
SLEEP, and PUD are common to all ports. All other signals are unique for each pin.
Table 12-2 on page 63 summarizes the function of the overriding signals. The pin and port
indexes from Figure 12-5 on page 62 are not shown in the succeeding tables. The overriding signals are generated internally in the modules having the alternate function.
62
ATtiny24/44/84
8006F–AVR–02/07
Page 63
ATtiny24/44/84
Table 12-2. Generic Description of Overriding Signals for Alternate Functions
Signal Name Full Name Description
PUOE
PUOV
DDOE
DDOV
PVOE
PVOV
PTOE
DIEOE
Pull-up Override Enable
Pull-up Override Val ue
Data Direction Override Enable
Data Direction Override Value
Por t Value Override Enable
Por t Value Override Value
Port Toggle Override Enable
Digital Input Enable Override Enable
If this signal is set, the pull-up enable is controlled by the PUOV signal. If this signal is cleared, the pull-up is enabled when {DDxn, PORTxn, PUD} = 0b010.
If PUOE is set, the pull-up is enabled/disabled when PUOV is set/cleared, regardless of the setting of the DDxn, PORTxn, and PUD Register bits.
If this signal is set, the Output Driver Enable is controlled by the DDOV signal. If this signal is cleared, the Output driver is enabled by the DDxn Register bit.
If DDOE is set, the Output Driver is enabled/disabled when DDOV is set/cleared, regardless of the setting of the DDxn Register bit.
If this signal is set and the Output Driver is enabled, the port value is controlled by the PVOV signal. If PVOE is cleared, and the Output Driver is enabled, the port Value is controlled by the PORTxn Register bit.
If PVOE is set, the port value is set to PVOV, regardless of the setting of the PORTxn Register bit.
If PTOE is set, the PORTxn Register bit is inverted.
If this bit is set, the Digital Input Enable is controlled by the DIEOV signal. If this signal is cleared, the Digital Input Enable is determined by MCU state (Normal mode, sleep mode).
Digital Input
DIEOV
DI Digital Input
AIO
Enable Override Val ue
Analog Input/Output
If DIEOE is set, the Digital Input is enabled/disabled when DIEOV is set/cleared, regardless of the MCU state (Normal mode, sleep mode).
This is the Digital Input to alternate functions. In the figure, the signal is connected to the output of the schmitt-trigger but before the synchronizer. Unless the Digital Input is used as a clock source, the module with the alternate function will use its own synchronizer.
This is the Analog Input/Output to/from alternate functions. The signal is connected directly to the pad, and can be used bi­directionally.
The following subsections shortly describe the alternate functions for each port, and relate the overriding signals to the alternate function. Refer to the alternate function description for further details.
8006F–AVR–02/07
63
Page 64

12.3.1 Alternate Functions of Port A

The Port A pins with alternate function are shown in Table 12-7 on page 68.
Table 12-3. Port A Pins Alternate Functions
Port Pin Alternate Function
PA 0
PA 1
PA 2
PA 3
PA 4
PA 5
PA 6
PA 7
ADC0: ADC input channel 0. AREF: External analog reference. PCINT0: Pin change interrupt 0 source 0.
ADC1: ADC input channel 1. AIN0: Analog Comparator Positive Input. PCINT1:Pin change interrupt 0 source 1.
ADC2: ADC input channel 2. AIN1: Analog Comparator Negative Input. PCINT2: Pin change interrupt 0 source 2.
ADC3: ADC input channel 3. T0: Timer/Counter0 counter source. PCINT3: Pin change interrupt 0 source 3.
ADC4: ADC input channel 4.
USCK: USI Clock three wire mode. SCL : USI Clock two wire mode.
T1: Timer/Counter1 counter source. PCINT4: Pin change interrupt 0 source 4.
ADC5: ADC input channel 5. DO: USI Data Output three wire mode. OC1B: Timer/Counter1 Compare Match B output. PCINT5: Pin change interrupt 0 source 5.
ADC6: ADC input channel 6. DI: USI Data Input three wire mode. SDA: USI Data Input two wire mode. OC1A: Timer/Counter1 Compare Match A output. PCINT6: Pin change interrupt 0 source 6.
ADC7: ADC input channel 7. OC0B: Timer/Counter0 Compare Match B output. ICP1: Timer/Counter1 Input Capture Pin. PCINT7: Pin change interrupt 0 source 7.
64
• Port A, Bit 0 – ADC0/AREF/PCINT0
ADC0: Analog to Digital Converter, Channel 0
AREF: External Analog Reference for ADC. Pullup and output driver are disabled on PA0 when the pin is used as an external reference or Internal Voltage Reference with external capacitor at the AREF pin by setting (one) the bit REFS0 in the ADC Multiplexer Selection Register (ADMUX).
PCINT0: Pin Change Interrupt source 0. The PA0 pin can serve as an external interrupt source for pin change interrupt 0.
ATtiny24/44/84
.
8006F–AVR–02/07
Page 65
ATtiny24/44/84
• Port A, Bit 1 – ADC1/AIN0/PCINT1
ADC1: Analog to Digital Converter, Channel 1
AIN0: Analog Comparator Positive Input. Configure the port pin as input with the internal pull-up switched off to avoid the digital port function from interfering with the function of the Analog Comparator.
PCINT1: Pin Change Interrupt source 1. The PA1 pin can serve as an external interrupt source for pin change interrupt 0.
• Port A, Bit 2 – ADC2/AIN1/PCINT2
ADC2: Analog to Digital Converter, Channel 2
AIN1: Analog Comparator Negative Input. Configure the port pin as input with the internal pull-up switched off to avoid the digital port function from interfering with the function of the Analog Comparator.
PCINT2: Pin Change Interrupt source 2. The PA2 pin can serve as an external interrupt source for pin change interrupt 0.
• Port A, Bit 3 – ADC3/T0/PCINT3
ADC3: Analog to Digital Converter, Channel 3
.
.
.
T0: Timer/Counter0 counter source.
PCINT3: Pin Change Interrupt source 3. The PA3 pin can serve as an external interrupt source for pin change interrupt 0.
• Port A, Bit 4 – ADC4/USCK/SCL/T1/PCINT4
ADC4: Analog to Digital Converter, Channel 4
USCK: Three-wire mode Universal Serial Interface Clock.
SCL: Two-wire mode Serial Clock for USI Two-wire mode.
T1: Timer/Counter1 counter source.
PCINT4: Pin Change Interrupt source 4. The PA4 pin can serve as an external interrupt source for pin change interrupt 0.
• Port A, Bit 5 – ADC5/DO/OC1B/PCINT5
ADC5: Analog to Digital Converter, Channel 5
DO: Data Output in USI Three-wire mode. Data output (DO) overrides PORTA5 value and it is driven to the port when the data direction bit DDA5 is set (one). However the PORTA5 bit still controls the pullup, enabling pullup if direction is input and PORTA5 is set(one).
OC1B: Output Compare Match output: The PA5 pin can serve as an external output for the Timer/Counter1 Compare Match B. The PA5 pin has to be configured as an output (DDA5 set (one)) to serve this function. The OC1B pin is also the output pin for the PWM mode timer function.
.
.
8006F–AVR–02/07
PCINT5: Pin Change Interrupt source 5. The PA5 pin can serve as an external interrupt source for pin change interrupt 0.
65
Page 66
• Port A, Bit 6 – ADC6/DI/SDA/OC1A/PCINT6
ADC6: Analog to Digital Converter, Channel 6
.
SDA: Two-wire mode Serial Interface Data.
DI: Data Input in USI Three-wire mode. USI Three-wire mode does not override normal port functions, so pin must be configure as an input for DI function.
OC1A, Output Compare Match output: The PA6 pin can serve as an external output for the Timer/Counter1 Compare Match A. The PA6 pin has to be configured as an output (DDA6 set (one)) to serve this function. The OC1A pin is also the output pin for the PWM mode timer function.
PCINT6: Pin Change Interrupt source 6. The PA6 pin can serve as an external interrupt source for pin change interrupt 0.
• Port A, Bit 7 – ADC7/OC0B/ICP1/PCINT7
ADC7: Analog to Digital Converter, Channel 7
.
OC1B, Output Compare Match output: The PA7 pin can serve as an external output for the Timer/Counter1 Compare Match B. The PA7 pin has to be configured as an output (DDA7 set (one)) to serve this function. The OC1B pin is also the output pin for the PWM mode timer function.
ICP1, Input Capture Pin: The PA7 pin can act as an Input Capture Pin for Timer/Counter1.
PCINT7: Pin Change Interrupt source 7. The PA7 pin can serve as an external interrupt source for pin change interrupt 0.
Table 12-4 on page 66 to Table 12-6 on page 67 relate the alternate functions of Port A to the
overriding signals shown in Figure 12-5 on page 62.
Table 12-4. Overriding Signals for Alternate Functions in PA7..PA5
Signal Name
PUOE 0 0 0
PUOV 0 0 0
DDOE 0 USIWM1 0
DDOV 0 (SDA + PORTA6) • DDRA6 0
PVOE OC0B enable
PVOV OC0B ( USIWM1
PTOE 0 0 0
DIEOE PCINT7 • PCIE0 + ADC7D
DIEOV PCINT7 • PCIE0 USISIE + PCINT7 • PCIE0 PCINT5 • PCIE
DI PCINT7/ICP1 Input DI/SDA/PCINT6 Input PCINT5 Input
PA7/ADC7/OC0B/ICP1/ PCINT7
PA6/ADC6/DI/SDA/OC1A/ PCINT6
(USIWM1 DDA6) + OC1A
enable
DDA6) OC1A
USISIE + (PCINT6 • PCIE0) + ADC6D
PA5/ADC5/DO/OC1B/ PCINT5
(USIWM1 OC1B enable
USIWM1 (~USIWM1 OC1B}
PCINT5 • PCIE + ADC5D
USIWM0) +
USIWM0 DO +
USIWM0)
66
AIO ADC7 Input ADC6 Input ADC5 Input
ATtiny24/44/84
8006F–AVR–02/07
Page 67
ATtiny24/44/84
Table 12-5. Overriding Signals for Alternate Functions in PA4..PA2
Signal Name
PUOE 0 0 0
PUOV 0 0 0
DDOE USIWM1 0 0
DDOV
PVOE USIWM1 • ADC4D 0 0
PVOV 0 0 0
PTOE USI_PTOE 0 0
DIEOE
DIEOV
DI USCK/SCL/T1/PCINT4 input PCINT1 Input PCINT0 Input
AIO ADC4 Input ADC3 Input
PA4/ADC4/USCK/SCL/T1/P CINT4 PA3/ADC3/T0/PCINT3 PA2/ADC2/AIN1/PCINT2
USI_SCL_HOLD + PORTA4) • ADC4D
USISIE + (PCINT4 • PCIE0) + ADC4D
USISIE + (PCINT4 • PCIE0)
00
(PCINT3 • PCIE0) + ADC3D PCINT2 • PCIE + ADC2D
PCINT3 • PCIE0 PCINT3 • PCIE0
ADC2/Analog Comparator Negative Input
Table 12-6. Overriding Signals for Alternate Functions in PA1..PA0
Signal Name
PUOE 0
PUOV 0 0
DDOE 0
DDOV 0 0
PVOE 0
PVOV 0 0
PTOE 0 0
DIEOE PCINT1 • PCIE0 + ADC1D PCINT0 • PCIE0 + ADC0D
DIEOV PCINT1 • PCIE0 PCINT0 • PCIE0
DI PCINT1 Input PCINT0 Input
AIO ADC1/Analog Comparator Positive Input
PA1/ADC1/AIN0/PCINT1
PA0/ADC0/AREF/PCINT0
RESET • (REFS1 • REFS0 + REFS1 • REFS0)
RESET • (REFS1
RESET • (REFS1
ADC1 Input Analog reference
• REFS0 + REFS1 • REFS0)
• REFS0 + REFS1 • REFS0)
8006F–AVR–02/07
67
Page 68

12.3.2 Alternate Functions of Port B

The Port B pins with alternate function are shown in Table 12-7 on page 68.
Table 12-7. Port B Pins Alternate Functions
Port Pin Alternate Function
PB0
PB1
PB2
PB3
• Port B, Bit 0 – XTAL1/PCINT8
XTAL1: Chip Clock Oscillator pin 1. Used for all chip clock sources except internal calibrateble RC oscillator. When used as a clock pin, the pin can not be used as an I/O pin. When using internal calibratable RC Oscillator as a chip clock source, PB0 serves as an ordinary I/O pin.
XTAL1: Crystal Oscillator Input. PCINT8: Pin change interrupt 1 source 8. CLKI: External Clock Input
XTAL2: Crystal Oscillator Output. PCINT9: Pin change interrupt 1 source 9.
INT0: External Interrupt 0 Input. OC0A: Timer/Counter0 Compare Match A output. CKOUT: System clock output. PCINT10:Pin change interrupt 1 source 10.
RESET: Reset pin. dW:debugWire I/O. PCINT11:Pin change interrupt 1 source 11.
PCINT8: Pin Change Interrupt source 8. The PB0 pin can serve as an external interrupt source for pin change interrupt 1.
CLKI: Clock Input from an external clock source, see ”External Clock” on page 30.
• Port B, Bit 1 – XTAL2/PCINT9
XTAL2: Chip Clock Oscillator pin 2. Used as clock pin for all chip clock sources except internal calibrateble RC Oscillator and external clock. When used as a clock pin, the pin can not be used as an I/O pin. When using internal calibratable RC Oscillator or External clock as a Chip clock sources, PB1 serves as an ordinary I/O pin.
PCINT9: Pin Change Interrupt source 9. The PB1 pin can serve as an external interrupt source for pin change interrupt 1.
• Port B, Bit 2 – INT0/OC0A/CKOUT/PCINT10
INT0: External Interrupt Request 0.
OC0A: Output Compare Match output: The PB2 pin can serve as an external output for the Timer/Counter0 Compare Match A. The PB2 pin has to be configured as an output (DDB2 set (one)) to serve this function. The OC0A pin is also the output pin for the PWM mode timer function.
CKOUT - System Clock Output: The system clock can be output on the PB2 pin. The system clock will be output if the CKOUT Fuse is programmed, regardless of the PORTB2 and DDB2 settings. It will also be output during reset.
68
PCINT10: Pin Change Interrupt source 10. The PB2 pin can serve as an external interrupt source for pin change interrupt 1.
ATtiny24/44/84
8006F–AVR–02/07
Page 69
ATtiny24/44/84
• Port B, Bit 3 – RESET/dW/PCINT11
RESET Fuse. Pullup is activated and output driver and digital input are deactivated when the pin is used as the RESET
dW: When the debugWIRE Enable (DWEN) Fuse is programmed and Lock bits are unpro­grammed, the debugWIRE system within the target device is activated. The RESET port pin is configured as a wire-AND (open-drain) bi-directional I/O pin with pull-up enabled and becomes the communication gateway between target and emulator.
PCINT11: Pin Change Interrupt source 11. The PB3 pin can serve as an external interrupt source for pin change interrupt 1.
Table 12-8 on page 69 and Table 12-9 on page 70 relate the alternate functions of Port B to the
overriding signals shown in Figure 12-5 on page 62.
Table 12-8. Overriding Signals for Alternate Functions in PB3..PB2
: External Reset input is active low and enabled by unprogramming (“1”) the RSTDISBL
pin.
Signal Name
PUOE RSTDISBL
PB3/RESET/dW/ PCINT11 PB2/INT0/OC0A/CKOUT/PCINT10
(1)
+ DEBUGWIRE_ENABLE
(2)
CKOUT
PUOV 1 0
DDOE RSTDISBL
DDOV
DEBUGWIRE_ENABLE Tr a ns m i t
PVOE RSTDISBL
(1)
+ DEBUGWIRE_ENABLE
(2)
• debugWire
(1)
+ DEBUGWIRE_ENABLE
PVOV 0 CKOUT • System Clock + CKOUT
(2)
CKOUT
1'b1
(2)
CKOUT + OC0A enable
• OC0A
PTOE 0 0
DIEOE
DIEOV
RSTDISBL
(1)
+ DEBUGWIRE_ENABLE
PCINT11 • PCIE1
DEBUGWIRE_ENABLE PCINT11 • PCIE1)
(2)
+ (RSTDISBL
(2)
+
PCINT10 • PCIE1 + INT0
(1)
• PCINT10 • PCIE1 + INT0
DI dW/PCINT11 Input INT0/PCINT10 Input
AIO
1. RSTDISBL is 1 when the Fuse is “0” (Programmed).
2. DebugWIRE is enabled wheb DWEN Fuse is programmed and Lock bits are unprogrammed.
8006F–AVR–02/07
69
Page 70
Table 12-9. Overriding Signals for Alternate Functions in PB1..PB0
Signal Name PB1/XTAL2/PCINT9 PB0/XTAL1/PCINT8
PUOE EXT_OSC
(1)
EXT_CLOCK
PUOV 0 0
DDOE EXT_OSC
(1)
EXT_CLOCK
DDOV 0 0
PVOE EXT_OSC
(1)
EXT_CLOCK
PVOV 0 0
PTOE 0 0
DIEOE
EXT_OSC PCINT9 • PCIE1
DIEOV EXT_OSC
(1)
+
(1)
• PCINT9 • PCIE1
EXT_CLOCK (PCINT8 • PCIE1)
( EXT_CLOCK (EXT_CLOCK
DI PCINT9 Input CLOCK/PCINT8 Input
AIO XTAL2 XTAL1
1. EXT_OSC = crystal oscillator or low frequency crystal oscillator is selected as system clock.
2. EXT_CLOCK = external clock is selected as system clock.
(2)
+ EXT_OSC
(2)
+ EXT_OSC
(2)
+ EXT_OSC
(2)
+ EXT_OSC
(2)
• PWR_DOWN ) +
(2)
• EXT_OSC
(1)
(1)
(1)
(1)
+
(1)
• PCINT8 • PCIE1)
70
ATtiny24/44/84
8006F–AVR–02/07
Page 71

12.4 Register Description

12.4.1 MCUCR – MCU Control Register
Bit 7 6 5 4 3 2 1 0
–PUD
Read/Write R R/W R/W R/W R/W RRR
Initial Value 0 0 0 0 0 0 0 0
• Bits 7, 2– Res: Reserved Bits
These bits are reserved bits in the ATtiny24/44/84 and will always read as zero.
• Bit 6 – PUD: Pull-up Disable
When this bit is written to one, the pull-ups in the I/O ports are disabled even if the DDxn and PORTxn Registers are configured to enable the pull-ups ({DDxn, PORTxn} = 0b01). See ”Con-
figuring the Pin” on page 57 for more details about this feature.
12.4.2 PORTA – Port A Data Register
Bit 76543210
0x1B (0x3B)
Read/Write R/W R/W R/W R/W R/W R/W R/W R/W
Initial Value 0 0 0 0 0 0 0 0
12.4.3 DDRA – Port A Data Direction Register
PORTA7 PORTA6 PORTA5 PORTA4 PORTA3 PORTA2 PORTA1 PORTA0 PORTA
ATtiny24/44/84
SE SM1 SM0 ISC01 ISC00 MCUCR
Bit 76543210
0x1A (0x3A) DDA7 DDA6 DDA5 DDA4 DDA3 DDA2 DDA1 DDA0 DDRB
Read/Write R/W R/W R/W R/W R/W R/W R/W R/W
Initial Value 0 0 0 0 0 0 0 0
12.4.4 PINA – Port A Input Pins Address
Bit 76543210
0x19 (0x39) PINA7 PINA6 PINA5 PINA4 PINA3 PINA2 PINA1 PINA0 PINB
Read/Write R/W R/W R/W R/W R/W R/W R/W R/W
Initial Value 0 0 N/A N /A N/A N/A N/A N/A
12.4.5 PORTB – Port B Data Register
Bit 76543210
0x18 (0x38)
Read/WriteRRRRR/W R/W R/W R/W
Initial Value 0 0 0 0 0 0 0 0
PORTB3 PORTB2 PORTB1 PORTB0 PORTB
12.4.6 DDRB – Port B Data Direction Register
Bit 76543210
0x17 (0x37)
Read/WriteRRRRR/W R/W R/W R/W
Initial Value 0 0 0 0 0 0 0 0
––
DDB3 DDB2 DDB1 DDB0 DDRB
8006F–AVR–02/07
71
Page 72
12.4.7 PINB – Port BInput Pins Address
Bit 76543210
0x16 (0x36)
Read/WriteRRRRR/W R/W R/W R/W
Initial Value 0 0 N/A N/A N/A N/A N/A N/A
––
PINB3 PINB2 PINB1 PINB0 PINB
72
ATtiny24/44/84
8006F–AVR–02/07
Page 73

13. 8-bit Timer/Counter0 with PWM

13.1 Features

Two Independent Output Compare Units
Double Buffered Output Compare Registers
Clear Timer on Compare Match (Auto Reload)
Glitch Free, Phase Correct Pulse Width Modulator (PWM)
Variable PWM Period
Frequency Generator
Three Independent Interrupt Sources (TOV0, OCF0A, and OCF0B)

13.2 Overview

Timer/Counter0 is a general purpose 8-bit Timer/Counter module, with two independent Output Compare Units, and with PWM support. It allows accurate program execution timing (event man­agement) and wave generation.
A simplified block diagram of the 8-bit Timer/Counter is shown in Figure 13-1 on page 73. For the actual placement of I/O pins, refer to Figure 1-1 on page 2. CPU accessible I/O Registers, including I/O bits and I/O pins, are shown in bold. The device-specific I/O Register and bit loca­tions are listed in the ”Register Description” on page 84.
ATtiny24/44/84

13.2.1 Registers

Figure 13-1. 8-bit Timer/Counter Block Diagram
Count
Clear
Control Logic
Direction
TOP BOT TOM
Timer/Counter
TCNTn
=
OCRnA
=
DATA B US
OCRnB
TCCRnA TCCRnB
clk
Tn
=
Fixed TOP
Val ue
=
0
TOVn
(Int.Req.)
Clock Select
Edge
Detector
( From Prescaler )
OCnA
(Int.Req.)
Wavefor m Generation
OCnB
(Int.Req.)
Wavefor m Generation
Tn
OCnA
OCnB
The Timer/Counter (TCNT0) and Output Compare Registers (OCR0A and OCR0B) are 8-bit registers. Interrupt request (abbreviated to Int.Req. in the figure) signals are all visible in the Timer Interrupt Flag Register (TIFR0). All interrupts are individually masked with the Timer Inter­rupt Mask Register (TIMSK0). TIFR0 and TIMSK0 are not shown in the figure.
8006F–AVR–02/07
The Timer/Counter can be clocked internally, via the prescaler, or by an external clock source on the T0 pin. The Clock Select logic block controls which clock source and edge the Timer/Counter
73
Page 74

13.2.2 Definitions

uses to increment (or decrement) its value. The Timer/Counter is inactive when no clock source is selected. The output from the Clock Select logic is referred to as the timer clock (clk
T0
).
The double buffered Output Compare Registers (OCR0A and OCR0B) is compared with the Timer/Counter value at all times. The result of the compare can be used by the Waveform Gen­erator to generate a PWM or variable frequency output on the Output Compare pins (OC0A and OC0B). See ”Output Compare Unit” on page 75 for details. The Compare Match event will also set the Compare Flag (OCF0A or OCF0B) which can be used to generate an Output Compare interrupt request.
Many register and bit references in this section are written in general form. A lower case “n” replaces the Timer/Counter number, in this case 0. A lower case “x” replaces the Output Com­pare Unit, in this case Compare Unit A or Compare Unit B. However, when using the register or bit defines in a program, the precise form must be used, i.e., TCNT0 for accessing Timer/Counter0 counter value and so on.
The definitions in Table 13-1 on page 74 are also used extensively throughout the document.
Table 13-1. Definitions
BOTTOM The counter reaches the BOTTOM when it becomes 0x00.
MAX The counter reaches its MAXimum when it becomes 0xFF (decimal 255).
TOP The counter reaches the TOP when it becomes equal to the highest value in the
count sequence. The TOP value can be assigned to be the fixed value 0xFF (MAX) or the value stored in the OCR0A Register. The assignment is depen­dent on the mode of operation.

13.3 Timer/Counter Clock Sources

The Timer/Counter can be clocked by an internal or an external clock source. The clock source is selected by the Clock Select logic which is controlled by the Clock Select (CS02:0) bits located in the Timer/Counter Control Register (TCCR0B). For details on clock sources and pres­caler, see ”Timer/Counter Prescaler” on page 120.

13.4 Counter Unit

The main part of the 8-bit Timer/Counter is the programmable bi-directional counter unit. Figure
13-2 on page 74 shows a block diagram of the counter and its surroundings.
Figure 13-2. Counter Unit Block Diagram
DATA B US
TCNTn Control Logic
count
clear
direction
bottom
top
TOVn
(Int.Req.)
clk
Tn
Clock Select
Edge
Detector
( From Prescaler )
Tn
74
ATtiny24/44/84
8006F–AVR–02/07
Page 75
Signal description (internal signals):
count Increment or decrement TCNT0 by 1.
direction Select between increment and decrement.
clear Clear TCNT0 (set all bits to zero).
ATtiny24/44/84
clk
top Signalize that TCNT0 has reached maximum value.
bottom Signalize that TCNT0 has reached minimum value (zero).
Depending of the mode of operation used, the counter is cleared, incremented, or decremented at each timer clock (clk selected by the Clock Select bits (CS02:0). When no clock source is selected (CS02:0 = 0) the timer is stopped. However, the TCNT0 value can be accessed by the CPU, regardless of whether clk count operations.
The counting sequence is determined by the setting of the WGM01 and WGM00 bits located in the Timer/Counter Control Register (TCCR0A) and the WGM02 bit located in the Timer/Counter Control Register B (TCCR0B). There are close connections between how the counter behaves (counts) and how waveforms are generated on the Output Compare output OC0A. For more details about advanced counting sequences and waveform generation, see ”Modes of Opera-
tion” on page 78.
The Timer/Counter Overflow Flag (TOV0) is set according to the mode of operation selected by the WGM01:0 bits. TOV0 can be used for generating a CPU interrupt.

13.5 Output Compare Unit

The 8-bit comparator continuously compares TCNT0 with the Output Compare Registers (OCR0A and OCR0B). Whenever TCNT0 equals OCR0A or OCR0B, the comparator signals a match. A match will set the Output Compare Flag (OCF0A or OCF0B) at the next timer clock cycle. If the corresponding interrupt is enabled, the Output Compare Flag generates an Output Compare interrupt. The Output Compare Flag is automatically cleared when the interrupt is exe­cuted. Alternatively, the flag can be cleared by software by writing a logical one to its I/O bit location. The Waveform Generator uses the match signal to generate an output according to operating mode set by the WGM02:0 bits and Compare Output mode (COM0x1:0) bits. The max and bottom signals are used by the Waveform Generator for handling the special cases of the extreme values in some modes of operation. See ”Modes of Operation” on page 78.
Tn
is present or not. A CPU write overrides (has priority over) all counter clear or
T0
Timer/Counter clock, referred to as clkT0 in the following.
). clkT0 can be generated from an external or internal clock source,
T0
8006F–AVR–02/07
Figure 13-3 on page 76 shows a block diagram of the Output Compare unit.
75
Page 76
Figure 13-3. Output Compare Unit, Block Diagram
DATA BU S
OCRnx
TCNTn
= (8-bit Comparator )
OCFnx (Int.Req.)
top
bottom
FOCn
The OCR0x Registers are double buffered when using any of the Pulse Width Modulation (PWM) modes. For the normal and Clear Timer on Compare (CTC) modes of operation, the dou­ble buffering is disabled. The double buffering synchronizes the update of the OCR0x Compare Registers to either top or bottom of the counting sequence. The synchronization prevents the occurrence of odd-length, non-symmetrical PWM pulses, thereby making the output glitch-free.
The OCR0x Register access may seem complex, but this is not case. When the double buffering is enabled, the CPU has access to the OCR0x Buffer Register, and if double buffering is dis­abled the CPU will access the OCR0x directly.
Waveform Generator
WGMn1:0
COMnX1:0
OCnx

13.5.1 Force Output Compare

In non-PWM waveform generation modes, the match output of the comparator can be forced by writing a one to the Force Output Compare (0x) bit. Forcing Compare Match will not set the OCF0x Flag or reload/clear the timer, but the OC0x pin will be updated as if a real Compare Match had occurred (the COM0x1:0 bits settings define whether the OC0x pin is set, cleared or toggled).

13.5.2 Compare Match Blocking by TCNT0 Write

All CPU write operations to the TCNT0 Register will block any Compare Match that occur in the next timer clock cycle, even when the timer is stopped. This feature allows OCR0x to be initial­ized to the same value as TCNT0 without triggering an interrupt when the Timer/Counter clock is enabled.

13.5.3 Using the Output Compare Unit

Since writing TCNT0 in any mode of operation will block all Compare Matches for one timer clock cycle, there are risks involved when changing TCNT0 when using the Output Compare Unit, independently of whether the Timer/Counter is running or not. If the value written to TCNT0 equals the OCR0x value, the Compare Match will be missed, resulting in incorrect waveform
76
ATtiny24/44/84
8006F–AVR–02/07
Page 77
generation. Similarly, do not write the TCNT0 value equal to BOTTOM when the counter is down-counting.
The setup of the OC0x should be performed before setting the Data Direction Register for the port pin to output. The easiest way of setting the OC0x value is to use the Force Output Com­pare (0x) strobe bits in Normal mode. The OC0x Registers keep their values even when changing between Waveform Generation modes.
Be aware that the COM0x1:0 bits are not double buffered together with the compare value. Changing the COM0x1:0 bits will take effect immediately.

13.6 Compare Match Output Unit

The Compare Output mode (COM0x1:0) bits have two functions. The Waveform Generator uses the COM0x1:0 bits for defining the Output Compare (OC0x) state at the next Compare Match. Also, the COM0x1:0 bits control the OC0x pin output source. Figure 13-4 on page 77 shows a simplified schematic of the logic affected by the COM0x1:0 bit setting. The I/O Registers, I/O bits, and I/O pins in the figure are shown in bold. Only the parts of the general I/O Port Control Registers (DDR and PORT) that are affected by the COM0x1:0 bits are shown. When referring to the OC0x state, the reference is for the internal OC0x Register, not the OC0x pin. If a system reset occur, the OC0x Register is reset to “0”.
ATtiny24/44/84
Figure 13-4. Compare Match Output Unit, Schematic
COMnx1
COMnx0 FOCn
clk
I/O
Waveform
Generator
DQ
1
OCnx
DQ
PORT
DATA BU S
DQ
DDR
0
OCn
Pin
The general I/O port function is overridden by the Output Compare (OC0x) from the Waveform Generator if either of the COM0x1:0 bits are set. However, the OC0x pin direction (input or out­put) is still controlled by the Data Direction Register (DDR) for the port pin. The Data Direction Register bit for the OC0x pin (DDR_OC0x) must be set as output before the OC0x value is visi­ble on the pin. The port override function is independent of the Waveform Generation mode.
8006F–AVR–02/07
The design of the Output Compare pin logic allows initialization of the OC0x state before the out­put is enabled. Note that some COM0x1:0 bit settings are reserved for certain modes of operation, see ”Register Description” on page 84
77
Page 78

13.6.1 Compare Output Mode and Waveform Generation

The Waveform Generator uses the COM0x1:0 bits differently in Normal, CTC, and PWM modes. For all modes, setting the COM0x1:0 = 0 tells the Waveform Generator that no action on the OC0x Register is to be performed on the next Compare Match. For compare output actions in the non-PWM modes refer to Table 13-2 on page 84. For fast PWM mode, refer to Table 13-3 on
page 84, and for phase correct PWM refer to Table 13-4 on page 85.
A change of the COM0x1:0 bits state will have effect at the first Compare Match after the bits are written. For non-PWM modes, the action can be forced to have immediate effect by using the 0x strobe bits.

13.7 Modes of Operation

The mode of operation, i.e., the behavior of the Timer/Counter and the Output Compare pins, is defined by the combination of the Waveform Generation mode (WGM02:0) and Compare Output mode (COM0x1:0) bits. The Compare Output mode bits do not affect the counting sequence, while the Waveform Generation mode bits do. The COM0x1:0 bits control whether the PWM out­put generated should be inverted or not (inverted or non-inverted PWM). For non-PWM modes the COM0x1:0 bits control whether the output should be set, cleared, or toggled at a Compare Match (See ”Modes of Operation” on page 78).
For detailed timing information refer to Figure 13-8 on page 82, Figure 13-9 on page 83, Figure
13-10 on page 83 and Figure 13-11 on page 83 in ”Timer/Counter Timing Diagrams” on page
82.

13.7.1 Normal Mode

The simplest mode of operation is the Normal mode (WGM02:0 = 0). In this mode the counting direction is always up (incrementing), and no counter clear is performed. The counter simply overruns when it passes its maximum 8-bit value (TOP = 0xFF) and then restarts from the bot­tom (0x00). In normal operation the Timer/Counter Overflow Flag (TOV0) will be set in the same timer clock cycle as the TCNT0 becomes zero. The TOV0 Flag in this case behaves like a ninth bit, except that it is only set, not cleared. However, combined with the timer overflow interrupt that automatically clears the TOV0 Flag, the timer resolution can be increased by software. There are no special cases to consider in the Normal mode, a new counter value can be written anytime.
The Output Compare Unit can be used to generate interrupts at some given time. Using the Out­put Compare to generate waveforms in Normal mode is not recommended, since this will occupy too much of the CPU time.

13.7.2 Clear Timer on Compare Match (CTC) Mode

In Clear Timer on Compare or CTC mode (WGM02:0 = 2), the OCR0A Register is used to manipulate the counter resolution. In CTC mode the counter is cleared to zero when the counter value (TCNT0) matches the OCR0A. The OCR0A defines the top value for the counter, hence also its resolution. This mode allows greater control of the Compare Match output frequency. It also simplifies the operation of counting external events.
The timing diagram for the CTC mode is shown in Figure 13-5 on page 79. The counter value (TCNT0) increases until a Compare Match occurs between TCNT0 and OCR0A, and then counter (TCNT0) is cleared.
78
ATtiny24/44/84
8006F–AVR–02/07
Page 79
Figure 13-5. CTC Mode, Timing Diagram
TCNTn
ATtiny24/44/84
OCnx Interrupt Flag Set
OCn (Toggle)
Period
1 4
2 3
(COMnx1:0 = 1)
An interrupt can be generated each time the counter value reaches the TOP value by using the OCF0A Flag. If the interrupt is enabled, the interrupt handler routine can be used for updating the TOP value. However, changing TOP to a value close to BOTTOM when the counter is run­ning with none or a low prescaler value must be done with care since the CTC mode does not have the double buffering feature. If the new value written to OCR0A is lower than the current value of TCNT0, the counter will miss the Compare Match. The counter will then have to count to its maximum value (0xFF) and wrap around starting at 0x00 before the Compare Match can occur.
For generating a waveform output in CTC mode, the OC0A output can be set to toggle its logical level on each Compare Match by setting the Compare Output mode bits to toggle mode (COM0A1:0 = 1). The OC0A value will not be visible on the port pin unless the data direction for the pin is set to output. The waveform generated will have a maximum frequency of
0
= f
clk_I/O
/2 when OCR0A is set to zero (0x00). The waveform frequency is defined by the following equation:
f
f
OCnx
------------------------------------------------- -=
2 N 1 OCRnx+()⋅⋅
clk_I/O
The N variable represents the prescale factor (1, 8, 64, 256, or 1024).

13.7.3 Fast PWM Mode

8006F–AVR–02/07
As for the Normal mode of operation, the TOV0 Flag is set in the same timer clock cycle that the counter counts from MAX to 0x00.
The fast Pulse Width Modulation or fast PWM mode (WGM02:0 = 3 or 7) provides a high fre­quency PWM waveform generation option. The fast PWM differs from the other PWM option by its single-slope operation. The counter counts from BOTTOM to TOP then restarts from BOT­TOM. TOP is defined as 0xFF when WGM2:0 = 3, and OCR0A when WGM2:0 = 7. In non­inverting Compare Output mode, the Output Compare (OC0x) is cleared on the Compare Match between TCNT0 and OCR0x, and set at BOTTOM. In inverting Compare Output mode, the out­put is set on Compare Match and cleared at BOTTOM. Due to the single-slope operation, the operating frequency of the fast PWM mode can be twice as high as the phase correct PWM mode that use dual-slope operation. This high frequency makes the fast PWM mode well suited for power regulation, rectification, and DAC applications. High frequency allows physically small sized external components (coils, capacitors), and therefore reduces total system cost.
In fast PWM mode, the counter is incremented until the counter value matches the TOP value. The counter is then cleared at the following timer clock cycle. The timing diagram for the fast
79
Page 80
PWM mode is shown in Figure 13-6 on page 80. The TCNT0 value is in the timing diagram shown as a histogram for illustrating the single-slope operation. The diagram includes non­inverted and inverted PWM outputs. The small horizontal line marks on the TCNT0 slopes repre­sent Compare Matches between OCR0x and TCNT0.
Figure 13-6. Fast PWM Mode, Timing Diagram
OCRnx Interrupt Flag Set
OCRnx Update and TOVn Interrupt Flag Set
TCNTn
OCn
OCn
Period
1
2 3
4 5 6 7
(COMnx1:0 = 2)
(COMnx1:0 = 3)
The Timer/Counter Overflow Flag (TOV0) is set each time the counter reaches TOP. If the inter­rupt is enabled, the interrupt handler routine can be used for updating the compare value.
In fast PWM mode, the compare unit allows generation of PWM waveforms on the OC0x pins. Setting the COM0x1:0 bits to two will produce a non-inverted PWM and an inverted PWM output can be generated by setting the COM0x1:0 to three: Setting the COM0A1:0 bits to one allowes the AC0A pin to toggle on Compare Matches if the WGM02 bit is set. This option is not available for the OC0B pin (See Table 13-3 on page 84). The actual OC0x value will only be visible on the port pin if the data direction for the port pin is set as output. The PWM waveform is generated by setting (or clearing) the OC0x Register at the Compare Match between OCR0x and TCNT0, and clearing (or setting) the OC0x Register at the timer clock cycle the counter is cleared (changes from TOP to BOTTOM).
The PWM frequency for the output can be calculated by the following equation:
f
f
OCnxPWM
clk_I/O
----------------- -=
N 256
The N variable represents the prescale factor (1, 8, 64, 256, or 1024).
80
The extreme values for the OCR0A Register represents special cases when generating a PWM waveform output in the fast PWM mode. If the OCR0A is set equal to BOTTOM, the output will be a narrow spike for each MAX+1 timer clock cycle. Setting the OCR0A equal to MAX will result in a constantly high or low output (depending on the polarity of the output set by the COM0A1:0 bits.)
A frequency (with 50% duty cycle) waveform output in fast PWM mode can be achieved by set­ting OC0x to toggle its logical level on each Compare Match (COM0x1:0 = 1). The waveform generated will have a maximum frequency of
ATtiny24/44/84
0
= f
/2 when OCR0A is set to zero. This fea-
clk_I/O
8006F–AVR–02/07
Page 81
ture is similar to the OC0A toggle in CTC mode, except the double buffer feature of the Output Compare unit is enabled in the fast PWM mode.

13.7.4 Phase Correct PWM Mode

The phase correct PWM mode (WGM02:0 = 1 or 5) provides a high resolution phase correct PWM waveform generation option. The phase correct PWM mode is based on a dual-slope operation. The counter counts repeatedly from BOTTOM to TOP and then from TOP to BOT­TOM. TOP is defined as 0xFF when WGM2:0 = 1, and OCR0A when WGM2:0 = 5. In non­inverting Compare Output mode, the Output Compare (OC0x) is cleared on the Compare Match between TCNT0 and OCR0x while upcounting, and set on the Compare Match while down­counting. In inverting Output Compare mode, the operation is inverted. The dual-slope operation has lower maximum operation frequency than single slope operation. However, due to the sym­metric feature of the dual-slope PWM modes, these modes are preferred for motor control applications.
In phase correct PWM mode the counter is incremented until the counter value matches TOP. When the counter reaches TOP, it changes the count direction. The TCNT0 value will be equal to TOP for one timer clock cycle. The timing diagram for the phase correct PWM mode is shown on Figure 13-7 on page 81. The TCNT0 value is in the timing diagram shown as a histogram for illustrating the dual-slope operation. The diagram includes non-inverted and inverted PWM out­puts. The small horizontal line marks on the TCNT0 slopes represent Compare Matches between OCR0x and TCNT0.
ATtiny24/44/84
Figure 13-7. Phase Correct PWM Mode, Timing Diagram
OCnx Interrupt Flag Set
OCRnx Update
TOVn Interrupt Flag Set
TCNTn
OCn
OCn
Period
1 2 3
(COMnx1:0 = 2)
(COMnx1:0 = 3)
The Timer/Counter Overflow Flag (TOV0) is set each time the counter reaches BOTTOM. The Interrupt Flag can be used to generate an interrupt each time the counter reaches the BOTTOM value.
8006F–AVR–02/07
In phase correct PWM mode, the compare unit allows generation of PWM waveforms on the OC0x pins. Setting the COM0x1:0 bits to two will produce a non-inverted PWM. An inverted PWM output can be generated by setting the COM0x1:0 to three: Setting the COM0A0 bits to
81
Page 82
one allows the OC0A pin to toggle on Compare Matches if the WGM02 bit is set. This option is not available for the OC0B pin (See Table 13-4 on page 85). The actual OC0x value will only be visible on the port pin if the data direction for the port pin is set as output. The PWM waveform is generated by clearing (or setting) the OC0x Register at the Compare Match between OCR0x and TCNT0 when the counter increments, and setting (or clearing) the OC0x Register at Com­pare Match between OCR0x and TCNT0 when the counter decrements. The PWM frequency for the output when using phase correct PWM can be calculated by the following equation:
f
f
OCnxPCPWM
clk_I/O
----------------- -=
N 510
The N variable represents the prescale factor (1, 8, 64, 256, or 1024).
The extreme values for the OCR0A Register represent special cases when generating a PWM waveform output in the phase correct PWM mode. If the OCR0A is set equal to BOTTOM, the output will be continuously low and if set equal to MAX the output will be continuously high for non-inverted PWM mode. For inverted PWM the output will have the opposite logic values.
At the very start of period 2 in Figure 13-7 on page 81 OCn has a transition from high to low even though there is no Compare Match. The point of this transition is to guaratee symmetry around BOTTOM. There are two cases that give a transition without Compare Match.
• OCR0A changes its value from MAX, like in Figure 13-7 on page 81. When the OCR0A value
is MAX the OCn pin value is the same as the result of a down-counting Compare Match. To ensure symmetry around BOTTOM the OCn value at MAX must correspond to the result of an up-counting Compare Match.
• The timer starts counting from a value higher than the one in OCR0A, and for that reason
misses the Compare Match and hence the OCn change that would have happened on the way up.

13.8 Timer/Counter Timing Diagrams

The Timer/Counter is a synchronous design and the timer clock (clkT0) is therefore shown as a clock enable signal in the following figures. The figures include information on when Interrupt Flags are set. Figure 13-8 on page 82 contains timing data for basic Timer/Counter operation. The figure shows the count sequence close to the MAX value in all modes other than phase cor­rect PWM mode.
Figure 13-8. Timer/Counter Timing Diagram, no Prescaling
clk
I/O
clk
Tn
(clk
/1)
I/O
TCNTn MAX - 1 MAX BOTTOM BOTTOM + 1
TOVn
Figure 13-9 on page 83 shows the same timing data, but with the prescaler enabled.
82
ATtiny24/44/84
8006F–AVR–02/07
Page 83
ATtiny24/44/84
Figure 13-9. Timer/Counter Timing Diagram, with Prescaler (f
clk
I/O
clk
Tn
(clk
/8)
I/O
TCNTn
TOVn
MAX - 1 MAX BOTTOM BOTTOM + 1
clk_I/O
/8)
Figure 13-10 on page 83 shows the setting of OCF0B in all modes and OCF0A in all modes
except CTC mode and PWM mode, where OCR0A is TOP.
Figure 13-10. Timer/Counter Timing Diagram, Setting of OCF0x, with Prescaler (f
clk
I/O
clk
Tn
(clk
/8)
I/O
TCNTn
OCRnx
OCRnx - 1 OCRnx OCRnx + 1 OCRnx + 2
OCRnx Value
clk_I/O
/8)
OCFnx
Figure 13-11 on page 83 shows the setting of OCF0A and the clearing of TCNT0 in CTC mode
and fast PWM mode where OCR0A is TOP.
Figure 13-11. Timer/Counter Timing Diagram, Clear Timer on Compare Match mode, with Pres-
clk
I/O
clk
Tn
(clk
I/O
TCNTn
(CTC)
OCRnx
OCFnx
caler (f
/8)
/8)
clk_I/O
TOP - 1 TOP BOTTOM BOTTOM + 1
TOP
8006F–AVR–02/07
83
Page 84

13.9 Register Description

13.9.1 TCCR0A – Timer/Counter Control Register A
Bit 7 6 5 4 3 2 1 0
0x30 (0x50)
Read/Write R/W R/W R/W R/W RRR/W R/W
Initial Value 0 0 0 0 0 0 0 0
• Bits 7:6 – COM0A1:0: Compare Match Output A Mode
These bits control the Output Compare pin (OC0A) behavior. If one or both of the COM0A1:0 bits are set, the OC0A output overrides the normal port functionality of the I/O pin it is connected to. However, note that the Data Direction Register (DDR) bit corresponding to the OC0A pin must be set in order to enable the output driver.
When OC0A is connected to the pin, the function of the COM0A1:0 bits depends on the WGM02:0 bit setting. Table 13-2 on page 84 shows the COM0A1:0 bit functionality when the WGM02:0 bits are set to a normal or CTC mode (non-PWM).
Table 13-2. Compare Output Mode, non-PWM Mode
COM0A1 COM0A0 Description
00Normal port operation, OC0A disconnected.
0 1 Toggle OC0A on Compare Match
1 0 Clear OC0A on Compare Match
1 1 Set OC0A on Compare Match
COM0A1 COM0A0 COM0B1 COM0B0 WGM01 WGM00 TCCR0A
Table 13-3 on page 84 shows the COM0A1:0 bit functionality when the WGM01:0 bits are set to
fast PWM mode.
Table 13-3. Compare Output Mode, Fast PWM Mode
COM0A1 COM0A0 Description
00Normal port operation, OC0A disconnected.
01
10
11
Note: 1. A special case occurs when OCR0A equals TOP and COM0A1 is set. In this case, the Com-
pare Match is ignored, but the set or clear is done at BOTTOM. See ”Fast PWM Mode” on
page 79 for more details.
WGM02 = 0: Normal Port Operation, OC0A Disconnected. WGM02 = 1: Toggle OC0A on Compare Match.
Clear OC0A on Compare Match, set OC0A at BOTTOM (non-inverting mode)
Set OC0A on Compare Match, clear OC0A at BOTTOM (inverting mode)
(1)
84
ATtiny24/44/84
8006F–AVR–02/07
Page 85
ATtiny24/44/84
Table 13-4 on page 85 shows the COM0A1:0 bit functionality when the WGM02:0 bits are set to
phase correct PWM mode.
Table 13-4. Compare Output Mode, Phase Correct PWM Mode
COM0A1 COM0A0 Description
00Normal port operation, OC0A disconnected.
01
10
11
Note: 1. A special case occurs when OCR0A equals TOP and COM0A1 is set. In this case, the Com-
pare Match is ignored, but the set or clear is done at TOP. See ”Phase Correct PWM Mode” on
page 81 for more details.
WGM02 = 0: Normal Port Operation, OC0A Disconnected. WGM02 = 1: Toggle OC0A on Compare Match.
Clear OC0A on Compare Match when up-counting. Set OC0A on Compare Match when down-counting.
Set OC0A on Compare Match when up-counting. Clear OC0A on Compare Match when down-counting.
(1)
• Bits 5:4 – COM0B1:0: Compare Match Output B Mode
These bits control the Output Compare pin (OC0B) behavior. If one or both of the COM0B1:0 bits are set, the OC0B output overrides the normal port functionality of the I/O pin it is connected to. However, note that the Data Direction Register (DDR) bit corresponding to the OC0B pin must be set in order to enable the output driver.
When OC0B is connected to the pin, the function of the COM0B1:0 bits depends on the WGM02:0 bit setting. Table 13-5 on page 85 shows the COM0B1:0 bit functionality when the WGM02:0 bits are set to a normal or CTC mode (non-PWM).
Table 13-5. Compare Output Mode, non-PWM Mode
COM0B1 COM0B0 Description
00Normal port operation, OC0B disconnected.
0 1 Toggle OC0B on Compare Match
1 0 Clear OC0B on Compare Match
1 1 Set OC0B on Compare Match
Table 13-6 on page 85 shows the COM0B1:0 bit functionality when the WGM02:0 bits are set to
fast PWM mode.
Table 13-6. Compare Output Mode, Fast PWM Mode
COM0B1 COM0B0 Description
00Normal port operation, OC0B disconnected.
01Reserved
10
11
Clear OC0B on Compare Match, set OC0B at BOTTOM (non-inverting mode)
Set OC0B on Compare Match, clear OC0B at BOTTOM (inverting mode)
(1)
8006F–AVR–02/07
85
Page 86
Note: 1. A special case occurs when OCR0B equals TOP and COM0B1 is set. In this case, the Com-
pare Match is ignored, but the set or clear is done at BOTTOM. See ”Fast PWM Mode” on
page 79 for more details.
Table 13-7 shows the COM0B1:0 bit functionality when the WGM02:0 bits are set to phase cor-
rect PWM mode.
Table 13-7. Compare Output Mode, Phase Correct PWM Mode
COM0B1 COM0B0 Description
00Normal port operation, OC0B disconnected.
01Reserved
10
11
Note: 1. A special case occurs when OCR0B equals TOP and COM0B1 is set. In this case, the Com-
pare Match is ignored, but the set or clear is done at TOP. See ”Phase Correct PWM Mode” on
page 81 for more details.
Clear OC0B on Compare Match when up-counting. Set OC0B on Compare Match when down-counting.
Set OC0B on Compare Match when up-counting. Clear OC0B on Compare Match when down-counting.
(1)
• Bits 3, 2 – Res: Reserved Bits
These bits are reserved bits in the ATtiny24/44/84 and will always read as zero.
• Bits 1:0 – WGM01:0: Waveform Generation Mode
Combined with the WGM02 bit found in the TCCR0B Register, these bits control the counting sequence of the counter, the source for maximum (TOP) counter value, and what type of wave­form generation to be used, see Table 13-8 on page 86. Modes of operation supported by the Timer/Counter unit are: Normal mode (counter), Clear Timer on Compare Match (CTC) mode, and two types of Pulse Width Modulation (PWM) modes (see ”Modes of Operation” on page 78).
86
Table 13-8. Waveform Generation Mode Bit Description
Mode WGM02 WGM01 WGM00
0000Normal 0xFF Immediate MAX
1001
2 0 1 0 CTC OCRA Immediate MAX
3011Fast PWM0xFFBOTTOMMAX
4100Reserved – – –
5101
6110Reserved – – –
7111Fast PWMOCRABOTTOMTOP
Note: 1. MAX = 0xFF
ATtiny24/44/84
BOTTOM = 0x00
Timer/Counter Mode of Operation TOP
PWM, Phase Correct
PWM, Phase Correct
0xFF TOP BOTTOM
OCRA TOP BOTTOM
Update of
OCRx at
TOV Flag
(1)
Set on
8006F–AVR–02/07
Page 87
13.9.2 TCCR0B – Timer/Counter Control Register B
Bit 7 6 5 4 3 2 1 0
0x33 (0x53)
Read/Write WWRRR/W R/W R/W R/W
Initial Value 0 0 0 0 0 0 0 0
FOC0A FOC0B WGM02 CS02 CS01 CS00 TCCR0B
• Bit 7 – FOC0A: Force Output Compare A
The FOC0A bit is only active when the WGM bits specify a non-PWM mode.
However, for ensuring compatibility with future devices, this bit must be set to zero when TCCR0B is written when operating in PWM mode. When writing a logical one to the FOC0A bit, an immediate Compare Match is forced on the Waveform Generation unit. The OC0A output is changed according to its COM0A1:0 bits setting. Note that the FOC0A bit is implemented as a strobe. Therefore it is the value present in the COM0A1:0 bits that determines the effect of the forced compare.
A FOC0A strobe will not generate any interrupt, nor will it clear the timer in CTC mode using OCR0A as TOP.
The FOC0A bit is always read as zero.
ATtiny24/44/84
• Bit 6 – FOC0B: Force Output Compare B
The FOC0B bit is only active when the WGM bits specify a non-PWM mode.
However, for ensuring compatibility with future devices, this bit must be set to zero when TCCR0B is written when operating in PWM mode. When writing a logical one to the FOC0B bit, an immediate Compare Match is forced on the Waveform Generation unit. The OC0B output is changed according to its COM0B1:0 bits setting. Note that the FOC0B bit is implemented as a strobe. Therefore it is the value present in the COM0B1:0 bits that determines the effect of the forced compare.
A FOC0B strobe will not generate any interrupt, nor will it clear the timer in CTC mode using OCR0B as TOP.
The FOC0B bit is always read as zero.
• Bits 5:4 – Res: Reserved Bits
These bits are reserved bits in the ATtiny24/44/84 and will always read as zero.
• Bit 3 – WGM02: Waveform Generation Mode
See the description in the ”TCCR0A – Timer/Counter Control Register A” on page 84.
• Bits 2:0 – CS02:0: Clock Select
The three Clock Select bits select the clock source to be used by the Timer/Counter.
8006F–AVR–02/07
87
Page 88
Table 13-9. Clock Select Bit Description
CS02 CS01 CS00 Description
000No clock source (Timer/Counter stopped)
001clk
010clk
011clk
100clk
101clk
1 1 0 External clock source on T0 pin. Clock on falling edge.
1 1 1 External clock source on T0 pin. Clock on rising edge.
If external pin modes are used for the Timer/Counter0, transitions on the T0 pin will clock the counter even if the pin is configured as an output. This feature allows software control of the counting.
13.9.3 TCNT0 – Timer/Counter Register
Bit 76543210
0x32 (0x52) TCNT0[7:0] TCNT0
Read/Write R/W R/W R/W R/W R/W R/W R/W R/W
Initial Value 0 0 0 0 0 0 0 0
/(No prescaling)
I/O
/8 (From prescaler)
I/O
/64 (From prescaler)
I/O
/256 (From prescaler)
I/O
/1024 (From prescaler)
I/O
The Timer/Counter Register gives direct access, both for read and write operations, to the Timer/Counter unit 8-bit counter. Writing to the TCNT0 Register blocks (removes) the Compare Match on the following timer clock. Modifying the counter (TCNT0) while the counter is running, introduces a risk of missing a Compare Match between TCNT0 and the OCR0x Registers.
13.9.4 OCR0A – Output Compare Register A
Bit 76543210
0x36 (0x56) OCR0A[7:0] OCR0A
Read/Write R/W R/W R/W R/W R/W R/W R/W R/W
Initial Value 0 0 0 0 0 0 0 0
The Output Compare Register A contains an 8-bit value that is continuously compared with the counter value (TCNT0). A match can be used to generate an Output Compare interrupt, or to generate a waveform output on the OC0A pin.
13.9.5 OCR0B – Output Compare Register B
Bit 76543210
0x3C (0x5C) OCR0B[7:0] OCR0B
Read/Write R/W R/W R/W R/W R/W R/W R/W R/W
Initial Value 0 0 0 0 0 0 0 0
The Output Compare Register B contains an 8-bit value that is continuously compared with the counter value (TCNT0). A match can be used to generate an Output Compare interrupt, or to generate a waveform output on the OC0B pin.
88
ATtiny24/44/84
8006F–AVR–02/07
Page 89
13.9.6 TIMSK0 – Timer/Counter 0 Interrupt Mask Register
Bit 76543 2 10
0x39 (0x59) –––––OCIE0BOCIE0ATOIE0TIMSK0
Read/Write RRRRRR/W R/W R/W
Initial Value000000 00
• Bits 7..3 – Res: Reserved Bits
These bits are reserved bits in the ATtiny24/44/84 and will always read as zero.
• Bit 2– OCIE0B: Timer/Counter Output Compare Match B Interrupt Enable
When the OCIE0B bit is written to one, and the I-bit in the Status Register is set, the Timer/Counter Compare Match B interrupt is enabled. The corresponding interrupt is executed if a Compare Match in Timer/Counter occurs, i.e., when the OCF0B bit is set in the Timer/Counter Interrupt Flag Register – TIFR0.
• Bit 1– OCIE0A: Timer/Counter0 Output Compare Match A Interrupt Enable
When the OCIE0A bit is written to one, and the I-bit in the Status Register is set, the Timer/Counter0 Compare Match A interrupt is enabled. The corresponding interrupt is executed if a Compare Match in Timer/Counter0 occurs, i.e., when the OCF0A bit is set in the Timer/Counter 0 Interrupt Flag Register – TIFR0.
ATtiny24/44/84
• Bit 0– TOIE0: Timer/Counter0 Overflow Interrupt Enable
When the TOIE0 bit is written to one, and the I-bit in the Status Register is set, the Timer/Counter0 Overflow interrupt is enabled. The corresponding interrupt is executed if an overflow in Timer/Counter0 occurs, i.e., when the TOV0 bit is set in the Timer/Counter 0 Inter­rupt Flag Register – TIFR0.
13.9.7 TIFR0 – Timer/Counter 0 Interrupt Flag Register
Bit 76543210
0x38 (0x58) OCF0B OCF0A TOV0 TIFR0
Read/Write RRRRRR/W R/W R/W
Initial Value 0 0 0 0 0 0 0 0
• Bits 7..3 – Res: Reserved Bits
These bits are reserved bits in the ATtiny24/44/84 and will always read as zero.
• Bit 2– OCF0B: Output Compare Flag 0 B
The OCF0B bit is set when a Compare Match occurs between the Timer/Counter and the data in OCR0B – Output Compare Register0 B. OCF0B is cleared by hardware when executing the cor­responding interrupt handling vector. Alternatively, OCF0B is cleared by writing a logic one to the flag. When the I-bit in SREG, OCIE0B (Timer/Counter Compare B Match Interrupt Enable), and OCF0B are set, the Timer/Counter Compare Match Interrupt is executed.
• Bit 1– OCF0A: Output Compare Flag 0 A
The OCF0A bit is set when a Compare Match occurs between the Timer/Counter0 and the data in OCR0A – Output Compare Register0. OCF0A is cleared by hardware when executing the cor­responding interrupt handling vector. Alternatively, OCF0A is cleared by writing a logic one to the flag. When the I-bit in SREG, OCIE0A (Timer/Counter0 Compare Match Interrupt Enable), and OCF0A are set, the Timer/Counter0 Compare Match Interrupt is executed.
8006F–AVR–02/07
89
Page 90
• Bit 0– TOV0: Timer/Counter0 Overflow Flag
The bit TOV0 is set when an overflow occurs in Timer/Counter0. TOV0 is cleared by hardware when executing the corresponding interrupt handling vector. Alternatively, TOV0 is cleared by writing a logic one to the flag. When the SREG I-bit, TOIE0 (Timer/Counter0 Overflow Interrupt Enable), and TOV0 are set, the Timer/Counter0 Overflow interrupt is executed.
The setting of this flag is dependent of the WGM02:0 bit setting. See Table 13-8 on page 86 and
Waveform Generation Mode Bit Description” on page 86.
90
ATtiny24/44/84
8006F–AVR–02/07
Page 91

14. 16-bit Timer/Counter1

14.1 Features

True 16-bit Design (i.e., Allows 16-bit PWM)
Two independent Output Compare Units
Double Buffered Output Compare Registers
One Input Capture Unit
Input Capture Noise Canceler
Clear Timer on Compare Match (Auto Reload)
Glitch-free, Phase Correct Pulse Width Modulator (PWM)
Variable PWM Period
Frequency Generator
External Event Counter
Four independent interrupt Sources (TOV1, OCF1A, OCF1B, and ICF1)

14.2 Overview

The 16-bit Timer/Counter unit allows accurate program execution timing (event management), wave generation, and signal timing measurement.
Most register and bit references in this section are written in general form. A lower case “n” replaces the Timer/Counter number, and a lower case “x” replaces the Output Compare unit channel. However, when using the register or bit defines in a program, the precise form must be used, i.e., TCNT1 for accessing Timer/Counter1 counter value and so on.
ATtiny24/44/84
A simplified block diagram of the 16-bit Timer/Counter is shown in Figure 14-1 on page 92. For the actual placement of I/O pins, refer to ”Pinout ATtiny24/44/84” on page 2. CPU accessible I/O Registers, including I/O bits and I/O pins, are shown in bold. The device-specific I/O Register and bit locations are listed in the ”Register Description” on page 113.
8006F–AVR–02/07
91
Page 92
Figure 14-1. 16-bit Timer/Counter Block Diagram
(1)
Count
Clear
Direction
Timer/Counter
TCNTn
Control Logic
TOP BOT TOM
=
clk
Tn
=
0
=
OCRnA
Fixed
TOP
Values
=
DATA B US
OCRnB
ICRn
ICFn (Int.Req.)
Edge
Detector
TOVn
(Int.Req.)
Clock Select
Edge
Detector
( From Prescaler )
OCnA
(Int.Req.)
Wavefo rm
Generation
OCnB
(Int.Req.)
Wavefo rm
Generation
Noise
Canceler
Tn
OCnA
OCnB
( From Analog
Comparator Ouput )
ICPn

14.2.1 Registers

TCCRnA TCCRnB
Note: 1. See Figure 1-1 on page 2 for Timer/Counter1 pin placement and description.
The Timer/Counter (TCNT1), Output Compare Registers (OCR1A/B), and Input Capture Regis- ter (ICR1) are all 16-bit registers. Special procedures must be followed when accessing the 16­bit registers. These procedures are described in the section ”Accessing 16-bit Registers” on
page 94. The Timer/Counter Control Registers (TCCR1A/B) are 8-bit registers and have no CPU
access restrictions. Interrupt requests (abbreviated to Int.Req. in the figure) signals are all visible in the Timer Interrupt Flag Register (TIFR). All interrupts are individually masked with the Timer Interrupt Mask Register (TIMSK). TIFR and TIMSK are not shown in the figure.
The Timer/Counter can be clocked internally, via the prescaler, or by an external clock source on the T1 pin. The Clock Select logic block controls which clock source and edge the Timer/Counter uses to increment (or decrement) its value. The Timer/Counter is inactive when no clock source is selected. The output from the Clock Select logic is referred to as the timer clock (clk
).
1
T
The double buffered Output Compare Registers (OCR1A/B) are compared with the Timer/Counter value at all time. The result of the compare can be used by the Waveform Gener­ator to generate a PWM or variable frequency output on the Output Compare pin (OC1A/B). See
”Output Compare Units” on page 100. The compare match event will also set the Compare
Match Flag (OCF1A/B) which can be used to generate an Output Compare interrupt request.
92
ATtiny24/44/84
8006F–AVR–02/07
Page 93

14.2.2 Definitions

ATtiny24/44/84
The Input Capture Register can capture the Timer/Counter value at a given external (edge trig­gered) event on either the Input Capture pin (ICP1) or on the Analog Comparator pins (See
”Analog Comparator” on page 134). The Input Capture unit includes a digital filtering unit (Noise
Canceler) for reducing the chance of capturing noise spikes.
The TOP value, or maximum Timer/Counter value, can in some modes of operation be defined by either the OCR1A Register, the ICR1 Register, or by a set of fixed values. When using OCR1A as TOP value in a PWM mode, the OCR1A Register can not be used for generating a PWM output. However, the TOP value will in this case be double buffered allowing the TOP value to be changed in run time. If a fixed TOP value is required, the ICR1 Register can be used as an alternative, freeing the OCR1A to be used as PWM output.
The following definitions are used extensively throughout the section:
BOTTOM The counter reaches the BOTTOM when it becomes 0x0000. MAX The coun ter reaches its MAXimum when it becomes 0xFFFF (decimal 65535).
The counter reaches the TOP when it becomes equal to the highest value in the count
TOP
sequence. The TOP value can be assigned to be one of the fixed values: 0x00FF , 0x01FF, or 0x03FF, or to the value stored in the OCR1A or ICR1 Register. The assignment is dependent of the mode of operation.

14.2.3 Compatibility

The 16-bit Timer/Counter has been updated and improved from previous versions of the 16-bit AVR Timer/Counter. This 16-bit Timer/Counter is fully compatible with the earlier version regarding:
• All 16-bit Timer/Counter related I/O Register address locations, including Timer Interrupt
Registers.
• Bit locations inside all 16-bit Timer/Counter Registers, including Timer Interrupt Registers.
• Interrupt Vectors.
The following control bits have changed name, but have same functionality and register location:
•PWM10 is changed to WGM10.
•PWM11 is changed to WGM11.
• CTC1 is changed to WGM12.
The following bits are added to the 16-bit Timer/Counter Control Registers:
• 1A and 1B are added to TCCR1A.
WGM13 is added to TCCR1B.
The 16-bit Timer/Counter has improvements that will affect the compatibility in some special cases.
8006F–AVR–02/07
93
Page 94

14.3 Accessing 16-bit Registers

The TCNT1, OCR1A/B, and ICR1 are 16-bit registers that can be accessed by the AVR CPU via the 8-bit data bus. The 16-bit register must be byte accessed using two read or write operations. Each 16-bit timer has a single 8-bit register for temporary storing of the high byte of the 16-bit access. The same temporary register is shared between all 16-bit registers within each 16-bit timer. Accessing the low byte triggers the 16-bit read or write operation. When the low byte of a 16-bit register is written by the CPU, the high byte stored in the temporary register, and the low byte written are both copied into the 16-bit register in the same clock cycle. When the low byte of a 16-bit register is read by the CPU, the high byte of the 16-bit register is copied into the tempo­rary register in the same clock cycle as the low byte is read.
Not all 16-bit accesses uses the temporary register for the high byte. Reading the OCR1A/B 16­bit registers does not involve using the temporary register.
To do a 16-bit write, the high byte must be written before the low byte. For a 16-bit read, the low byte must be read before the high byte.
The following code examples show how to access the 16-bit timer registers assuming that no interrupts updates the temporary register. The same principle can be used directly for accessing the OCR1A/B and ICR1 Registers. Note that when using “C”, the compiler handles the 16-bit access.
Assembly Code Examples
...
; Set TCNT1 to 0x01FF
ldi r17,0x01
ldi r16,0xFF
out TCNT1H,r17 out TCNT1L,r16
; Read TCNT1 into r17:r16
in r16,TCNT1L in r17,TCNT1H
...
C Code Examples
(1)
(1)
94
unsigned int i;
...
/* Set TCNT1 to 0x01FF */ TCNT1 = 0x1FF; /* Read TCNT1 into i */ i = TCNT1;
...
Note: 1. See ”About Code Examples” on page 7.
The assembly code example returns the TCNT1 value in the r17:r16 register pair.
It is important to notice that accessing 16-bit registers are atomic operations. If an interrupt occurs between the two instructions accessing the 16-bit register, and the interrupt code updates the temporary register by accessing the same or any other of the 16-bit timer registers, then the result of the access outside the interrupt will be corrupted. Therefore, when both the
ATtiny24/44/84
8006F–AVR–02/07
Page 95
ATtiny24/44/84
main code and the interrupt code update the temporary register, the main code must disable the interrupts during the 16-bit access.
The following code examples show how to do an atomic read of the TCNT1 Register contents. Reading any of the OCR1A/B or ICR1 Registers can be done by using the same principle.
Assembly Code Example
TIM16_ReadTCNT1:
; Save global interrupt flag
in r18,SREG
; Disable interrupts
cli
; Read TCNT1 into r17:r16
in r16,TCNT1L in r17,TCNT1H
; Restore global interrupt flag
out SREG,r18
ret
C Code Example
unsigned int TIM16_ReadTCNT1( void )
{
unsigned char sreg;
unsigned int i;
/* Save global interrupt flag */
sreg = SREG;
/* Disable interrupts */
_CLI();
/* Read TCNT1 into i */ i = TCNT1;
/* Restore global interrupt flag */
SREG = sreg;
return i;
}
(1)
(1)
8006F–AVR–02/07
Note: 1. See ”About Code Examples” on page 7.
The assembly code example returns the TCNT1 value in the r17:r16 register pair.
95
Page 96
The following code examples show how to do an atomic write of the TCNT1 Register contents. Writing any of the OCR1A/B or ICR1 Registers can be done by using the same principle.
Assembly Code Example
TIM16_WriteTCNT1:
; Save global interrupt flag
in r18,SREG
; Disable interrupts
cli
; Set TCNT1 to r17:r16
out TCNT1H,r17 out TCNT1L,r16
; Restore global interrupt flag
out SREG,r18
ret
C Code Example
void TIM16_WriteTCNT1( unsigned int i )
{
unsigned char sreg;
unsigned int i;
/* Save global interrupt flag */
sreg = SREG;
/* Disable interrupts */
_CLI();
/* Set TCNT1 to i */ TCNT1 = i;
/* Restore global interrupt flag */
SREG = sreg;
}
(1)
(1)
Note: 1. See ”About Code Examples” on page 7.
The assembly code example requires that the r17:r16 register pair contains the value to be writ­ten to TCNT1.

14.3.1 Reusing the Temporary High Byte Register

If writing to more than one 16-bit register where the high byte is the same for all registers written, then the high byte only needs to be written once. However, note that the same rule of atomic operation described previously also applies in this case.

14.4 Timer/Counter Clock Sources

The Timer/Counter can be clocked by an internal or an external clock source. The clock source is selected by the Clock Select logic which is controlled by the Clock Select (CS12:0) bits located in the Timer/Counter control Register B (TCCR1B). For details on clock sources and prescaler, see ”Timer/Counter Prescaler” on page 120.
96
ATtiny24/44/84
8006F–AVR–02/07
Page 97

14.5 Counter Unit

ATtiny24/44/84
The main part of the 16-bit Timer/Counter is the programmable 16-bit bi-directional counter unit.
Figure 14-2 on page 97 shows a block diagram of the counter and its surroundings.
Figure 14-2. Counter Unit Block Diagram
DATA BUS
TEMP (8-bit)
TCNTnH (8-bit) TCNTnL (8-bit)
TCNTn (16-bit Counter)
Signal description (internal signals):
Count Increment or decrement TCNT1 by 1.
Direction Select between increment and decrement.
(8-bit)
Count
Clear
Direction
Control Logic
TOP BOTTOM
TOVn
(Int.Req.)
clk
Tn
Clock Select
Edge
Detector
( From Prescaler )
Tn
Clear Clear TCNT1 (set all bits to zero).
clk
1
T
Timer/Counter clock.
TOP Signalize that TCNT1 has reached maximum value.
BOTTOM Signalize that TCNT1 has reached minimum value (zero).
The 16-bit counter is mapped into two 8-bit I/O memory locations: Counter High (TCNT1H) con­taining the upper eight bits of the counter, and Counter Low (TCNT1L) containing the lower eight bits. The TCNT1H Register can only be indirectly accessed by the CPU. When the CPU does an access to the TCNT1H I/O location, the CPU accesses the high byte temporary register (TEMP). The temporary register is updated with the TCNT1H value when the TCNT1L is read, and TCNT1H is updated with the temporary register value when TCNT1L is written. This allows the CPU to read or write the entire 16-bit counter value within one clock cycle via the 8-bit data bus. It is important to notice that there are special cases of writing to the TCNT1 Register when the counter is counting that will give unpredictable results. The special cases are described in the sections where they are of importance.
Depending on the mode of operation used, the counter is cleared, incremented, or decremented at each timer clock (clk
). The clk
1
T
can be generated from an external or internal clock source,
1
T
selected by the Clock Select bits (CS12:0). When no clock source is selected (CS12:0 = 0) the timer is stopped. However, the TCNT1 value can be accessed by the CPU, independent of whether clk
is present or not. A CPU write overrides (has priority over) all counter clear or
1
T
count operations.
8006F–AVR–02/07
The counting sequence is determined by the setting of the Waveform Generation mode bits (WGM13:0) located in the Timer/Counter Control Registers A and B (TCCR1A and TCCR1B). There are close connections between how the counter behaves (counts) and how waveforms are generated on the Output Compare outputs OC1x. For more details about advanced counting sequences and waveform generation, see ”Modes of Operation” on page 103.
97
Page 98
The Timer/Counter Overflow Flag (TOV1) is set according to the mode of operation selected by the WGM13:0 bits. TOV1 can be used for generating a CPU interrupt.

14.6 Input Capture Unit

The Timer/Counter incorporates an Input Capture unit that can capture external events and give them a time-stamp indicating time of occurrence. The external signal indicating an event, or mul­tiple events, can be applied via the ICP1 pin or alternatively, via the analog-comparator unit. The time-stamps can then be used to calculate frequency, duty-cycle, and other features of the sig­nal applied. Alternatively the time-stamps can be used for creating a log of the events.
The Input Capture unit is illustrated by the block diagram shown in Figure 14-3 on page 98. The elements of the block diagram that are not directly a part of the Input Capture unit are gray shaded. The small “n” in register and bit names indicates the Timer/Counter number.
Figure 14-3. Input Capture Unit Block Diagram
ICPn
WRITE
TEMP (8-bit)
ICRnH (8-bit)
ICRn (16-bit Register)
ACO*
Analog
Comparator
DATA BUS
ICRnL (8-bit)
ACIC* ICNC ICES
Canceler
Noise
(8-bit)
TCNTnH (8-bit) TCNTnL (8-bit)
TCNTn (16-bit Counter)
Edge
Detector
ICFn (Int.Req.)
When a change of the logic level (an event) occurs on the Input Capture pin (ICP1), alternatively on the Analog Comparator output (ACO), and this change confirms to the setting of the edge detector, a capture will be triggered. When a capture is triggered, the 16-bit value of the counter (TCNT1) is written to the Input Capture Register (ICR1). The Input Capture Flag (ICF1) is set at the same system clock as the TCNT1 value is copied into ICR1 Register. If enabled (ICIE1 = 1), the Input Capture Flag generates an Input Capture interrupt. The ICF1 flag is automatically cleared when the interrupt is executed. Alternatively the ICF1 flag can be cleared by software by writing a logical one to its I/O bit location.
98
Reading the 16-bit value in the Input Capture Register (ICR1) is done by first reading the low byte (ICR1L) and then the high byte (ICR1H). When the low byte is read the high byte is copied into the high byte temporary register (TEMP). When the CPU reads the ICR1H I/O location it will access the TEMP Register.
The ICR1 Register can only be written when using a Waveform Generation mode that utilizes the ICR1 Register for defining the counter’s TOP value. In these cases the Waveform Genera-
ATtiny24/44/84
8006F–AVR–02/07
Page 99
tion mode (WGM13:0) bits must be set before the TOP value can be written to the ICR1 Register. When writing the ICR1 Register the high byte must be written to the ICR1H I/O location before the low byte is written to ICR1L.
For more information on how to access the 16-bit registers refer to ”Accessing 16-bit Registers”
on page 94.

14.6.1 Input Capture Trigger Source

The main trigger source for the Input Capture unit is the Input Capture pin (ICP1). Timer/Counter1 can alternatively use the Analog Comparator output as trigger source for the Input Capture unit. The Analog Comparator is selected as trigger source by setting the Analog Comparator Input Capture (ACIC) bit in the Analog Comparator Control and Status Register (ACSR). Be aware that changing trigger source can trigger a capture. The Input Capture Flag must therefore be cleared after the change.
Both the Input Capture pin (ICP1) and the Analog Comparator output (ACO) inputs are sampled using the same technique as for the T1 pin (Figure 15-1 on page 120). The edge detector is also identical. However, when the noise canceler is enabled, additional logic is inserted before the edge detector, which increases the delay by four system clock cycles. Note that the input of the noise canceler and edge detector is always enabled unless the Timer/Counter is set in a Wave- form Generation mode that uses ICR1 to define TOP.
ATtiny24/44/84
An Input Capture can be triggered by software by controlling the port of the ICP1 pin.

14.6.2 Noise Canceler

The noise canceler improves noise immunity by using a simple digital filtering scheme. The noise canceler input is monitored over four samples, and all four must be equal for changing the output that in turn is used by the edge detector.
The noise canceler is enabled by setting the Input Capture Noise Canceler (ICNC1) bit in Timer/Counter Control Register B (TCCR1B). When enabled the noise canceler introduces addi- tional four system clock cycles of delay from a change applied to the input, to the update of the ICR1 Register. The noise canceler uses the system clock and is therefore not affected by the prescaler.

14.6.3 Using the Input Capture Unit

The main challenge when using the Input Capture unit is to assign enough processor capacity for handling the incoming events. The time between two events is critical. If the processor has not read the captured value in the ICR1 Register before the next event occurs, the ICR1 will be overwritten with a new value. In this case the result of the capture will be incorrect.
When using the Input Capture interrupt, the ICR1 Register should be read as early in the inter­rupt handler routine as possible. Even though the Input Capture interrupt has relatively high priority, the maximum interrupt response time is dependent on the maximum number of clock cycles it takes to handle any of the other interrupt requests.
8006F–AVR–02/07
Using the Input Capture unit in any mode of operation when the TOP value (resolution) is actively changed during operation, is not recommended.
Measurement of an external signal’s duty cycle requires that the trigger edge is changed after each capture. Changing the edge sensing must be done as early as possible after the ICR1 Register has been read. After a change of the edge, the Input Capture Flag (ICF1) must be
99
Page 100
cleared by software (writing a logical one to the I/O bit location). For measuring frequency only, the clearing of the ICF1 flag is not required (if an interrupt handler is used).

14.7 Output Compare Units

The 16-bit comparator continuously compares TCNT1 with the Output Compare Register (OCR1x). If TCNT equals OCR1x the comparator signals a match. A match will set the Output Compare Flag (OCF1x) at the next timer clock cycle. If enabled (OCIE1x = 1), the Output Com­pare Flag generates an Output Compare interrupt. The OCF1x flag is automatically cleared when the interrupt is executed. Alternatively the OCF1x flag can be cleared by software by writ­ing a logical one to its I/O bit location. The Waveform Generator uses the match signal to generate an output according to operating mode set by the Waveform Generation mode (WGM13:0) bits and Compare Output mode (COM1x1:0) bits. The TOP and BOTTOM signals are used by the Waveform Generator for handling the special cases of the extreme values in some modes of operation (”Modes of Operation” on page 103).
A special feature of Output Compare unit A allows it to define the Timer/Counter TOP value (i.e., counter resolution). In addition to the counter resolution, the TOP value defines the period time for waveforms generated by the Waveform Generator.
Figure 14-4 on page 100 shows a block diagram of the Output Compare unit. The small “n” in
the register and bit names indicates the device number (n = 1 indicates Output Compare unit (A/B). The elements of the block diagram that are not directly a part of the Output Compare unit are gray shaded.
for Timer/Counter 1), and the “x”
Figure 14-4. Output Compare Unit, Block Diagram
DATA BUS
TEMP (8-bit)
OCRnxH Buf. (8-bit)
OCRnx Buffer (16-bit Register)
OCRnxH (8-bit) OCRnxL (8-bit)
OCRnx (16-bit Register)
TOP
BOTTOM
OCRnxL Buf. (8-bit)
(8-bit)
TCNTnH (8-bit) TCNTnL (8-bit)
=
(16-bit Comparator )
OCFnx (Int.Req.)
Waveform Generator
COMnx1:0WGMn3:0
TCNTn (16-bit Counter)
OCnx
The OCR1x Register is double buffered when using any of the twelve Pulse Width Modulation (PWM) modes. For the Normal and Clear Timer on Compare (CTC) modes of operation, the double buffering is disabled. The double buffering synchronizes the update of the OCR1x Com­pare Register to either TOP or BOTTOM of the counting sequence. The synchronization
100
ATtiny24/44/84
8006F–AVR–02/07
Loading...