ATTiny24 Data PDF
ATTiny24 Data PDF
DATASHEET
Features
7701G-AVR-02/15
● Operating voltage:
● 2.7 - 5.5V for Atmel ATtiny24/44/84
● Speed grade
● Atmel ATtiny24/44/84: 0 - 8MHz at 2.7 - 5.5V, 0 - 16MHz at 4.5 - 5.5V
● Automotive temperature range
● Low power consumption
● Active mode:
● 1MHz, 2.7V: 800µA
● Power-down mode:
● 2.7V: 2.0µA
2 ATtiny24/44/84 [DATASHEET]
7701G–AVR–02/15
1. Pin Configurations
VCC 1 14 GND
QFN/MLF
DNC
DNC
PA6
Pin20: PA5 (ADC5/DO/MISO/OC1B/PCINT5)
20 19 18 17 16
(ADC4/USCK/SCL/PCINT4) PA4 1 15 PA7 (PCINT7/ICP/OC0B/ADC7)
Note
DNC
DNC
GND
VCC
DNC
1.1 Disclaimer
Typical values contained in this data sheet are based on simulations and characterization of actual Atmel® ATtiny24/44/84
AVR® microcontrollers manufactured on the typical process technology. Applicable automotive min. and max. values will be
available after devices representative of the whole process excursion (corner run) have been characterized.
ATtiny24/44/84 [DATASHEET] 3
7701G–AVR–02/15
2. Overview
The Atmel® 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 Atmel ATtiny24/44/84 achieves throughputs approaching 1MIPS
per MHz allowing the system designer to optimize power consumption versus processing speed.
4 ATtiny24/44/84 [DATASHEET]
7701G–AVR–02/15
2.1 Block Diagram
8-bit Databus
Internal
Internal Calibrated
Oscillator Oscillator
GND
Program Stack Watchdog Timing and
Counter Pointer Timer Control
X Timer/
Instruction Y Counter 0
Decoder Z
Timer/
Counter 1
Control ALU
Lines
Status
Register
Interrupt
Unit
Programming ISP
EEPROM Oscillators
Logic Interface
Comparator
Analog
Data Register Data Dir. Register ADC Data Register Data Dir. Register
+ - Port A Port A Port B Port B
PA7-PA0 PB3-PB0
ATtiny24/44/84 [DATASHEET] 5
7701G–AVR–02/15
The Atmel® AVR® core combines a rich instruction set with 32 general purpose working registers. All 32 registers are directly
connected to the arithmetic logic unit (ALU), allowing two independent registers to be accessed in one single instruction
executed in one clock cycle. The resulting architecture is more code efficient while achieving throughputs up to ten times
faster than conventional CISC microcontrollers.
The Atmel ATtiny24/44/84 provides the following features: 2/4/8K bytes 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, an 8-bit
Timer/Counter with two PWM channels, a 16-bit Timer/Counter with two PWM channels, internal and external interrupts, an
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 selectable 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 interrupt 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 crystal/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 using 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 Atmel 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.
2.3.1 VCC
Supply voltage.
2.3.2 GND
Ground.
6 ATtiny24/44/84 [DATASHEET]
7701G–AVR–02/15
2.3.4 RESET
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 9-1 on page 37. Shorter pulses are not guaranteed to generate a reset.
ATtiny24/44/84 [DATASHEET] 7
7701G–AVR–02/15
3. Resources
A comprehensive set of development tools, driver and application notes, and datasheets are available for download on
http://www.atmel.com/avr.
8 ATtiny24/44/84 [DATASHEET]
7701G–AVR–02/15
5. CPU Core
5.1 Overview
This section discusses the Atmel® 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.
32 x 8
General
Instruction
Purpose
Register
Registers Interrupt
Unit
Indirect Addressing
Instruction
Direct Addressing
Decoder Watchdog
ALU Timer
Timer/Counter 0
Data
SRAM
Timer/Counter 1
Universal
EEPROM 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 instruction 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.
ATtiny24/44/84 [DATASHEET] 9
7701G–AVR–02/15
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 typical ALU operation, two operands are output from the
register file, the operation is executed, and the result is stored back in the register file, all 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 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-registers, 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 operation, 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 format. 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 stack pointer (SP) in the reset routine (before subroutines or
interrupts are executed). The 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 Atmel® 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 position. The lower the interrupt vector address, the higher the priority.
The I/O memory space contains 64 addresses for CPU peripheral functions such as control registers, 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.
10 ATtiny24/44/84 [DATASHEET]
7701G–AVR–02/15
5.4.1 SREG – AVR Status Register
Bit 7 6 5 4 3 2 1 0
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 Value 0 0 0 0 0 0 0 0
ATtiny24/44/84 [DATASHEET] 11
7701G–AVR–02/15
5.5 General Purpose Register File
The register file is optimized for the Atmel® 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 12 shows the structure of the 32 general purpose working registers in the CPU.
7 0 Addr.
R0 0x00
R1 0x01
R2 0x02
…
R13 0x0D
General R14 0x0E
Purpose R15 0x0F
Working R16 0x10
Registers R17 0x11
…
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 physically implemented as SRAM locations, this memory organization
provides great flexibility in access of the registers, as the X-, Y- and Z-registers can be set to index any register in the file.
12 ATtiny24/44/84 [DATASHEET]
7701G–AVR–02/15
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 registers 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 13.
15 XH XL 0
X-register 7 0 7 0
R27 (0x1B) R26 (0x1A)
15 YH YL 0
Y-register 7 0 7 0
R29 (0x1D) R28 (0x1C)
15 ZH ZL 0
Z-register 7 0 7 0
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 summary for details).
ATtiny24/44/84 [DATASHEET] 13
7701G–AVR–02/15
5.7 Instruction Execution Timing
This section describes the general access timing concepts for instruction execution. The Atmel® AVR® CPU is driven by the
CPU clock clkCPU, directly generated from the selected clock source for the chip. No internal clock division is used.
Figure 5-4 shows the parallel instruction fetches and instruction executions enabled by the Harvard architecture and the fast
access register file concept. This is the basic pipelining concept to obtain up to 1MIPS per MHz with the corresponding
unique results for functions per cost, functions per clocks, and functions per power-unit.
Figure 5-5 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.
clkCPU
14 ATtiny24/44/84 [DATASHEET]
7701G–AVR–02/15
The second type of interrupt 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 Atmel® 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) */
When using the SEI instruction to enable interrupts, the instruction following SEI will be executed 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) */
ATtiny24/44/84 [DATASHEET] 15
7701G–AVR–02/15
6. Memories
This section describes the different memories in the Atmel® ATtiny24/44/84. The AVR® architecture has two main memory
spaces, the data memory space and the program memory space. In addition, the Atmel ATtiny24/44/84 features an
EEPROM memory for data storage. All three memory spaces are linear and regular.
0x03FF/0x07FF/0xFFF
16 ATtiny24/44/84 [DATASHEET]
7701G–AVR–02/15
Figure 6-2. Data Memory Map
Data Memory
32 Registers 0x0000 - 0x001F
64 I/O Registers 0x0020 - 0x005F
0x0060
Internal SRAM
(128/256/512 x 8)
0x0DF/0x015F/0x025F
clkCPU
Data
Write
WR
Data
Read
RD
ATtiny24/44/84 [DATASHEET] 17
7701G–AVR–02/15
6.3.2 Atomic Byte Programming
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 operations are completed. While
the device is busy with programming, it is not possible to do any other EEPROM operations.
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 (programming 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.
6.3.5 Write
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 frequency is within the
requirements described in Section 7.10.1 “Oscillator Calibration Register – OSCCAL” on page 29.
18 ATtiny24/44/84 [DATASHEET]
7701G–AVR–02/15
The following code examples show one assembly function 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.
Note: The code examples are only valid for Atmel® ATtiny24 and Atmel ATtiny44, using 8-bit addressing mode.
ATtiny24/44/84 [DATASHEET] 19
7701G–AVR–02/15
The next code examples show assembly and C functions for reading the EEPROM. The examples 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 Atmel® ATtiny24 and Atmel ATtiny44, using 8-bit addressing mode.
20 ATtiny24/44/84 [DATASHEET]
7701G–AVR–02/15
Some of the status flags are cleared by writing a logical one to them. Note that, unlike most other AVR®s, 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 registers 0x00 to 0x1F only.
The I/O and peripherals control registers are explained in later sections.
ATtiny24/44/84 [DATASHEET] 21
7701G–AVR–02/15
6.5.4 EECR – EEPROM Control Register
Bit 7 6 5 4 3 2 1 0
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
22 ATtiny24/44/84 [DATASHEET]
7701G–AVR–02/15
6.5.5 GPIOR2 – General Purpose I/O Register 2
Bit 7 6 5 4 3 2 1 0
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
ATtiny24/44/84 [DATASHEET] 23
7701G–AVR–02/15
7. System Clock and Clock Options
clkI/O clkCPU
AVR Clock
Control Unit
clkADC clkFLASH
System Clock
Prescaler
Clock Watchdog
Multiplexer Oscillator
24 ATtiny24/44/84 [DATASHEET]
7701G–AVR–02/15
7.1.4 ADC Clock – clkADC
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 their digital circuitry. This gives more accurate ADC conversion results.
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 commencing 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.
ATtiny24/44/84 [DATASHEET] 25
7701G–AVR–02/15
Figure 7-2. Crystal Oscillator Connections
C2
XTAL2
C1
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.
The CKSEL0 fuse together with the SUT1..0 fuses select the start-up times as shown in Table 7-4.
Table 7-4. Start-up Times for the Crystal Oscillator Clock Selection
26 ATtiny24/44/84 [DATASHEET]
7701G–AVR–02/15
7.5 Low-frequency Crystal Oscillator
To use a 32.768kHz 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 on page 26. See the 32kHz crystal
oscillator application note for details on oscillator 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
When this oscillator is selected, start-up times are determined by the SUT fuses as shown in Table 7-7.
Table 7-7. Start-up Times for the Internal Calibrated RC Oscillator Clock Selection
ATtiny24/44/84 [DATASHEET] 27
7701G–AVR–02/15
7.7 External Clock
To drive the device from an external clock source, CLKI should be driven as shown in Figure 7-3. To run the device on an
external clock, the CKSEL fuses must be programmed to “0000”.
External
Clock CLKI
Signal
GND
When this clock source is selected, start-up times are determined by the SUT fuses as shown in Table 7-8.
When applying an external clock, sudden changes in the applied clock frequency must be avoided 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 Section 7.9 “System Clock Prescaler” on page 29 for details.
28 ATtiny24/44/84 [DATASHEET]
7701G–AVR–02/15
7.9 System Clock Prescaler
The Atmel® ATtiny24/44/84 system clock can be divided by setting the clock prescaler 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. clkI/O, clkADC, clkCPU, and
clkFLASH are divided by a factor as shown in Table 7-10 on page 30.
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-2 on page 157. The application software can write this register to
change the oscillator frequency. The oscillator can be calibrated to frequencies as specified in Table 22-2 on page 157.
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.8MHz. Otherwise, the EEPROM or flash write may fail.
The CAL7 bit determines the range of operation for the oscillator. Setting this bit to logical zero gives the lowest frequency
range, setting this bit to logical one gives the highest frequency range. The two frequency ranges are overlapping, in other
words a setting of OSCCAL = 0x7F gives a higher frequency than OSCCAL = 0x80.
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.
ATtiny24/44/84 [DATASHEET] 29
7701G–AVR–02/15
• Bits 6..4 – Res: Reserved Bits
These bits are reserved bits in the Atmel® ATtiny24/44/84 and will always read as zero.
30 ATtiny24/44/84 [DATASHEET]
7701G–AVR–02/15
8. Power Management and Sleep Modes
Sleep modes enable the application to shut down unused modules in the MCU, thereby saving power. The Atmel® AVR®
provides various sleep modes allowing the user to tailor the power consumption to the application’s requirements.
Table 8-1. Active Clock Domains and Wake-up Sources in the Different Sleep Modes
Source Enabled
Pin Change
Main Clock
Watchdog
EEPROM
Other I/O
INT0 and
Interrupt
clkFLASH
Ready
clkADC
clkCPU
SPM/
ADC
clkIO
Sleep Mode
Idle X X X X X X X X
ADC noise
X X X(1) X X X
Reduction
Power-down X(1) X
(2)
Stand-by X X X(1)
Notes: 1. For INT0, only level interrupt.
2. Only recommended with external crystal or resonator selected as clock source
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 34 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.
ATtiny24/44/84 [DATASHEET] 31
7701G–AVR–02/15
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 watchdog 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.
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 Section 11. “External Interrupts” on page 46 for details
32 ATtiny24/44/84 [DATASHEET]
7701G–AVR–02/15
8.8.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
disabled in all sleep modes. Otherwise, the internal voltage reference will be enabled, independent of sleep mode. See
Section 17. “Analog Comparator” on page 115 for details on how to configure the analog comparator.
ATtiny24/44/84 [DATASHEET] 33
7701G–AVR–02/15
8.9 Register Description
34 ATtiny24/44/84 [DATASHEET]
7701G–AVR–02/15
8.9.2 PRR – Power Reduction Register
Bit 7 6 5 4 3 2 1 0
– – – – PRTIM1 PRTIM0 PRUSI PRADC PRR
Read/Write R R R R R/W R/W R/W R/W
Initial Value 0 0 0 0 0 0 0 0
ATtiny24/44/84 [DATASHEET] 35
7701G–AVR–02/15
9. System Control and Reset
MCU Status
Register (MCUSR)
PORF
BORF
EXTRF
WDRF
Power-on Reset
VCC
Circuit
Brown-out
BODLEVEL [1..0] Reset Circuit
INTERNAL RESET
Pull-up Resistor
Spike Q
RESET Reset Circuit S
Filter
R
COUNTER RESET
Watchdog
Timer
Watchdog
Oscillator
CK Delay Counters
Clock
TIMEOUT
Generator
CKSEL[1:0]
SUT[1:0]
36 ATtiny24/44/84 [DATASHEET]
7701G–AVR–02/15
9.3 Power-on Reset
A power-on reset (POR) pulse is generated by an on-chip detection circuit. The detection level is defined in Section 22.4
“System and Reset Characterizations” on page 158. The POR is activated whenever VCC is below the detection level. The
POR circuit can be used to trigger the start-up reset, as well as to detect a failure in supply voltage.
A 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 VCC rise. The RESET signal is activated again,
without any delay, when VCC decreases below the detection level.
VPORMIN
RESET
VRST
tTOUT
Time-out
Internal
Reset
VCC V POT
RESET V RST
tTOUT
Time-out
Internal
Reset
ATtiny24/44/84 [DATASHEET] 37
7701G–AVR–02/15
9.4 External Reset
An external reset is generated by a low level on the RESET pin if enabled. Reset pulses longer than the minimum pulse
width (see Section 22.4 “System and Reset Characterizations” on page 158) 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
– VRST – on its positive edge, the delay counter starts the MCU after the time-out period – tTOUT – has expired.
RESET V RST
tTOUT
Time-out
Internal
Reset
RESET
tTOUT
Time-out
Internal
Reset
38 ATtiny24/44/84 [DATASHEET]
7701G–AVR–02/15
9.6 Watchdog Reset
When the watchdog times out, it will generate a short reset pulse of one CK cycle duration. On the falling edge of this pulse,
the delay timer starts counting the time-out period tTOUT. See Section 9.8 “Watchdog Timer” on page 39 for details on
operation of the watchdog timer.
RESET
1 CK Cycle
WDT
Time-out
tTOUT
RESET
Time-out
Internal
Reset
ATtiny24/44/84 [DATASHEET] 39
7701G–AVR–02/15
To prevent unintentional disabling of the watchdog or unintentional change of time-out period, two different safety levels are
selected by the WDTON fuse, as shown in Table 9-2 See Section 9.9 “Timed Sequences for Changing the Configuration of
the Watchdog Timer” on page 40 for details.
WDTON Safety Level WDT Initial State How to Disable the WDT How to Change Time-out
Unprogrammed 1 Disabled Timed sequence No limitations
Programmed 2 Enabled Always enabled Timed sequence
128kHz
Oscillator Watchdog
Prescaler
OSC/2K
OSC/4K
OSC/8K
OSC/16K
OSC/32K
OSC/64K
OSC/128K
OSC/256K
OSC/512K
OSC/1024K
Watchdog
Reset
WDP0
WDP1
WDP2
WDP3
WDE
MCU Reset
9.9 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.
40 ATtiny24/44/84 [DATASHEET]
7701G–AVR–02/15
9.10 Register Description
ATtiny24/44/84 [DATASHEET] 41
7701G–AVR–02/15
Table 9-3. Watchdog Timer Configuration
42 ATtiny24/44/84 [DATASHEET]
7701G–AVR–02/15
Table 9-4. Watchdog Timer Prescale Select
Typical Time-out at
WDP3 WDP2 WDP1 WDP0 Number of WDT Oscillator Cycles VCC = 5.0V
0 0 0 0 2Kcycles 16ms
0 0 0 1 4Kcycles 32ms
0 0 1 0 8Kcycles 64ms
0 0 1 1 16Kcycles 0.125s
0 1 0 0 32Kcycles 0.25s
0 1 0 1 64Kcycles 0.5s
0 1 1 0 128Kcycles 1.0s
0 1 1 1 256Kcycles 2.0s
1 0 0 0 512Kcycles 4.0s
1 0 0 1 1024Kcycles 8.0s
1 0 1 0
1 0 1 1
1 1 0 0
Reserved
1 1 0 1
1 1 1 0
1 1 1 1
The following code example shows one assembly function 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(1)
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, WDTCR
ori r16, (1<<WDCE)|(1<<WDE)
out WDTCR, r16
; Turn off WDT
ldi r16, (0<<WDE)
out WDTCR, r16
ret
C Code Example(1)
void WDT_off(void)
{
_WDR();
/* Clear WDRF in MCUSR */
MCUSR = 0x00
/* Write logical one to WDCE and WDE */
WDTCR |= (1<<WDCE) | (1<<WDE);
/* Turn off WDT */
WDTCR = 0x00;
}
Note: 1. See Section 4. “About Code Examples” on page 8.
ATtiny24/44/84 [DATASHEET] 43
7701G–AVR–02/15
10. Interrupts
This section describes the specifics of the interrupt handling as performed in Atmel® ATtiny24/44/84. For a general
explanation of the AVR® interrupt handling, see Section 5.8 “Reset and Interrupt Handling” on page 14.
44 ATtiny24/44/84 [DATASHEET]
7701G–AVR–02/15
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 Atmel®
ATtiny24/44/84 is:
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
... ... ... ...
ATtiny24/44/84 [DATASHEET] 45
7701G–AVR–02/15
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 asynchronously. This implies that these
interrupts also can be used for waking the part 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 Section 7.1 “Clock Systems and their Distribution” on page 24. Low level interrupt on
INT0 is detected asynchronously. This implies that this interrupt also can be used for waking the part 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 interrupt will be generated. The start-up time is defined by the SUT and CKSEL fuses as
described in Section 7. “System Clock and Clock Options” on page 24.
clk
PCINT(0)
pin_lat
pin_sync
pcint_in_(0)
pcint_syn
pcint_setflag
PCIF
46 ATtiny24/44/84 [DATASHEET]
7701G–AVR–02/15
11.2 Register Description
ATtiny24/44/84 [DATASHEET] 47
7701G–AVR–02/15
11.2.3 GIFR – General Interrupt Flag Register
Bit 7 6 5 4 3 2 1 0
0x3A (0x5A – INTF0 PCIF1 PCIF0 – – – – GIFR
Read/Write R R/W R/W R/W R R R R
Initial Value 0 0 0 0 0 0 0 0
48 ATtiny24/44/84 [DATASHEET]
7701G–AVR–02/15
12. I/O Ports
12.1 Overview
All Atmel® AVR® ports have true read-modify-write functionality when used as general digital I/O ports. This means that the
SBI and CBI instructions can be used to change direction of one port pin without unintentionally changing the direction of any
other pin. The same applies when changing 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 individually selectable pull-up resistors with a
supply-voltage invariant resistance. All I/O pins have protection diodes to both VCC and ground as indicated in Figure 12-1.
See Section 22. “Electrical Characteristics” on page 155 for a complete list of parameters.
Rpu
Pxn
Logic
See Figure
Cpin ”General Digital I/O”
for Details
All registers and bit references in this section are written in general form. A lower case “x” represents 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 registers and bit locations are listed in “EXT_CLOCK = external clock is selected as system clock.” on page 61.
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 logical one to a bit in the PINx register, will result in a toggle in the
corresponding bit in the data register. In addition, the pull-up disable (PUD) bit in the 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 Section 12.2 “Ports as General Digital I/O” on page 50. Most port pins
are multiplexed with alternate functions for the peripheral features on the device. How each alternate function interferes with
the port pin is described in Section 12.3 “Alternate Port Functions” on page 54. Refer to the individual module sections for a
full description of the alternate functions.
Note that enabling the alternate function of some of the port pins does not affect the use of the other pins in the port as
general digital I/O.
ATtiny24/44/84 [DATASHEET] 49
7701G–AVR–02/15
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 functional description of one
I/O-port pin, here generically called Pxn.
PUD
Q D
DDxn
Q
CLR
WDx
RESET
RDx
DATA BUS
Pxn Q D
0
PORTxn
Q
CLR
RESET WPx
SLEEP WRx
RRx
Synchronizer
RPx
D Q D Q
PINxn
L Q Q
CLKI/O
Note: 1. WRx, WPx, WDx, RRx, RPx, and RDx are common to all pins within the same port. clkI/O, SLEEP, and PUD
are common to all ports.
50 ATtiny24/44/84 [DATASHEET]
7701G–AVR–02/15
12.2.2 Toggling the Pin
Writing a logical 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.
PUD
DDxn PORTxn (in MCUCR) I/O Pull-up Comment
0 0 X Input No Tri-state (Hi-Z)
0 1 0 Input Yes Pxn will source current if ext. pulled low.
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)
System CLK
SYNC Latch
PINxn
tpd, max
tpd, 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 indicated 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.
ATtiny24/44/84 [DATASHEET] 51
7701G–AVR–02/15
When reading back a software assigned pin value, a nop instruction must be inserted as indicated in Figure 12-4. 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.
System CLK
r16 0xFF
SYNC Latch
PINxn
tpd
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 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(1)
...
; 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
...
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.
52 ATtiny24/44/84 [DATASHEET]
7701G–AVR–02/15
12.2.5 Digital Input Enable and Sleep Modes
As shown in Figure 12-2 on page 50, 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 VCC/2.
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 Section 12.3
“Alternate Port Functions” on page 54.
If a logic high level (logical 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.
ATtiny24/44/84 [DATASHEET] 53
7701G–AVR–02/15
12.3 Alternate Port Functions
Most port pins have alternate functions in addition to being general digital I/Os. Figure 12-5 shows how the port pin control
signals from the simplified Figure 12-2 on page 50 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 Atmel® AVR®
microcontroller family.
1 PUOVxn
PUD
0
DDOExn
1 DDOVxn
0 Q D
DDxn
Q
CLR
WDx
RESET
RDx
PVOExn
1 PVOVxn
Pxn 1
DATA BUS
0 Q D
0
PORTxn
DIEOExn Q PTOExn
CLR
1 DIEOVxn RESET
WRx WPx
0 SLEEP RRx
Synchronizer
RPx
D SET Q D Q
PINxn
L Q Q
CLR CLR
CLKI/O
DIxn
AIOxn
Note: 1. WRx, WPx, WDx, RRx, RPx, and RDx are common to all pins within the same port. clkI/O, SLEEP, and PUD
are common to all ports. All other signals are unique for each pin.
54 ATtiny24/44/84 [DATASHEET]
7701G–AVR–02/15
Table 12-2 summarizes the function of the overriding signals. The pin and port indexes from Figure 12-5 on page 54 are not
shown in the succeeding tables. The overriding signals are generated internally in the modules having the alternate function.
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.
ATtiny24/44/84 [DATASHEET] 55
7701G–AVR–02/15
12.3.1 Alternate Functions of Port A
The port A pins with alternate function are shown in Table 12-7 on page 59.
56 ATtiny24/44/84 [DATASHEET]
7701G–AVR–02/15
• 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.
ATtiny24/44/84 [DATASHEET] 57
7701G–AVR–02/15
• 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 to Table 12-6 on page 59 relate the alternate functions of Port A to the overriding signals shown in Figure 12-5 on
page 54.
58 ATtiny24/44/84 [DATASHEET]
7701G–AVR–02/15
Table 12-6. Overriding Signals for Alternate Functions in PA1..PA0
ATtiny24/44/84 [DATASHEET] 59
7701G–AVR–02/15
• 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/Counter 0 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.
PCINT10: Pin change interrupt source 10. The PB2 pin can serve as an external interrupt source for pin change interrupt 1.
60 ATtiny24/44/84 [DATASHEET]
7701G–AVR–02/15
Table 12-9. Overriding Signals for Alternate Functions in PB1..PB0
Bit 7 6 5 4 3 2 1 0
BODS PUD SE SM1 SM0 BODSE ISC01 ISC00 MCUCR
Read/Write R/W R/W R/W R/W R/W R/W R R
Initial Value 0 0 0 0 0 0 0 0
ATtiny24/44/84 [DATASHEET] 61
7701G–AVR–02/15
12.4.4 PINA – Port A Input Pins Address
Bit 7 6 5 4 3 2 1 0
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
62 ATtiny24/44/84 [DATASHEET]
7701G–AVR–02/15
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/Counter 0 is a general purpose 8-bit Timer/Counter module, with two independent out- put compare units, and with
PWM support. It allows accurate program execution timing (event management) and wave generation.
A simplified block diagram of the 8-bit Timer/Counter is shown in Figure 13-1. For the actual placement of I/O pins, refer to
Figure 1-1 on page 3. 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 Section 13.9 “Register Description” on page 73.
(from Prescaler)
TOP BOTTOM
Timer/Counter
TCNTn
= = 0
Waveform
= Generation
OCnA
OCRnA
Fixed
TOP
DATA BUS
Waveform
= Generation
OCnB
OCRnB
TCCRnA TCCRnB
ATtiny24/44/84 [DATASHEET] 63
7701G–AVR–02/15
13.2.1 Registers
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/Counter 0 interrupt flag register (TIFR0). All
interrupts are individually masked with the timer interrupt mask register (TIMSK0). TIFR0 and TIMSK0 are not shown in the
figure.
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 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 (clkT0).
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 generator to generate a PWM or variable frequency output on the
output compare pins (OC0A and OC0B). See Section 13.5 “Output Compare Unit” on page 65 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.
13.2.2 Definitions
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 compare 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/Counter 0 counter value and so on.
The definitions in Table are also used extensively throughout the document.
Parameter Definition
BOTTOM The counter reaches the BOTTOM when it becomes 0x00.
MAX The counter reaches its MAXimum when it becomes 0xFF (decimal 255).
The counter reaches the TOP when it becomes equal to the highest value in the count sequence. The
TOP TOP value can be assigned to be the fixed value 0xFF (MAX) or the value stored in the OCR0A register.
The assignment is dependent on the mode of operation.
Clock Select
count Edge
Tn
clear clkTn Detector
TCNTn Control Logic
direction
(from Prescaler)
bottom top
64 ATtiny24/44/84 [DATASHEET]
7701G–AVR–02/15
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).
clkTn Timer/Counter clock, referred to as clkT0 in the following.
top Signalize that TCNT0 has reached maximum value.
bottom Signalize that TCNT0 has reached minimum value (zero).
Depending on the mode of operation used, the counter is cleared, incremented, or decremented at each timer clock (clkT0).
clkT0 can be generated from an external or internal clock source, 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 clkT0 is present or not. A CPU write overrides (has priority over) all counter clear or count operations.
The counting sequence is determined by the setting of the WGM01 and WGM00 bits located in the Timer/Counter control
register A (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 Section 13.7 “Modes of
Operation” on page 67.
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.
OCRnx TCNTn
= (8-bit Comparator)
top
FOCn
WGMn1:0 COMnx1:0
ATtiny24/44/84 [DATASHEET] 65
7701G–AVR–02/15
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 double 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 the case. When the double buffering is enabled, the CPU has
access to the OCR0x buffer register, and if double buffering is disabled the CPU will access the OCR0x directly.
66 ATtiny24/44/84 [DATASHEET]
7701G–AVR–02/15
Figure 13-4. Compare Match Output Unit, Schematic
COMnx1
COMnx0 Waveform
Generator D Q
FOCn
1
OCnx
OCnx Pin
0
D Q
D Q
DDR
clkI/O
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 output) 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 visible on the pin. The port override function is independent of the waveform generation mode.
The design of the output compare pin logic allows initialization of the OC0x state before the output is enabled. Note that
some COM0x1:0 bit settings are reserved for certain modes of operation, see
Section 13.9 “Register Description” on page 73.
ATtiny24/44/84 [DATASHEET] 67
7701G–AVR–02/15
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 bottom (0x00). In normal operation the Timer/Counter overflow flag (TOV0) will be
set on the same timer clock cycle on which 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 output compare to generate
waveforms in normal mode is not recommended, since this will occupy too much CPU time.
TCNTn
OCn (COMnx1:0 = 1)
(Toggle)
1 2 3 4
Period
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 running with no 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
f0 = fclk_I/O/2 when OCR0A is set to zero (0x00). The waveform frequency is defined by the following equation:
f clk_I/O
f OCnx = ----------------------------------------------------
2 N 1 + OCRnx
The variable N represents the prescale factor (1, 8, 64, 256, or 1024).
As for the normal mode of operation, the TOV0 flag is set on the same timer clock cycle on which the counter counts from
max to 0x00.
68 ATtiny24/44/84 [DATASHEET]
7701G–AVR–02/15
13.7.3 Fast PWM Mode
The fast pulse width modulation, or fast PWM, mode (WGM02:0 = 3 or 7) provides a high-frequency PWM waveform
generation option. The fast PWM mode differs from the other PWM option by its single-slope operation. The counter counts
from bottom to top then restarts from bottom. Top is defined as 0xFF when WGM2:0 = 3, and as 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 output 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 uses dual-slope operation.
This high frequency makes the fast PWM mode well suited for power regulation, rectification, and DAC applications. High
frequency allows the use of physically smaller external components (coils, capacitors, etc.), and hence 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 PWM mode is shown in Figure 13-6. 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 represent Compare Matches between OCR0x
and TCNT0.
OCRnx Interrupt
Flag Set
TCNTn
OCnx (COMnx1:0 = 2)
OCnx (COMnx1:0 = 3)
1 2 3 4 5 6 7
Period
The Timer/Counter overflow flag (TOV0) is set each time the counter reaches TOP. If the interrupt 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 bits to three.
Setting the COM0A1:0 bits to one allows 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 73). 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 when
the counter is cleared (changes from top to bottom).
The PWM frequency for the output can be calculated by the following equation:
f clk_I/O
f OCnxPWM = -------------------
N 256
The variable N represents the prescale factor (1, 8, 64, 256, or 1024).
ATtiny24/44/84 [DATASHEET] 69
7701G–AVR–02/15
The extreme values for the OCR0A register represent special cases when generating a PWM waveform output in the fast
PWM mode. If the OCR0A is set equal to bottom, the out- put 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 setting OC0x to toggle its logical
level on each compare match (COM0x1:0 = 1). The waveform generated will have a maximum frequency of f0 = fclk_I/O/2
when OCR0A is set to zero. This feature 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.
OCnx Interrupt
Flag Set
OCRnx Update
TOVn Interrupt
Flag Set
TCNTn
OCnx (COMnx1:0 = 2)
OCnx (COMnx1:0 = 3)
1 2 3
Period
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.
70 ATtiny24/44/84 [DATASHEET]
7701G–AVR–02/15
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 bits to three. Setting the COM0A0 bits to 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 73). 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 compare 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 clk_I/O
f OCnxPCPWM = -------------------
N 510
The variable N 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 70 OCn has a transition from high to low even though there is no compare
match. The point of this transition is to guarantee symmetry around bottom. There are two cases that give a transition
without a compare match.
● OCR0A changes its value from MAX, as in Figure 13-7 on page 70. 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.
clkI/O
clkTn
(clkI/O/1)
TOVn
ATtiny24/44/84 [DATASHEET] 71
7701G–AVR–02/15
Figure 13-9 shows the same timing data, but with the prescaler enabled.
clkI/O
clkTn
(clkI/O/8)
TOVn
Figure 13-10 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 (fclk_I/O/8)
clkI/O
clkTn
(clkI/O/8)
OCFnx
Figure 13-11 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 Prescaler (fclk_I/O/8)
clkI/O
clkTn
(clkI/O/8)
TCNTn
(CTC) TOP - 1 TOP BOTTOM BOTTOM + 1
OCRnx TOP
OCFnx
72 ATtiny24/44/84 [DATASHEET]
7701G–AVR–02/15
13.9 Register Description
Table 13-3 shows the COM0A1:0 bit functionality when the WGM01:0 bits are set to fast PWM mode.
Table 13-4 shows the COM0A1:0 bit functionality when the WGM02:0 bits are set to phase correct PWM mode.
ATtiny24/44/84 [DATASHEET] 73
7701G–AVR–02/15
• 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-2 on
page 73 shows the COM0A1:0 bit functionality when the WGM02:0 bits are set to a normal or CTC mode (non-PWM).
Table 13-3 on page 73 shows the COM0B1:0 bit functionality when the WGM02:0 bits are set to fast PWM mode.
Table 13-4 on page 73 shows the COM0B1:0 bit functionality when the WGM02:0 bits are set to phase correct PWM mode.
74 ATtiny24/44/84 [DATASHEET]
7701G–AVR–02/15
Table 13-8. Waveform Generation Mode Bit Description
ATtiny24/44/84 [DATASHEET] 75
7701G–AVR–02/15
Table 13-9. Clock Select Bit Description
The Timer/Counter register gives direct access, for both 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.
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.
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.
76 ATtiny24/44/84 [DATASHEET]
7701G–AVR–02/15
13.9.6 TIMSK0 – Timer/Counter 0 Interrupt Mask Register
Bit 7 6 5 4 3 2 1 0
0x39 (0x59) – – – – – OCIE0B OCIE0A TOIE0 TIMSK0
Read/Write R R R R R R/W R/W R/W
Initial Value 0 0 0 0 0 0 0 0
ATtiny24/44/84 [DATASHEET] 77
7701G–AVR–02/15
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.
A simplified block diagram of the 16-bit Timer/Counter is shown in Figure 14-1 on page 79. For the actual placement of I/O
pins, refer to Section 1-1 “Pinout Atmel ATtiny24/44/84” on page 3. 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 Section 14.11 “Register
Description” on page 97.
78 ATtiny24/44/84 [DATASHEET]
7701G–AVR–02/15
Figure 14-1. 16-bit Timer/Counter Block Diagram(1)
TOVn (Int. Req.)
Count
Clear Clock Select
Control Logic
Direction Edge
clkTn
Tn
Detector
(from Prescaler)
TOP BOTTOM
Timer/Counter
TCNTn
= = 0
Waveform
= Generation
OCnA
OCRnA
Fixed
DATA BUS
Edge Noise
ICRn
Detector Canceler
ICPn
TCCRnA TCCRnB
Note: 1. See Figure 1-1 on page 3 for Timer/Counter1 pin placement and description.
14.2.1 Registers
The Timer/Counter (TCNT1), output compare registers (OCR1A/B), and input capture register (ICR1) are all 16-bit registers.
Special procedures must be followed when accessing the 16-bit registers. These procedures are described in the Section
14.3 “Accessing 16-bit Registers” on page 80. The Timer/Counter control registers (TCCR1A/B) are 8-bit registers, and have
no CPU access restrictions. Interrupt request (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 (clkT1).
The double buffered output compare registers (OCR1A/B) are compared with the Timer/Counter value at all times. The
result of the compare can be used by the waveform generator to generate a PWM or variable frequency output on the output
compare pin (OC1A/B). See Section 14.7 “Output Compare Units” on page 86. The compare match event will also set the
compare match flag (OCF1A/B) which can be used to generate an output compare interrupt request.
ATtiny24/44/84 [DATASHEET] 79
7701G–AVR–02/15
The input capture register can capture the Timer/Counter value at a given external (edge-triggered) event on either the input
capture pin (ICP1) or on the analog comparator pins (see Section 17. “Analog Comparator” on page 115). The input capture
unit includes a digital filtering unit (noise canceller) 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 cannot
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 at 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.
14.2.2 Definitions
The following definitions are used extensively throughout the section:
Parameter Definition
BOTTOM The counter reaches the BOTTOM when it becomes 0x0000.
MAX The counter 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 sequence. The top
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 on 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 Atmel® 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.
80 ATtiny24/44/84 [DATASHEET]
7701G–AVR–02/15
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(1)
...
; 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)
unsigned int i;
...
/* Set TCNT1 to 0x01FF */
TCNT1 = 0x1FF;
/* Read TCNT1 into i */
i = TCNT1;
...
Note: 1. See Section 4. “About Code Examples” on page 8.
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 of the other 16-bit timer registers, then the result of the access outside the interrupt will be corrupted. Therefore, when
both the main code and the interrupt code update the temporary register, the main code must disable the interrupts during
the 16-bit access.
ATtiny24/44/84 [DATASHEET] 81
7701G–AVR–02/15
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(1)
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(1)
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;
}
Note: 1. See Section 4. “About Code Examples” on page 8.
The assembly code example returns the TCNT1 value in the r17:r16 register pair.
82 ATtiny24/44/84 [DATASHEET]
7701G–AVR–02/15
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(1)
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(1)
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;
}
Note: 1. See Section 4. “About Code Examples” on page 8.
The assembly code example requires that the r17:r16 register pair contains the value to be written to TCNT1.
ATtiny24/44/84 [DATASHEET] 83
7701G–AVR–02/15
14.5 Counter Unit
The main part of the 16-bit Timer/Counter is the programmable 16-bit bi-directional counter unit. Figure 14-2 shows a block
diagram of the counter and its surroundings.
TOVn
(Int. Req.)
TEMP (8-bit)
Clock Select
Count Edge
TCNTnH (8-bit) TCNTnL (8-bit) Tn
Clear clkTn Detector
Control Logic
Direction
TCNTn (16-bit Counter) (from Prescaler)
TOP BOTTOM
84 ATtiny24/44/84 [DATASHEET]
7701G–AVR–02/15
Figure 14-3. Input Capture Unit Block Diagram
DATA BUS (8-bit)
TEMP (8-bit)
- Analog
Comparator
Noise Edge
ICFn (Int. Req.)
Canceler Detector
ICPn
When a change of the logic level (an event) occurs on the input capture pin (ICP1), or alternatively on the analog comparator
output (ACO), and this change conforms 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.
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 generation 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 Section 14.3 “Accessing 16-bit Registers” on page 80.
ATtiny24/44/84 [DATASHEET] 85
7701G–AVR–02/15
14.6.2 Noise Canceller
The noise canceller improves noise immunity by using a simple digital filtering scheme. The noise canceller input is
monitored over four samples, and all four must be equal to change the output, which in turn is used by the edge detector.
The noise canceller is enabled by setting the input capture noise canceller (ICNC1) bit in Timer/Counter control register B
(TCCR1B). When enabled, the noise canceller introduces an additional four system clock cycles of delay between a change
applied to the input and the update of the ICR1 register. The noise canceller uses the system clock, and is, therefore, not
affected by the prescaler.
86 ATtiny24/44/84 [DATASHEET]
7701G–AVR–02/15
Figure 14-4. Output Compare Unit, Block Diagram
DATA BUS (8-bit)
TEMP (8-bit)
OCRnxH Buf. (8-bit) OCRnxL Buf. (8-bit) TCNTnH (8-bit) TCNTnL (8-bit)
= (16-bit Comparator)
TOP
Waveform Generator OCnx
BOTTOM
WGMn3:0 COMnx1:0
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 compare register 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 OCR1x register access may seem complex, but this is not the case. When the double buffering is enabled, the CPU has
access to the OCR1x buffer register, and if double buffering is disabled the CPU will access the OCR1x directly. The content
of the OCR1x (buffer or compare) register is changed only by a write operation (the Timer/Counter does not update this
register automatically as it does for the TCNT1 and ICR1 registers). Therefore, OCR1x is not read via the high byte
temporary register (TEMP). However, it is a good practice to read the low byte first, as when accessing other 16-bit registers.
Writing the OCR1x registers must be done via the TEMP register because the compare of all 16 bits is done continuously.
The high byte (OCR1xH) has to be written first. When the high byte I/O location is written by the CPU, the TEMP register will
be updated by the value written. Then when the low byte (OCR1xL) is written to the lower eight bits, the high byte will be
copied into the upper 8-bits of either the OCR1x buffer or the OCR1x compare register in the same system clock cycle.
For more information of how to access the 16-bit registers, refer to Section 14.3 “Accessing 16-bit Registers” on page 80.
ATtiny24/44/84 [DATASHEET] 87
7701G–AVR–02/15
14.7.3 Using the Output Compare Unit
Because writing TCNT1 in any mode of operation will block all compare matches for one timer clock cycle, there are risks
involved in changing TCNT1 when using any of the output compare channels, independent of whether the Timer/Counter is
running or not. If the value written to TCNT1 equals the OCR1x value, the compare match will be missed, resulting in
incorrect waveform generation. Do not write the TCNT1 equal to top in PWM modes with variable top values. The compare
match for the top will be ignored, and the counter will continue to 0xFFFF. Similarly, do not write the TCNT1 value equal to
bottom when the counter is down-counting.
The setup of the OC1x should be performed before setting the data direction register for the port pin to output. The easiest
way of setting the OC1x value is to use the force output compare (1x) strobe bits in normal mode. The OC1x register keeps
its value even when changing between waveform generation modes.
Be aware that the COM1x1:0 bits are not double buffered together with the compare value. Changing the COM1x1:0 bits will
take effect immediately.
COMnx1
COMnx0 Waveform
Generator D Q
FOCn
1
OCnx
OCnx Pin
0
D Q
DATA BUS
PORT
D Q
DDR
clkI/O
The general I/O port function is overridden by the output compare (OC1x) from the waveform generator if either of the
COM1x1:0 bits are set. However, the OC1x pin direction (input or output) is still controlled by the data direction register
(DDR) for the port pin. The data direction register bit for the OC1x pin (DDR_OC1x) must be set as output before the OC1x
value is visible on the pin. The port override function is generally independent of the waveform generation mode, but there
are some exceptions. See Table 14-2 on page 97, Table 14-3 on page 97 and Table 14-4 on page 98 for details.
The design of the output compare pin logic allows initialization of the OC1x state before the output is enabled. Note that
some COM1x1:0 bit settings are reserved for certain modes of operation.
See Section 14.11 “Register Description” on page 97.
The COM1x1:0 bits have no effect on the input capture unit.
88 ATtiny24/44/84 [DATASHEET]
7701G–AVR–02/15
14.8.1 Compare Output Mode and Waveform Generation
The waveform generator uses the COM1x1:0 bits differently in normal, CTC, and PWM modes. For all modes, setting the
COM1x1:0 = 0 tells the waveform generator that no action on the OC1x register is to be performed on the next compare
match. For compare output actions in the non-PWM modes refer to Table 14-2 on page 97. For fast PWM mode refer to
Table 14-3 on page 97, and for phase correct and phase and frequency correct PWM refer to Table 14-4 on page 98.
A change of the COM1x1:0 bit states will have an 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 1x strobe bits.
TCNTn
OCnA (COMnA1:0 = 1)
(Toggle)
1 2 3 4
Period
ATtiny24/44/84 [DATASHEET] 89
7701G–AVR–02/15
An interrupt can be generated each time the counter value reaches the top value by either using the OCF1A or ICF1 flag
according to the register used to define the top value. If the interrupt is enabled, the interrupt handler routine can be used for
updating the top value. However, changing the top to a value close to bottom when the counter is running with no or a low
prescaler value must be done with care because the CTC mode does not have the double buffering feature. If the new value
written to OCR1A or ICR1 is lower than the current value of TCNT1, the counter will miss the compare match. The counter
will then have to count to its maximum value (0xFFFF) and wrap around starting at 0x0000 before the compare match can
occur. In many cases this feature is not desirable. An alternative will then be to use the fast PWM mode, using OCR1A for
defining top (WGM13:0 = 15) because OCR1A then will be double buffered.
For generating a waveform output in CTC mode, the OC1A output can be set to toggle its logical level on each compare
match by setting the compare output mode bits to toggle mode (COM1A1:0 = 1). The OC1A value will not be visible on the
port pin unless the data direction for the pin is set to output (DDR_OC1A = 1). The waveform generated will have a
maximum frequency of 1A = fclk_I/O/2 when OCR1A is set to zero (0x0000). The waveform frequency is defined by the
following equation:
f clk_I/O
f OCnA = -----------------------------------------------------
2 N 1 + OCRnA
The variable N represents the prescaler factor (1, 8, 64, 256, or 1024).
As for the normal mode of operation, the TOV1 flag is set on the same timer clock cycle on which the counter counts from
max to 0x0000.
In fast PWM mode the counter is incremented until the counter value matches either one of the fixed values 0x00FF,
0x01FF, or 0x03FF (WGM13:0 = 5, 6, or 7), the value in ICR1 (WGM13:0 = 14), or the value in OCR1A (WGM13:0 = 15).
The counter is then cleared at the following timer clock cycle. The timing diagram for the fast PWM mode is shown in Figure
14-7 on page 91. The figure shows fast PWM mode when OCR1A or ICR1 is used to define top. The TCNT1 value in the
timing diagram is shown as a histogram for illustrating the single-slope operation. The diagram includes non-inverted and
inverted PWM outputs. The small horizontal lines on the TCNT1 slopes represent compare matches between OCR1x and
TCNT1. The OC1x interrupt flag will be set when a compare match occurs.
90 ATtiny24/44/84 [DATASHEET]
7701G–AVR–02/15
Figure 14-7. Fast PWM Mode, Timing Diagram
TCNTn
OCnx (COMnx1:0 = 2)
OCnx (COMnx1:0 = 3)
1 2 3 4 5 6 7 8
Period
The Timer/Counter overflow flag (TOV1) is set each time the counter reaches top. In addition, the OC1A or ICF1 flag is set
on the same timer clock cycle on which TOV1 is set when either OCR1A or ICR1 is used for defining the top value. If one of
the interrupts are enabled, the interrupt handler routine can be used for updating the top and compare values.
When changing the top value, the program must ensure that the new top value is higher or equal to the value of all of the
compare registers. If the top value is lower than any of the compare registers, a compare match will never occur between
TCNT1 and OCR1x. Note that when using fixed top values, the unused bits are masked to zero when any of the OCR1x
registers are written
The procedure for updating ICR1 differs from that for updating OCR1A when used for defining the top value. The ICR1
register is not double buffered. This means that if ICR1 is changed to a low value when the counter is running with no or a
low prescaler value, there is a risk that the new ICR1 value written is lower than the current value of TCNT1. The result will
then be that the counter will miss the compare match at the top value. The counter will then have to count to the max value
(0xFFFF) and wrap around starting at 0x0000 before the compare match can occur. The OCR1A register, however, is
double buffered. This feature allows the OCR1A I/O location to be written anytime. When the OCR1A I/O location is written,
the value written will be put into the OCR1A buffer register. The OCR1A compare register will then be updated with the value
in the buffer register at the next timer clock cycle when TCNT1 matches top. The update is done on the same timer clock
cycle on which TCNT1 is cleared and the TOV1 flag is set.
Using the ICR1 register for defining top works well when using fixed top values. By using ICR1, the OCR1A register is free to
be used for generating a PWM output on OC1A. How- ever, if the base PWM frequency is actively changed (by changing the
top value), using the OCR1A as top is clearly a better choice due to its double buffer feature.
In fast PWM mode, the compare units allow generation of PWM waveforms on the OC1x pins. Setting the COM1x1:0 bits to
two will produce a non-inverted PWM, and an inverted PWM out- put can be generated by setting the COM1x1:0 to three
(see Table 14-3 on page 97). The actual OC1x value will only be visible on the port pin if the data direction for the port pin is
set as output (DDR_OC1x). The PWM waveform is generated by setting (or clearing) the OC1x register at the compare
match between OCR1x and TCNT1, and clearing (or setting) the OC1x register on the timer clock cycle on which the counter
is cleared (changes from top to bottom). The PWM frequency for the output can be calculated by the following equation:
f clk_I/O
f OCnxPWM = ------------------------------------
N 1 + TOP
The variable N represents the prescaler divider (1, 8, 64, 256, or 1024). The extreme values for the OCR1x register
represent special cases when generating a PWM waveform output in the fast PWM mode. If the OCR1x is set equal to
bottom (0x0000), the output will be a narrow spike for each top+1 timer clock cycle. Setting OCR1x equal to top will result in
a constant high or low output (depending on the polarity of the output set by the COM1x1:0 bits.) A frequency waveform
output (with 50% duty cycle) in fast PWM mode can be achieved by setting OC1A to toggle its logical level on each compare
match (COM1A1:0 = 1). The waveform generated will have a maximum frequency of 1A = fclk_I/O/2 when OCR1A is set to zero
(0x0000). This feature is similar to the OC1A toggle in CTC mode, except the double buffer feature of the output compare
unit is enabled in the fast PWM mode.
ATtiny24/44/84 [DATASHEET] 91
7701G–AVR–02/15
14.9.4 Phase Correct PWM Mode
The phase correct pulse width modulation, or phase correct PWM, mode (WGM13:0 = 1, 2, 3, 10, or 11) provides a
high-resolution phase correct PWM waveform generation option. The phase correct PWM mode is, like the phase and
frequency correct PWM mode, based on a dual-slope operation. The counter counts repeatedly from bottom (0x0000) to top
and then from top to bottom.
In non-inverting compare output mode, the output compare (OC1x) is cleared on the compare match between TCNT1 and
OCR1x while up-counting, 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 symmetric feature of the dual-slope PWM modes, these modes are preferred for motor control
applications.
The PWM resolution for the phase correct PWM mode can be fixed to 8, 9, or 10 bits, or defined by either ICR1 or OCR1A.
The minimum resolution allowed is 2 bits (ICR1 or OCR1A set to 0x0003), and the maximum resolution is 16 bits (ICR1 or
OCR1A set to max). The PWM resolution in bits can be calculated by using the following equation:
log TOP + 1
R PCPWM = ---------------------------------
log 2
In phase correct PWM mode, the counter is incremented until the counter value matches either one of the fixed values
0x00FF, 0x01FF, or 0x03FF (WGM13:0 = 1, 2, or 3), the value in ICR1 (WGM13:0 = 10), or the value in OCR1A
(WGM13:0 = 11). The counter has then reached the top, and changes the count direction. The TCNT1 value will be equal to
top for one timer clock cycle. The timing diagram for the phase correct PWM mode is shown on Figure 14-8. The figure
shows phase correct PWM mode when OCR1A or ICR1 is used to define top. The TCNT1 value in the timing diagram is
shown as a histogram for illustrating the dual-slope operation. The diagram includes non-inverted and inverted PWM
outputs. The small horizontal lines on the TCNT1 slopes represent compare matches between OCR1x and TCNT1. The
OC1x interrupt flag will be set when a compare match occurs.
TCNTn
OCnx (COMnx1:0 = 2)
OCnx (COMnx1:0 = 3)
1 2 3 4
Period
The Timer/Counter overflow flag (TOV1) is set each time the counter reaches bottom. When either OCR1A or ICR1 is used
for defining the TOP value, the OC1A or ICF1 flag is set accordingly on the same timer clock cycle on which the OCR1x
registers are updated with the double buffer value (at top). The interrupt flags can be used to generate an interrupt each time
the counter reaches the top or bottom value.
92 ATtiny24/44/84 [DATASHEET]
7701G–AVR–02/15
When changing the top value, the program must ensure that the new top value is higher or equal to the value of all of the
compare registers. If the top value is lower than any of the compare registers, a compare match will never occur between
TCNT1 and OCR1x. Note that when using fixed top values, the unused bits are masked to zero when any of the OCR1x
registers are written. As the third period shown in Figure 14-8 on page 92 illustrates, changing the top actively while the
Timer/Counter is running in the phase correct mode can result in an asymmetrical output. The reason for this can be found in
the time of update of the OCR1x register. Since the OCR1x update occurs at top, the PWM period starts and ends at top.
This implies that the length of the falling slope is determined by the previous top value, while the length of the rising slope is
determined by the new top value. When these two values differ, the two slopes of the period will differ in length. The
difference in length gives the asymmetrical result on the output.
It is recommended to use the phase and frequency correct mode instead of the phase correct mode when changing the top
value while the Timer/Counter is running. When using a static top value, there are practically no differences between the two
modes of operation.
In phase correct PWM mode, the compare units allow generation of PWM waveforms on the OC1x pins. Setting the
COM1x1:0 bits to two will produce a non-inverted PWM, and an inverted PWM output can be generated by setting the
COM1x1:0 to three (See Table 14-4 on page 98). The actual OC1x value will only be visible on the port pin if the data
direction for the port pin is set as output (DDR_OC1x). The PWM waveform is generated by setting (or clearing) the OC1x
register at the compare match between OCR1x and TCNT1 when the counter increments, and clearing (or setting) the OC1x
register at compare match between OCR1x and TCNT1 when the counter decrements. The PWM frequency for the output
when using phase correct PWM can be calculated by the following equation:
f clk_I/O
f OCnxPCPWM = -------------------------------
2 N TOP
The variable N represents the prescaler divider (1, 8, 64, 256, or 1024).
The extreme values for the OCR1x register represent special cases when generating a PWM waveform output in the phase
correct PWM mode. If the OCR1x is set equal to bottom, the output will be continuously low, and if set equal to top, the
output will be continuously high for non-inverted PWM mode. For inverted PWM the output will have the opposite logic
values.
log TOP + 1
R PFCPWM = ---------------------------------
log 2
In phase and frequency correct PWM mode, the counter is incremented until the counter value matches either the value in
ICR1 (WGM13:0 = 8), or the value in OCR1A (WGM13:0 = 9). The counter has then reached the top, and changes the count
direction. The TCNT1 value will be equal to top for one timer clock cycle. The timing diagram for the phase and frequency
correct PWM mode is shown on Figure 14-9 on page 94. The figure shows phase and frequency correct PWM mode when
OCR1A or ICR1 is used to define top. The TCNT1 value in the timing diagram is shown as a histogram for illustrating the
dual-slope operation. The diagram includes non-inverted and inverted PWM outputs. The small horizontal lines on the
TCNT1 slopes represent compare matches between OCR1x and TCNT1. The OC1x interrupt flag will be set when a
compare match occurs.
ATtiny24/44/84 [DATASHEET] 93
7701G–AVR–02/15
Figure 14-9. Phase and Frequency Correct PWM Mode, Timing Diagram
TCNTn
OCnx (COMnx1:0 = 2)
OCnx (COMnx1:0 = 3)
1 2 3 4
Period
The Timer/Counter overflow flag (TOV1) is set on the same timer clock cycle on which the OCR1x registers are updated with
the double buffer value (at bottom). When either OCR1A or ICR1 is used for defining the top value, the OC1A or ICF1 flag is
set accordingly when TCNT1 has reached top. The interrupt flags can then be used to generate an interrupt each time the
counter reaches the top or bottom value.
When changing the top value, the program must ensure that the new top value is higher or equal to the value of all of the
compare registers. If the top value is lower than any of the compare registers, a compare match will never occur between
TCNT1 and OCR1x.
As Figure 14-9 shows, the output generated is, in contrast to the phase correct mode, symmetrical in all periods. Since the
OCR1x registers are updated at bottom, the length of the rising and the falling slopes will always be equal. This gives
symmetrical output pulses, and is, therefore, frequency correct.
Using the ICR1 register for defining top works well when using fixed top values. By using ICR1, the OCR1A register is free to
be used for generating a PWM output on OC1A. However, if the base PWM frequency is actively changed by changing the
top value, using the OCR1A as top is clearly a better choice due to its double buffer feature.
In phase and frequency correct PWM mode, the compare units allow generation of PWM waveforms on the OC1x pins.
Setting the COM1x1:0 bits to two will produce a non-inverted PWM, and an inverted PWM output can be generated by
setting the COM1x1:0 to three (see Table 14-4 on page 98). The actual OC1x value will only be visible on the port pin if the
data direction for the port pin is set as output (DDR_OC1x). The PWM waveform is generated by setting (or clearing) the
OC1x register at the compare match between OCR1x and TCNT1 when the counter increments, and clearing (or setting) the
OC1x register at compare match between OCR1x and TCNT1 when the counter decrements. The PWM frequency for the
output when using phase and frequency correct PWM can be calculated by the following equation:
f clk_I/O
f OCnxPFCPWM = -------------------------------
2 N TOP
The variable N represents the prescaler divider (1, 8, 64, 256, or 1024).
The extreme values for the OCR1x register represent special cases when generating a PWM waveform output in the phase
and frequency correct PWM mode. If the OCR1x is set equal to bottom the output will be continuously low, and if set equal to
top, the output will be set to high for non-inverted PWM mode. For inverted PWM the output will have the opposite logic
values.
94 ATtiny24/44/84 [DATASHEET]
7701G–AVR–02/15
14.10 Timer/Counter Timing Diagrams
The Timer/Counter is a synchronous design, and the timer clock (clkT1) is, therefore, shown as a clock enable signal in the
following figures. The figures include information on when interrupt flags are set, and when the OCR1x register is updated
with the OCR1x buffer value (only for modes utilizing double buffering). Figure 14-10 shows a timing diagram for the setting
of OCF1x.
clkI/O
clkTn
(clkI/O/1)
OCFnx
Figure 14-11 shows the same timing data, but with the prescaler enabled.
Figure 14-11. Timer/Counter Timing Diagram, Setting of OCF1x, with Prescaler (fclk_I/O/8)
clkI/O
clkTn
(clkI/O/8)
OCFnx
ATtiny24/44/84 [DATASHEET] 95
7701G–AVR–02/15
Figure 14-12 shows the count sequence close to TOP in various modes. When using phase and frequency correct PWM
mode the OCR1x register is updated at BOTTOM. The timing diagrams will be the same, but TOP should be replaced by
BOTTOM, TOP-1 by BOTTOM+1 and so on. The same renaming applies for modes that set the TOV1 flag at BOTTOM.
clkI/O
clkTn
(clkI/O/1)
TCNTn
(CTC and FPWM) TOP - 1 TOP BOTTOM BOTTOM + 1
TCNTn
(PC and PFC PWM) TOP - 1 TOP TOP -1 TOP -2
TOVn (FPWM)
and ICFn
(if used as TOP)
Figure 14-13 shows the same timing data, but with the prescaler enabled.
clkI/O
clkTn
(clkI/O/8)
TCNTn
(CTC and FPWM) TOP - 1 TOP BOTTOM BOTTOM + 1
TCNTn
(PC and PFC PWM) TOP - 1 TOP TOP - 1 TOP - 2
TOVn (FPWM)
and ICFn
(if used as TOP)
96 ATtiny24/44/84 [DATASHEET]
7701G–AVR–02/15
14.11 Register Description
Table 14-3 shows the COM1x1:0 bit functionality when the WGM13:0 bits are set to the fast PWM mode.
ATtiny24/44/84 [DATASHEET] 97
7701G–AVR–02/15
Table 14-4 shows the COM1x1:0 bit functionality when the WGM13:0 bits are set to the phase correct or the phase and
frequency correct, PWM mode.
Table 14-4. Compare Output Mode, Phase Correct and Phase and Frequency Correct PWM(1)
98 ATtiny24/44/84 [DATASHEET]
7701G–AVR–02/15
14.11.2 TCCR1B – Timer/Counter1 Control Register B
Bit 7 6 5 4 3 2 1 0
0x2E (0x4E) ICNC1 ICES1 – WGM13 WGM12 CS12 CS11 CS10 TCCR1B
Read/Write R/W R/W R R/W R/W R/W R/W R/W
Initial Value 0 0 0 0 0 0 0 0
If external pin modes are used for the Timer/Counter1, transitions on the T1 pin will clock the counter even if the pin is
configured as an output. This feature allows software control of the counting.
ATtiny24/44/84 [DATASHEET] 99
7701G–AVR–02/15
14.11.3 TCCR1C – Timer/Counter1 Control Register C
Bit 7 6 5 4 3 2 1 0
0x22 (0x42) FOC1A FOC1B – – – – – – TCCR1C
Read/Write W W R R R R R R
Initial Value 0 0 0 0 0 0 0 0
The two Timer/Counter I/O locations (TCNT1H and TCNT1L, combined TCNT1) give direct access for both read and for
write operations to the Timer/Counter unit's 16-bit counter. To ensure that both the high and low bytes are read and written
simultaneously when the CPU accesses these registers, the access is performed using an 8-bit temporary high byte register
(TEMP). This temporary register is shared by all the other 16-bit registers. See Section 14.3 “Accessing 16-bit Registers” on
page 80.
Modifying the counter (TCNT1) while the counter is running introduces a risk of missing a compare match between TCNT1
and one of the OCR1x registers.
Writing to the TCNT1 register blocks (removes) the compare match on the following timer clock for all compare units.
The output compare registers contain a 16-bit value that is continuously compared with the counter value (TCNT1). A match
can be used to generate an output compare interrupt, or to generate a waveform output on the OC1x pin.
The output compare registers are 16 bits in size. To ensure that both the high and low bytes are written simultaneously when
the CPU writes to these registers, the access is performed using an 8-bit temporary high byte register (TEMP). This
temporary register is shared by all the other 16-bit registers. See Section 14.3 “Accessing 16-bit Registers” on page 80.
The input capture is updated with the counter (TCNT1) value each time an event occurs on the ICP1 pin (or optionally on the
analog comparator output for Timer/Counter1). The input capture can be used for defining the counter TOP value.
The input capture register is 16 bits in size. To ensure that both the high and low bytes are read simultaneously when the
CPU accesses these registers, the access is performed using an 8-bit temporary high byte register (TEMP). This temporary
register is shared by all the other 16-bit registers. Section 14.3 “Accessing 16-bit Registers” on page 80.
Tn D Q D Q D Q Tn_sync
(to Clock
Select Logic)
LE
clkI/O
The synchronization and edge detector logic introduces a delay of 2.5 to 3.5 system clock cycles from when an edge has
been applied to the Tn pin to when the counter is updated.
Enabling and disabling of the clock input must be done when Tn has been stable for at least one system clock cycle,
otherwise there is a risk that a false Timer/Counter clock pulse could be generated.
Each half period of the external clock applied must be longer than one system clock cycle to ensure correct sampling. The
external clock must be guaranteed to have less than half the system clock frequency (fExtClk < fclk_I/O/2) given a 50/50 duty
cycle. Because the edge detector uses sampling, the maximum frequency of an external clock it can detect is half the
sampling frequency (Nyquist sampling theorem). However, due to variation of the system clock frequency and duty cycle
caused by oscillator source (crystal, resonator, and capacitor) tolerances, it is recommended that the maximum frequency of
an external clock source is less than fclk_I/O/2.5.
An external clock source can not be prescaled.
CK/8
CK/64
CK/256
CK/1024
PSR10
T0 Synchronization
CS00
CS01
CS02
Note: The synchronization logic on the input pins (T0) is shown in Figure 15-1 on page 103.
16.1 Features
● Two-wire synchronous data transfer (master or slave)
● Three-wire synchronous data transfer (master or slave)
● Data received interrupt
● Wake up from idle mode
● In two-wire mode: Wake up from all sleep modes, including power-down mode
● Two-wire start condition detector with interrupt capability
16.2 Overview
The universal serial interface (USI) provides the basic hardware resources needed for serial communication. Combined with
a minimum of control software, the USI allows significantly higher transfer rates and uses less code space than solutions
based on software only. Interrupts are included to minimize the processor load.
A simplified block diagram of the USI is shown in Figure 16-1. For the actual placement of I/O pins, refer to Section 1-1
“Pinout Atmel ATtiny24/44/84” on page 3. 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 Section 16.5 “Register Descriptions” on page 111.
DO (Output only)
D Q
LE
DI/SDA (Input/ Open Drain))
Bit7
Bit0
3
2
USIDR
1 TIM0 COMP
0
3 0
2 USCK/SCL (Input/ Open Drain))
4-bit Counter 1
USIOIF
CLOCK
USISIF
USIDC
1
USIPF
0 HOLD
2
USIWM1
USIWM0
USICS1
USICS0
USICLK
USIOIE
USISIE
USITC
USICR
The 8-bit shift register is directly accessible via the data bus and contains the incoming and outgoing data. The register has
no buffering, so the data must be read as quickly as possible to ensure that no data are lost. The most significant bit is
connected to one of two output pins, depending on the wire mode configuration. A transparent latch is inserted between the
serial register output and output pin, which delays the change of data output to the opposite clock edge of the data input
sampling. The serial input is always sampled from the data input (DI) pin independent of the configuration.
The 4-bit counter can be both read and written via the data bus, and can generate an overflow interrupt. Both the serial
register and the counter are clocked simultaneously by the same clock source. This allows the counter to count the number
of bits received or transmitted and generate an interrupt when the transfer is complete. Note that when an external clock
source is selected, the counter counts both clock edges. In this case, the counter counts the number of edges, and not the
number of bits. The clock can be selected from three different sources: the USCK pin, Timer/Counter 0 compare match, or
from software. The two-wire clock control unit can generate an interrupt when a start condition is detected on the two-wire
bus. It can also generate wait states by holding the clock pin low after a start condition is detected, or after the counter
overflows.
DO
USCK
SLAVE
DO
USCK
PORTxn
MASTER
Figure 16-2 shows two USI units operating in three-wire mode, one as master and one as slave. The two shift registers are
interconnected in such way that after eight USCK clocks, the data in each register are interchanged. The same clock also
increments the USI's 4-bit counter. The counter overflow (interrupt) flag, or USIOIF, can therefore be used to determine
when a transfer is completed. The clock is generated by the master device software by toggling the USCK pin via the PORT
register, or by writing a logical one to the USITC bit in USICR.
USCK
USCK
DO MSB 6 5 4 3 2 1 LSB
DI MSB 6 5 4 3 2 1 LSB
A B C D E
The three-wire mode timing is shown in Figure 16-3. At the top of the figure is a USCK cycle reference. One bit is shifted into
the USI shift register (USIDR) for each of these cycles. The USCK timing is shown for both external clock modes. In external
clock mode 0 (USICS0 = 0), DI is sampled at positive edges, and DO is changed (data register is shifted by one) at negative
edges. External clock mode 1 (USICS0 = 1) uses the opposite edges versus mode 0, i.e., it samples data at negative edges
and changes the output at positive edges. The USI clock modes correspond to the SPI data mode 0 and 1.
out USIDR,r16
ldi r16,(1<<USIWM0)|(0<<USICS0)|(1<<USITC)
ldi r17,(1<<USIWM0)|(0<<USICS0)|(1<<USITC)|(1<<USICLK)
in r16,USIDR
ret
SDA
Bit7 Bit6 Bit5 Bit4 Bit3 Bit2 Bit1 Bit0
SCL
Two-wire HOLD
SCL
Clock
Control Unit
SLAVE
SDA
Bit7 Bit6 Bit5 Bit4 Bit3 Bit2 Bit1 Bit0
SCL
PORTxn
MASTER
Figure 16-4 shows two USI units operating in two-wire mode, one as master and one as slave. Only the physical layer is
shown because the system operation is highly dependent of the communication scheme used. The main differences
between the master and slave operation at this level are that the serial clock generation is always done by the master, and
only the slave uses the clock control unit. Clock generation must be implemented in software, but the shift operation is done
automatically by both devices. Note that only clocking on the negative edge to shift data is practical in this mode. The slave
can insert wait states at the start or end of a transfer by forcing the SCL clock low. This means that the master must always
check if the SCL line was actually released after it has generated a positive edge.
Because the clock also increments the counter, a counter overflow can be used to indicate that the transfer has completed.
The master generates clock by the by toggling the USCK pin via the PORT register.
The data direction is not given by the physical layer. A protocol, like the one used by the TWI-bus, must be implemented to
control the data flow.
SDA
SCL 1 to 7 8 9 1 to 8 9 1 to 8 9
A B C D E F
D Q D Q CLOCK
HOLD
SDA CLR CLR
SCL
Write (USISIF)
The USI uses no buffering for the serial register, i.e., when accessing the data register (USIDR) the serial register is
accessed directly. If a serial clock occurs during the same cycle the register is written, the register will contain the value
written and no shift is performed. A (left) shift operation is performed depending on the USICS1..0 bit settings. The shift
operation can be controlled by an external clock edge, by a Timer/Counter 0 compare match, or directly by software using
the USICLK strobe bit. Note that even when no wire mode is selected (USIWM1..0 = 0), both the external data input
(DI/SDA) and the external clock input (USCK/SCL) can still be used by the shift register.
The output pin in use - DO or SDA, depending on the wire mode - is connected via the output latch to the most-significant Bit
(bit 7) of the data register. The output latch is open (transparent) during the first half of a serial clock cycle when an external
clock source is selected (USICS1 = 1), and constantly open when an internal clock source is used (USICS1 = 0). The output
will be changed immediately when a new MSB is written as long as the latch is open. The latch ensures that data input is
sampled and data output is changed on opposite clock edges.
Note that the corresponding data direction register to the pin must be set to one for enabling data output from the shift
register.
The status register contains interrupt flags, line status flags and the counter value.
The control register includes interrupt enable control, wire mode setting, clock select setting, and clock strobe.
1 1 Same operation as for the Two-wire mode described above, except that the SCL line is also
held low when a counter overflow occurs, and is held low until the counter overflow flag
(USIOIF) is cleared.
Note: 1. The DI and USCK pins are renamed to serial data (SDA) and serial clock (SCL), respectively, to avoid
confusion between the modes of operation.
USICS1 USICS0 USICLK Shift Register Clock Source 4-bit Counter Clock Source
0 0 0 No clock No Clock
0 0 1 Software clock strobe (USICLK) Software clock strobe (USICLK)
0 1 X Timer/Counter0 compare match Timer/Counter0 compare match
1 0 0 External, positive edge External, both edges
1 1 0 External, negative edge External, both edges
1 0 1 External, positive edge Software clock strobe (USITC)
1 1 1 External, negative edge Software clock strobe (USITC)
Bandgap VCC
Reference
ACBG
ACD
ACIE
AIN0
Analog
+
Interrupt Comparator
Select IRQ
-
AIN1 ACI
ACME
ADEN
To T/C1 Capture
ACO Trigger MUX
ADC Multiplexer
Output(1)
When changing the ACIS1/ACIS0 bits, the analog comparator interrupt must be disabled by clearing its interrupt enable bit in
the ACSR. Otherwise, an interrupt can occur when the bits are changed.
18.1 Features
● 10-bit resolution
● 1.0 LSB integral non-linearity
● ±2 LSB absolute accuracy
● 65 - 260µs conversion time
● Up to 76kSPS at maximum resolution
● Eight multiplexed single-ended input channels
● Twelve differential input channels with selectable gain (1x, 20x)
● Temperature sensor input channel
● Optional left adjustment for ADC result readout
● 0 to VCC ADC input voltage range
● 1.1V ADC reference voltage
● Free-running or single-conversion mode
● ADC start conversion by auto triggering on interrupt sources
● Interrupt on ADC conversion complete
● Sleep mode noise canceller
● Unipolar / bipolar input mode
● Input polarity reversal channels
18.2 Overview
The Atmel® ATtiny24/44/84 features a 10-bit successive approximation analog-to-digital converter (ADC). The ADC is
connected to 8-pin port A for external sources. In addition to external sources, the internal temperature sensor can be
measured by the ADC. The analog multiplexer allows 8 single-ended channels or 12 differential channels from port A. The
programmable gain stage provides amplification steps 0dB (1x) and 26dB (20x) for 12 differential ADC channels.
The ADC contains a sample-and-hold circuit which ensures that the input voltage to the ADC is held at a constant level
during conversion. A block diagram of the ADC is shown in Figure 18-1 on page 119.
And internal reference voltage of nominally 1.1V is provided on chip. Alternatively, VCC can be used as reference voltage for
single-ended channels. There is also an option to use an external voltage reference and turn off the internal voltage
reference.
Interrupt
Flags
ADTS2 to ADTS0
8-bit Data Bus
ADIE
ADIF
15 0
ADC CTRL & Status B ADC Multiplexer ADC CTRL and Status A ADC Data Register
Register (ADCSRB) Select (ADMUX) Register (ADCSRA) (ADCH/ADCL)
MUX4...MUX0
ADLAR
ADEN
ADSC
ADATE
ADIF
ADPS2
ADPS1
ADPS0
BIN
ADC[9:0]
IPR
Trigger
Select
REFS1...REFS0
AREF START
MUX Decoder Prescaler
Channel Selection
Gain Selection
VCC
Conversion Logic
Internal
Reference
1.1V
Temperature
Sample and Hold
Sensor
Comparator
10-bit DAC -
ADC8
+
AGND Single Ended/ Differential Selection
ADC7
ADC
ADC6 MULTIPLEXER
POS. OUTPUT
ADC5
Input
ADC4 MUX
+
ADC3 -
Gain
ADC2 Amplifier
ADC1
ADC0
NEG.
Input
MUX
START CLKADC
ADIF ADATE
SOURCE 1
.
Conversion
. Logic
.
. Edge
Detector
SOURCE n
ADSC
Using the ADC interrupt flag as a trigger source makes the ADC start a new conversion as soon as the ongoing conversion
has finished. The ADC then operates in free running mode, constantly sampling and updating the ADC data register. The
first conversion must be started by writing a logical one to the ADSC bit in ADCSRA. In this mode, the ADC will perform
successive conversions independently of whether the ADC interrupt flag (ADIF) is cleared or not.
If auto triggering is enabled, single conversions can be started by writing ADSC in ADCSRA to logical one. ADSC can also
be used to determine if a conversion is in progress. The ADSC bit will be read as logical one during a conversion
independently of how the conversion was started.
CK/4
CK/8
CK/16
CK/32
CK/64
CK/128
ADPS0
ADPS1
ADPS2
By default, the successive approximation circuitry requires an input clock frequency between 50kHz and 200kHz to get
maximum resolution. If a lower resolution than 10 bits is needed, the input clock frequency to the ADC can be higher than
200kHz to get a higher sample rate.The ADC module contains a prescaler, which generates an acceptable ADC clock
frequency from any CPU frequency above 100kHz.
The prescaling is set by the ADPS bits in ADCSRA. The prescaler starts counting from the moment the ADC is switched on
by setting the ADEN bit in ADCSRA. The prescaler keeps running for as long as the ADEN bit is set, and is continuously
reset when ADEN is low.
When initiating a single-ended conversion by setting the ADSC bit in ADCSRA, the conversion starts at the following rising
edge of the ADC clock cycle.
A normal conversion takes 13 ADC clock cycles. The first conversion after the ADC is switched on (ADEN in ADCSRA is set)
takes 25 ADC clock cycles in order to initialize the analog circuitry.
The actual sample-and-hold takes place 1.5 ADC clock cycles after the start of a normal conversion and 14.5 ADC clock
cycles after the start of a first conversion. When a conversion is complete, the result is written to the ADC data registers, and
ADIF is set. In single-conversion mode, ADSC is cleared simultaneously. The software may then set ADSC again, and a
new conversion will be initiated on the first rising ADC clock edge.
Figure 18-4. ADC Timing Diagram, First Conversion (Single Conversion Mode)
Next
First Conversion Conversion
Cycle Number 1 2 12 13 14 15 16 17 18 19 20 21 22 23 24 25 1 2 3
ADC Clock
ADEN
ADSC
ADIF
Cycle Number 1 2 3 4 5 6 7 8 9 10 11 12 13 1 2 3
ADC Clock
ADSC
ADIF
Cycle Number 1 2 3 4 5 6 7 8 9 10 11 12 13 1 2
ADC Clock
Trigger
Source
ADATE
ADIF
Cycle Number 11 12 13 1 2 3 4
ADC Clock
ADSC
ADIF
IIH
ADCn
1 to 100kΩ
VCC/2
Output Code
Ideal ADC
Actual ADC
Offset
Error
VREF Input Voltage
● Gain error: After adjusting for offset, the gain error is found as the deviation of the last transition (0x3FE to 0x3FF)
compared to the ideal transition (at 1.5LSB below maximum). Ideal value: 0LSB
Ideal ADC
Actual ADC
Output Code
INL
Ideal ADC
Actual ADC
● Differential non-linearity (DNL): The maximum deviation of the actual code width (the interval between two adjacent
transitions) from the ideal code width (1LSB). Ideal value: 0LSB.
Output Code
0x3FF
1 LSB
DNL
0x000
● Quantization error: Due to the quantization of the input voltage into a finite number of codes, a range of input voltages
(1LSB wide) will code to the same value. Always ±0.5LSB.
● Absolute Accuracy: The maximum deviation of an actual (unadjusted) transition compared to an ideal transition for
any code. This is the compound effect of offset error, gain error, differential error, non-linearity, and quantization error.
Ideal value: ±0.5LSB.
V IN 1024
ADC = ---------------------------
V REF
where VIN is the voltage on the selected input pin and VREF the selected voltage reference (see Table 18-3 on page 129 and
Table 18-4 on page 130). 0x000 represents analog ground, and 0x3FF represents the selected reference voltage minus
1LSB. The result is presented in one-sided form, from 0x3FF to 0x000.
where VPOS is the voltage on the positive input pin, VNEG the voltage on the negative input pin, and VREF the selected voltage
reference.
The voltage of the positive pin must always be larger than the voltage of the negative pin or otherwise the voltage difference
is saturated to zero. The result is presented in one-sided form, from 0x000 (0d) through 0x3FF (+1023d). The GAIN is either
1x or 20x.
where VPOS is the voltage on the positive input pin, VNEG the voltage on the negative input pin, and VREF the selected voltage
reference. The result is presented in two’s complement form, from 0x200 (–512d) through 0x1FF (+511d). The GAIN is
either 1x or 20x. Note that if the user wants to perform a quick polarity check of the result, it is sufficient to read the MSB of
the result (ADC9 in ADCH). If the bit is one, the result is negative, and if this bit is zero, the result is positive.
As default the ADC converter operates in the unipolar input mode, but the bipolar input mode can be selected by writing the
BIN bit in the ADCSRB to one. In the bipolar input mode two-sided voltage differences are allowed and thus the voltage on
the negative input pin can also be larger than the voltage on the positive input pin.
See Table 18-5 on page 131 for details of selection of differential input channels as well as selection of offset calibration
channels. The MUX0 bit works as a gain selection bit for differential channels, as shown in Table 18-5 on page 131. When
the MUX0 bit is cleared (zero) 1x gain is selected, and when it is set (one) 20x gain is selected. For normal differential
channel pairs, the MUX5 bit works as a polarity reversal bit. Toggling of the MUX5 bit reverses the positive and negative
channel orientation.
For offset calibration purposes, the offset of certain differential channels can be measured by selecting the same input for
both negative and positive input. This calibration can be done for ADC0, ADC3, and ADC7. Section 18.3 “ADC Operation” on
page 120 describes offset calibration in a more detailed manner.
MUX5..0
Positive Differential Input Negative Differential Input Gain 1x Gain 20x
(1)
ADC0 (PA0) N/A 100011
ADC0 (PA0) ADC1 (PA1) 001000 001001
ADC3 (PA3) 001010 001011
ADC0 (PA0) 101000 101001
ADC1 (PA1) ADC2 (PA2) 001100 001101
ADC3 (PA3) 001110 001111
ADC1 (PA1) 101100 101101
ADC2 (PA2)
ADC3 (PA3) 010000 010001
ADC0 (PA0) 101010 101011
ADC1 (PA1) 101110 101111
ADC2 (PA2) 110000 110001
ADC3 (PA3)(1) 100100 100101
ADC3 (PA3)
ADC4 (PA4 010010 010011
ADC5 (PA5) 010100 010101
ADC6 (PA6) 010110 010111
ADC7 (PA7) 011000 011001
ADC3 (PA3) 110010 110011
ADC4 (PA4
ADC5 (PA5) 011010 011011
ADC3 (PA3) 110100 110101
ADC5 (PA5) ADC4 (PA4) 111010 111011
ADC6 (PA6) 011100 011101
ADC3 (PA3) 110110 110111
ADC6 (PA6) ADC5 (PA5) 111100 111101
ADC7 (PA7) 011110 011111
ADC3 (PA3) 111000 111001
ADC7 (PA7) ADC6 (PA6) 111110 111111
ADC7 (PA7)(1) 100110 100111
Note: 1. For offset calibration only.See Section 18.3 “ADC Operation” on page 120
18.10.3.1 ADLAR = 0
Bit 15 14 13 12 11 10 9 8
0x05 (0x25) – – – – – – ADC9 ADC8 ADCH
0x04 (0x24) ADC7 ADC6 ADC5 ADC4 ADC3 ADC2 ADC1 ADC0 ADCL
7 6 5 4 3 2 1 0
Read/Write R R R R R R R R
R R R R R R R R
Initial Value 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0
18.10.3.2 ADLAR = 1
Bit 15 14 13 12 11 10 9 8
0x05 (0x25) ADC9 ADC8 ADC7 ADC6 ADC5 ADC4 ADC3 ADC2 ADCH
0x04 (0x24) ADC1 ADC0 – – – – – – ADCL
7 6 5 4 3 2 1 0
Read/Write R R R R R R R R
R R R R R R R R
Initial Value 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0
When an ADC conversion is complete, the result is found in these two registers.
When ADCL is read, the ADC data register is not updated until ADCH is read. Consequently, if the result is left adjusted and
no more than 8-bit precision is required, it is sufficient to read ADCH. Otherwise, ADCL must be read first, then ADCH.
The ADLAR bit in ADCSRB, and the MUXn bits in ADMUX affect the way the result is read from the registers. If ADLAR is
set, the result is left adjusted. If ADLAR is cleared (default), the result is right adjusted.
19.1 Features
● Complete program flow control
● Emulates all on-chip functions, both digital and analog, except RESET pin
● Real-time operation
● Symbolic debugging support (both at C and assembler source level, or for other HLLs)
● Unlimited number of program break points (using software break points)
● Non-intrusive operation
● Electrical characteristics identical to real device
● Automatic configuration system
● High-speed operation
● Programming of non-volatile memories
19.2 Overview
The debugWIRE on-chip debug system uses a one-wire, bi-directional interface to control the program flow, execute AVR®
instructions in the CPU and to program the different non-volatile memories.
VCC
dw dw(RESET)
GND
Figure 19-1 shows the schematic of a target MCU, with debugWIRE enabled, and the emulator connector. The system clock
is not affected by debugWIRE and will always be the clock source selected by the CKSEL fuses.
When designing a system where debugWIRE will be used, the following observations must be made for correct operation:
● Pull-up resistor on the dW/(RESET) line must be in the range of 10k to 20k. However, the pull-up resistor is optional.
● Connecting the RESET pin directly to VCC will not work.
● Capacitors inserted on the RESET pin must be disconnected when using debugWire.
● All external reset sources must be disconnected.
The debugWire data register (DWDR) provides a communication channel from the program running in the MCU to the
debugger. This register is only accessible by the debugWIRE system, and can, therefore, not be used as a general purpose
register in normal operations.
Because the flash is organized in pages (see Table 21-7 on page 144), the program counter can be treated as having two
different sections. One section, consisting of the least significant bits, is addressing the words within a page, while the most
significant bits are addressing the pages. This is shown in Figure 21-1 on page 144. Note that the page erase and page write
operations are addressed independently. Therefore it is of major importance that the software addresses the same page in
both the page erase and page write operation.
The LPM instruction uses the Z-pointer to store the address. Because this instruction addresses the flash byte-by-byte, the
LSB (bit Z0) of the Z-pointer is also used.
PCMSB PAGEMSB
Program
Counter PCPAGE PCWORD
Page Address Word Address
within the Flash within a Page
01
02
PAGEEND
Note: 1. The different variables used in Figure 20-1 are listed in Table 21-7 on page 144.
The algorithm for reading the fuse low byte (FLB) is similar to the one described above for reading the lock bits. To read the
fuse low byte, load the Z-pointer with 0x0000 and set the RFLB and SPMEN bits in SPMCSR. When an LPM instruction is
executed within three cycles after the RFLB and SPMEN bits are set in the SPMCSR, the value of the fuse low byte will be
loaded in the destination register as shown below. See Table 21-5 on page 143 for a detailed description and mapping of the
fuse low byte.
Bit 7 6 5 4 3 2 1 0
Rd FLB7 FLB6 FLB5 FLB4 FLB3 FLB2 FLB1 FLB0
Similarly, when reading the fuse high byte (FHB), load 0x0003 in the Z-pointer. When an LPM instruction is executed within
three cycles after the RFLB and SPMEN bits are set in the SPMCSR, the value of the fuse high byte will be loaded in the
destination register as shown below. See Table 21-4 on page 142 for detailed description and mapping of the fuse high byte.
Bit 7 6 5 4 3 2 1 0
Rd FHB7 FHB6 FHB5 FHB4 FHB3 FHB2 FHB1 FHB0
Lock and fuse bits that are programmed will be read as zero. Lock and fuse bits that are unprogrammed, will be read as one.
Table 21-7. No. of Words in a Page and No. of Pages in the Flash
Device Flash Size Page Size PCWORD No. of Pages PCPAGE PCMSB
1Kwords
ATtiny24 16 words PC[3:0] 64 PC[9:4] 9
(2Kbytes)
2Kwords
ATtiny44 32 words PC[4:0] 64 PC[10:5] 10
(4Kbytes)
4Kwords
ATtiny84 32 words PC[4:0] 128 PC[11:5] 11
(8Kbytes)
Table 21-8. No. of Words in a Page and No. of Pages in the EEPROM
Device EEPROM Size Page Size PCWORD No. of Pages PCPAGE EEAMSB
ATtiny24 128 bytes 4 bytes EEA[1:0] 32 EEA[6:2] 6
ATtiny44 256 bytes 4 bytes EEA[1:0] 64 EEA[7:2] 7
ATtiny84 512 bytes 4 bytes EEA[1:0] 128 EEA[8:2] 8
VCC
MOSI
MISO
SCK
RESET
GND
Note: 1. If the device is clocked by the internal oscillator, it is not needed to connect a clock source to the CLKI pin.
Instruction Format
(1)
Instruction/Operation Byte 1 Byte 2 Byte 3 Byte4
Programming enable $AC $53 $00 $00
Chip erase (program memory/EEPROM) $AC $80 $00 $00
Poll RDY/BSY $F0 $00 $00 data byte out
Load Instructions
Load extended address byte $4D $00 Extended adr $00
Load program memory page, high byte $48 adr MSB adr LSB high data byte in
Load program memory page, low byte $40 adr MSB adr LSB low data byte in
Load EEPROM memory Page (page
$C1 $00 adr LSB data byte in
access)
Read Instructions
Read program memory, high byte $28 adr MSB adr LSB high data byte out
Read program memory, low byte $20 adr MSB adr LSB low data byte out
Read EEPROM memory $A0 $00 adr LSB data byte out
Read lock bits $58 $00 $00 data byte out
Read signature byte $30 $00 adr LSB data byte out
Read fuse bits $50 $00 $00 data byte out
Read fuse high bits $58 $08 $00 data byte out
Read extended fuse bits $50 $08 $00 data byte out
Read calibration byte $38 $00 $00 data byte out
Notes: 1. Not all instructions are applicable for all parts.
2. adr = address
3. Bits are programmed ‘0’, unprogrammed ‘1’.
4. To ensure future compatibility, unused fuses and lock bits should be unprogrammed (‘1’).
5. Refer to the corresponding section for fuse and lock bits, calibration and signature bytes and page size.
6. Instructions accessing program memory use a word address. This address may be random within the page
range.
7. See http://www.atmel.com/avr for application notes regarding programming and programmers.
If the LSB in RDY/BSY data byte out is ‘1’, a programming operation is still pending. Wait until this bit returns ‘0’ before the
next instruction is carried out.
Within the same page, the low data byte must be loaded prior to the high data byte.
After data are loaded to the page buffer, program the EEPROM page (see Figure 21-2 on page 148).
Load Program Memory Page (High/Low Byte)/ Write Program Memory Page/
Load EEPROM Memory Page (page access) Write EEPROM Memory Page
Bit 15 B 0 Bit 15 B 0
Page Buffer
Page Offset
Page 0
Page 1
Page 2
Page Number
Page N-1
Program Memory/
EEPROM Memory
PA5 SII
01
02
PAGEEND
SCI 0 1 2 3 4 5 6 7 8 9 10
PB3
Table 22-1. DC Characteristics TA = –40°C to 125°C, VCC = 2.7V to 5.5V (unless otherwise noted)(1)
16MHz
8MHz
Safe Operating
Area
Example: With oscillator divided by 32, jitter standard deviation will be 32 0.4ns = 12.8ns.
VIH1
VIL1
tCLCX
tCLCL
BODLEVEL [2..0] Fuses Min VBOT Typ VBOT Max VBOT Units
111 BOD disabled
110 1.8
101 2.5 2.7 2.9
100 4.0 4.3 4.6
011 2.3 V
010 2.2
001 1.9
000 2.0
Note: 1. VBOT may be below nominal minimum operating voltage for some devices. For devices where this is the case,
the device is tested down to VCC = VBOT during the production test. This guarantees that a brown-out reset will
occur before VCC drops to a voltage where correct operation of the microcontroller is no longer guaranteed.
MOSI
SCK tSHSL
MISO
tSLIV
SAMPLE
Table 22-8. Serial Programming Characteristics, TA = –40°C to +125°C, VCC = 2.7 to 5.5V (Unless Otherwise Noted)
RESET
1 CK Cycle
WDT
Time-out
tTOUT
RESET
Time-out
Internal
Reset
Figure 23-1. Active Supply Current versus Low Frequency (0.1 to 1.0MHz) - Temperature = 25°C
1.2
5.5V
1
5.0V
4.5V
0.8
ICC (mA)
0.6 3.3V
3.0V
2.7V
0.4
0.2
0
0 0.1 0.2 0.3 0.4 0.5 0.6 0.7 0.8 0.9 1
Frequency (MHz)
1 5.5V
5.0V
0.8
4.5V
ICC (mA)
0.6
3.3V
3.0V
0.4 2.7V
0.2
0
0 0.1 0.2 0.3 0.4 0.5 0.6 0.7 0.8 0.9 1
Frequency (MHz)
Figure 23-3. Active Supply Current versus Frequency (1 to 20MHz) - Temperature = 25°C
25
20
15
ICC (mA)
5.5V
5.0V
10
4.5V
3.3V
3.0V
5 2.7V
0
0 2 4 6 8 10 12 14 16 18 20
Frequency (MHz)
Figure 23-4. Active Supply Current versus Frequency (1 to 20MHz) - Temperature = 125°C
25
20
15
ICC (mA)
5.5V
5.0V
10 4.5V
3.3V
3.0V
5 2.7V
0
0 2 4 6 8 10 12 14 16 18 20
Frequency (MHz)
6 125°C
85°C
25°C
5 -45°C
ICC (mA)
0
2.5 3 3.5 4 4.5 5 5.5
VCC (V)
Figure 23-6. Active Supply Current versus VCC (Internal RC Oscillator, 1MHz)
1.4
125°C
1.2 85°C
25°C
1 -40°C
ICC (mA)
0.8
0.6
0.4
0.2
0
2.5 3 3.5 4 4.5 5 5.5
VCC (V)
Figure 23-7. Active Supply Current versus VCC (Internal RC Oscillator, 128kHz)
0.2
0.16
-40°C
25°C
0.12 85°C
ICC (mA)
125°C
0.08
0.04
0
2.5 3 3.5 4 4.5 5 5.5
VCC (V)
Figure 23-8. Idle Supply Current versus Low Frequency (0.1 to 1.0MHz)
0.012
0.01 5.5V
5.0V
0.008 4.5V
ICC (mA)
0.006 3.3V
3.0V
2.7V
0.004
0.002
0
0 0.1 0.2 0.3 0.4 0.5 0.6 0.7 0.8 0.9 1
Frequency (MHz)
3.5
5.5V
3 5.0V
2.5 4.5V
ICC (mA)
2
3.3V
1.5 3.0V
2.7V
1
0.5
0
0 2 4 6 8 10 12 14 16 18 20
Frequency (MHz)
1.8
1.6
125°C
1.4 85°C
25°C
1.2 -45°C
ICC (mA)
0.8
0.6
0.4
0.2
0
2.5 3 3.5 4 4.5 5 5.5
VCC (V)
Figure 23-11. Idle Supply Current versus VCC (Internal RC Oscillator, 1MHz)
0.35
125°C
0.3 85°C
25°C
-40°C
0.25
ICC (mA)
0.2
0.15
0.1
0.05
0
2.5 3 3.5 4 4.5 5 5.5
VCC (V)
Figure 23-12. Idle Supply Current versus VCC (Internal RC Oscillator, 128kHz)
0.035
0.03 125°C
85°C
0.025 25°C
-40°C
ICC (mA)
0.02
0.015
0.01
0.005
0
2.5 3 3.5 4 4.5 5 5.5
VCC (V)
Table 23-1. Additional Current Consumption for the different I/O Modules (Absolute Values)
Figure 23-13. Power-down Supply Current versus VCC (Watchdog Timer Disabled)
5
4.5
3.5
3 125°C
ICC (µA)
2.5
1.5 85°C
1
0.5
25°C
0 -45°C
2.5 3 3.5 4 4.5 5 5.5
VCC (V)
Figure 23-14. Power-down Supply Current versus VCC (Watchdog Timer Enabled)
10
6
ICC (µA)
3 125°C
-45°C
2 85°C
25°C
1
0
2.5 3 3.5 4 4.5 5 5.5
VCC (V)
Figure 23-15. I/O Pin Pull-up Resistor Current versus input Voltage (VCC = 2.7V)
90
80
70
60
IOP (µA)
50
40
30
20
-45°C
10 25°C
85°C
0 125°C
0 0.5 1 1.5 2 2.5 3
VOP (V)
Figure 23-16. I/O pin Pull-up Resistor Current versus Input Voltage (VCC = 5V)
160
140
120
100
IOP (µA)
80
60
40
-45°C
20 25°C
85°C
0 125°C
0 0.5 1 1.5 2 2.5 3 3.5 4 4.5 5
VOP (V)
-40°C
50
125°C
40
IRESET (µA)
30
20
10
0
0 0.5 1 1.5 2 2.5 3
VRESET (V)
Figure 23-18. Reset Pull-up Resistor Current versus Reset Pin Voltage (VCC = 5V)
120
-40°C
100
125°C
80
IRESET (µA)
60
40
20
0
0 0.5 1 1.5 2 2.5 3 3.5 4 4.5 5
VRESET (V)
Figure 23-19. I/O Pin Output Voltage versus Sink Current (VCC = 3V)
0.06
125°C
0.05
0.04
85°C
25°C
VOL (V)
0.03 -40°C
0.02
0.01
0
0 2 4 6 8 10 12 14 16 18 20
IOL (mA)
Figure 23-20. I/O pin Output Voltage versus Sink Current (VCC = 5V)
0.7
0.6 125°C
85°C
0.5
25°C
0.4
VOL (V)
-45°C
0.3
0.2
0.1
0
0 2 4 6 8 10 12 14 16 18 20
IOL (mA)
Figure 23-21. I/O Pin Output Voltage versus Source Current (VCC = 3V)
3.5
3
VOH (V)
2.5
-45°C
25°C
2 85°C
125°C
1.5
0 2 4 6 8 10 12 14 16 18 20
IOH (mA)
4.9
4.8
VOH (V)
4.7
4.6
-45°C
4.5 25°C
4.4 85°C
125°C
4.3
0 2 4 6 8 10 12 14 16 18 20
IOH (mA)
Figure 23-23. I/O Pin Input Threshold Voltage versus VCC (VIH, IO Pin Read as ‘1’)
3.5
125°C
3 85°C
25°C
-40°C
2.5
Threshold (V)
1.5
0.5
0
2.5 3 3.5 4 4.5 5 5.5
VCC (V)
1.5
0.5
0
2.5 3 3.5 4 4.5 5 5.5
VCC (V)
0.35
0.3
0.25
0.2
0.15
0.1
0.05
0
2.5 3 3.5 4 4.5 5 5.5
VCC (V)
Figure 23-26. Reset Input Threshold Voltage versus VCC (VIH, IO Pin Threshold as ‘1’)
3 125°C
85°C
25°C
2.5 -40°C
Threshold (mV)
1.5
0.5
0
2.5 3 3.5 4 4.5 5 5.5
VCC (V)
2.5 125°C
85°C
25°C
Threshold (mV)
2 -45°C
1.5
0.5
0
2.5 3 3.5 4 4.5 5 5.5
VCC (V)
0.9
0.8
Input Hysteresis (mV)
0.7
0.6
0.5
-40°C
0.4
0.3
25°C
0.2
85°C
0.1
125°C
0
2.5 3 3.5 4 4.5 5 5.5
VCC (V)
1
4.35
Threshold (V)
4.3
0
4.25
4.2
4.15
-40 -30 -20 -10 0 10 20 30 40 50 60 70 80 90 100 110 120
Temperature (°C)
2.74
Threshold (V)
2.72
2.7 0
2.68
2.66
2.64
-40 -30 -20 -10 0 10 20 30 40 50 60 70 80 90 100 110 120
Temperature (°C)
1.84 1
1.83
Threshold (V)
1.82
1.81
0
1.8
1.79
1.78
-40 -30 -20 -10 0 10 20 30 40 50 60 70 80 90 100 110 120
Temperature (°C)
122
120 -40°C
118
116 25°C
FRC (kHz)
114
112
85°C
110
108
106 125°C
104
2.5 3 3.5 4 4.5 5 5.5
VCC (V)
8.5
-40°C
25°C
8
85°C
FRC (MHz)
125°C
7.5
6.5
6
2.5 3 3.5 4 4.5 5 5.5
VCC (V)
8.3 5.0V
3.0V
8.2
FRC (MHz)
8.1
7.9
7.8
7.7
-40 -30 -20 -10 0 10 20 30 40 50 60 70 80 90 100 110 120
Temperature (°C)
10
FRC (MHz)
0
0 16 32 48 64 80 96 112 128 144 160 176 192 208 224 240
OSCCAL (X1)
500
ICC (µA)
400
300
200
100
0
2.5 3 3.5 4 4.5 5 5.5
VCC (V)
25°C
12
AREF Pin Current (µA)
10
0
1.5 2 2.5 3 3.5 4 4.5 5 5.5
AREF (V)
90 -40°C
25°C
80 85°C
125°C
70
60
ICC (µA)
50
40
30
20
10
0
2.5 3 3.5 4 4.5 5 5.5
VCC (V)
25°C
10000
8000
ICC (µA)
6000
4000
2000
0
2.5 3.5 4.5 5.5
VCC (V)
-40°C
25 25°C
85°C
125°C
20
ICC (µA)
15
10
0
2.5 3 3.5 4 4.5 5 5.5
VCC (V)
Figure 23-41. Reset Supply Current versus VCC (0.1 to 1.0MHz, excluding Current Through the Reset Pull-up)
0.2
0.18 5.5V
5.0V
0.16
4.5V
0.14
0.12
ICC (mA)
3.3V
0.1 3.0V
2.7V
0.08
0.06
0.04
0.02
0
0 0.1 0.2 0.3 0.4 0.5 0.6 0.7 0.8 0.9 1
Frequency (MHz)
Figure 23-42. Reset Supply Current versus VCC (1 to 20MHz, Excluding Current Through the Reset Pull-up)
3
2.5 5.5V
5.0V
2
4.5V
ICC (mA)
1.5
3.6V
3.3V
3.0V
1 2.7V
0.5
0
0 2 4 6 8 10 12 14 16 18 20
Frequency (MHz)
800
600
125°C
400 85°C
25°C
-40°C
200
0
2.5 3 3.5 4 4.5 5 5.5
VCC (V)
26.1 ATtiny24/44/84
Ordering Code Speed (MHz)(3) Power Supply (V) Package(1)(2) Operation Range
27.1 PC
ccc C eee C
DRAWINGS NOT SCALED
A
D A1
D2
COMMON DIMENSIONS
6 10 (Unit of Measure = mm)
SYMBOL MIN NOM MAX NOTE
A 0.70 0.75 0.80
5 11
A1 0.00 0.02 0.05
D/E 3.90 4.00 4.10
0.35
Notes: 1. This drawing is for general information only. Refer to JEDEC Drawing MO-220, Variation WGGD-5 for proper dimensions, tolerances, datums, etc.
(excepted D2/E2 Min et Nom.)
2. Dimensions b applies to metallized terminal and is measured between 0.15mm and 0.30mm from the terminal tip.
If the terminal has the optical radius on the other end of the terminal, the dimensions should not be measured in that radius area. 08/01/14
E H
L
B A1 A E
MM INCH
MIN NOM MAX MIN NOM MAX
A 1.35 1.60 1.75 .053 .063 .069
A1 0.10 0.25 .004 .010
B 0.33 0.41 0.51 .013 .016 .020
D 8.53 8.64 8.74 .336 .340 .344
E 3.80 3.91 3.99 .149 .154 .157
H 5.79 5.99 6.20 .228 .236 .244
L 0.40 0.71 1.27 .016 .028 .050
e 1.27 BSC .050 BSC
07/27/07
28.1.1 Rev. E
1. No known errata.
28.2.1 Rev. D
1. No known errata.
28.3.1 Rev. B
1. No known errata.
Please note that the following page numbers referred to in this section refer to the specific revision mentioned, not to this
document.
1. Pin Configurations . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 3
1.1 Disclaimer . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 3
2. Overview . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 4
2.1 Block Diagram . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 5
2.2 Automotive Quality Grade . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 6
2.3 Pin Descriptions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 6
3. Resources . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 8
5. CPU Core . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 9
5.1 Overview . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 9
5.2 Architectural Overview . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 9
5.3 ALU – Arithmetic Logic Unit . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 10
5.4 Status Register . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 10
5.5 General Purpose Register File . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 12
5.6 Stack Pointer . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 13
5.7 Instruction Execution Timing . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 14
5.8 Reset and Interrupt Handling . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 14
6. Memories . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 16
6.1 In-System Re-programmable Flash Program Memory . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 16
6.2 SRAM Data Memory . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 16
6.3 EEPROM Data Memory. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 17
6.4 I/O Memory . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 20
6.5 Register Description. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 21
10. Interrupts . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 44
10.1 Interrupt Vectors . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 44
Atmel®, Atmel logo and combinations thereof, Enabling Unlimited Possibilities®, AVR®, AVR Studio ®, and others are registered trademarks or trademarks of Atmel
Corporation in U.S. and other countries. Other terms and product names may be trademarks of others.
DISCLAIMER: The information in this document is provided in connection with Atmel products. No license, express or implied, by estoppel or otherwise, to any intellectual property right
is granted by this document or in connection with the sale of Atmel products. EXCEPT AS SET FORTH IN THE ATMEL TERMS AND CONDITIONS OF SALES LOCATED ON THE
ATMEL WEBSITE, ATMEL ASSUMES NO LIABILITY WHATSOEVER AND DISCLAIMS ANY EXPRESS, IMPLIED OR STATUTORY WARRANTY RELATING TO ITS PRODUCTS
INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTY OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE, OR NON-INFRINGEMENT. IN NO EVENT
SHALL ATMEL BE LIABLE FOR ANY DIRECT, INDIRECT, CONSEQUENTIAL, PUNITIVE, SPECIAL OR INCIDENTAL DAMAGES (INCLUDING, WITHOUT LIMITATION, DAMAGES
FOR LOSS AND PROFITS, BUSINESS INTERRUPTION, OR LOSS OF INFORMATION) ARISING OUT OF THE USE OR INABILITY TO USE THIS DOCUMENT, EVEN IF ATMEL HAS
BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGES. Atmel makes no representations or warranties with respect to the accuracy or completeness of the contents of this
document and reserves the right to make changes to specifications and products descriptions at any time without notice. Atmel does not make any commitment to update the information
contained herein. Unless specifically provided otherwise, Atmel products are not suitable for, and shall not be used in, automotive applications. Atmel products are not intended,
authorized, or warranted for use as components in applications intended to support or sustain life.
SAFETY-CRITICAL, MILITARY, AND AUTOMOTIVE APPLICATIONS DISCLAIMER: Atmel products are not designed for and will not be used in connection with any applications where
the failure of such products would reasonably be expected to result in significant personal injury or death (“Safety-Critical Applications”) without an Atmel officer's specific written
consent. Safety-Critical Applications include, without limitation, life support devices and systems, equipment or systems for the operation of nuclear facilities and weapons systems.
Atmel products are not designed nor intended for use in military or aerospace applications or environments unless specifically designated by Atmel as military-grade. Atmel products are
not designed nor intended for use in automotive applications unless specifically designated by Atmel as automotive-grade.