ATmega88/ATmega168 High Temperature Automotive Microcontroller DATASHEET Features High performance, low power AVR(R) 8-bit microcontroller Advanced RISC architecture 131 powerful instructions - most single clock cycle execution 32 8 general purpose working registers Fully static operation Up to 16MIPS throughput at 16MHz On-chip 2-cycle multiplier Non-volatile program and data memories 4/8/16Kbytes of in-system self-programmable flash (ATmega88/168) Endurance: 10,000 write/erase cycles Optional boot code section with independent lock bits In-system programming by on-chip boot program True read-while-write operation 256/512/512 Bytes EEPROM (ATmega88/168) Endurance: 50,000 write/erase cycles 512/1K/1Kbyte internal SRAM (ATmega88/168) Programming lock for software security Peripheral features Two 8-bit Timer/Counters with separate prescaler and compare mode One 16-bit Timer/Counter with separate prescaler, compare mode, and capture mode Real time counter with separate oscillator Six PWM channels 8-channel 10-bit ADC Programmable serial USART Master/slave SPI serial interface Byte-oriented 2-wire serial interface Programmable watchdog timer with separate on-chip oscillator On-chip analog comparator Interrupt and wake-up on pin change Special microcontroller features Power-on reset and programmable brown-out detection Internal calibrated oscillator External and internal interrupt sources Five sleep modes: Idle, ADC noise reduction, power-save, power-down, and standby 9365A-AVR-02/16 I/O and packages 23 programmable I/O lines Green/ROHS 32-lead TQFP and 32-pad QFN Operating voltage: 2.7 - 5.5V Temperature range: -40C to 150C Speed grade: ATmega88/168: 0 to 8MHz at 2.7 to 5.5V, 0 - 16MHz at 4.5 to 5.5V Low power consumption Active mode: 4MHz, 3.0V: 1.8mA Power-down mode: 5A at 3.0V AEC-Q100 Grade 0 qualified 2 ATmega88/ATmega168 Automotive [DATASHEET] 9365A-AVR-02/16 1. Pin Configurations Figure 1-1. Pinout ATmega88/168 PC2 (ADC2/PCINT10) PC3 (ADC3/PCINT11) PC4 (ADC4/SDA/PCINT12) PC5 (ADC5/SCL/PCINT13) PC6 (RESET/PCINT14) PD0 (RXD/PCINT16) PD2 (INT0/PCINT18) 32 31 30 29 28 27 26 25 PD1 (TXD/PCINT17) MLF Top View PC2 (ADC2/PCINT10) PC3 (ADC3/PCINT11) PC4 (ADC4/SDA/PCINT12) PC5 (ADC5/SCL/PCINT13) PC6 (RESET/PCINT14) PD0 (RXD/PCINT16) PD1 (TXD/PCINT17) PD2 (INT0/PCINT18) TQFP Top View 32 31 30 29 28 27 26 25 (PCINT19/OC2B/INT1) PD3 1 24 PC1 (ADC1/PCINT9) (PCINT19/OC2B/INT1) PD3 1 24 PC1 (ADC1/PCINT9) (PCINT20/XCK/T0) PD4 2 23 PC0 (ADC0/PCINT8) (PCINT20/XCK/T0) PD4 2 23 PC0 (ADC0/PCINT8) GND 3 22 ADC7 GND 3 22 ADC7 VCC 4 21 GND VCC 4 21 GND GND 5 20 AREF GND 5 20 AREF VCC 6 19 ADC6 VCC 6 19 ADC6 (PCINT6/XTAL1/TOSC1) PB6 7 18 AVCC (PCINT6/XTAL1/TOSC1) PB6 7 18 AVCC (PCINT7/XTAL2/TOSC2) PB7 8 17 PB5 (SCK/PCINT5) (PCINT7/XTAL2/TOSC2) PB7 8 17 PB5 (SCK/PCINT5) (PCINT4/MISO) PB4 (PCINT3/OC2A/MOSI) PB3 (PCINT2/SS/OC1B) PB2 (PCINT1/OC1A) PB1 (PCINT0/CLKO/ICP1) PD5 10 11 12 13 14 15 16 (PCINT23/AIN1) PD7 NOTE: Bottom pad should be soldered to ground. (PCINT21/OC0B/T1) PD5 9 (PCINT22/OC0A/AIN0) PD6 (PCINT4/MISO) PB4 (PCINT3/OC2A/MOSI) PB3 (PCINT2/SS/OC1B) PB2 (PCINT1/OC1A) PB1 (PCINT0/CLKO/ICP1) PD5 (PCINT23/AIN1) PD7 (PCINT21/OC0B/T1) PD5 1.1 10 11 12 13 14 15 16 (PCINT22/OC0A/AIN0) PD6 9 Disclaimer Typical values contained in this datasheet are based on simulations and characterization of other AVR(R) microcontrollers manufactured on the same process technology. Min and Max values will be available after the device is characterized. ATmega88/ATmega168 Automotive [DATASHEET] 9365A-AVR-02/16 3 2. Overview The Atmel(R) ATmega88/168 is a low-power CMOS 8-bit microcontroller based on the AVR(R) enhanced RISC architecture. By executing powerful instructions in a single clock cycle, the Atmel ATmega88/168 achieves throughputs approaching 1MIPS per MHz allowing the system designer to optimize power consumption versus processing speed. 2.1 Block Diagram Figure 2-1. Block Diagram GND Watchdog Timer Watchdog Oscillator VCC Power Supervision POR/ BOD and RESET debugWIRE Flash SRAM Oscillator Circuits/ Clock Generation Program Logic AVR CPU EEPROM AVCC AREF DATA BUS GND 8 bit T/C 0 16 bit T/C 1 A/D Conv. 8 bit T/C 2 Analog Comp. Internal Bandgap USART 0 SPI TWI PORT D (8) PORT B (8) PORT C (7) 2 6 RESET XTAL[1 to 2] PD[0 to 7] 4 PB[0 to 7] ATmega88/ATmega168 Automotive [DATASHEET] 9365A-AVR-02/16 PC[0 to 6] ADC[6 to 7] The AVR(R) core combines a rich instruction set with 32 general purpose working registers. All the 32 registers are directly connected to the arithmetic logic unit (ALU), allowing two independent 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(R) ATmega88/168 provides the following features: 4K/8K/16Kbytes of in-system programmable flash with read-while-write capabilities, 256/512/512 bytes EEPROM, 512/1K/1Kbytes SRAM, 23 general purpose I/O lines, 32 general purpose working registers, three flexible Timer/Counters with compare modes, internal and external interrupts, a serial programmable USART, a byte-oriented 2-wire serial interface, an SPI serial port, a 6-channel 10-bit ADC (8 channels in TQFP and QFN packages), a programmable watchdog timer with internal oscillator, and five software selectable power saving modes. The idle mode stops the CPU while allowing the SRAM, Timer/Counters, USART, 2-wire serial interface, SPI port, and interrupt system to continue functioning. The power-down mode saves the register contents but freezes the oscillator, disabling all other chip functions until the next interrupt or hardware reset. In power-save mode, the asynchronous timer continues to run, allowing the user to maintain a timer base while the rest of the device is sleeping. The ADC noise reduction mode stops the CPU and all I/O modules except asynchronous timer and 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 reprogrammed in-system through an SPI serial interface, by a conventional non-volatile memory programmer, or by an on-chip boot program running on the AVR core. The boot program can use any interface to download the application program in the application flash memory. Software in the boot flash section will continue to run while the application flash section is updated, providing true read-while-write operation. By combining an 8-bit RISC CPU with in-system self-programmable flash on a monolithic chip, the Atmel ATmega88/168 is a powerful microcontroller that provides a highly flexible and cost effective solution to many embedded control applications. The Atmel ATmega88/168 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.2 Automotive Quality Grade The high temperature automotive ATmega88 and ATmega168 have been developed and manufactured according to the most stringent requirements of the international standard ISO-TS-16949 grade 1. This data sheet contains limit values extracted from the results of extensive characterization (temperature and voltage). The quality and reliability of the automotive high temperature ATmega88 and ATmega168 have been verified during regular product qualification as per AEC-Q100. As indicated in the ordering information paragraph (see Section 30. "Ordering Information" on page 281), the products are available in three different temperature grades, but with equivalent quality and reliability objectives. Different temperature identifiers have been defined as listed in Table 2-1. Table 2-1. Temperature Grade Identification for Automotive Products Temperature Temperature Identifier -40; +150 D, T2 Comments Automotive high temperature range ATmega88/ATmega168 Automotive [DATASHEET] 9365A-AVR-02/16 5 2.3 Comparison between ATmega88 and ATmega168 The Atmel(R) ATmega88 and ATmega168 differ only in memory sizes, boot loader support, and interrupt vector sizes. Table 2-2 summarizes the different memory and interrupt vector sizes for the three devices. Table 2-2. Memory Size Summary Device Flash EEPROM RAM Interrupt Vector Size ATmega88 8Kbytes 512 Bytes 1K Bytes 1 instruction word/vector ATmega168 16Kbytes 512 Bytes 1K Bytes 2 instruction words/vector ATmega88 and ATmega168 support a real read-while-write self-programming mechanism. There is a separate boot loader section, and the SPM instruction can only execute from there. 2.4 Pin Descriptions 2.4.1 VCC Digital supply voltage. 2.4.2 GND Ground. 2.4.3 Port B (PB7..0) XTAL1/XTAL2/TOSC1/TOSC2 Port B is an 8-bit bi-directional I/O port with internal pull-up resistors (selected for each bit). The port B output buffers have symmetrical drive characteristics with both high sink and source capability. As inputs, port B pins that are externally pulled low will source current if the pull-up resistors are activated. The port B pins are tri-stated when a reset condition becomes active, even if the clock is not running. Depending on the clock selection fuse settings, PB6 can be used as input to the inverting oscillator amplifier and input to the internal clock operating circuit. Depending on the clock selection fuse settings, PB7 can be used as output from the inverting oscillator amplifier. If the internal calibrated RC oscillator is used as chip clock source, PB7..6 is used as TOSC2..1 input for the asynchronous Timer/Counter2 if the AS2 bit in ASSR is set. The various special features of port B are elaborated in Section 10.3.2 "Alternate Functions of Port B" on page 62 and Section 6. "System Clock and Clock Options" on page 23. 2.4.4 Port C (PC5..0) Port C is a 7-bit bi-directional I/O port with internal pull-up resistors (selected for each bit). The PC5..0 output buffers have symmetrical drive characteristics with both high sink and source capability. As inputs, Port C pins that are externally pulled low will source current if the pull-up resistors are activated. The port C pins are tri-stated when a reset condition becomes active, even if the clock is not running. 2.4.5 PC6/RESET If the RSTDISBL Fuse is programmed, PC6 is used as an I/O pin. Note that the electrical characteristics of PC6 differ from those of the other pins of Port C. If the RSTDISBL fuse is unprogrammed, PC6 is used as a 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 8-1 on page 40. Shorter pulses are not guaranteed to generate a reset. The various special features of port C are elaborated in Section 10.3.3 "Alternate Functions of Port C" on page 65. 2.4.6 Port D (PD7..0) Port D is an 8-bit bi-directional I/O port with internal pull-up resistors (selected for each bit). The port D output buffers have symmetrical drive characteristics with both high sink and source capability. As inputs, port D pins that are externally pulled 6 ATmega88/ATmega168 Automotive [DATASHEET] 9365A-AVR-02/16 low will source current if the pull-up resistors are activated. The port D pins are tri-stated when a reset condition becomes active, even if the clock is not running. The various special features of port D are elaborated in Section 10.3.4 "Alternate Functions of Port D" on page 67. 2.4.7 AVCC AVCC is the supply voltage pin for the A/D converter, PC3..0, and ADC7..6. It should be externally connected to VCC, even if the ADC is not used. If the ADC is used, it should be connected to VCC through a low-pass filter. Note that PC6..4 use digital supply voltage, VCC. 2.4.8 AREF AREF is the analog reference pin for the A/D converter. 2.4.9 ADC7..6 (TQFP and QFN Package Only) In the TQFP and QFN package, ADC7..6 serve as analog inputs to the A/D converter. These pins are powered from the analog supply and serve as 10-bit ADC channels. ATmega88/ATmega168 Automotive [DATASHEET] 9365A-AVR-02/16 7 3. About Code Examples This documentation contains simple code examples that briefly show how to use various parts of the device. These code examples assume that the part specific header file is included before compilation. Be aware that not all C compiler vendors include bit definitions in the header files and interrupt handling in C is compiler dependent. Please confirm with the C compiler documentation for more details. 4. AVR CPU Core 4.1 Introduction This section discusses the AVR(R) 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. 4.2 Architectural Overview Figure 4-1. Block Diagram of the AVR Architecture Data Bus 8-bit Flash Program Memory Program Counter Status and Control 32 x 8 General Purpose Registers Control Lines Indirect Addressing Instruction Decoder Direct Addressing Instruction Register ALU Interrupt Unit SPI Unit Watchdog Timer Analog Comparator I/O Module 1 Data SRAM I/O Module 2 I/O Module n EEPROM I/O Lines 8 ATmega88/ATmega168 Automotive [DATASHEET] 9365A-AVR-02/16 In order to maximize performance and parallelism, the AVR(R) 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. 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 - in one clock cycle. Six of the 32 registers can be used as three 16-bit indirect address register pointers for data space addressing - enabling efficient address calculations. One of the these address pointers can also be used as an address pointer for look up tables in flash program memory. These added function registers are the 16-bit X-, Y-, and Z-register, described later in this section. The ALU supports arithmetic and logic operations between registers or between a constant and a register. Single register operations can also be executed in the ALU. After an arithmetic 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. Program flash memory space is divided in two sections, the boot program section and the application program section. Both sections have dedicated lock bits for write and read/write protection. The SPM instruction that writes into the application flash memory section must reside in the boot program section. During interrupts and subroutine calls, the return address program counter (PC) is stored on the stack. The stack is effectively allocated in the general data SRAM, and consequently the stack size is only limited by the total SRAM size and the usage of the SRAM. All user programs must initialize the SP in the reset routine (before subroutines or interrupts are executed). The stack pointer (SP) is read/write accessible in the I/O space. The data SRAM can easily be accessed through the five different addressing modes supported in the AVR architecture. The memory spaces in the AVR architecture are all linear and regular memory maps. A flexible interrupt module has its control registers in the I/O space with an additional global interrupt enable bit in the status register. All interrupts have a separate interrupt vector in the interrupt vector table. The interrupts have priority in accordance with their interrupt vector position. The lower the interrupt vector address, the higher the priority. The I/O memory space contains 64 addresses for CPU peripheral functions 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. In addition, the ATmega88/168 has extended I/O space from 0x60 - 0xFF in SRAM where only the ST/STS/STD and LD/LDS/LDD instructions can be used. 4.3 ALU - Arithmetic Logic Unit The high-performance AVR ALU operates in direct connection with all the 32 general purpose working registers. Within a single clock cycle, arithmetic operations between general purpose registers or between a register and an immediate are executed. The ALU operations are divided into three main categories - arithmetic, logical, and bit-functions. Some implementations of the architecture also provide a powerful multiplier supporting both signed/unsigned multiplication and fractional format. See the "Instruction Set" section for a detailed description. ATmega88/ATmega168 Automotive [DATASHEET] 9365A-AVR-02/16 9 4.4 Status Register The status register contains information about the result of the most recently executed arithmetic instruction. This information can be used for altering program flow in order to perform conditional operations. Note that the status register is updated after all ALU operations, as specified in the instruction set reference. This will in many cases remove the need for using the dedicated compare instructions, resulting in faster and more compact code. The status register is not automatically stored when entering an interrupt routine and restored when returning from an interrupt. This must be handled by software. The AVR(R) status register - SREG - is defined as: Bit 7 6 5 4 3 2 1 0 I T H S V N Z C 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 SREG * Bit 7 - I: Global Interrupt Enable The global interrupt enable bit must be set for the interrupts to be enabled. The individual interrupt enable control is then performed in separate control registers. If the global interrupt enable register is cleared, none of the interrupts are enabled independent of the individual interrupt enable settings. The I-bit is cleared by hardware after an interrupt has occurred, and is set by the RETI instruction to enable subsequent interrupts. The I-bit can also be set and cleared by the application with the SEI and CLI instructions, as described in the instruction set reference. * Bit 6 - T: Bit Copy Storage The bit copy instructions BLD (Bit LoaD) and BST (Bit STore) use the T-bit as source or destination for the operated bit. A bit from a register in the register file can be copied into T by the BST instruction, and a bit in T can be copied into a bit in a register in the register file by the BLD instruction. * Bit 5 - H: Half Carry Flag The half carry flag H indicates a half carry in some arithmetic operations. half carry Is useful in BCD arithmetic. See the "Instruction Set Description" for detailed information. * Bit 4 - S: Sign Bit, S = N V The S-bit is always an exclusive or between the negative flag N and the two's complement overflow flag V. See the "Instruction Set Description" for detailed information. * Bit 3 - V: Two's Complement Overflow Flag The two's complement overflow flag V supports two's complement arithmetics. See the "Instruction Set Description" for detailed information. * Bit 2 - N: Negative Flag The negative flag N indicates a negative result in an arithmetic or logic operation. See the "Instruction Set Description" for detailed information. * Bit 1 - Z: Zero Flag The zero flag Z indicates a zero result in an arithmetic or logic operation. See the "Instruction Set Description" for detailed information. * Bit 0 - C: Carry Flag The carry flag C indicates a carry in an arithmetic or logic operation. See the "Instruction Set Description" for detailed information. 10 ATmega88/ATmega168 Automotive [DATASHEET] 9365A-AVR-02/16 4.5 General Purpose Register File The register file is optimized for the AVR(R) 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 4-2 shows the structure of the 32 general purpose working registers in the CPU. Figure 4-2. AVR CPU General Purpose Working Registers 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 4-2, each register is also assigned a data memory address, mapping them directly into the first 32 locations of the user data space. Although not being physically implemented as SRAM locations, this memory organization provides great flexibility in access of the registers, as the X-, Y- and Z-pointer registers can be set to index any register in the file. ATmega88/ATmega168 Automotive [DATASHEET] 9365A-AVR-02/16 11 4.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 4-3. Figure 4-3. The X-, Y-, and Z-registers 15 XH XL 0 7 0 7 0 X-register R27 (0x1B) R26 (0x1A) 15 YH YL 0 7 0 7 0 Y-register R29 (0x1D) R28 (0x1C) 15 ZH ZL 0 7 0 7 0 Z-register R31 (0x1F) R30 (0x1E) In the different addressing modes these address registers have functions as fixed displacement, automatic increment, and automatic decrement (see the instruction set reference for details). 4.6 Stack Pointer The stack is mainly used for storing temporary data, for storing local variables and for storing return addresses after interrupts and subroutine calls. The stack pointer register always points to the top of the stack. Note that the stack is implemented as growing from higher memory locations to lower memory locations. This implies that a stack PUSH command decreases the stack pointer. The stack pointer points to the data SRAM stack area where the subroutine and interrupt stacks are located. This stack space in the data SRAM must be defined by the program before any subroutine calls are executed or interrupts are enabled. The stack pointer must be set to point above 0x0100, preferably RAMEND. The stack pointer is decremented by one when data is pushed onto the stack with the PUSH instruction, and it is decremented by two when the return address is pushed onto the stack with subroutine call or interrupt. The stack pointer is incremented by one when data is popped from the stack with the POP instruction, and it is incremented by two when data is popped from the stack with return from subroutine RET or return from interrupt RETI. The AVR(R) stack pointer is implemented as two 8-bit registers in the I/O space. The number of bits actually used is implementation dependent. Note that the data space in some implementations of the AVR architecture is so small that only SPL is needed. In this case, the SPH register will not be present. Bit Read/Write Initial Value 15 14 13 12 11 10 9 8 SP15 SP14 SP13 SP12 SP11 SP10 SP9 SP8 SPH SP7 SP6 SP5 SP4 SP3 SP2 SP1 SP0 SPL 7 6 5 4 3 2 1 0 R/W R/W R/W R/W R/W R/W R/W R/W R/W R/W R/W R/W R/W R/W R/W R/W RAMEND RAMEND RAMEND RAMEND RAMEND RAMEND RAMEND RAMEND RAMEND RAMEND RAMEND RAMEND RAMEND RAMEND RAMEND RAMEND 12 ATmega88/ATmega168 Automotive [DATASHEET] 9365A-AVR-02/16 4.7 Instruction Execution Timing This section describes the general access timing concepts for instruction execution. The AVR(R) 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 4-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 4-4. The Parallel Instruction Fetches and Instruction Executions T1 T2 T3 T4 clkCPU 1st Instruction Fetch 1st Instruction Execute 2nd Instruction Fetch 2nd Instruction Execute 3rd Instruction Fetch 3rd Instruction Execute 4th Instruction Fetch Figure 4-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. Figure 4-5. Single Cycle ALU Operation T1 T2 T3 T4 clkCPU Total Execution Time Register Operands Fetch ALU Operation Execute Result Write Back 4.8 Reset and Interrupt Handling The AVR provides several different interrupt sources. These interrupts and the separate reset vector each have a separate program vector in the program memory space. All interrupts are assigned individual enable bits which must be written logic one together with the global interrupt enable bit in the status register in order to enable the interrupt. Depending on the program counter value, interrupts may be automatically disabled when boot lock bits BLB02 or BLB12 are programmed. This feature improves software security. See the Section 24. "Memory Programming" on page 234 for details. The lowest addresses in the program memory space are by default defined as the reset and interrupt vectors. The complete list of vectors is shown in Section 9. "Interrupts" on page 48. The list also determines the priority levels of the different interrupts. The lower the address the higher is the priority level. RESET has the highest priority, and next is INT0 - the external interrupt request 0. The interrupt vectors can be moved to the start of the boot flash section by setting the IVSEL bit in the MCU control register (MCUCR). Refer to Section 9. "Interrupts" on page 48 for more information. The reset vector can also be moved to the start of the boot flash section by programming the BOOTRST fuse, see Section 23. "Boot Loader Support - Read-While-Write Self-Programming, ATmega88 and ATmega168" on page 221. When an interrupt occurs, the global interrupt enable I-bit is cleared and all interrupts are disabled. The user software can write logic one to the I-bit to enable nested interrupts. All enabled interrupts can then interrupt the current interrupt routine. The I-bit is automatically set when a return from interrupt instruction - RETI - is executed. ATmega88/ATmega168 Automotive [DATASHEET] 9365A-AVR-02/16 13 There are basically two types of interrupts. The first type is triggered by an event that sets the interrupt flag. For these interrupts, the program counter is vectored to the actual interrupt vector in order to execute the interrupt handling routine, and hardware clears the corresponding interrupt flag. Interrupt flags can also be cleared by writing a logic one to the flag bit position(s) to be cleared. If an interrupt condition occurs while the corresponding interrupt enable bit is cleared, the interrupt flag will be set and remembered until the interrupt is enabled, or the flag is cleared by software. Similarly, if one or more interrupt conditions occur while the global interrupt enable bit is cleared, the corresponding interrupt flag(s) will be set and remembered until the global interrupt enable bit is set, and will then be executed by order of priority. The second type of interrupts will trigger as long as the interrupt condition is present. These interrupts do not necessarily have interrupt flags. If the interrupt condition disappears before the interrupt is enabled, the interrupt will not be triggered. When the AVR(R) 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 cli sbi sbi out r16, SREG EECR, EEMPE EECR, EEPE SREG, r16 ; store SREG value ; disable interrupts during timed sequence ; start EEPROM write ; restore SREG value (I-bit) C Code Example char cSREG; cSREG = SREG; /* store SREG value */ /* disable interrupts during timed sequence */ _CLI(); EECR |= (1< xxx ... ... ... ... ATmega88/ATmega168 Automotive [DATASHEET] 9365A-AVR-02/16 49 When the BOOTRST fuse is unprogrammed, the boot section size set to 2K bytes and the IVSEL bit in the MCUCR register is set before any interrupts are enabled, the most typical and general program setup for the reset and interrupt vector addresses in Atmel ATmega88 is: Address Labels Code Comments 0x000 RESET: ldi r16,high(RAMEND); Main program start 0x001 out SPH,r16 ; Set Stack Pointer to top of RAM 0x002 ldi r16,low(RAMEND) 0x003 out SPL,r16 0x004 sei ; Enable interrupts 0x005 xxx ; .org 0xC01 0xC01 rjmp EXT_INT0 ; IRQ0 Handler 0xC02 rjmp EXT_INT1 ; IRQ1 Handler ... ... ... ; 0xC19 rjmp SPM_RDY ; Store Program Memory Ready Handler When the BOOTRST fuse is programmed and the boot section size set to 2Kbytes, the most typical and general program setup for the reset and interrupt vector addresses in Atmel(R) ATmega88 is: Address Labels Code Comments .org 0x001 0x001 rjmp EXT_INT0 ; IRQ0 Handler 0x002 rjmp EXT_INT1 ; IRQ1 Handler ... ... ... ; 0x019 rjmp SPM_RDY ; Store Program Memory Ready Handler ; .org 0xC00 0xC00 RESET: ldi r16,high(RAMEND); Main program start 0xC01 out SPH,r16 ; Set Stack Pointer to top of RAM 0xC02 ldi r16,low(RAMEND) 0xC03 out SPL,r16 0xC04 sei ; Enable interrupts 0xC05 xxx When the BOOTRST fuse is programmed, the boot section size set to 2Kbytes and the IVSEL bit in the MCUCR register is set before any interrupts are enabled, the most typical and general program setup for the reset and interrupt vector addresses in Atmel ATmega88 is: Address ; .org 0xC00 0xC00 0xC01 0xC02 ... 0xC19 ; 0xC1A 0xC1B 0xC1C 0xC1D 0xC1E 0xC1F 50 Labels Code rjmp rjmp rjmp ... Comments RESET EXT_INT0 EXT_INT1 ... RESET: ldi r16,high(RAMEND); Main program start out SPH,r16 ; Set Stack Pointer to top of RAM ldi r16,low(RAMEND) out SPL,r16 sei ; Enable interrupts xxx ATmega88/ATmega168 Automotive [DATASHEET] 9365A-AVR-02/16 ; Reset handler ; IRQ0 Handler ; IRQ1 Handler ; rjmpSPM_RDY; Store Program Memory Ready Handler 9.2 Interrupt Vectors in ATmega168 Table 9-3. Reset and Interrupt Vectors in ATmega168 VectorNo. Program Address(2) Source Interrupt Definition 1 0x0000(1) RESET External pin, power-on reset, brown-out reset and watchdog system reset 2 0x0002 INT0 External interrupt request 0 3 0x0004 INT1 External interrupt request 1 4 0x0006 PCINT0 Pin change interrupt request 0 5 0x0008 PCINT1 Pin change interrupt request 1 6 0x000A PCINT2 Pin change interrupt request 2 7 0x000C WDT Watchdog time-out interrupt 8 0x000E TIMER2 COMPA Timer/Counter2 compare match A 9 0x0010 TIMER2 COMPB Timer/Counter2 compare match B 10 0x0012 TIMER2 OVF Timer/Counter2 overflow 11 0x0014 TIMER1 CAPT Timer/Counter1 capture event 12 0x0016 TIMER1 COMPA Timer/Counter1 compare match A 13 0x0018 TIMER1 COMPB Timer/coutner1 compare match B 14 0x001A TIMER1 OVF Timer/Counter1 overflow 15 0x001C TIMER0 COMPA Timer/Counter0 compare match A 16 0x001E TIMER0 COMPB Timer/Counter0 compare match B 17 0x0020 TIMER0 OVF Timer/Counter0 overflow 18 0x0022 SPI, STC SPI serial transfer complete 19 0x0024 USART, RX USART Rx complete 20 0x0026 USART, UDRE USART, data register empty 21 0x0028 USART, TX USART, Tx complete 22 0x002A ADC ADC conversion vomplete 23 0x002C EE READY EEPROM ready 24 0x002E ANALOG COMP Analog comparator 25 0x0030 TWI 2-wire serial interface 26 Notes: 1. 2. 0x0032 SPM READY Store program memory ready When the BOOTRST fuse is programmed, the device will jump to the boot loader address at reset, see Section 23. "Boot Loader Support - Read-While-Write Self-Programming, ATmega88 and ATmega168" on page 221. When the IVSEL bit in MCUCR is set, interrupt vectors will be moved to the start of the boot flash section. The address of each interrupt vector will then be the address in this table added to the start address of the boot flash Section. ATmega88/ATmega168 Automotive [DATASHEET] 9365A-AVR-02/16 51 Table 9-4 shows reset and interrupt vectors placement for the various combinations of BOOTRST and IVSEL settings. If the program never enables an interrupt source, the interrupt vectors are not used, and regular program code can be placed at these locations. This is also the case if the reset vector is in the application section while the Interrupt Vectors are in the boot section or vice versa. Table 9-4. Reset and Interrupt Vectors Placement in ATmega168(1) BOOTRST IVSEL 1 Note: Reset Address Interrupt Vectors Start Address 0 0x000 0x001 1 1 0x000 Boot Reset Address + 0x0002 0 0 Boot Reset Address 0x001 0 1. 1 Boot Reset Address Boot Reset Address + 0x0002 The boot reset address is shown in Table 23-6 on page 232. For the BOOTRST fuse "1" means unprogrammed while "0" means programmed. The most typical and general program setup for the reset and interrupt vector addresses in Atmel(R) ATmega168 is: Address Labels Code Comments 0x0000 jmp RESET ; Reset Handler 0x0002 jmp EXT_INT0 ; IRQ0 Handler 0x0004 jmp EXT_INT1 ; IRQ1 Handler 0x0006 jmp PCINT0 ; PCINT0 Handler 0x0008 jmp PCINT1 ; PCINT1 Handler 0x000A jmp PCINT2 ; PCINT2 Handler 0x000C jmp WDT ; Watchdog Timer Handler 0x000E jmp TIM2_COMPA ; Timer2 Compare A Handler 0x0010 jmp TIM2_COMPB ; Timer2 Compare B Handler 0x0012 jmp TIM2_OVF ; Timer2 Overflow Handler 0x0014 jmp TIM1_CAPT ; Timer1 Capture Handler 0x0016 jmp TIM1_COMPA ; Timer1 Compare A Handler 0x0018 jmp TIM1_COMPB ; Timer1 Compare B Handler 0x001A jmp TIM1_OVF ; Timer1 Overflow Handler 0x001C jmp TIM0_COMPA ; Timer0 Compare A Handler 0x001E jmp TIM0_COMPB ; Timer0 Compare B Handler 0x0020 jmp TIM0_OVF ; Timer0 Overflow Handler 0x0022 jmp SPI_STC ; SPI Transfer Complete Handler 0x0024 jmp USART_RXC ; USART, RX Complete Handler 0x0026 jmp USART_UDRE ; USART, UDR Empty Handler 0x0028 jmp USART_TXC ; USART, TX Complete Handler 0x002A jmp ADC ; ADC Conversion Complete Handler 0x002C jmp EE_RDY ; EEPROM Ready Handler 0x002E jmp ANA_COMP ; Analog Comparator Handler 0x0030 jmp TWI ; 2-wire Serial Interface Handler 0x0032 jmp SPM_RDY ; Store Program Memory Ready Handler ; 0x0033 RESET: ldi r16, high(RAMEND); Main program start 0x0034 r16 ; Set Stack Pointer to top of RAM 0x0035 ldi r16, low(RAMEND) 0x0036 out SPL,r16 0x0037 sei ; Enable interrupts 0x0038 xxx ... ... ... ... 52 ATmega88/ATmega168 Automotive [DATASHEET] 9365A-AVR-02/16 When the BOOTRST fuse is unprogrammed, the boot section size set to 2K bytes and the IVSEL bit in the MCUCR register is set before any interrupts are enabled, the most typical and general program setup for the reset and interrupt vector addresses in ATmega168 is: Address Labels Code Comments 0x0000 RESET: ldi r16,high(RAMEND); Main program start 0x0001 out SPH,r16 ; Set Stack Pointer to top of RAM 0x0002 ldi r16,low(RAMEND) 0x0003 out SPL,r16 0x0004 sei ; Enable interrupts 0x0005 xxx ; .org 0xC02 0x1C02 jmp EXT_INT0 ; IRQ0 Handler 0x1C04 jmp EXT_INT1 ; IRQ1 Handler ... ... ... ; 0x1C32 jmp SPM_RDY ; Store Program Memory Ready Handler When the BOOTRST fuse is programmed and the boot section size set to 2Kbytes, the most typical and general program setup for the reset and interrupt vector addresses in Atmel(R) ATmega168 is: Address Labels Code Comments .org 0x0002 0x0002 jmp EXT_INT0 ; IRQ0 Handler 0x0004 jmp EXT_INT1 ; IRQ1 Handler ... ... ... ; 0x0032 jmp SPM_RDY ; Store Program Memory Ready Handler ; .org 0x1C00 0x1C00 RESET: ldi r16,high(RAMEND); Main program start 0x1C01 out SPH,r16 ; Set Stack Pointer to top of RAM 0x1C02 ldi r16,low(RAMEND) 0x1C03 out SPL,r16 0x1C04 sei ; Enable interrupts 0x1C05 xxx When the BOOTRST Fuse is programmed, the Boot section size set to 2K bytes and the IVSEL bit in the MCUCR Register is set before any interrupts are enabled, the most typical and general program setup for the Reset and Interrupt Vector Addresses in ATmega168 is: Address Labels Code Comments ; .org 0x1C00 0x1C00 jmp RESET ; Reset handler 0x1C02 jmp EXT_INT0 ; IRQ0 Handler 0x1C04 jmp EXT_INT1 ; IRQ1 Handler ... ... ... ; 0x1C32 jmp SPM_RDY ; Store Program Memory Ready Handler ; 0x1C33 RESET: ldi r16,high(RAMEND); Main program start 0x1C34 out SPH,r16 ; Set Stack Pointer to top of RAM 0x1C35 ldi r16,low(RAMEND) 0x1C36 out SPL,r16 0x1C37 sei ; Enable interrupts 0x1C38 xxx ATmega88/ATmega168 Automotive [DATASHEET] 9365A-AVR-02/16 53 9.2.1 Moving Interrupts Between Application and Boot Space, ATmega88 and ATmega168 The MCU control register controls the placement of the interrupt vector table. 9.2.2 MCU Control Register - MCUCR Bit 7 6 5 4 3 2 1 0 - - - PUD - - IVSEL IVCE Read/Write R R R R/W R R R/W R/W Initial Value 0 0 0 0 0 0 0 0 MCUCR * Bit 1 - IVSEL: Interrupt Vector Select When the IVSEL bit is cleared (zero), the interrupt vectors are placed at the start of the flash memory. When this bit is set (one), the interrupt vectors are moved to the beginning of the boot loader section of the flash. The actual address of the start of the boot flash section is determined by the BOOTSZ fuses. Refer to the Section 23. "Boot Loader Support - Read-WhileWrite Self-Programming, ATmega88 and ATmega168" on page 221 for details. To avoid unintentional changes of interrupt vector tables, a special write procedure must be followed to change the IVSEL bit: 1. Write the interrupt vector change enable (IVCE) bit to one. 2. Within four cycles, write the desired value to IVSEL while writing a zero to IVCE. Interrupts will automatically be disabled while this sequence is executed. Interrupts are disabled in the cycle IVCE is set, and they remain disabled until after the instruction following the write to IVSEL. If IVSEL is not written, interrupts remain disabled for four cycles. The I-bit in the status register is unaffected by the automatic disabling. Note: If interrupt vectors are placed in the boot loader section and boot lock bit BLB02 is programmed, interrupts are disabled while executing from the Application section. If Interrupt Vectors are placed in the Application section and boot lock bit BLB12 is programed, interrupts are disabled while executing from the boot loader section. Refer to Section 23. "Boot Loader Support - Read-While-Write Self-Programming, ATmega88 and ATmega168" on page 221 for details on boot lock bits. * Bit 0 - IVCE: Interrupt Vector Change Enable The IVCE bit must be written to logic one to enable change of the IVSEL bit. IVCE is cleared by hardware four cycles after it is written or when IVSEL is written. Setting the IVCE bit will disable interrupts, as explained in the IVSEL description above. See code example below. Assembly Code Example Move_interrupts: ; Enable change of Interrupt Vectors ldi r16, (1< CSn2:0 > 1). The number of system clock cycles from when the timer is enabled to the first count occurs can be from 1 to N+1 system clock cycles, where N equals the prescaler divisor (8, 64, 256, or 1024). It is possible to use the prescaler reset for synchronizing the Timer/Counter to program execution. However, care must be taken if the other Timer/Counter that shares the same prescaler also uses prescaling. A prescaler reset will affect the prescaler period for all Timer/Counters it is connected to. 13.3 External Clock Source An external clock source applied to the T1/T0 pin can be used as Timer/Counter clock (clkT1/clkT0). The T1/T0 pin is sampled once every system clock cycle by the pin synchronization logic. The synchronized (sampled) signal is then passed through the edge detector. Figure 13-1 shows a functional equivalent block diagram of the T1/T0 synchronization and edge detector logic. The registers are clocked at the positive edge of the internal system clock (clkI/O). The latch is transparent in the high period of the internal system clock. The edge detector generates one clkT1/clkT0 pulse for each positive (CSn2:0 = 7) or negative (CSn2:0 = 6) edge it detects. Figure 13-1. T1/T0 Pin Sampling Tn D Q D Q D Tn_sync (to Clock Select Logic) Q LE clkI/O Synchronization Edge Detector The synchronization and edge detector logic introduces a delay of 2.5 to 3.5 system clock cycles from an edge has been applied to the T1/T0 pin to the counter is updated. Enabling and disabling of the clock input must be done when T1/T0 has been stable for at least one system clock cycle, otherwise it is a risk that a false Timer/Counter clock pulse is generated. 90 ATmega88/ATmega168 Automotive [DATASHEET] 9365A-AVR-02/16 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. Since 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 capacitors) tolerances, it is recommended that maximum frequency of an external clock source is less than fclk_I/O/2.5. An external clock source can not be prescaled. Figure 13-2. Prescaler for Timer/Counter0 and Timer/Counter1(1) 10-bit T/C Prescaler CK/64 PSRSYNC Note: T0 Synchronization T1 Synchronization 1. 0 CK/1024 CK/8 Clear CK/256 clkI/O 0 CS10 CS00 CS11 CS01 CS12 CS02 Timer/Counter 1Clock Source Timer/Counter 0 Clock Source clkT1 clkT0 The synchronization logic on the input pins (T1/T0) is shown in Figure 13-1 on page 90. 13.3.1 General Timer/Counter Control Register - GTCCR Bit 7 6 5 4 3 2 1 0 TSM - - - - - PSRASY PSRSYNC Read/Write R/W R R R R R R/W R/W Initial Value 0 0 0 0 0 0 0 0 GTCCR * Bit 7 - TSM: Timer/Counter Synchronization Mode Writing the TSM bit to one activates the Timer/Counter synchronization mode. In this mode, the value that is written to the PSRASY and PSRSYNC bits is kept, hence keeping the corresponding prescaler reset signals asserted. This ensures that the corresponding Timer/Counters are halted and can be configured to the same value without the risk of one of them advancing during configuration. When the TSM bit is written to zero, the PSRASY and PSRSYNC bits are cleared by hardware, and the Timer/Counters start counting simultaneously. * Bit 0 - PSRSYNC: Prescaler Reset When this bit is one, Timer/Counter1 and Timer/Counter0 prescaler will be reset. This bit is normally cleared immediately by hardware, except if the TSM bit is set. Note that Timer/Counter1 and Timer/Counter0 share the same prescaler and a reset of this prescaler will affect both timers. ATmega88/ATmega168 Automotive [DATASHEET] 9365A-AVR-02/16 91 14. 16-bit Timer/Counter1 with PWM The 16-bit Timer/Counter unit allows accurate program execution timing (event management), wave generation, and signal timing measurement. The main features are: True 16-bit Design (i.e., allows 16-bit PWM) 14.1 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) Overview 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 93. For the actual placement of I/O pins, refer to Section 1-1 "Pinout ATmega88/168" 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.10 "16-bit Timer/Counter Register Description" on page 111. The PRTIM1 bit in Section 7.7.1 "Power Reduction Register - PRR" on page 35 must be written to zero to enable Timer/Counter1 module. 92 ATmega88/ATmega168 Automotive [DATASHEET] 9365A-AVR-02/16 Figure 14-1. 16-bit Timer/Counter Block Diagram(1) TOVn (Int. Req.) Count Clear Direction Clock Select Control Logic clkTn TOP BOTTOM = = Edge Detector Tn (from Prescaler) Timer/Counter TCNTn 0 OCnA (Int. Req.) Waveform Generation = OCnA DATA BUS OCRnA Fixed TOP Value OCnB (Int. Req.) Waveform Generation = OCnB (From Analog Comparator Output) OCRnB ICFn (Int. Req.) Edge Detector ICRn TCCRnA Note: 1. Noise Canceler ICPn TCCRnB Refer to Figure 1-1 on page 3, Table 10-3 on page 62 and Table 10-9 on page 67 for Timer/Counter1 pin placement and description. 14.1.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.2 "Accessing 16-bit Registers" on page 94. The Timer/Counter control registers (TCCR1A/B) are 8-bit registers and have no CPU access restrictions. Interrupt requests (abbreviated to int.req. in the figure) signals are all visible in the timer interrupt flag register (TIFR1). All interrupts are individually masked with the timer interrupt mask register (TIMSK1). TIFR1 and TIMSK1 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). ATmega88/ATmega168 Automotive [DATASHEET] 9365A-AVR-02/16 93 The double buffered output compare registers (OCR1A/B) are compared with the Timer/Counter value at all time. The result of the compare can be used by the waveform generator to generate a PWM or variable frequency output on the output compare pin (OC1A/B). See Section 14.6 "Output Compare Units" on page 100. The compare match event will also set the compare match flag (OCF1A/B) which can be used to generate an output compare interrupt request. 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 20. "Analog Comparator" on page 201) The input capture unit includes a digital filtering unit (noise canceler) for reducing the chance of capturing noise spikes. The TOP value, or maximum Timer/Counter value, can in some modes of operation be defined by either the OCR1A register, the ICR1 register, or by a set of fixed values. When using OCR1A as TOP value in a PWM mode, the OCR1A register can not be used for generating a PWM output. However, the TOP value will in this case be double buffered allowing the TOP value to be changed in run time. If a fixed TOP value is required, the ICR1 register can be used as an alternative, freeing the OCR1A to be used as PWM output. 14.1.2 Definitions The following definitions are used extensively throughout the section: Table 14-1. Definitions 14.2 Parameter Definitions BOTTOM The counter reaches the BOTTOM when it becomes 0x0000. MAX The counter reaches its MAXimum when it becomes 0xFFFF (decimal 65535). TOP The counter reaches the TOP when it becomes equal to the highest value in the count sequence. The TOP value can be assigned to be one of the fixed values: 0x00FF, 0x01FF, or 0x03FF, or to the value stored in the OCR1A or ICR1 register. The assignment is dependent of the mode of operation. Accessing 16-bit Registers The TCNT1, OCR1A/B, and ICR1 are 16-bit registers that can be accessed by the AVR CPU via the 8-bit data bus. The 16-bit register must be byte accessed using two read or write operations. Each 16-bit timer has a single 8-bit register for temporary storing of the high byte of the 16-bit access. The same temporary register is shared between all 16-bit registers within each 16-bit timer. Accessing the low byte triggers the 16-bit read or write operation. When the low byte of a 16-bit register is written by the CPU, the high byte stored in the temporary register, and the low byte written are both copied into the 16-bit register in the same clock cycle. When the low byte of a 16-bit register is read by the CPU, the high byte of the 16-bit register is copied into the temporary register in the same clock cycle as the low byte is read. Not all 16-bit accesses uses the temporary register for the high byte. Reading the OCR1A/B 16-bit registers does not involve using the temporary register. To do a 16-bit write, the high byte must be written before the low byte. For a 16-bit read, the low byte must be read before the high byte. 94 ATmega88/ATmega168 Automotive [DATASHEET] 9365A-AVR-02/16 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. The example code assumes that the part specific header file is included. For I/O registers located in extended I/O map, "IN", "OUT", "SBIS", "SBIC", "CBI", and "SBI" instructions must be replaced with instructions that allow access to extended I/O. Typically "LDS" and "STS" combined with "SBRS", "SBRC", "SBR", and "CBR". The assembly code example returns the TCNT1 value in the r17:r16 register pair. It is important to notice that accessing 16-bit registers are atomic operations. If an interrupt occurs between the two instructions accessing the 16-bit register, and the interrupt code updates the temporary register by accessing the same or any other of the 16-bit timer registers, then the result of the access outside the interrupt will be corrupted. Therefore, when both the main code and the interrupt code update the temporary register, the main code must disable the interrupts during the 16-bit access. ATmega88/ATmega168 Automotive [DATASHEET] 9365A-AVR-02/16 95 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. The example code assumes that the part specific header file is included. For I/O registers located in extended I/O map, "IN", "OUT", "SBIS", "SBIC", "CBI", and "SBI" instructions must be replaced with instructions that allow access to extended I/O. Typically "LDS" and "STS" combined with "SBRS", "SBRC", "SBR", and "CBR". The assembly code example returns the TCNT1 value in the r17:r16 register pair. 96 ATmega88/ATmega168 Automotive [DATASHEET] 9365A-AVR-02/16 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. The example code assumes that the part specific header file is included. For I/O registers located in extended I/O map, "IN", "OUT", "SBIS", "SBIC", "CBI", and "SBI" instructions must be replaced with instructions that allow access to extended I/O. Typically "LDS" and "STS" combined with "SBRS", "SBRC", "SBR", and "CBR". The assembly code example requires that the r17:r16 register pair contains the value to be written to TCNT1. 14.2.1 Reusing the Temporary High Byte Register If writing to more than one 16-bit register where the high byte is the same for all registers written, then the high byte only needs to be written once. However, note that the same rule of atomic operation described previously also applies in this case. 14.3 Timer/Counter Clock Sources The Timer/Counter can be clocked by an internal or an external clock source. The clock source is selected by the clock select logic which is controlled by the clock select (CS12:0) bits located in the Timer/Counter control register B (TCCR1B). For details on clock sources and prescaler, see Section 13. "Timer/Counter0 and Timer/Counter1 Prescalers" on page 90. ATmega88/ATmega168 Automotive [DATASHEET] 9365A-AVR-02/16 97 14.4 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. Figure 14-2. Counter Unit Block Diagram DATA BUS (8-bit) TOVn (Int. Req.) TEMP (8-bit) Clock Select Count TCNTnH (8-bit) TCNTnL (8-bit) TCNTnH (16-bit Counter) Clear Control Logic clkTn Edge Detector Tn Direction (from Prescaler) TOP BOTTOM Signal description (internal signals): Count Increment or decrement TCNT1 by 1. Direction Select between increment and decrement. Clear Clear TCNT1 (set all bits to zero). clkT1 Timer/Counter clock. TOP Signalize that TCNT1 has reached maximum value. BOTTOM Signalize that TCNT1 has reached minimum value (zero). The 16-bit counter is mapped into two 8-bit I/O memory locations: counter high (TCNT1H) containing the upper eight bits of the counter, and counter low (TCNT1L) containing the lower eight bits. The TCNT1H register can only be indirectly accessed by the CPU. When the CPU does an access to the TCNT1H I/O location, the CPU accesses the high byte temporary register (TEMP). The temporary register is updated with the TCNT1H value when the TCNT1L is read, and TCNT1H is updated with the temporary register value when TCNT1L is written. This allows the CPU to read or write the entire 16-bit counter value within one clock cycle via the 8-bit data bus. It is important to notice that there are special cases of writing to the TCNT1 register when the counter is counting that will give unpredictable results. The special cases are described in the sections where they are of importance. Depending on the mode of operation used, the counter is cleared, incremented, or decremented at each timer clock (clkT1). The clkT1 can be generated from an external or internal clock source, selected by the clock select bits (CS12:0). When no clock source is selected (CS12:0 = 0) the timer is stopped. However, the TCNT1 value can be accessed by the CPU, independent of whether clkT1 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 waveform generation mode bits (WGM13:0) located in the Timer/Counter control registers A and B (TCCR1A and TCCR1B). There are close connections between how the counter behaves (counts) and how waveforms are generated on the output compare outputs OC1x. For more details about advanced counting sequences and waveform generation, see Section 14.8 "Modes of Operation" on page 103. The Timer/Counter overflow flag (TOV1) is set according to the mode of operation selected by the WGM13:0 bits. TOV1 can be used for generating a CPU interrupt. 98 ATmega88/ATmega168 Automotive [DATASHEET] 9365A-AVR-02/16 14.5 Input Capture Unit The Timer/Counter incorporates an input capture unit that can capture external events and give them a time-stamp indicating time of occurrence. The external signal indicating an event, or multiple events, can be applied via the ICP1 pin or alternatively, via the analog-comparator unit. The time-stamps can then be used to calculate frequency, duty-cycle, and other features of the signal applied. Alternatively the time-stamps can be used for creating a log of the events. The input capture unit is illustrated by the block diagram shown in Figure 14-3. The elements of the block diagram that are not directly a part of the input capture unit are gray shaded. The small "n" in register and bit names indicates the Timer/Counter number. Figure 14-3. Input Capture Unit Block Diagram DATA BUS (8-bit) TEMP (8-bit) ICRnH (8-bit) ICRnL (8-bit) TCNTnH (8-bit) ICRn (16-bit Register) WRITE + - ACO* Analog Comparator TCNTnL (8-bit) TCNTn (16-bit Counter) ACIC* ICNC ICES Noise Canceler Edge Detector ICFn (Int. Req.) ICPn When a change of the logic level (an event) occurs on the input capture pin (ICP1), alternatively on the analog comparator output (ACO), and this change confirms to the setting of the edge detector, a capture will be triggered. When a capture is triggered, the 16-bit value of the counter (TCNT1) is written to the input capture register (ICR1). The input capture flag (ICF1) is set at the same system clock as the TCNT1 value is copied into ICR1 Register. If enabled (ICIE1 = 1), the Input Capture flag generates an input capture interrupt. The ICF1 flag is automatically cleared when the interrupt is executed. Alternatively the ICF1 flag can be cleared by software by writing a logical one to its I/O bit location. 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.2 "Accessing 16-bit Registers" on page 94. ATmega88/ATmega168 Automotive [DATASHEET] 9365A-AVR-02/16 99 14.5.1 Input Capture Trigger Source The main trigger source for the input capture unit is the input capture pin (ICP1). Timer/Counter1 can alternatively use the analog comparator output as trigger source for the Input Capture unit. The Analog Comparator is selected as trigger source by setting the analog comparator input capture (ACIC) bit in the analog comparator control and status register (ACSR). Be aware that changing trigger source can trigger a capture. The input capture flag must therefore be cleared after the change. Both the input capture pin (ICP1) and the analog comparator output (ACO) inputs are sampled using the same technique as for the T1 pin (Figure 13-1 on page 90). The edge detector is also identical. However, when the noise canceler is enabled, additional logic is inserted before the edge detector, which increases the delay by four system clock cycles. Note that the input of the noise canceler and edge detector is always enabled unless the Timer/Counter is set in a waveform generation mode that uses ICR1 to define TOP. An input capture can be triggered by software by controlling the port of the ICP1 pin. 14.5.2 Noise Canceler The noise canceler improves noise immunity by using a simple digital filtering scheme. The noise canceler input is monitored over four samples, and all four must be equal for changing the output that in turn is used by the edge detector. The noise canceler is enabled by setting the input capture noise canceler (ICNC1) bit in Timer/Counter control register B (TCCR1B). When enabled the noise canceler introduces additional four system clock cycles of delay from a change applied to the input, to the update of the ICR1 register. The noise canceler uses the system clock and is therefore not affected by the prescaler. 14.5.3 Using the Input Capture Unit The main challenge when using the input capture unit is to assign enough processor capacity for handling the incoming events. The time between two events is critical. If the processor has not read the captured value in the ICR1 register before the next event occurs, the ICR1 will be overwritten with a new value. In this case the result of the capture will be incorrect. When using the input capture interrupt, the ICR1 register should be read as early in the interrupt handler routine as possible. Even though the input capture interrupt has relatively high priority, the maximum interrupt response time is dependent on the maximum number of clock cycles it takes to handle any of the other interrupt requests. Using the input capture unit in any mode of operation when the TOP value (resolution) is actively changed during operation, is not recommended. Measurement of an external signal's duty cycle requires that the trigger edge is changed after each capture. Changing the edge sensing must be done as early as possible after the ICR1 register has been read. After a change of the edge, the input capture flag (ICF1) must be cleared by software (writing a logical one to the I/O bit location). For measuring frequency only, the clearing of the ICF1 flag is not required (if an interrupt handler is used). 14.6 Output Compare Units The 16-bit comparator continuously compares TCNT1 with the output compare register (OCR1x). If TCNT equals OCR1x the comparator signals a match. A match will set the output compare flag (OCF1x) at the next timer clock cycle. If enabled (OCIE1x = 1), the output compare flag generates an output compare interrupt. The OCF1x flag is automatically cleared when the interrupt is executed. Alternatively the OCF1x flag can be cleared by software by writing a logical one to its I/O bit location. The waveform generator uses the match signal to generate an output according to operating mode set by the waveform generation mode (WGM13:0) bits and compare output mode (COM1x1:0) bits. The TOP and BOTTOM signals are used by the waveform generator for handling the special cases of the extreme values in some modes of operation (See Section 14.8 "Modes of Operation" on page 103) A special feature of output compare unit A allows it to define the Timer/Counter TOP value (i.e., counter resolution). In addition to the counter resolution, the TOP value defines the period time for waveforms generated by the waveform generator. 100 ATmega88/ATmega168 Automotive [DATASHEET] 9365A-AVR-02/16 Figure 14-4 shows a block diagram of the output compare unit. The small "n" in the register and bit names indicates the device number (n = 1 for Timer/Counter 1), and the "x" indicates output compare unit (A/B). The elements of the block diagram that are not directly a part of the output compare unit are gray shaded. 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) OCRnx Buffer (16-bit Register) OCRnxH (8-bit) TCNTnL (8-bit) TCNTn (16-bit Counter) OCRnxL (8-bit) OCRnx (16-bit Register) = (16-bitComparator) OCFnx (Int. Req.) 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 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 only changed by a write operation (the Timer/Counter does not update this register automatically as the TCNT1 and ICR1 register). 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 since 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 OCR1x compare register in the same system clock cycle. For more information of how to access the 16-bit registers refer to Section 14.2 "Accessing 16-bit Registers" on page 94. 14.6.1 Force Output Compare In non-PWM waveform generation modes, the match output of the comparator can be forced by writing a one to the force output compare (FOC1x) bit. Forcing compare match will not set the OCF1x flag or reload/clear the timer, but the OC1x pin will be updated as if a real compare match had occurred (the COM11:0 bits settings define whether the OC1x pin is set, cleared or toggled). ATmega88/ATmega168 Automotive [DATASHEET] 9365A-AVR-02/16 101 14.6.2 Compare Match Blocking by TCNT1 Write All CPU writes to the TCNT1 register will block any compare match that occurs in the next timer clock cycle, even when the timer is stopped. This feature allows OCR1x to be initialized to the same value as TCNT1 without triggering an interrupt when the Timer/Counter clock is enabled. 14.6.3 Using the Output Compare Unit Since writing TCNT1 in any mode of operation will block all compare matches for one timer clock cycle, there are risks involved when 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 downcounting. 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 (FOC1x) 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. 14.7 Compare Match Output Unit The compare output mode (COM1x1:0) bits have two functions. The waveform generator uses the COM1x1:0 bits for defining the output compare (OC1x) state at the next compare match. Secondly the COM1x1:0 bits control the OC1x pin output source. Figure 14-5 shows a simplified schematic of the logic affected by the COM1x1:0 bit setting. The I/O registers, I/O bits, and I/O pins in the figure are shown in bold. Only the parts of the general I/O port control registers (DDR and PORT) that are affected by the COM1x1:0 bits are shown. When referring to the OC1x state, the reference is for the internal OC1x register, not the OC1x pin. If a system reset occur, the OC1x register is reset to "0". Figure 14-5. Compare Match Output Unit, Schematic COMnx1 COMnx0 FOCn Waveform Generator D Q 1 OCnx Pin OCnx 0 DATA BUS D Q PORT D Q DDR clkI/O 102 ATmega88/ATmega168 Automotive [DATASHEET] 9365A-AVR-02/16 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. Refer to Table 14-2 on page 111, Table 14-3 on page 112 and Table 14-4 on page 112 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.10 "16-bit Timer/Counter Register Description" on page 111 The COM1x1:0 bits have no effect on the input capture unit. 14.7.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 111. For fast PWM mode refer to Table 14-3 on page 112, and for phase correct and phase and frequency correct PWM refer to Table 14-4 on page 112. A change of the COM1x1:0 bits state will have effect at the first compare match after the bits are written. For non-PWM modes, the action can be forced to have immediate effect by using the FOC1x strobe bits. 14.8 Modes of Operation The mode of operation, i.e., the behavior of the Timer/Counter and the output compare pins, is defined by the combination of the waveform generation mode (WGM13:0) and compare output mode (COM1x1:0) bits. The compare output mode bits do not affect the counting sequence, while the waveform generation mode bits do. The COM1x1:0 bits control whether the PWM output generated should be inverted or not (inverted or non-inverted PWM). For non-PWM modes the COM1x1:0 bits control whether the output should be set, cleared or toggle at a compare match (see Section 14.7 "Compare Match Output Unit" on page 102) For detailed timing information refer to Section 14.9 "Timer/Counter Timing Diagrams" on page 109. 14.8.1 Normal Mode The simplest mode of operation is the normal mode (WGM13: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 16-bit value (MAX = 0xFFFF) and then restarts from the BOTTOM (0x0000). In normal operation the Timer/Counter overflow flag (TOV1) will be set in the same timer clock cycle as the TCNT1 becomes zero. The TOV1 flag in this case behaves like a 17th bit, except that it is only set, not cleared. However, combined with the timer overflow interrupt that automatically clears the TOV1 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 input capture unit is easy to use in normal mode. However, observe that the maximum interval between the external events must not exceed the resolution of the counter. If the interval between events are too long, the timer overflow interrupt or the prescaler must be used to extend the resolution for the capture unit. The output compare units 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 of the CPU time. 14.8.2 Clear Timer on Compare Match (CTC) Mode In clear timer on compare or CTC mode (WGM13:0 = 4 or 12), the OCR1A or ICR1 register are used to manipulate the counter resolution. In CTC mode the counter is cleared to zero when the counter value (TCNT1) matches either the OCR1A (WGM13:0 = 4) or the ICR1 (WGM13:0 = 12). The OCR1A or ICR1 define the top value for the counter, hence also its resolution. This mode allows greater control of the compare match output frequency. It also simplifies the operation of counting external events. The timing diagram for the CTC mode is shown in Figure 14-6 on page 104. The counter value (TCNT1) increases until a compare match occurs with either OCR1A or ICR1, and then counter (TCNT1) is cleared. ATmega88/ATmega168 Automotive [DATASHEET] 9365A-AVR-02/16 103 Figure 14-6. CTC Mode, Timing Diagram OCnA Interrupt Flag Set or ICFn Interrupt Flag Set (Interrupt on TOP) TCNTn OCnA (Toggle) Period (COMnA1:0 = 1) 1 2 3 4 An interrupt can be generated at 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 none or a low prescaler value must be done with care since the CTC mode does not have the double buffering feature. If the new value written to 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) since the 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 fOC1A = 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 N variable represents the prescaler factor (1, 8, 64, 256, or 1024). As for the normal mode of operation, the TOV1 flag is set in the same timer clock cycle that the counter counts from MAX to 0x0000. 14.8.3 Fast PWM Mode The fast pulse width modulation or fast PWM mode (WGM13:0 = 5, 6, 7, 14, or 15) provides a high frequency PWM waveform generation option. The fast PWM differs from the other PWM options by its single-slope operation. The counter counts from BOTTOM to TOP then restarts from BOTTOM. In non-inverting compare output mode, the output compare (OC1x) is set on the compare match between TCNT1 and OCR1x, and cleared at TOP. In inverting compare output mode output is cleared on compare match and set at TOP. Due to the single-slope operation, the operating frequency of the fast PWM mode can be twice as high as the phase correct and phase and frequency correct PWM modes that use dual-slope operation. This high frequency makes the fast PWM mode well suited for power regulation, rectification, and DAC applications. High frequency allows physically small sized external components (coils, capacitors), hence reduces total system cost. The PWM resolution for fast PWM can be fixed to 8-, 9-, or 10-bit, or defined by either ICR1 or OCR1A. The minimum resolution allowed is 2-bit (ICR1 or OCR1A set to 0x0003), and the maximum resolution is 16-bit (ICR1 or OCR1A set to MAX). The PWM resolution in bits can be calculated by using the following equation: log TOP + 1 R FPWM = ---------------------------------log 2 104 ATmega88/ATmega168 Automotive [DATASHEET] 9365A-AVR-02/16 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. The figure shows fast PWM mode when OCR1A or ICR1 is used to define TOP. The TCNT1 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 TCNT1 slopes represent compare matches between OCR1x and TCNT1. The OC1x interrupt flag will be set when a compare match occurs. Figure 14-7. Fast PWM Mode, Timing Diagram OCRnx/ TOP Update and TOVn Interrupt Flag Set and OCnA Interrupt Flag Set or ICFn Interrupt Flag Set (Interrupt on TOP) TCNTn OCnx (COMnx1:0 = 2) OCnx (COMnx1:0 = 3) Period 1 2 3 4 5 6 7 8 The Timer/Counter overflow flag (TOV1) is set each time the counter reaches TOP. In addition the OC1A or ICF1 flag is set at the same timer clock cycle as 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 the TCNT1 and the 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 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 none 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 the TCNT1 matches TOP. The update is done at the same timer clock cycle as the 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. 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 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 output can be generated by setting the COM1x1:0 to three (see Table on page 112). 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 at the timer clock cycle the counter is cleared (changes from TOP to BOTTOM). ATmega88/ATmega168 Automotive [DATASHEET] 9365A-AVR-02/16 105 The PWM frequency for the output can be calculated by the following equation: f clk_I/O f OCnxPWM = ---------------------------------N 1 + TOP The N variable represents the prescaler divider (1, 8, 64, 256, or 1024). The extreme values for the OCR1x register represents 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 the 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 (with 50% duty cycle) waveform output in fast PWM mode can be achieved by setting OC1A to toggle its logical level on each compare match (COM1A1:0 = 1). This applies only if OCR1A is used to define the TOP value (WGM13:0 = 15). The waveform generated will have a maximum frequency of fOC1A = 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. 14.8.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 upcounting, and set on the compare match while downcounting. 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-bit, or defined by either ICR1 or OCR1A. The minimum resolution allowed is 2-bit (ICR1 or OCR1A set to 0x0003), and the maximum resolution is 16-bit (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 on page 107. The figure shows phase correct PWM mode when OCR1A or ICR1 is used to define TOP. The TCNT1 value is in the timing diagram shown as a histogram for illustrating the dual-slope operation. The diagram includes non-inverted and inverted PWM outputs. The small horizontal line marks on the TCNT1 slopes represent compare matches between OCR1x and TCNT1. The OC1x interrupt flag will be set when a compare match occurs. 106 ATmega88/ATmega168 Automotive [DATASHEET] 9365A-AVR-02/16 Figure 14-8. Phase Correct PWM Mode, Timing Diagram OCRnx/ TOP Update and OCnA Interrupt Flag Set or ICFn Interrupt Flag Set (Interrupt on TOP) TOVn Interrupt Flag Set (Interrupt on Bottom) TCNTn OCnx (COMnx1:0 = 2) OCnx (COMnx1:0 = 3) Period 1 2 3 4 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 at the same timer clock cycle as 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. 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 the TCNT1 and the 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 illustrates, changing the TOP actively while the Timer/Counter is running in the phase correct mode can result in an unsymmetrical 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 unsymmetrical 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 on page 112). 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 N variable 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. If OCR1A is used to define the TOP value (WGM13:0 = 11) and COM1A1:0 = 1, the OC1A output will toggle with a 50% duty cycle. ATmega88/ATmega168 Automotive [DATASHEET] 9365A-AVR-02/16 107 14.8.5 Phase and Frequency Correct PWM Mode The phase and frequency correct pulse width modulation, or phase and frequency correct PWM mode (WGM13:0 = 8 or 9) provides a high resolution phase and frequency correct PWM waveform generation option. The phase and frequency correct PWM mode is, like the phase 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 upcounting, and set on the compare match while downcounting. In inverting compare output mode, the operation is inverted. The dual-slope operation gives a lower maximum operation frequency compared to the single-slope operation. However, due to the symmetric feature of the dual-slope PWM modes, these modes are preferred for motor control applications. The main difference between the phase correct, and the phase and frequency correct PWM mode is the time the OCR1x register is updated by the OCR1x buffer register, (see Figure 14-8 on page 107 and Figure 14-9). The PWM resolution for the phase and frequency correct PWM mode can be defined by either ICR1 or OCR1A. The minimum resolution allowed is 2-bit (ICR1 or OCR1A set to 0x0003), and the maximum resolution is 16-bit (ICR1 or OCR1A set to MAX). The PWM resolution in bits can be calculated using the following equation: 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 correct and frequency correct PWM mode is shown on Figure 14-9. The figure shows phase and frequency correct PWM mode when OCR1A or ICR1 is used to define TOP. The TCNT1 value is in the timing diagram shown as a histogram for illustrating the dual-slope operation. The diagram includes non-inverted and inverted PWM outputs. The small horizontal line marks on the TCNT1 slopes represent compare matches between OCR1x and TCNT1. The OC1x interrupt flag will be set when a compare match occurs. Figure 14-9. Phase and Frequency Correct PWM Mode, Timing Diagram OCnA Interrupt Flag Set or ICFn Interrupt Flag Set (Interrupt on TOP) OCRnx/ TOP Update and TOVn Interrupt Flag Set (Interrupt on Bottom) TCNTn OCnx (COMnx1:0 = 2) OCnx (COMnx1:0 = 3) Period 1 2 3 4 The Timer/Counter overflow flag (TOV1) is set at the same timer clock cycle as 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 set 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. 108 ATmega88/ATmega168 Automotive [DATASHEET] 9365A-AVR-02/16 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 the TCNT1 and the OCR1x. As Figure 14-9 on page 108 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-3 on page 112). 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 N variable represents the prescaler divider (1, 8, 64, 256, or 1024). The extreme values for the OCR1x register represents 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 set to high for non-inverted PWM mode. For inverted PWM the output will have the opposite logic values. If OCR1A is used to define the TOP value (WGM13:0 = 9) and COM1A1:0 = 1, the OC1A output will toggle with a 50% duty cycle. 14.9 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. Figure 14-10.Timer/Counter Timing Diagram, Setting of OCF1x, no Prescaling clkI/O clkTn (clkI/O/1) TCNTn OCRnx OCRnx - 1 OCRnx OCRnx + 1 OCRnx + 2 OCRnx Value OCFnx ATmega88/ATmega168 Automotive [DATASHEET] 9365A-AVR-02/16 109 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) OCRnx - 1 TCNTn OCRnx OCRnx OCRnx + 1 OCRnx + 2 OCRnx Value OCFnx 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. Figure 14-12.Timer/Counter Timing Diagram, no Prescaling 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) OCRnx (Update at TOP) 110 Old OCRnx Value ATmega88/ATmega168 Automotive [DATASHEET] 9365A-AVR-02/16 New OCRnx Value Figure 14-13 shows the same timing data, but with the prescaler enabled. Figure 14-13.Timer/Counter Timing Diagram, with Prescaler (fclk_I/O/8) 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) OCRnx (Update at TOP) Old OCRnx Value New OCRnx Value 14.10 16-bit Timer/Counter Register Description 14.10.1 Timer/Counter1 Control Register A - TCCR1A Bit 7 6 5 4 3 2 1 0 COM1A1 COM1A0 COM1B1 COM1B0 - - WGM11 WGM10 Read/Write R/W R/W R/W R/W R R R/W R/W Initial Value 0 0 0 0 0 0 0 0 TCCR1A * Bit 7:6 - COM1A1:0: Compare Output Mode for Channel A * Bit 5:4 - COM1B1:0: Compare Output Mode for Channel B The COM1A1:0 and COM1B1:0 control the output compare pins (OC1A and OC1B respectively) behavior. If one or both of the COM1A1:0 bits are written to one, the OC1A output overrides the normal port functionality of the I/O pin it is connected to. If one or both of the COM1B1:0 bit are written to one, the OC1B 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 OC1A or OC1B pin must be set in order to enable the output driver. When the OC1A or OC1B is connected to the pin, the function of the COM1x1:0 bits is dependent of the WGM13:0 bits setting. Table 14-2 shows the COM1x1:0 bit functionality when the WGM13:0 bits are set to a normal or a CTC mode (non-PWM). Table 14-2. Compare Output Mode, non-PWM COM1A1/COM1B1 COM1A0/COM1B0 Description 0 0 Normal port operation, OC1A/OC1B disconnected. 0 1 Toggle OC1A/OC1B on compare match. 1 0 Clear OC1A/OC1B on compare match (set output to low level). 1 1 Set OC1A/OC1B on compare match (set output to high level). ATmega88/ATmega168 Automotive [DATASHEET] 9365A-AVR-02/16 111 Table 14-3 shows the COM1x1:0 bit functionality when the WGM13:0 bits are set to the fast PWM mode. Table 14-3. Compare Output Mode, Fast PWM(1) COM1A1/COM1B1 COM1A0/COM1B0 0 0 Normal port operation, OC1A/OC1B disconnected. 0 1 WGM13:0 = 14 or 15: Toggle OC1A on compare match, OC1B disconnected (normal port operation). For all other WGM1 settings, normal port operation, OC1A/OC1B disconnected. 1 0 Clear OC1A/OC1B on compare match, set OC1A/OC1B at TOP Note: 1. Description 1 1 Set OC1A/OC1B on compare match, clear OC1A/OC1B at TOP A special case occurs when OCR1A/OCR1B equals TOP and COM1A1/COM1B1 is set. In this case the compare match is ignored, but the set or clear is done at TOP. See Section 14.8.3 "Fast PWM Mode" on page 104 for more details. 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) COM1A1/COM1B1 COM1A0/COM1B0 0 0 Normal port operation, OC1A/OC1B disconnected. 0 1 WGM13:0 = 8, 9, 10 or 11: Toggle OC1A on compare match, OC1B disconnected (normal port operation). For all other WGM1 settings, normal port operation, OC1A/OC1B disconnected. 1 0 Clear OC1A/OC1B on compare match when up-counting. Set OC1A/OC1B on compare match when downcounting. Set OC1A/OC1B on compare match when up-counting. Clear OC1A/OC1B on compare match when downcounting. A special case occurs when OCR1A/OCR1B equals TOP and COM1A1/COM1B1 is set. See Section 14.8.4 "Phase Correct PWM Mode" on page 106 for more details. 1 Note: 1. Description 1 * Bit 1:0 - WGM11:0: Waveform Generation Mode Combined with the WGM13:2 bits found in the TCCR1B register, these bits control the counting sequence of the counter, the source for maximum (TOP) counter value, and what type of waveform generation to be used, see Table 14-5. Modes of operation supported by the Timer/Counter unit are: Normal mode (counter), clear timer on compare match (CTC) mode, and three types of pulse width modulation (PWM) modes. (See Section 14.8 "Modes of Operation" on page 103). Table 14-5. Waveform Generation Mode Bit Description(1) Mode WGM13 WGM12 (CTC1) WGM11 (PWM11) TOP Update of OCR1x at TOV1 Flag Set on 0 0 0 0 0 Normal 0xFFFF Immediate MAX 1 0 0 0 2 0 0 1 1 PWM, phase correct, 8-bit 0x00FF TOP BOTTOM 0 PWM, phase correct, 9-bit 0x01FF TOP BOTTOM 3 0 0 1 1 PWM, phase correct, 10-bit 0x03FF TOP BOTTOM 4 0 1 0 0 CTC OCR1A Immediate MAX 5 6 0 1 0 1 Fast PWM, 8-bit 0x00FF TOP TOP 0 1 1 0 Fast PWM, 9-bit 0x01FF TOP TOP 7 0 1 1 1 Fast PWM, 10-bit 0x03FF TOP TOP 8 1 Note: 112 1. WGM10 Timer/Counter Mode of (PWM10) Operation PWM, phase and frequency ICR1 BOTTOM BOTTOM correct The CTC1 and PWM11:0 bit definition names are obsolete. Use the WGM12:0 definitions. However, the functionality and location of these bits are compatible with previous versions of the timer. 0 0 0 ATmega88/ATmega168 Automotive [DATASHEET] 9365A-AVR-02/16 Table 14-5. Waveform Generation Mode Bit Description(1) (Continued) Mode WGM13 WGM12 (CTC1) WGM11 (PWM11) WGM10 Timer/Counter Mode of (PWM10) Operation Update of OCR1x at TOV1 Flag Set on 9 1 0 0 1 PWM, phase and frequency OCR1A correct BOTTOM BOTTOM 10 1 0 1 0 PWM, phase correct ICR1 TOP BOTTOM TOP 11 1 0 1 1 PWM, phase correct OCR1A TOP BOTTOM 12 1 1 0 0 CTC ICR1 Immediate MAX 13 1 1 0 1 (Reserved) - - - 14 1 1 1 0 Fast PWM ICR1 TOP TOP 15 Note: 1. 1 1 1 1 Fast PWM OCR1A TOP TOP The CTC1 and PWM11:0 bit definition names are obsolete. Use the WGM12:0 definitions. However, the functionality and location of these bits are compatible with previous versions of the timer. 14.10.2 Timer/Counter1 Control Register B - TCCR1B Bit 7 6 5 4 3 2 1 0 ICNC1 ICES1 - WGM13 WGM12 CS12 CS11 CS10 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 TCCR1B * Bit 7 - ICNC1: Input Capture Noise Canceler Setting this bit (to one) activates the input capture noise canceler. When the noise canceler is activated, the input from the input capture pin (ICP1) is filtered. The filter function requires four successive equal valued samples of the ICP1 pin for changing its output. The input capture is therefore delayed by four oscillator cycles when the noise canceler is enabled. * Bit 6 - ICES1: Input Capture Edge Select This bit selects which edge on the input capture pin (ICP1) that is used to trigger a capture event. When the ICES1 bit is written to zero, a falling (negative) edge is used as trigger, and when the ICES1 bit is written to one, a rising (positive) edge will trigger the capture. When a capture is triggered according to the ICES1 setting, the counter value is copied into the input capture register (ICR1). The event will also set the input capture flag (ICF1), and this can be used to cause an input capture interrupt, if this interrupt is enabled. When the ICR1 is used as TOP value (see description of the WGM13:0 bits located in the TCCR1A and the TCCR1B register), the ICP1 is disconnected and consequently the input capture function is disabled. * Bit 5 - Reserved Bit This bit is reserved for future use. For ensuring compatibility with future devices, this bit must be written to zero when TCCR1B is written. * Bit 4:3 - WGM13:2: Waveform Generation Mode See TCCR1A register description. * Bit 2:0 - CS12:0: Clock Select The three clock select bits select the clock source to be used by the Timer/Counter, see Figure 14-10 on page 109 and Figure 14-11 on page 110. ATmega88/ATmega168 Automotive [DATASHEET] 9365A-AVR-02/16 113 Table 14-6. Clock Select Bit Description CS12 CS11 CS10 Description 0 0 0 No clock source (Timer/Counter stopped). 0 0 1 clkI/O/1 (no prescaling) 0 1 0 clkI/O/8 (from prescaler) 0 1 1 clkI/O/64 (from prescaler) 1 0 0 clkI/O/256 (from prescaler) 1 0 1 clkI/O/1024 (from prescaler) 1 1 0 External clock source on T1 pin. Clock on falling edge. 1 1 1 External clock source on T1 pin. Clock on rising edge. 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. 14.10.3 Timer/Counter1 Control Register C - TCCR1C Bit 7 6 5 4 3 2 1 0 FOC1A FOC1B - - - - - - Read/Write R/W R/W R R R R R R Initial Value 0 0 0 0 0 0 0 0 TCCR1C * Bit 7 - FOC1A: Force Output Compare for Channel A * Bit 6 - FOC1B: Force Output Compare for Channel B The FOC1A/FOC1B bits are only active when the WGM13:0 bits specifies a non-PWM mode. However, for ensuring compatibility with future devices, these bits must be set to zero when TCCR1A is written when operating in a PWM mode. When writing a logical one to the FOC1A/FOC1B bit, an immediate compare match is forced on the waveform generation unit. The OC1A/OC1B output is changed according to its COM1x1:0 bits setting. Note that the FOC1A/FOC1B bits are implemented as strobes. Therefore it is the value present in the COM1x1:0 bits that determine the effect of the forced compare. A FOC1A/FOC1B strobe will not generate any interrupt nor will it clear the timer in clear timer on compare match (CTC) mode using OCR1A as TOP. The FOC1A/FOC1B bits are always read as zero. 14.10.4 Timer/Counter1 - TCNT1H and TCNT1L Bit 7 6 5 4 3 2 1 0 TCNT1[15:8] TCNT1H TCNT1[7:0] TCNT1L Read/Write R/W R/W R/W R/W R/W R/W R/W R/W Initial Value 0 0 0 0 0 0 0 0 The two Timer/Counter I/O locations (TCNT1H and TCNT1L, combined TCNT1) give direct access, both for read and for write operations, to the Timer/Counter unit 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.2 "Accessing 16-bit Registers" on page 94 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. 114 ATmega88/ATmega168 Automotive [DATASHEET] 9365A-AVR-02/16 14.10.5 Output Compare Register 1 A - OCR1AH and OCR1AL Bit 7 6 5 4 3 2 1 0 OCR1A[15:8] OCR1AH OCR1A[7:0] OCR1AL 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 2 1 0 14.10.6 Output Compare Register 1 B - OCR1BH and OCR1BL Bit 7 6 5 4 3 OCR1B[15:8] OCR1BH OCR1B[7:0] OCR1BL Read/Write R/W R/W R/W R/W R/W R/W R/W R/W Initial Value 0 0 0 0 0 0 0 0 The output compare 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-bit 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.2 "Accessing 16-bit Registers" on page 94. 14.10.7 Input Capture Register 1 - ICR1H and ICR1L Bit 7 6 5 4 3 2 1 0 ICR1[15:8] ICR1H ICR1[7:0] ICR1L Read/Write R/W R/W R/W R/W R/W R/W R/W R/W Initial Value 0 0 0 0 0 0 0 0 The 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-bit 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. See Section 14.2 "Accessing 16-bit Registers" on page 94. 14.10.8 Timer/Counter1 Interrupt Mask Register - TIMSK1 Bit 7 6 5 4 3 2 1 0 - - ICIE1 - - OCIE1B OCIE1A TOIE1 Read/Write R R R/W R R R/W R/W R/W Initial Value 0 0 0 0 0 0 0 0 TIMSK1 * Bit 7, 6 - Res: Reserved Bits These bits are unused bits in the Atmel(R) ATmega88/168, and will always read as zero. * Bit 5 - ICIE1: Timer/Counter1, Input Capture Interrupt Enable When this bit is written to one, and the I-flag in the status register is set (interrupts globally enabled), the Timer/Counter1 input capture interrupt is enabled. The corresponding interrupt vector (see Section 9. "Interrupts" on page 48) is executed when the ICF1 flag, located in TIFR1, is set. * Bit 4, 3 - Res: Reserved Bits These bits are unused bits in the Atmel ATmega88/168, and will always read as zero. ATmega88/ATmega168 Automotive [DATASHEET] 9365A-AVR-02/16 115 * Bit 2 - OCIE1B: Timer/Counter1, Output Compare B Match Interrupt Enable When this bit is written to one, and the I-flag in the status register is set (interrupts globally enabled), the Timer/Counter1 output compare B match interrupt is enabled. The corresponding interrupt vector (see Section 9. "Interrupts" on page 48) is executed when the OCF1B flag, located in TIFR1, is set. * Bit 1 - OCIE1A: Timer/Counter1, Output Compare A Match Interrupt Enable When this bit is written to one, and the I-flag in the status register is set (interrupts globally enabled), the Timer/Counter1 output compare A match interrupt is enabled. The corresponding interrupt vector (see Section 9. "Interrupts" on page 48) is executed when the OCF1A flag, located in TIFR1, is set. * Bit 0 - TOIE1: Timer/Counter1, Overflow Interrupt Enable When this bit is written to one, and the I-flag in the status register is set (interrupts globally enabled), the Timer/Counter1 overflow interrupt is enabled. The corresponding interrupt vector (see Section 8.9 "Watchdog Timer" on page 44) is executed when the TOV1 flag, located in TIFR1, is set. 14.10.9 Timer/Counter1 Interrupt Flag Register - TIFR1 Bit 7 6 5 4 3 2 1 0 - - ICF1 - - OCF1B OCF1A TOV1 Read/Write R R R/W R R R/W R/W R/W Initial Value 0 0 0 0 0 0 0 0 TIFR1 * Bit 7, 6 - Res: Reserved Bits These bits are unused bits in the Atmel ATmega88/168, and will always read as zero. * Bit 5 - ICF1: Timer/Counter1, Input Capture Flag This flag is set when a capture event occurs on the ICP1 pin. When the input capture register (ICR1) is set by the WGM13:0 to be used as the TOP value, the ICF1 flag is set when the counter reaches the TOP value. ICF1 is automatically cleared when the input capture interrupt vector is executed. Alternatively, ICF1 can be cleared by writing a logic one to its bit location. * Bit 4, 3 - Res: Reserved Bits These bits are unused bits in the Atmel ATmega88/168, and will always read as zero. * Bit 2 - OCF1B: Timer/Counter1, Output Compare B Match Flag This flag is set in the timer clock cycle after the counter (TCNT1) value matches the output compare register B (OCR1B). Note that a forced output compare (FOC1B) strobe will not set the OCF1B flag. OCF1B is automatically cleared when the output compare match B interrupt vector is executed. Alternatively, OCF1B can be cleared by writing a logic one to its bit location. * Bit 1 - OCF1A: Timer/Counter1, Output Compare A Match Flag This flag is set in the timer clock cycle after the counter (TCNT1) value matches the output compare register A (OCR1A). Note that a forced output compare (FOC1A) strobe will not set the OCF1A flag. OCF1A is automatically cleared when the output compare match A interrupt vector is executed. Alternatively, OCF1A can be cleared by writing a logic one to its bit location. * Bit 0 - TOV1: Timer/Counter1, Overflow Flag The setting of this flag is dependent of the WGM13:0 bits setting. In normal and CTC modes, the TOV1 flag is set when the timer overflows. Refer to Table 14-5 on page 112 for the TOV1 flag behavior when using another WGM13:0 bit setting. TOV1 is automatically cleared when the Timer/Counter1 overflow interrupt vector is executed. Alternatively, TOV1 can be cleared by writing a logic one to its bit location. 116 ATmega88/ATmega168 Automotive [DATASHEET] 9365A-AVR-02/16 15. 8-bit Timer/Counter2 with PWM and Asynchronous Operation Timer/Counter2 is a general purpose, single channel, 8-bit Timer/Counter module. The main features are: Single channel counter Glitch-free, phase correct pulse width modulator (PWM) Frequency generator 10-bit clock prescaler Overflow and compare match interrupt sources (TOV2, OCF2A and OCF2B) Allows clocking from external 32kHz watch crystal independent of the I/O clock Overview A simplified block diagram of the 8-bit Timer/Counter is shown in Figure 15-1. For the actual placement of I/O pins, refer to Section 1-1 "Pinout ATmega88/168" 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 Section 15.8 "8-bit Timer/Counter Register Description" on page 127. The PRTIM2 bit in Section 7.7.1 "Power Reduction Register - PRR" on page 35 must be written to zero to enable Timer/Counter2 module. Figure 15-1. 8-bit Timer/Counter Block Diagram Count Clear Direction TOVn (Int. Req.) Control Logic clkTn TOSC1 T/C Oscillator Prescaler TOP TOSC2 BOTTOM clkI/O Timer/Counter TCNTn = = 0 OCnA (Int. Req.) Waveform Generation = OCnA OCRnA DATA BUS 15.1 Clear timer on compare match (auto reload) Fixed TOP Value OCnB (Int. Req.) Waveform Generation = OCnB OCRnB TCCRnA TCCRnB ATmega88/ATmega168 Automotive [DATASHEET] 9365A-AVR-02/16 117 15.1.1 Registers The Timer/Counter (TCNT2) and output compare register (OCR2A and OCR2B) are 8-bit registers. Interrupt request (shorten as int.req.) signals are all visible in the timer interrupt flag register (TIFR2). All interrupts are individually masked with the timer interrupt mask register (TIMSK2). TIFR2 and TIMSK2 are not shown in the figure. The Timer/Counter can be clocked internally, via the prescaler, or asynchronously clocked from the TOSC1/2 pins, as detailed later in this section. The asynchronous operation is controlled by the asynchronous status register (ASSR). The clock select logic block controls which clock source he 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 (clkT2). The double buffered output compare register (OCR2A and OCR2B) 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 pins (OC2A and OC2B). See Section 15.4 "Output Compare Unit" on page 119 for details. The compare match event will also set the compare flag (OCF2A or OCF2B) which can be used to generate an output compare interrupt request. 15.1.2 Definitions Many register and bit references in this document are written in general form. A lower case "n" replaces the Timer/Counter number, in this case 2. However, when using the register or bit defines in a program, the precise form must be used, i.e., TCNT2 for accessing Timer/Counter2 counter value and so on. The definitions in the following table are also used extensively throughout the section. Table 15-1. Definitions 15.2 Parameter Definitions BOTTOM The counter reaches the BOTTOM when it becomes zero (0x00). MAX The counter reaches its MAXimum when it becomes 0xFF (decimal 255). TOP The counter reaches the TOP when it becomes equal to the highest value in the count sequence. The TOP value can be assigned to be the fixed value 0xFF (MAX) or the value stored in the OCR2A register. The assignment is dependent on the mode of operation. Timer/Counter Clock Sources The Timer/Counter can be clocked by an internal synchronous or an external asynchronous clock source. The clock source clkT2 is by default equal to the MCU clock, clkI/O. When the AS2 bit in the ASSR register is written to logic one, the clock source is taken from the Timer/Counter oscillator connected to TOSC1 and TOSC2. For details on asynchronous operation, see Section 15.9.2 "Asynchronous Status Register - ASSR" on page 133. For details on clock sources and prescaler, see Section 15.10 "Timer/Counter Prescaler" on page 134. 15.3 Counter Unit The main part of the 8-bit Timer/Counter is the programmable bi-directional counter unit. Figure 15-2 shows a block diagram of the counter and its surrounding environment. Figure 15-2. Counter Unit Block Diagram TOVn (Int. Req.) DATA BUS TOSC1 T/C Oscillator count TCNTn clear Control Logic clkTn Prescaler TOSC2 direction clkI/O bottom 118 ATmega88/ATmega168 Automotive [DATASHEET] 9365A-AVR-02/16 top Signal description (internal signals): count Increment or decrement TCNT2 by 1. direction Selects between increment and decrement. clear Clear TCNT2 (set all bits to zero). clkTn Timer/Counter clock, referred to as clkT2 in the following. top Signalizes that TCNT2 has reached maximum value. bottom Signalizes that TCNT2 has reached minimum value (zero). Depending on the mode of operation used, the counter is cleared, incremented, or decremented at each timer clock (clkT2). clkT2 can be generated from an external or internal clock source, selected by the clock select bits (CS22:0). When no clock source is selected (CS22:0 = 0) the timer is stopped. However, the TCNT2 value can be accessed by the CPU, regardless of whether clkT2 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 WGM21 and WGM20 bits located in the Timer/Counter control register (TCCR2A) and the WGM22 located in the Timer/Counter control register B (TCCR2B). There are close connections between how the counter behaves (counts) and how waveforms are generated on the output compare outputs OC2A and OC2B. For more details about advanced counting sequences and waveform generation, see Section 15.6 "Modes of Operation" on page 121. The Timer/Counter overflow flag (TOV2) is set according to the mode of operation selected by the WGM22:0 bits. TOV2 can be used for generating a CPU interrupt. 15.4 Output Compare Unit The 8-bit comparator continuously compares TCNT2 with the output compare register (OCR2A and OCR2B). Whenever TCNT2 equals OCR2A or OCR2B, the comparator signals a match. A match will set the output compare flag (OCF2A or OCF2B) at the next timer clock cycle. If the corresponding interrupt is enabled, the output compare flag generates an output compare interrupt. The output compare flag is automatically cleared when the interrupt is executed. Alternatively, the output compare flag can be cleared by software by writing a logical one to its I/O bit location. The waveform generator uses the match signal to generate an output according to operating mode set by the WGM22:0 bits and compare output mode (COM2x1:0) bits. The max and bottom signals are used by the waveform generator for handling the special cases of the extreme values in some modes of operation (Section 15.6 "Modes of Operation" on page 121). Figure 15-3 shows a block diagram of the output compare unit. Figure 15-3. Output Compare Unit, Block Diagram DATA BUS OCRnx TCNTn = (8-bit Comparator) OCFnx (Int. Req.) top bottom Waveform Generator OCnx FOCn WGMn1:0 COMnX1:0 ATmega88/ATmega168 Automotive [DATASHEET] 9365A-AVR-02/16 119 The OCR2x register is 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 OCR2x 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 OCR2x register access may seem complex, but this is not case. When the double buffering is enabled, the CPU has access to the OCR2x buffer register, and if double buffering is disabled the CPU will access the OCR2x directly. 15.4.1 Force Output Compare In non-PWM waveform generation modes, the match output of the comparator can be forced by writing a one to the force output compare (FOC2x) bit. Forcing compare match will not set the OCF2x flag or reload/clear the timer, but the OC2x pin will be updated as if a real compare match had occurred (the COM2x1:0 bits settings define whether the OC2x pin is set, cleared or toggled). 15.4.2 Compare Match Blocking by TCNT2 Write All CPU write operations to the TCNT2 register will block any compare match that occurs in the next timer clock cycle, even when the timer is stopped. This feature allows OCR2x to be initialized to the same value as TCNT2 without triggering an interrupt when the Timer/Counter clock is enabled. 15.4.3 Using the Output Compare Unit Since writing TCNT2 in any mode of operation will block all compare matches for one timer clock cycle, there are risks involved when changing TCNT2 when using the output compare channel, independently of whether the Timer/Counter is running or not. If the value written to TCNT2 equals the OCR2x value, the compare match will be missed, resulting in incorrect waveform generation. Similarly, do not write the TCNT2 value equal to BOTTOM when the counter is downcounting. The setup of the OC2x should be performed before setting the data direction register for the port pin to output. The easiest way of setting the OC2x value is to use the force output compare (FOC2x) strobe bit in normal mode. The OC2x register keeps its value even when changing between waveform generation modes. Be aware that the COM2x1:0 bits are not double buffered together with the compare value. Changing the COM2x1:0 bits will take effect immediately. 15.5 Compare Match Output Unit The compare output mode (COM2x1:0) bits have two functions. The waveform generator uses the COM2x1:0 bits for defining the output compare (OC2x) state at the next compare match. Also, the COM2x1:0 bits control the OC2x pin output source. Figure 15-4 on page 121 shows a simplified schematic of the logic affected by the COM2x1:0 bit setting. The I/O registers, I/O bits, and I/O pins in the figure are shown in bold. Only the parts of the general I/O port control registers (DDR and PORT) that are affected by the COM2x1:0 bits are shown. When referring to the OC2x state, the reference is for the internal OC2x register, not the OC2x pin. 120 ATmega88/ATmega168 Automotive [DATASHEET] 9365A-AVR-02/16 Figure 15-4. Compare Match Output Unit, Schematic COMnx1 COMnx0 FOCn Waveform Generator D Q 1 OCnx Pin OCnx 0 DATA BUS D Q PORT D Q DDR clkI/O The general I/O port function is overridden by the output compare (OC2x) from the waveform generator if either of the COM2x1:0 bits are set. However, the OC2x 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 OC2x pin (DDR_OC2x) must be set as output before the OC2x 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 OC2x state before the output is enabled. Note that some COM2x1:0 bit settings are reserved for certain modes of operation. See Section 15.8 "8-bit Timer/Counter Register Description" on page 127 15.5.1 Compare Output Mode and Waveform Generation The waveform generator uses the COM2x1:0 bits differently in normal, CTC, and PWM modes. For all modes, setting the COM2x1:0 = 0 tells the waveform generator that no action on the OC2x register is to be performed on the next compare match. For compare output actions in the non-PWM modes refer to Table 15-5 on page 128. For fast PWM mode, refer to Table 15-6 on page 128, and for phase correct PWM refer to Table 15-7 on page 128. A change of the COM2x1:0 bits state will have effect at the first compare match after the bits are written. For non-PWM modes, the action can be forced to have immediate effect by using the FOC2x strobe bits. 15.6 Modes of Operation The mode of operation, i.e., the behavior of the Timer/Counter and the output compare pins, is defined by the combination of the waveform generation mode (WGM22:0) and compare output mode (COM2x1:0) bits. The compare output mode bits do not affect the counting sequence, while the waveform generation mode bits do. The COM2x1:0 bits control whether the PWM output generated should be inverted or not (inverted or non-inverted PWM). For non-PWM modes the COM2x1:0 bits control whether the output should be set, cleared, or toggled at a compare match (see Section 15.5 "Compare Match Output Unit" on page 120). For detailed timing information refer to Section 15.7 "Timer/Counter Timing Diagrams" on page 125. ATmega88/ATmega168 Automotive [DATASHEET] 9365A-AVR-02/16 121 15.6.1 Normal Mode The simplest mode of operation is the normal mode (WGM22: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 (TOV2) will be set in the same timer clock cycle as the TCNT2 becomes zero. The TOV2 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 TOV2 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 of the CPU time. 15.6.2 Clear Timer on Compare Match (CTC) Mode In clear timer on compare or CTC mode (WGM22:0 = 2), the OCR2A register is used to manipulate the counter resolution. In CTC mode the counter is cleared to zero when the counter value (TCNT2) matches the OCR2A. The OCR2A defines the top value for the counter, hence also its resolution. This mode allows greater control of the compare match output frequency. It also simplifies the operation of counting external events. The timing diagram for the CTC mode is shown in Figure 15-5. The counter value (TCNT2) increases until a compare match occurs between TCNT2 and OCR2A, and then counter (TCNT2) is cleared. Figure 15-5. CTC Mode, Timing Diagram OCnx Interrupt Flag Set TCNTn OCnx (Toggle) Period (COMnx1:0 = 1) 1 2 3 4 An interrupt can be generated each time the counter value reaches the TOP value by using the OCF2A 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 none or a low prescaler value must be done with care since the CTC mode does not have the double buffering feature. If the new value written to OCR2A is lower than the current value of TCNT2, 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 OC2A output can be set to toggle its logical level on each compare match by setting the compare output mode bits to toggle mode (COM2A1:0 = 1). The OC2A 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 fOC2A = fclk_I/O/2 when OCR2A 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 N variable represents the prescale factor (1, 8, 32, 64, 128, 256, or 1024). As for the normal mode of operation, the TOV2 flag is set in the same timer clock cycle that the counter counts from MAX to 0x00. 122 ATmega88/ATmega168 Automotive [DATASHEET] 9365A-AVR-02/16 15.6.3 Fast PWM Mode The fast pulse width modulation or fast PWM mode (WGM22:0 = 3 or 7) provides a high frequency PWM waveform generation option. The fast PWM differs from the other PWM option by its single-slope operation. The counter counts from BOTTOM to TOP then restarts from BOTTOM. TOP is defined as 0xFF when WGM2:0 = 3, and OCR2A when MGM2:0 = 7. In non-inverting compare output mode, the output compare (OC2x) is cleared on the compare match between TCNT2 and OCR2x, 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 physically small sized external components (coils, capacitors), and therefore reduces total system cost. In fast PWM mode, the counter is incremented until the counter value matches the TOP value. The counter is then cleared at the following timer clock cycle. The timing diagram for the fast PWM mode is shown in Figure 15-6. The TCNT2 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 TCNT2 slopes represent compare matches between OCR2x and TCNT2. Figure 15-6. Fast PWM Mode, Timing Diagram OCRnx Interrupt Flag Set OCRnx Update and TOVn Interrupt Flag Set TCNTn OCnx (COMnx1:0 = 2) OCnx (COMnx1:0 = 3) Period 1 2 3 4 5 6 7 The Timer/Counter overflow flag (TOV2) 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 OC2x pin. Setting the COM2x1:0 bits to two will produce a non-inverted PWM and an inverted PWM output can be generated by setting the COM2x1:0 to three. TOP is defined as 0xFF when WGM2:0 = 3, and OCR2A when MGM2:0 = 7. (See Table 15-3 on page 127). The actual OC2x 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 OC2x register at the compare match between OCR2x and TCNT2, and clearing (or setting) the OC2x register at the timer clock cycle the counter is cleared (changes from TOP to BOTTOM). The PWM frequency for the output can be calculated by the following equation: f clk_I/O f OCnxPWM = ----------------N 256 The N variable represents the prescale factor (1, 8, 32, 64, 128, 256, or 1024). The extreme values for the OCR2A register represent special cases when generating a PWM waveform output in the fast PWM mode. If the OCR2A is set equal to BOTTOM, the output will be a narrow spike for each MAX+1 timer clock cycle. Setting the OCR2A equal to MAX will result in a constantly high or low output (depending on the polarity of the output set by the COM2A1:0 bits.) ATmega88/ATmega168 Automotive [DATASHEET] 9365A-AVR-02/16 123 A frequency (with 50% duty cycle) waveform output in fast PWM mode can be achieved by setting OC2x to toggle its logical level on each compare match (COM2x1:0 = 1). The waveform generated will have a maximum frequency of foc2 = fclk_I/O/2 when OCR2A is set to zero. This feature is similar to the OC2A toggle in CTC mode, except the double buffer feature of the output compare unit is enabled in the fast PWM mode. 15.6.4 Phase Correct PWM Mode The phase correct PWM mode (WGM22:0 = 1 or 5) provides a high resolution phase correct PWM waveform generation option. The phase correct PWM mode is based on a dual-slope operation. The counter counts repeatedly from BOTTOM to TOP and then from TOP to BOTTOM. TOP is defined as 0xFF when WGM2:0 = 3, and OCR2A when MGM2:0 = 7. In non-inverting compare output mode, the output compare (OC2x) is cleared on the compare match between TCNT2 and OCR2x while upcounting, and set on the compare match while downcounting. 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. In phase correct PWM mode the counter is incremented until the counter value matches TOP. When the counter reaches TOP, it changes the count direction. The TCNT2 value will be equal to TOP for one timer clock cycle. The timing diagram for the phase correct PWM mode is shown on Figure 15-7. The TCNT2 value is in the timing diagram shown as a histogram for illustrating the dual-slope operation. The diagram includes non-inverted and inverted PWM outputs. The small horizontal line marks on the TCNT2 slopes represent compare matches between OCR2x and TCNT2. Figure 15-7. Phase Correct PWM Mode, Timing Diagram OCnx Interrupt Flag Set OCRnx Update TOVn Interrupt Flag Set TCNTn OCnx (COMnx1:0 = 2) OCnx (COMnx1:0 = 3) Period 1 2 3 The Timer/Counter overflow flag (TOV2) 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. 124 ATmega88/ATmega168 Automotive [DATASHEET] 9365A-AVR-02/16 In phase correct PWM mode, the compare unit allows generation of PWM waveforms on the OC2x pin. Setting the COM2x1:0 bits to two will produce a non-inverted PWM. An inverted PWM output can be generated by setting the COM2x1:0 to three. TOP is defined as 0xFF when WGM2:0 = 3, and OCR2A when MGM2:0 = 7 (See Table 15-4 on page 127). The actual OC2x 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 OC2x register at the compare match between OCR2x and TCNT2 when the counter increments, and setting (or clearing) the OC2x register at compare match between OCR2x and TCNT2 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 N variable represents the prescale factor (1, 8, 32, 64, 128, 256, or 1024). The extreme values for the OCR2A register represent special cases when generating a PWM waveform output in the phase correct PWM mode. If the OCR2A 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 15-7 on page 124 OCnx 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 compare match. OCR2A changes its value from MAX, like in Figure 15-7 on page 124. When the OCR2A 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. 15.7 The timer starts counting from a value higher than the one in OCR2A, and for that reason misses the compare match and hence the OCn change that would have happened on the way up. Timer/Counter Timing Diagrams The following figures show the Timer/Counter in synchronous mode, and the timer clock (clkT2) is therefore shown as a clock enable signal. In asynchronous mode, clkI/O should be replaced by the Timer/Counter oscillator clock. The figures include information on when interrupt flags are set. Figure 15-8 contains timing data for basic Timer/Counter operation. The figure shows the count sequence close to the MAX value in all modes other than phase correct PWM mode. Figure 15-8. Timer/Counter Timing Diagram, no Prescaling clkI/O clkTn (clkI/O/1) TCNTn MAX - 1 MAX BOTTOM BOTTOM + 1 TOVn ATmega88/ATmega168 Automotive [DATASHEET] 9365A-AVR-02/16 125 Figure 15-9 shows the same timing data, but with the prescaler enabled. Figure 15-9. Timer/Counter Timing Diagram, with Prescaler (fclk_I/O/8) clkI/O clkTn (clkI/O/8) TCNTn MAX - 1 MAX BOTTOM BOTTOM + 1 TOVn Figure 15-10 shows the setting of OCF2A in all modes except CTC mode. Figure 15-10. Timer/Counter Timing Diagram, Setting of OCF2A, with Prescaler (fclk_I/O/8) clkI/O clkTn (clkI/O/8) TCNTn OCRnx - 1 OCRnx OCRnx OCRnx + 1 OCRnx + 2 OCRnx Value OCFnx Figure 15-11 shows the setting of OCF2A and the clearing of TCNT2 in CTC mode. Figure 15-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 OCRnx OCFnx 126 ATmega88/ATmega168 Automotive [DATASHEET] 9365A-AVR-02/16 TOP BOTTOM TOP BOTTOM + 1 15.8 8-bit Timer/Counter Register Description 15.8.1 Timer/Counter Control Register A - TCCR2A Bit 7 6 5 4 3 2 1 0 COM2A1 COM2A0 COM2B1 COM2B0 - - WGM21 WGM20 Read/Write R/W R/W R/W R/W R R R/W R/W Initial Value 0 0 0 0 0 0 0 0 TCCR2A * Bits 7:6 - COM2A1:0: Compare Match Output A Mode These bits control the output compare pin (OC2A) behavior. If one or both of the COM2A1:0 bits are set, the OC2A 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 OC2A pin must be set in order to enable the output driver. When OC2A is connected to the pin, the function of the COM2A1:0 bits depends on the WGM22:0 bit setting. Table 15-2 shows the COM2A1:0 bit functionality when the WGM22:0 bits are set to a normal or CTC mode (non-PWM). Table 15-2. Compare Output Mode, non-PWM Mode COM2A1 COM2A0 Description 0 0 Normal port operation, OC0A disconnected. 0 1 Toggle OC2A on compare match 1 0 Clear OC2A on compare match 1 1 Set OC2A on compare match Table 15-3 shows the COM2A1:0 bit functionality when the WGM21:0 bits are set to fast PWM mode. Table 15-3. Compare Output Mode, Fast PWM Mode(1) COM2A1 COM2A0 0 0 Normal port operation, OC2A disconnected. 0 1 WGM22 = 0: normal port operation, OC0A disconnected. WGM22 = 1: toggle OC2A on compare match. 1 0 Clear OC2A on compare match, set OC2A at TOP Note: 1 1. Description 1 Set OC2A on compare match, clear OC2A at TOP A special case occurs when OCR2A equals TOP and COM2A1 is set. In this case, the compare match is ignored, but the set or clear is done at TOP. See Section 15.6.3 "Fast PWM Mode" on page 123 for more details. Table 15-4 shows the COM2A1:0 bit functionality when the WGM22:0 bits are set to phase correct PWM mode. Table 15-4. Compare Output Mode, Phase Correct PWM Mode(1) COM2A1 COM2A0 0 0 Normal port operation, OC2A disconnected. 0 1 WGM22 = 0: normal port operation, OC2A disconnected. WGM22 = 1: toggle OC2A on compare match. 1 0 Clear OC2A on compare match when up-counting. Set OC2A on compare match when down-counting. 1 Note: 1. Description Set OC2A on compare match when up-counting. Clear OC2A on compare match when down-counting. A special case occurs when OCR2A equals TOP and COM2A1 is set. In this case, the compare match is ignored, but the set or clear is done at TOP. See Section 15.6.4 "Phase Correct PWM Mode" on page 124 for more details. 1 ATmega88/ATmega168 Automotive [DATASHEET] 9365A-AVR-02/16 127 * Bits 5:4 - COM2B1:0: Compare Match Output B Mode These bits control the output compare pin (OC2B) behavior. If one or both of the COM2B1:0 bits are set, the OC2B 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 OC2B pin must be set in order to enable the output driver. When OC2B is connected to the pin, the function of the COM2B1:0 bits depends on the WGM22:0 bit setting. Table 15-5 shows the COM2B1:0 bit functionality when the WGM22:0 bits are set to a normal or CTC mode (non-PWM). Table 15-5. Compare Output Mode, non-PWM Mode COM2B1 COM2B0 0 0 Description Normal port operation, OC2B disconnected. 0 1 Toggle OC2B on compare match 1 0 Clear OC2B on compare match 1 1 Set OC2B on compare match Table 15-6 shows the COM2B1:0 bit functionality when the WGM22:0 bits are set to fast PWM mode. Table 15-6. Compare Output Mode, Fast PWM Mode(1) COM2B1 COM2B0 0 0 Normal port operation, OC2B disconnected. 0 1 Reserved 1 0 Clear OC2B on compare match, set OC2B at TOP Note: 1 1. Description 1 Set OC2B on compare match, clear OC2B at TOP A special case occurs when OCR2B equals TOP and COM2B1 is set. In this case, the compare match is ignored, but the set or clear is done at TOP. See Section 15.6.4 "Phase Correct PWM Mode" on page 124 for more details. Table 15-7 shows the COM2B1:0 bit functionality when the WGM22:0 bits are set to phase correct PWM mode. Table 15-7. Compare Output Mode, Phase Correct PWM Mode(1) COM2B1 COM2B0 0 0 Normal port operation, OC2B disconnected. 0 1 Reserved 1 0 Clear OC2B on compare match when up-counting. Set OC2B on compare match when down-counting. 1 Note: 1. Description Set OC2B on compare match when up-counting. Clear OC2B on compare match when down-counting. A special case occurs when OCR2B equals TOP and COM2B1 is set. In this case, the compare match is ignored, but the set or clear is done at TOP. See Section 15.6.4 "Phase Correct PWM Mode" on page 124 for more details. 1 * Bits 3, 2 - Res: Reserved Bits These bits are reserved bits in the Atmel(R) ATmega88/168 and will always read as zero. * Bits 1:0 - WGM21:0: Waveform Generation Mode Combined with the WGM22 bit found in the TCCR2B register, these bits control the counting sequence of the counter, the source for maximum (TOP) counter value, and what type of waveform generation to be used, see Table 15-8. Modes of operation supported by the Timer/Counter unit are: normal mode (counter), clear timer on compare match (CTC) mode, and two types of pulse width modulation (PWM) modes (see Section 15.6 "Modes of Operation" on page 121). 128 ATmega88/ATmega168 Automotive [DATASHEET] 9365A-AVR-02/16 Table 15-8. Waveform Generation Mode Bit Description Timer/Counter Mode of Operation TOP Update of OCRx at TOV Flag Set on(1)(2) 0 Normal 0xFF Immediate MAX 0 1 PWM, phase correct 1 0 CTC 0 1 1 4 1 0 5 1 6 1 Mode WGM2 WGM1 WGM0 0 0 0 1 0 2 0 3 7 Notes: 0xFF TOP BOTTOM OCRA Immediate MAX Fast PWM 0xFF TOP MAX 0 Reserved - - - 0 1 PWM, phase correct OCRA TOP BOTTOM 1 0 Reserved - - - 1 1 Fast PWM OCRA TOP TOP 1. 1 MAX = 0xFF 2. BOTTOM= 0x00 15.8.2 Timer/Counter Control Register B - TCCR2B Bit 7 6 5 4 3 2 1 0 FOC2A FOC2B - - WGM22 CS22 CS21 CS20 Read/Write W W R R R R R/W R/W Initial Value 0 0 0 0 0 0 0 0 TCCR2B * Bit 7 - FOC2A: Force Output Compare A The FOC2A bit is only active when the WGM bits specify a non-PWM mode. However, for ensuring compatibility with future devices, this bit must be set to zero when TCCR2B is written when operating in PWM mode. When writing a logical one to the FOC2A bit, an immediate compare match is forced on the waveform generation unit. The OC2A output is changed according to its COM2A1:0 bits setting. Note that the FOC2A bit is implemented as a strobe. Therefore it is the value present in the COM2A1:0 bits that determines the effect of the forced compare. A FOC2A strobe will not generate any interrupt, nor will it clear the timer in CTC mode using OCR2A as TOP. The FOC2A bit is always read as zero. * Bit 6 - FOC2B: Force Output Compare B The FOC2B bit is only active when the WGM bits specify a non-PWM mode. However, for ensuring compatibility with future devices, this bit must be set to zero when TCCR2B is written when operating in PWM mode. When writing a logical one to the FOC2B bit, an immediate compare match is forced on the waveform generation unit. The OC2B output is changed according to its COM2B1:0 bits setting. Note that the FOC2B bit is implemented as a strobe. Therefore it is the value present in the COM2B1:0 bits that determines the effect of the forced compare. A FOC2B strobe will not generate any interrupt, nor will it clear the timer in CTC mode using OCR2B as TOP. The FOC2B bit is always read as zero. * Bits 5:4 - Res: Reserved Bits These bits are reserved bits in the Atmel(R) ATmega88/168 and will always read as zero. * Bit 3 - WGM22: Waveform Generation Mode See the description in the Section 15.8.1 "Timer/Counter Control Register A - TCCR2A" on page 127. * Bit 2:0 - CS22:0: Clock Select The three clock select bits select the clock source to be used by the Timer/Counter, see Table 15-9. ATmega88/ATmega168 Automotive [DATASHEET] 9365A-AVR-02/16 129 Table 15-9. Clock Select Bit Description CS22 CS21 CS20 Description 0 0 0 No clock source (Timer/Counter stopped). 0 0 1 clkT2S/(no prescaling) 0 1 0 clkT2S/8 (from prescaler) 0 1 1 clkT2S/32 (from prescaler) 1 0 0 clkT2S/64 (from prescaler) 1 0 1 clkT2S/128 (from prescaler) 1 1 0 clkT2S/256 (from prescaler) 1 1 1 clkT2S/1024 (from prescaler) If external pin modes are used for the Timer/Counter0, transitions on the T0 pin will clock the counter even if the pin is configured as an output. This feature allows software control of the counting. 15.8.3 Timer/Counter Register - TCNT2 Bit 7 6 5 4 Read/Write R/W R/W R/W R/W Initial Value 0 0 0 0 3 2 1 0 R/W R/W R/W R/W 0 0 0 0 TCNT2[7:0] TCNT2 The Timer/Counter register gives direct access, both for read and write operations, to the Timer/Counter unit 8-bit counter. Writing to the TCNT2 register blocks (removes) the compare match on the following timer clock. Modifying the counter (TCNT2) while the counter is running, introduces a risk of missing a compare match between TCNT2 and the OCR2x registers. 15.8.4 Output Compare Register A - OCR2A Bit 7 6 5 4 3 2 1 0 OCR2A[7:0] OCR2A Read/Write R/W R/W R/W R/W R/W R/W R/W R/W Initial Value 0 0 0 0 0 0 0 0 The output compare register A contains an 8-bit value that is continuously compared with the counter value (TCNT2). A match can be used to generate an output compare interrupt, or to generate a waveform output on the OC2A pin. 15.8.5 Output Compare Register B - OCR2B Bit 7 6 5 4 Read/Write R/W R/W R/W R/W Initial Value 0 0 0 0 3 2 1 0 R/W R/W R/W R/W 0 0 0 0 OCR2B[7:0] OCR2B The output compare register B contains an 8-bit value that is continuously compared with the counter value (TCNT2). A match can be used to generate an output compare interrupt, or to generate a waveform output on the OC2B pin. 130 ATmega88/ATmega168 Automotive [DATASHEET] 9365A-AVR-02/16 15.8.6 Timer/Counter2 Interrupt Mask Register - TIMSK2 Bit 7 6 5 4 3 2 1 0 - - - - - OCIE2B OCIE2A TOIE2 Read/Write R R R R R R/W R/W R/W Initial Value 0 0 0 0 0 0 0 0 TIMSK2 * Bit 2 - OCIE2B: Timer/Counter2 Output Compare Match B Interrupt Enable When the OCIE2B bit is written to one and the I-bit in the status register is set (one), the Timer/Counter2 compare match B interrupt is enabled. The corresponding interrupt is executed if a compare match in Timer/Counter2 occurs, i.e., when the OCF2B bit is set in the Timer/Counter 2 interrupt flag register - TIFR2. * Bit 1 - OCIE2A: Timer/Counter2 Output Compare Match A Interrupt Enable When the OCIE2A bit is written to one and the I-bit in the status register is set (one), the Timer/Counter2 compare match A interrupt is enabled. The corresponding interrupt is executed if a compare match in Timer/Counter2 occurs, i.e., when the OCF2A bit is set in the Timer/Counter 2 interrupt flag register - TIFR2. * Bit 0 - TOIE2: Timer/Counter2 Overflow Interrupt Enable When the TOIE2 bit is written to one and the I-bit in the status register is set (one), the Timer/Counter2 overflow interrupt is enabled. The corresponding interrupt is executed if an overflow in Timer/Counter2 occurs, i.e., when the TOV2 bit is set in the Timer/Counter2 interrupt flag register - TIFR2. 15.8.7 Timer/Counter2 Interrupt Flag Register - TIFR2 Bit 7 6 5 4 3 2 1 0 - - - - - OCF2B OCF2A TOV2 Read/Write R R R R R R/W R/W R/W Initial Value 0 0 0 0 0 0 0 0 TIFR2 * Bit 2 - OCF2B: Output Compare Flag 2 B The OCF2B bit is set (one) when a compare match occurs between the Timer/Counter2 and the data in OCR2B - output compare register2. OCF2B is cleared by hardware when executing the corresponding interrupt handling vector. Alternatively, OCF2B is cleared by writing a logic one to the flag. When the I-bit in SREG, OCIE2B (Timer/Counter2 compare match interrupt enable), and OCF2B are set (one), the Timer/Counter2 compare match interrupt is executed. * Bit 1 - OCF2A: Output Compare Flag 2 A The OCF2A bit is set (one) when a compare match occurs between the Timer/Counter2 and the data in OCR2A - output compare register2. OCF2A is cleared by hardware when executing the corresponding interrupt handling vector. Alternatively, OCF2A is cleared by writing a logic one to the flag. When the I-bit in SREG, OCIE2A (Timer/Counter2 compare match interrupt enable), and OCF2A are set (one), the Timer/Counter2 compare match interrupt is executed. * Bit 0 - TOV2: Timer/Counter2 Overflow Flag The TOV2 bit is set (one) when an overflow occurs in Timer/Counter2. TOV2 is cleared by hardware when executing the corresponding interrupt handling vector. Alternatively, TOV2 is cleared by writing a logic one to the flag. When the SREG I-bit, TOIE2A (Timer/Counter2 overflow interrupt enable), and TOV2 are set (one), the Timer/Counter2 overflow interrupt is executed. In PWM mode, this bit is set when Timer/Counter2 changes counting direction at 0x00. ATmega88/ATmega168 Automotive [DATASHEET] 9365A-AVR-02/16 131 15.9 Asynchronous operation of the Timer/Counter 15.9.1 Asynchronous Operation of Timer/Counter2 When Timer/Counter2 operates asynchronously, some considerations must be taken. Warning: When switching between asynchronous and synchronous clocking of Timer/Counter2, the timer registers TCNT2, OCR2x, and TCCR2x might be corrupted. A safe procedure for switching clock source is: a. b. Select clock source by setting AS2 as appropriate. c. Write new values to TCNT2, OCR2x, and TCCR2x. d. To switch to asynchronous operation: Wait for TCN2xUB, OCR2xUB, and TCR2xUB. e. Clear the Timer/Counter2 interrupt flags. f. Enable interrupts, if needed. The CPU main clock frequency must be more than four times the oscillator frequency. When writing to one of the registers TCNT2, OCR2x, or TCCR2x, the value is transferred to a temporary register, and latched after two positive edges on TOSC1. The user should not write a new value before the contents of the temporary register have been transferred to its destination. Each of the five mentioned registers have their individual temporary register, which means that e.g. writing to TCNT2 does not disturb an OCR2x write in progress. To detect that a transfer to the destination register has taken place, the asynchronous status register - ASSR has been implemented. When entering power-save or ADC noise reduction mode after having written to TCNT2, OCR2x, or TCCR2x, the user must wait until the written register has been updated if Timer/Counter2 is used to wake up the device. Otherwise, the MCU will enter sleep mode before the changes are effective. This is particularly important if any of the output compare2 interrupt is used to wake up the device, since the output compare function is disabled during writing to OCR2x or TCNT2. If the write cycle is not finished, and the MCU enters sleep mode before the corresponding OCR2xUB bit returns to zero, the device will never receive a compare match interrupt, and the MCU will not wake up. If Timer/Counter2 is used to wake the device up from power-save or ADC noise reduction mode, precautions must be taken if the user wants to re-enter one of these modes: The interrupt logic needs one TOSC1 cycle to be reset. If the time between wake-up and re-entering sleep mode is less than one TOSC1 cycle, the interrupt will not occur, and the device will fail to wake up. If the user is in doubt whether the time before re-entering power-save or ADC noise reduction mode is sufficient, the following algorithm can be used to ensure that one TOSC1 cycle has elapsed: a. 132 Disable the Timer/Counter2 interrupts by clearing OCIE2x and TOIE2. Write a value to TCCR2x, TCNT2, or OCR2x. b. Wait until the corresponding update busy flag in ASSR returns to zero. c. Enter power-save or ADC noise reduction mode. When the asynchronous operation is selected, the 32.768kHz oscillator for Timer/Counter2 is always running, except in power-down and standby modes. After a power-up reset or wake-up from power-down or standby mode, the user should be aware of the fact that this oscillator might take as long as one second to stabilize. The user is advised to wait for at least one second before using Timer/Counter2 after power-up or wake-up from power-down or standby mode. The contents of all Timer/Counter2 registers must be considered lost after a wake-up from power-down or standby mode due to unstable clock signal upon start-up, no matter whether the oscillator is in use or a clock signal is applied to the TOSC1 pin. Description of wake up from power-save or ADC noise reduction mode when the timer is clocked asynchronously: When the interrupt condition is met, the wake up process is started on the following cycle of the timer clock, that is, the timer is always advanced by at least one before the processor can read the counter value. After wake-up, the MCU is halted for four cycles, it executes the interrupt routine, and resumes execution from the instruction following SLEEP. ATmega88/ATmega168 Automotive [DATASHEET] 9365A-AVR-02/16 Reading of the TCNT2 register shortly after wake-up from power-save may give an incorrect result. Since TCNT2 is clocked on the asynchronous TOSC clock, reading TCNT2 must be done through a register synchronized to the internal I/O clock domain. Synchronization takes place for every rising TOSC1 edge. When waking up from power-save mode, and the I/O clock (clkI/O) again becomes active, TCNT2 will read as the previous value (before entering sleep) until the next rising TOSC1 edge. The phase of the TOSC clock after waking up from power-save mode is essentially unpredictable, as it depends on the wake-up time. The recommended procedure for reading TCNT2 is thus as follows: a. Write any value to either of the registers OCR2x or TCCR2x. b. Wait for the corresponding update busy flag to be cleared. c. Read TCNT2. During asynchronous operation, the synchronization of the interrupt flags for the asynchronous timer takes 3 processor cycles plus one timer cycle. The timer is therefore advanced by at least one before the processor can read the timer value causing the setting of the interrupt flag. The output compare pin is changed on the timer clock and is not synchronized to the processor clock. 15.9.2 Asynchronous Status Register - ASSR Bit 7 6 5 4 3 2 1 0 - EXCLK AS2 TCN2UB OCR2AUB OCR2BUB TCR2AUB TCR2BUB Read/Write R R/W R/W R R R R R Initial Value 0 0 0 0 0 0 0 0 ASSR * Bit 6 - EXCLK: Enable External Clock Input When EXCLK is written to one, and asynchronous clock is selected, the external clock input buffer is enabled and an external clock can be input on timer oscillator 1 (TOSC1) pin instead of a 32kHz crystal. Writing to EXCLK should be done before asynchronous operation is selected. Note that the crystal oscillator will only run when this bit is zero. * Bit 5 - AS2: Asynchronous Timer/Counter2 When AS2 is written to zero, Timer/Counter2 is clocked from the I/O clock, clkI/O. When AS2 is written to one, Timer/Counter2 is clocked from a crystal oscillator connected to the timer oscillator 1 (TOSC1) pin. When the value of AS2 is changed, the contents of TCNT2, OCR2A, OCR2B, TCCR2A and TCCR2B might be corrupted. * Bit 4 - TCN2UB: Timer/Counter2 Update Busy When Timer/Counter2 operates asynchronously and TCNT2 is written, this bit becomes set. When TCNT2 has been updated from the temporary storage register, this bit is cleared by hardware. A logical zero in this bit indicates that TCNT2 is ready to be updated with a new value. * Bit 3 - OCR2AUB: Output Compare Register2 Update Busy When Timer/Counter2 operates asynchronously and OCR2A is written, this bit becomes set. When OCR2A has been updated from the temporary storage register, this bit is cleared by hardware. A logical zero in this bit indicates that OCR2A is ready to be updated with a new value. * Bit 2 - OCR2BUB: Output Compare Register2 Update Busy When Timer/Counter2 operates asynchronously and OCR2B is written, this bit becomes set. When OCR2B has been updated from the temporary storage register, this bit is cleared by hardware. A logical zero in this bit indicates that OCR2B is ready to be updated with a new value. * Bit 1 - TCR2AUB: Timer/Counter Control Register2 Update Busy When Timer/Counter2 operates asynchronously and TCCR2A is written, this bit becomes set. When TCCR2A has been updated from the temporary storage register, this bit is cleared by hardware. A logical zero in this bit indicates that TCCR2A is ready to be updated with a new value. ATmega88/ATmega168 Automotive [DATASHEET] 9365A-AVR-02/16 133 * Bit 0 - TCR2BUB: Timer/Counter Control Register2 Update Busy When Timer/Counter2 operates asynchronously and TCCR2B is written, this bit becomes set. When TCCR2B has been updated from the temporary storage register, this bit is cleared by hardware. A logical zero in this bit indicates that TCCR2B is ready to be updated with a new value. If a write is performed to any of the five Timer/Counter2 registers while its update busy flag is set, the updated value might get corrupted and cause an unintentional interrupt to occur. The mechanisms for reading TCNT2, OCR2A, OCR2B, TCCR2A and TCCR2B are different. When reading TCNT2, the actual timer value is read. When reading OCR2A, OCR2B, TCCR2A and TCCR2B the value in the temporary storage register is read. 15.10 Timer/Counter Prescaler Figure 15-12. Prescaler for Timer/Counter2 clkT2S/1024 clkT2S/256 clkT2S/128 AS2 clkT2S/64 10-bit T/C Prescaler Clear clkT2S/32 TOSC1 clkT2S clkT2S/8 clkI/O PSRASY 0 CS20 CS21 CS22 Timer/Counter2 Clock Source clkT2 The clock source for Timer/Counter2 is named clkT2S. clkT2S is by default connected to the main system I/O clock clkIO. By setting the AS2 bit in ASSR, Timer/Counter2 is asynchronously clocked from the TOSC1 pin. This enables use of Timer/Counter2 as a real time counter (RTC). When AS2 is set, pins TOSC1 and TOSC2 are disconnected from port C. A crystal can then be connected between the TOSC1 and TOSC2 pins to serve as an independent clock source for Timer/Counter2. The oscillator is optimized for use with a 32.768kHz crystal. Applying an external clock source to TOSC1 is not recommended. For Timer/Counter2, the possible prescaled selections are: clkT2S/8, clkT2S/32, clkT2S/64, clkT2S/128, clkT2S/256, and clkT2S/1024. Additionally, clkT2S as well as 0 (stop) may be selected. Setting the PSRASY bit in GTCCR resets the prescaler. This allows the user to operate with a predictable prescaler. 134 ATmega88/ATmega168 Automotive [DATASHEET] 9365A-AVR-02/16 15.10.1 General Timer/Counter Control Register - GTCCR Bit 7 6 5 4 3 2 TSM - - - - - 1 0 Read/Write R/W R R R R R R/W R/W Initial Value 0 0 0 0 0 0 0 0 PSRASY PSRSYNC GTCCR * Bit 1 - PSRASY: Prescaler Reset Timer/Counter2 When this bit is one, the Timer/Counter2 prescaler will be reset. This bit is normally cleared immediately by hardware. If the bit is written when Timer/Counter2 is operating in asynchronous mode, the bit will remain one until the prescaler has been reset. The bit will not be cleared by hardware if the TSM bit is set. Refer to the description of the Section * "Bit 7 - TSM: Timer/Counter Synchronization Mode" on page 91 for a description of the Timer/Counter synchronization mode. ATmega88/ATmega168 Automotive [DATASHEET] 9365A-AVR-02/16 135 16. Serial Peripheral Interface - SPI The serial peripheral interface (SPI) allows high-speed synchronous data transfer between the Atmel(R) ATmega88/168 and peripheral devices or between several AVR(R) devices. The Atmel ATmega88/168 SPI includes the following features: Full-duplex, three-wire synchronous data transfer Master or slave operation LSB first or MSB first data transfer Seven programmable bit rates End of transmission interrupt flag Write collision flag protection Wake-up from idle mode Double speed (CK/2) master SPI mode The USART can also be used in master SPI mode, see Section 18. "USART in SPI Mode" on page 166. The PRSPI bit in Section 7.7.1 "Power Reduction Register - PRR" on page 35 must be written to zero to enable SPI module. Figure 16-1. SPI Block Diagram(1) MISO S MSB XTAL M M LSB 8-Bit Shift Register Read Data Buffer Pin Control Logic Divider /2/4/8/16/32/64/128 Clock SPI Clock (Master) SPR0 DORD SPR0 SPR1 CPHA CPOL MSTR SPIE SPI Control Register 8 SPI Interrupt Request DORD SPI2X WCOL SPIF 8 SPE MSTR SPE SPI Status Register 136 SPE MSTR SPR1 SPI2X M SS SPI Control 1. SCK S Clock Logic Select Note: MOSI S 8 Internal Data Bus Refer to Figure 1-1 on page 3, and Table 10-3 on page 62 for SPI pin placement. ATmega88/ATmega168 Automotive [DATASHEET] 9365A-AVR-02/16 The interconnection between master and slave CPUs with SPI is shown in Figure 16-2. The system consists of two shift registers, and a master clock generator. The SPI master initiates the communication cycle when pulling low the slave select SS pin of the desired slave. Master and slave prepare the data to be sent in their respective shift registers, and the master generates the required clock pulses on the SCK line to interchange data. Data is always shifted from master to slave on the master out - slave In, MOSI, line, and from slave to master on the master In - slave out, MISO, line. After each data packet, the master will synchronize the slave by pulling high the slave select, SS, line. When configured as a master, the SPI interface has no automatic control of the SS line. This must be handled by user software before communication can start. When this is done, writing a byte to the SPI data register starts the SPI clock generator, and the hardware shifts the eight bits into the slave. After shifting one byte, the SPI clock generator stops, setting the end of transmission flag (SPIF). If the SPI interrupt enable bit (SPIE) in the SPCR register is set, an interrupt is requested. The master may continue to shift the next byte by writing it into SPDR, or signal the end of packet by pulling high the slave select, SS line. The last incoming byte will be kept in the buffer register for later use. When configured as a slave, the SPI interface will remain sleeping with MISO tri-stated as long as the SS pin is driven high. In this state, software may update the contents of the SPI data register, SPDR, but the data will not be shifted out by incoming clock pulses on the SCK pin until the SS pin is driven low. As one byte has been completely shifted, the end of transmission flag, SPIF is set. If the SPI interrupt enable bit, SPIE, in the SPCR register is set, an interrupt is requested. The slave may continue to place new data to be sent into SPDR before reading the incoming data. The last incoming byte will be kept in the buffer register for later use. Figure 16-2. SPI Master-slave Interconnection MSB MASTER LSB MISO MISO 8 Bit Shift Register MSB SLAVE LSB 8 Bit Shift Register SPI Clock Generator MOSI MOSI SCK SCK SS Shift Enable SS The system is single buffered in the transmit direction and double buffered in the receive direction. This means that bytes to be transmitted cannot be written to the SPI data register before the entire shift cycle is completed. When receiving data, however, a received character must be read from the SPI data register before the next character has been completely shifted in. Otherwise, the first byte is lost. In SPI slave mode, the control logic will sample the incoming signal of the SCK pin. To ensure correct sampling of the clock signal, the frequency of the SPI clock should never exceed fosc/4. When the SPI is enabled, the data direction of the MOSI, MISO, SCK, and SS pins is overridden according to Table 16-1 on page 137. For more details on automatic port overrides, refer to Section 10.3 "Alternate Port Functions" on page 60. Table 16-1. SPI Pin Overrides(1) Note: Pin Direction, Master SPI Direction, Slave SPI MOSI User defined Input MISO Input User defined SCK User defined Input SS 1. User defined Input See Section 10.3.2 "Alternate Functions of Port B" on page 62 for a detailed description of how to define the direction of the user defined SPI pins. ATmega88/ATmega168 Automotive [DATASHEET] 9365A-AVR-02/16 137 The following code examples show how to initialize the SPI as a master and how to perform a simple transmission. DDR_SPI in the examples must be replaced by the actual data direction register controlling the SPI pins. DD_MOSI, DD_MISO and DD_SCK must be replaced by the actual data direction bits for these pins. E.g. if MOSI is placed on pin PB5, replace DD_MOSI with DDB5 and DDR_SPI with DDRB. Assembly Code Example(1) SPI_MasterInit: ; Set MOSI and SCK output, all others input ldi r17,(1<>8); UBRRnL = (unsigned char)baud; /* Enable receiver and transmitter */ UCSRnB = (1<> 1) & 0x01; return ((resh << 8) | resl); } Note: 1. The example code assumes that the part specific header file is included. For I/O registers located in extended I/O map, "IN", "OUT", "SBIS", "SBIC", "CBI", and "SBI" instructions must be replaced with instructions that allow access to extended I/O. Typically "LDS" and "STS" combined with "SBRS", "SBRC", "SBR", and "CBR". The receive function example reads all the I/O registers into the register file before any computation is done. This gives an optimal receive buffer utilization since the buffer location read will be free to accept new data as early as possible. ATmega88/ATmega168 Automotive [DATASHEET] 9365A-AVR-02/16 153 17.6.3 Receive Compete Flag and Interrupt The USART receiver has one flag that indicates the receiver state. The receive complete (RXCn) flag indicates if there are unread data present in the receive buffer. This flag is one when unread data exist in the receive buffer, and zero when the receive buffer is empty (i.e., does not contain any unread data). If the receiver is disabled (RXENn = 0), the receive buffer will be flushed and consequently the RXCn bit will become zero. When the receive complete interrupt enable (RXCIEn) in UCSRnB is set, the USART receive complete interrupt will be executed as long as the RXCn flag is set (provided that global interrupts are enabled). When interrupt-driven data reception is used, the receive complete routine must read the received data from UDRn in order to clear the RXCn flag, otherwise a new interrupt will occur once the interrupt routine terminates. 17.6.4 Receiver Error Flags The USART receiver has three error flags: frame error (FEn), data overrun (DORn) and parity arror (UPEn). All can be accessed by reading UCSRnA. Common for the error flags is that they are located in the receive buffer together with the frame for which they indicate the error status. Due to the buffering of the error flags, the UCSRnA must be read before the receive buffer (UDRn), since reading the UDRn I/O location changes the buffer read location. Another equality for the error flags is that they can not be altered by software doing a write to the flag location. However, all flags must be set to zero when the UCSRnA is written for upward compatibility of future USART implementations. None of the error flags can generate interrupts. The frame error (FEn) flag indicates the state of the first stop bit of the next readable frame stored in the receive buffer. The FEn flag is zero when the stop bit was correctly read (as one), and the FEn flag will be one when the stop bit was incorrect (zero). This flag can be used for detecting out-of-sync conditions, detecting break conditions and protocol handling. The FEn flag is not affected by the setting of the USBSn bit in UCSRnC since the receiver ignores all, except for the first, stop bits. For compatibility with future devices, always set this bit to zero when writing to UCSRnA. The data overrun (DORn) flag indicates data loss due to a receiver buffer full condition. A data overrun occurs when the receive buffer is full (two characters), it is a new character waiting in the receive shift register, and a new start bit is detected. If the DORn flag is set there was one or more serial frame lost between the frame last read from UDRn, and the next frame read from UDRn. For compatibility with future devices, always write this bit to zero when writing to UCSRnA. The DORn flag is cleared when the frame received was successfully moved from the shift register to the receive buffer. The parity error (UPEn) flag indicates that the next frame in the receive buffer had a parity error when received. If parity check is not enabled the UPEn bit will always be read zero. For compatibility with future devices, always set this bit to zero when writing to UCSRnA. For more details see Section 17.3.1 "Parity Bit Calculation" on page 149 and Section 17.6.5 "Parity Checker" on page 154. 17.6.5 Parity Checker The parity checker is active when the high USART parity mode (UPMn1) bit is set. Type of parity check to be performed (odd or even) is selected by the UPMn0 bit. When enabled, the parity checker calculates the parity of the data bits in incoming frames and compares the result with the parity bit from the serial frame. The result of the check is stored in the receive buffer together with the received data and stop bits. The parity error (UPEn) flag can then be read by software to check if the frame had a parity error. The UPEn bit is set if the next character that can be read from the receive buffer had a parity error when received and the parity checking was enabled at that point (UPMn1 = 1). This bit is valid until the receive buffer (UDRn) is read. 17.6.6 Disabling the Receiver In contrast to the transmitter, disabling of the receiver will be immediate. Data from ongoing receptions will therefore be lost. When disabled (i.e., the RXENn is set to zero) the receiver will no longer override the normal function of the RxDn port pin. The receiver buffer FIFO will be flushed when the receiver is disabled. Remaining data in the buffer will be lost. 154 ATmega88/ATmega168 Automotive [DATASHEET] 9365A-AVR-02/16 17.6.7 Flushing the Receive Buffer The receiver buffer FIFO will be flushed when the receiver is disabled, i.e., the buffer will be emptied of its contents. Unread data will be lost. If the buffer has to be flushed during normal operation, due to for instance an error condition, read the UDRn I/O location until the RXCn flag is cleared. The following code example shows how to flush the receive buffer. Assembly Code Example(1) USART_Flush: sbis UCSRnA, RXCn ret in r16, UDRn rjmp USART_Flush C Code Example(1) void USART_Flush (void) { unsigned char dummy; while (UCSRnA & (1< 2 CPU clock cycles for fck < 12MHz, 3 CPU clock cycles for fck 12MHz High: > 2 CPU clock cycles for fck < 12MHz, 3 CPU clock cycles for fck 12MHz 24.8.1 Serial Programming Algorithm When writing serial data to the Atmel(R) ATmega88/168, data is clocked on the rising edge of SCK. When reading data from the ATmega88/168, data is clocked on the falling edge of SCK. See Figure 24-11 on page 249 for timing details. To program and verify the ATmega88/168 in the serial programming mode, the following sequence is recommended (See four byte instruction formats in Table 24-16 on page 250): 1. Power-up sequence: Apply power between VCC and GND while RESET and SCK are set to "0". In some systems, the programmer can not guarantee that SCK is held low during power-up. In this case, RESET must be given a positive pulse of at least two CPU clock cycles duration after SCK has been set to "0". 248 2. Wait for at least 20 ms and enable serial programming by sending the programming enable serial instruction to pin MOSI. 3. The serial programming instructions will not work if the communication is out of synchronization. When in sync. the second byte (0x53), will echo back when issuing the third byte of the programming enable instruction. Whether the echo is correct or not, all four bytes of the instruction must be transmitted. If the 0x53 did not echo back, give RESET a positive pulse and issue a new programming enable command. ATmega88/ATmega168 Automotive [DATASHEET] 9365A-AVR-02/16 4. The flash is programmed one page at a time. The memory page is loaded one byte at a time by supplying the 6 LSB of the address and data together with the load program memory page instruction. To ensure correct loading of the page, the data low byte must be loaded before data high byte is applied for a given address. The program memory page is stored by loading the write program memory page instruction with the 8MSB of the address. If polling is not used, the user must wait at least tWD_FLASH before issuing the next page. (See Table 24-15.) Accessing the serial programming interface before the flash write operation completes can result in incorrect programming. 5. The EEPROM array is programmed one byte at a time by supplying the address and data together with the appropriate Write instruction. An EEPROM memory location is first automatically erased before new data is written. If polling is not used, the user must wait at least tWD_EEPROM before issuing the next byte. (See Table 24-15.) In a chip erased device, no 0xFFs in the data file(s) need to be programmed. 6. Any memory location can be verified by using the read instruction which returns the content at the selected address at serial output MISO. 7. At the end of the programming session, RESET can be set high to commence normal operation. 8. Power-off sequence (if needed): Set RESET to "1". Turn VCC power off. 24.8.2 Data Polling Flash When a page is being programmed into the flash, reading an address location within the page being programmed will give the value 0xFF. At the time the device is ready for a new page, the programmed value will read correctly. This is used to determine when the next page can be written. Note that the entire page is written simultaneously and any address within the page can be used for polling. Data polling of the flash will not work for the value 0xFF, so when programming this value, the user will have to wait for at least tWD_FLASH before programming the next page. As a chip-erased device contains 0xFF in all locations, programming of addresses that are meant to contain 0xFF, can be skipped. See Table 24-15 for tWD_FLASH value. 24.8.3 Data Polling EEPROM When a new byte has been written and is being programmed into EEPROM, reading the address location being programmed will give the value 0xFF. At the time the device is ready for a new byte, the programmed value will read correctly. This is used to determine when the next byte can be written. This will not work for the value 0xFF, but the user should have the following in mind: As a chip-erased device contains 0xFF in all locations, programming of addresses that are meant to contain 0xFF, can be skipped. This does not apply if the EEPROM is re-programmed without chip erasing the device. In this case, data polling cannot be used for the value 0xFF, and the user will have to wait at least tWD_EEPROM before programming the next byte. See Table 24-15 for tWD_EEPROM value. Table 24-15. Minimum Wait Delay Before Writing the Next Flash or EEPROM Location Symbol Minimum Wait Delay tWD_FLASH 4.5ms tWD_EEPROM 3.6ms tWD_ERASE 9.0ms Figure 24-11.Serial Programming Waveforms SERIAL DATA INPUT (MOSI) MSB LSB SERIAL DATA OUTPUT (MISO) MSB LSB SERIAL CLOCK INPUT (SCK) SAMPLE ATmega88/ATmega168 Automotive [DATASHEET] 9365A-AVR-02/16 249 Table 24-16. Serial Programming Instruction Set Instruction Format Instruction Byte 1 Byte 2 Byte 3 Byte4 Programming enable 1010 1100 0101 0011 xxxx xxxx xxxx xxxx Enable serial programming after RESET goes low. Chip erase 1010 1100 100x xxxx xxxx xxxx xxxx xxxx Chip erase EEPROM and flash. Read program memory 0010 H000 000a aaaa bbbb bbbb Read H (high or low) data o from oooo oooo program memory at word address a:b. Write H (high or low) data i to program memory page at word address b. Data low byte must be loaded before data high byte is applied within the same address. Load program memory page 0100 H000 000x xxxx xxbb bbbb iiii iiii Write program memory page 0100 1100 000a aaaa bbxx xxxx xxxx xxxx Operation Write program memory page at address a:b. Read EEPROM memory 1010 0000 000x xxaa bbbb bbbb oooo oooo Read data o from EEPROM memory at address a:b. Write EEPROM memory 1100 0000 000x xxaa bbbb bbbb iiii iiii Write data i to EEPROM memory at address a:b. Load EEPROM memory page ( page access) 1100 0001 0000 0000 0000 00bb iiii iiii Load data i to EEPROM memory page buffer. After data is loaded, program EEPROM page. Write EEPROM memory page ( page access) 1100 0010 00xx xxaa bbbb bb00 xxxx xxxx Read lock bits 0101 1000 0000 0000 xxxx xxxx Write lock bits 1010 1100 111x xxxx xxxx xxxx Read signature byte 0011 0000 000x xxxx xxxx xxbb Write fuse bits 1010 1100 1010 0000 xxxx xxxx iiii iiii Set bits = "0" to program, "1" to unprogram. See Table 21-1 on page 209 for details. Write fuse high bits 1010 1100 1010 1000 xxxx xxxx iiii iiii Set bits = "0" to program, "1" to unprogram. See Table 21-1 on page 209 for details. Write extended fuse bits 1010 1100 1010 0100 xxxx xxxx xxxx xxii Read fuse bits 0101 0000 0000 0000 xxxx xxxx Write EEPROM page at address a:b. Read lock bits. "0" = programmed, "1" xxoo oooo = unprogrammed. See Table 24-1 on page 234 for details. 11ii iiii Write lock bits. Set bits = "0" to program lock bits. See Table 24-1 on page 234 for details. oooo oooo Read signature byte o at address b. Set bits = "0" to program, "1" to unprogram. Read Fuse bits. "0" = programmed, oooo oooo "1" = unprogrammed. See Table 21-1 on page 209 for details. Read fuse high bits. "0" = programmed, "1" = unprogrammed. Read fuse high bits 0101 1000 0000 1000 xxxx xxxx oooo oooo See Table 21-1 on page 209 for details. Note: a = address high bits, b = address low bits, H = 0 - low byte, 1 - high byte, o = data out, i = data in, x = don't care 250 ATmega88/ATmega168 Automotive [DATASHEET] 9365A-AVR-02/16 Table 24-16. Serial Programming Instruction Set (Continued) Instruction Format Instruction Byte 1 Byte 2 Byte 3 Byte4 Operation Read extended fuse bits 0101 0000 0000 1000 xxxx xxxx oooo oooo Read calibration byte 0011 1000 000x xxxx 0000 0000 oooo oooo Read calibration byte Read extended fuse bits. "0" = programmed, "1" = unprogrammed. If o = "1", a programming operation is still busy. Wait until this bit returns to Poll RDY/BSY 1111 0000 0000 0000 xxxx xxxx xxxx xxxo "0" before applying another command. Note: a = address high bits, b = address low bits, H = 0 - low byte, 1 - high byte, o = data out, i = data in, x = don't care 24.8.4 SPI Serial Programming Characteristics For characteristics of the SPI module see Section 26.1 "SPI Timing Characteristics" on page 259. ATmega88/ATmega168 Automotive [DATASHEET] 9365A-AVR-02/16 251 25. Electrical Characteristics 25.1 Absolute Maximum Ratings Stresses beyond those listed under "Absolute Maximum Ratings" may cause permanent damage to the device. This is a stress rating only and functional operation of the device at these or any other conditions beyond those indicated in the operational sections of this specification is not implied. Exposure to absolute maximum rating conditions for extended periods may affect device reliability. Parameters Test Conditions Unit Operating temperature -55 to +150 C Storage temperature -65 to +175 C -0.5 to VCC+0.5 V -0.5 to +13.0 V 6.0 V 30 200.0 mA Voltage on any pin except RESET with respect to ground Voltage on RESET with respect to ground Maximum operating voltage DC current per I/O pin DC current VCC and GND 25.2 DC Characteristics TA = -40C to +150C, VCC = 2.7V to 5.5V (unless otherwise noted) Parameters Test Conditions Symbol Min. Input low voltage, except XTAL1 and RESET pin VCC = 2.7V to 5.5V VIL Input high voltage, except XTAL1 and RESET pins VCC = 2.7V to 5.5V Input low voltage, XTAL1 pin Max. Unit -0.5 +0.3VCC(1) V VIH 0.6VCC(2) VCC + 0.5 V VCC = 2.7V to 5.5V VIL1 -0.5 +0.1VCC(2) V Input high voltage, XTAL1 pin VCC = 2.7V to 5.5V VIH1 0.7VCC(2) VCC + 0.5 V Input low voltage, RESET pin VCC = 2.7V to 5.5V VIL2 -0.5 +0.2VCC(1) V VCC + 0.5 V Input high voltage, VCC = 2.7V to 5.5V VIH2 0.9VCC(2) RESET pin Notes: 1. "Max" means the highest value where the pin is guaranteed to be read as low Typ. 2. "Min" means the lowest value where the pin is guaranteed to be read as high 3. Although each I/O port can sink more than the test conditions (20mA at VCC = 5V) under steady state conditions (non-transient), the following must be observed: 1] The sum of all IOL, for all ports, should not exceed 400mA. 2] The sum of all IOL, for ports C0 - C5, should not exceed 200mA. 3] The sum of all IOL, for ports C6, D0 - D4, should not exceed 300mA. 4] The sum of all IOL, for ports B0 - B7, D5 - D7, should not exceed 300mA. If IOL exceeds the test condition, VOL may exceed the related specification. Pins are not guaranteed to sink current greater than the listed test condition. 4. Although each I/O port can source more than the test conditions (20mA at VCC = 5V) under steady state conditions (non-transient), the following must be observed: 1] The sum of all IOH, for all ports, should not exceed 400mA. 2] The sum of all IOH, for ports C0 - C5, should not exceed 200mA. 3] The sum of all IOH, for ports C6, D0 - D4, should not exceed 300mA. 4] The sum of all IOH, for ports B0 - B7, D5 - D7, should not exceed 300mA. If IOH exceeds the test condition, VOH may exceed the related specification. Pins are not guaranteed to source current greater than the listed test condition. 5. Minimum VCC for Power-down is 2.5V 252 ATmega88/ATmega168 Automotive [DATASHEET] 9365A-AVR-02/16 25.2 DC Characteristics (Continued) TA = -40C to +150C, VCC = 2.7V to 5.5V (unless otherwise noted) Parameters Test Conditions Input low voltage, RESET pin as I/O Symbol Min. Typ. Max. Unit VCC = 2.7V to 5.5V VIL3 -0.5 +0.3VCC(1) V Input high voltage, RESET pin as I/O VCC = 2.7V to 5.5V VIH3 0.6VCC(2) VCC + 0.5 V Output low voltage(3), I/O pin except RESET IOL = 20mA, VCC = 5V IOL = 5mA, VCC = 3V VOL 0.8 0.5 V Output high voltage(4) I/O pin except RESET IOH = -20mA, VCC = 5V IOH = -10mA, VCC = 3V VOH Input leakage current I/O pin VCC = 5.5V, pin low (absolute value) IIL 1 A Input leakage current I/O pin VCC = 5.5V, pin high (absolute value) IIH 1 A 4.0 2.2 V Reset pull-up resistor RRST 30 60 k I/O pin pull-up resistor RPU 20 50 k 8 16 mA 25 mA 6 12 mA Idle 16MHz, VCC = 5V 14 mA WDT enabled, VCC = 3V WDT enabled, VCC = 5V 90 140 A 80 120 A 40 mV +50 nA Active 4MHz, VCC = 3V Active 8MHz, VCC = 5V Power supply current(5) Power-down mode ICC Active 16MHz, VCC = 5V Idle 4MHz, VCC = 3V Idle 8MHz, VCC = 5V WDT disabled, VCC = 3V WDT disabled, VCC = 5V ICC IDLE ICC PWD Analog comparator input offset voltage VCC = 5V Vin = VCC/2 VACIO Analog comparator input leakage current VCC = 5V Vin = VCC/2 IACLK < 10 -50 Analog comparator VCC = 4.0V tACPD 500 propagation delay Notes: 1. "Max" means the highest value where the pin is guaranteed to be read as low ns 2. "Min" means the lowest value where the pin is guaranteed to be read as high 3. Although each I/O port can sink more than the test conditions (20mA at VCC = 5V) under steady state conditions (non-transient), the following must be observed: 1] The sum of all IOL, for all ports, should not exceed 400mA. 2] The sum of all IOL, for ports C0 - C5, should not exceed 200mA. 3] The sum of all IOL, for ports C6, D0 - D4, should not exceed 300mA. 4] The sum of all IOL, for ports B0 - B7, D5 - D7, should not exceed 300mA. If IOL exceeds the test condition, VOL may exceed the related specification. Pins are not guaranteed to sink current greater than the listed test condition. 4. Although each I/O port can source more than the test conditions (20mA at VCC = 5V) under steady state conditions (non-transient), the following must be observed: 1] The sum of all IOH, for all ports, should not exceed 400mA. 2] The sum of all IOH, for ports C0 - C5, should not exceed 200mA. 3] The sum of all IOH, for ports C6, D0 - D4, should not exceed 300mA. 4] The sum of all IOH, for ports B0 - B7, D5 - D7, should not exceed 300mA. If IOH exceeds the test condition, VOH may exceed the related specification. Pins are not guaranteed to source current greater than the listed test condition. 5. Minimum VCC for Power-down is 2.5V ATmega88/ATmega168 Automotive [DATASHEET] 9365A-AVR-02/16 253 25.3 Memory Endurance EEPROM endurance: 50,000 write/erase cycles. Flash endurance: 10,000 write/erase cycles. 25.4 Maximum Speed versus VCC Maximum frequency is dependent on VCC. As shown in Figure 25-1, the maximum frequency versus VCC curve is linear between 2.7V < VCC < 4.5V. Figure 25-1. Maximum Frequency versus VCC 16MHz 8MHz Safe Operating Area 4MHz 2.7V 25.5 4.5V 5.5V External Clock Drive Waveforms Figure 25-2. External Clock Drive Waveforms tCHCX tCLCH tCHCX tCHCL VIH1 VIL1 tCLCX tCLCL 25.6 External Clock Drive Table 25-1. External Clock Drive VCC = 2.7 to 5.5V VCC = 4.5 to 5.5V Parameter Symbol Min. Max. Min. Max. Unit Oscillator frequency 1/tCLCL 0 8 0 16 MHz Clock period tCLCL 125 62.5 ns High time tCHCX 50 25 ns 50 Low time tCLCX Rise time tCLCH 1.6 0.5 s Fall time tCHCL 1.6 0.5 s Change in period from one clock cycle to the next tCLCL 2 2 % 254 ATmega88/ATmega168 Automotive [DATASHEET] 9365A-AVR-02/16 25 ns 25.7 LIN Re-synchronization Algorithm 25.8 Synchronization Algorithm The possibility to change the value of OSCCAL during the oscillator operation allows for in-situ calibration of the slave node to entering Master frames. The principle of operation is to measure the TBit during the SYNCH byte and to change the calibration value of OSCCAL to recover from local frequency drifts due to local voltage or temperature deviation. The algorithm used for the synchronization of the internal RC oscillator is depicted in Figure 25-3 on page 255. Figure 25-3. Dichotomic Algorithm Used for LIN Slave Clock Re-synchronization Measuring actual TBit -2% < Delta (TBit) < 2% Y STOP: Oscillator Calibrated N Decrement OSCCAL Delta(TBit) < 2% N Increment OSCCAL Delta(TBit) < -2% ATmega88/ATmega168 Automotive [DATASHEET] 9365A-AVR-02/16 255 25.9 Precaution Against OSCCAL Discontinuity The Figure 27-18 on page 266 illustrates the on-purpose discontinuity of RC frequency versus OSCCAL value. For one correct re-synchronization, the frequency change must be kept on the same side of the discontinuity (no change of OSCCAL[7]). Since there will be no device having frequency changed by more than 10% (see Figure 27-17 on page 266), thus no reason to change the frequency value by more than 10%. Therefore, when calibration tries to cross the border because of subsequent increase (or decrease) in OSCCAL values, then the routine must be stopped. Example: For parts operating in the lower part of the curve, if new_OSCCAL >127 then new_OSCCAL = 127. Similar for parts operating on the high side of the discontinuity. 25.9.1 RC Oscillator Precision for LIN Slave implementation For LIN slave devices, the precision of the RC oscillator before and after re-synchronization are described in the Table 25-2. Table 25-2. Oscillator Tolerance Before and After Re-Synchronization Algorithm (2.7V < VCC < 5.5V, -40C to +125C) Parameter Clock Tolerance FTOL_UNSYNCH Deviation of slave node clock from the nominal clock rate before synchronization; relevant for nodes making use of synchronization and direct SYNCH BREAK detection. FTOL_SYNCH Deviation of slave node clock relative to the master node clock after synchronization; relevant for nodes making use of synchronization; any slave node must stay within this tolerance for all fields of a frame which follow the SYNCH FIELD. Note: For communication between any two nodes their bit rate must not differ by more than 2%. 256 ATmega88/ATmega168 Automotive [DATASHEET] 9365A-AVR-02/16 F/FMaster 14.0% 2.0% 26. 2-wire Serial Interface Characteristics Table 26-1 describes the requirements for devices connected to the 2-wire serial bus. The Atmel(R) ATmega88/168 2-wire serial interface meets or exceeds these requirements under the noted conditions. Timing symbols refer to Figure 26-1. Table 26-1. 2-wire Serial Bus Requirements Parameter Condition Input low-voltage Input high-voltage 3mA sink current Rise time for both SDA and SCL Output fall time from VIHmin to VILmax 10pF < Cb < 400pF(3) Spikes suppressed by input filter Input current each I/O pin 0.1VCC < Vi < 0.9VCC Capacitance for each I/O Pin SCL clock frequency fCK(4) > max(16fSCL, 250kHz) Min. Max. Unit VIL -0.5 0.3 VCC V 0.7 VCC VCC + 0.5 V - V VIH (1) Vhys (1) VOL (1) tr (1) tof (1) tSP Hysteresis of schmitt trigger inputs Output low-voltage Symbol (5) 0.05 0 0.4 V 20 + 0.1Cb(3)(2) 300 ns 20 + 0.1Cb(3)(2) 250 ns (2) ns Low period of the SCL clock High period of the SCL clock Set-up time for a repeated START condition Data hold time Notes: 1. -10 10 A - 10 pF fSCL 0 400 kHz V CC - 0,4V ---------------------------3mA 1000ns ----------------Cb V CC - 0,4V ---------------------------3mA 300ns -------------Cb 4.0 - s 0.6 - s 4.7 - s 1.3 - s 4.0 - s 0.6 - s 4.7 - s 0.6 - s 0 3.45 s 0 0.9 s Rp fSCL 100kHz fSCL > 100kHz fSCL 100kHz tHD;STA (6) (7) fSCL > 100kHz fSCL 100kHz fSCL > 100kHz fSCL 100kHz fSCL > 100kHz fSCL 100kHz tLOW tHIGH tSU;STA tHD;DAT fSCL > 100kHz In Atmel ATmega88/168, this parameter is characterized and not 100% tested. 2. Required only for fSCL > 100kHz. 3. Cb = capacitance of one bus line in pF. 50 Ii fSCL > 100kHz Hold time (repeated) START Condition 0 Ci(1) fSCL 100kHz Value of pull-up resistor VCC(2) 4. fCK = CPU clock frequency 5. This requirement applies to all ATmega88/168 2-wire serial interface operation. Other devices connected to the 2-wire serial bus need only obey the general fSCL requirement. 6. The actual low period generated by the Atmel ATmega88/168 2-wire serial interface is (1/fSCL - 2/fCK), thus fCK must be greater than 6MHz for the low time requirement to be strictly met at fSCL = 100kHz. 7. The actual low period generated by the ATmega88/168 2-wire serial interface is (1/fSCL - 2/fCK), thus the low time requirement will not be strictly met for fSCL > 308kHz when fCK = 8MHz. Still, ATmega88/168 devices connected to the bus may communicate at full speed (400kHz) with other ATmega88/168 devices, as well as any other device with a proper tLOW acceptance margin. ATmega88/ATmega168 Automotive [DATASHEET] 9365A-AVR-02/16 257 Table 26-1. 2-wire Serial Bus Requirements (Continued) Parameter Condition Symbol fSCL 100kHz Data setup time tSU;DAT fSCL > 100kHz fSCL 100kHz Setup time for STOP condition tSU;STO fSCL > 100kHz Max. Unit 250 - ns 100 - ns 4.0 - s 0.6 - s 4.7 - s 1.3 - s 1. fSCL 100kHz tBUF fSCL > 100kHz In Atmel ATmega88/168, this parameter is characterized and not 100% tested. 2. Required only for fSCL > 100kHz. 3. Cb = capacitance of one bus line in pF. 4. fCK = CPU clock frequency 5. This requirement applies to all ATmega88/168 2-wire serial interface operation. Other devices connected to the 2-wire serial bus need only obey the general fSCL requirement. 6. The actual low period generated by the Atmel ATmega88/168 2-wire serial interface is (1/fSCL - 2/fCK), thus fCK must be greater than 6MHz for the low time requirement to be strictly met at fSCL = 100kHz. 7. The actual low period generated by the ATmega88/168 2-wire serial interface is (1/fSCL - 2/fCK), thus the low time requirement will not be strictly met for fSCL > 308kHz when fCK = 8MHz. Still, ATmega88/168 devices connected to the bus may communicate at full speed (400kHz) with other ATmega88/168 devices, as well as any other device with a proper tLOW acceptance margin. Bus free time between a STOP and START condition Notes: Min. Figure 26-1. 2-wire Serial Bus Timing tof tHIGH tLOW tr tLOW SCL tSU,STA tHD,STA tHD,DAT tSU,DAT tSU,STO SDA tBUF 258 ATmega88/ATmega168 Automotive [DATASHEET] 9365A-AVR-02/16 26.1 SPI Timing Characteristics See Figure 26-2 and Figure 26-3 for details. Table 26-2. SPI Timing Parameters No. Description Mode 1 SCK period Master See Table 16-4 2 SCK high/low Master 50% duty cycle 3 Rise/fall time Master 3.6 4 Setup Master 10 5 Hold Master 10 6 Out to SCK Master 0.5 tsck 7 SCK to out Master 10 8 SCK to out high Master 10 9 SS low to out Slave 15 10 SCK period Slave 4 tck 11 SCK high/low(1) Slave 2 tck 12 Rise/fall time Slave 13 Setup Slave 10 14 Hold Slave tck 15 SCK to out Slave 16 SCK to SS high Slave 17 SS high to tri-state Slave 18 Note: 1. Min. Typ. Max. Unit ns 1600 15 20 10 SS low to SCK Slave 20 In SPI programming mode the minimum SCK high/low period is: - 2 tCLCL for fCK < 12MHz - 3 tCLCL for fCK > 12MHz Figure 26-2. SPI Interface Timing Requirements (Master Mode) SS 6 1 SCK (CPOL = 0) 2 2 SCK (CPOL = 1) 4 MISO (Data Input) 5 3 ... MSB LSB 8 7 MOSI (Data Output) MSB ... LSB ATmega88/ATmega168 Automotive [DATASHEET] 9365A-AVR-02/16 259 Figure 26-3. SPI Interface Timing Requirements (Slave Mode) SS 16 10 9 SCK (CPOL = 0) 11 11 SCK (CPOL = 1) 13 MOSI (Data Input) 14 12 ... MSB LSB 17 15 MISO (Data Output) 26.2 ... MSB LSB X ADC Characteristics(1) TA = -40C to +150C, VCC = 4.5V to 5.5V (unless otherwise noted) Parameters Test Conditions Symbol Min Resolution Typ Max 10 Unit Bits VREF = 4V, VCC = 4V, ADC clock = 200kHz 2 3.5 LSB VREF = 4V, VCC = 4V, ADC clock = 200kHz Noise reduction mode 2 3.5 LSB Integral non-linearity (INL) VREF = 4V, VCC = 4V, ADC clock = 200kHz 0.6 2.5 LSB Differential non-linearity (DNL) VREF = 4V, VCC = 4V, ADC clock = 200kHz 0.30 1.0 LSB Gain error VREF = 4V, VCC = 4V, ADC clock = 200kHz -1.3 +3.5 LSB Offset error VREF = 4V, VCC = 4V, ADC clock = 200kHz 1.8 3.5 LSB Conversion time Free running conversion 50 200 kHz Absolute accuracy (including INL, DNL, quantization error, gain and offset error) -3.5 13 cycles Clock frequency s Analog supply voltage AVCC VCC - 0.3 VCC + 0.3 V Reference voltage VREF 1.0 AVCC V VIN GND Input voltage Input bandwidth VREF 38.5 V kHz Internal voltage reference VINT 1.0 1.1 1.2 V Reference input resistance RREF 25.6 32 38.4 k Analog input resistance RAIN 260 ATmega88/ATmega168 Automotive [DATASHEET] 9365A-AVR-02/16 100 M 27. ATmega88/168 Typical Characteristics 27.1 Active Supply Current Figure 27-1. Active Supply Current versus Frequency (1MHz to 20MHz) 16 5.5V 14 5.0V ICC (mA) 12 10 8 3.3V 3.0V 6 4 2 0 0 2 4 6 8 10 12 16 14 18 20 Frequency (MHz) Figure 27-2. Idle Supply Current versus Frequency (1MHz to 20MHz) 8 ICC (mA) 6 4 5.5V 5.0V 2 3.3V 3.0V 0 4 6 8 10 12 14 16 18 20 Frequency (MHz) 27.2 Power-Down Supply Current Figure 27-3. Power-down Supply Current versus VCC (Watchdog Timer Disabled) ATmega88/ATmega168 Automotive [DATASHEET] 9365A-AVR-02/16 261 Figure 27-4. Power-down Supply Current versus VCC (Watchdog Timer Enabled) 35 150C 30 ICC (A) 25 20 15 125C 10 -40C 85C 25C 5 0 2.5 3 3.5 4 4.5 5 5.5 VCC (V) 27.3 Pin Pull-up Figure 27-5. I/O Pin Pull-up Resistor Current versus Input Voltage (VCC = 5V) 160 150C 140 IOP (A) 120 -40C 100 80 60 40 20 0 0 1 2 3 4 5 6 VOP (V) Figure 27-6. Output Low Voltage versus Output Low Current (VCC = 5V) 0.8 0.7 150C 125C 0.6 VOL (V) 85C 0.5 25C 0.4 -40C 0.3 0.2 0.1 0 0 2 4 6 8 10 IOL (mA) 262 ATmega88/ATmega168 Automotive [DATASHEET] 9365A-AVR-02/16 12 14 16 18 20 Figure 27-7. Output Low Voltage versus Output Low Current (VCC = 3V) 1.4 1.2 150C 125C VOL (V) 1.0 85C 0.8 25C 0.6 -40C 0.4 0.2 0 0 2 4 6 8 10 12 14 16 18 20 IOL (mA) Figure 27-8. Output High Voltage versus Output High Current (VCC = 5V) 5.2 5.0 VOH (V) 4.8 4.6 -40C 25C 85C 125C 150C 4.4 4.2 4 0 2 4 6 8 10 12 14 16 18 20 IOH (mA) Figure 27-9. Output High Voltage versus Output High Current (VCC = 3V) 3.5 3.0 Current (V) 2.5 -40C 25C 85C 125C 150C 2.0 1.5 1.0 0.5 0 0 2 4 6 8 10 12 14 16 18 20 IOH (mA) ATmega88/ATmega168 Automotive [DATASHEET] 9365A-AVR-02/16 263 Figure 27-10. Reset Pull-up Resistor Current versus Reset Pin Voltage (VCC = 5V) 140 IRESET (A) 120 150C 100 80 -40C 60 40 20 0 0 1 2 3 4 5 6 VRESET (V) 27.4 Pin Thresholds and Hysteresis Figure 27-11. I/O Pin Input Threshold versus VCC (VIH, I/O Pin Read as `1') 3 150C -40C 2.5 VIH (V) 2.0 1.5 1.0 0.5 0 2.5 3 3.5 4 4.5 5 5.5 VCC (V) Figure 27-12. I/O Pin Input Threshold versus VCC (VIL, I/O Pin Read as `0') 3 150C -40C 2.5 VIL (V) 2.0 1.5 1.0 0.5 0 2.5 3 3.5 4 VCC (V) 264 ATmega88/ATmega168 Automotive [DATASHEET] 9365A-AVR-02/16 4.5 5 5.5 Figure 27-13. Reset Input Threshold Voltage versus VCC (VIH, Reset Pin Read as `1') 3 Threshold (V) 2.5 2.0 -40C 1.5 1.0 150C 0.5 0 2.5 3 3.5 4 4.5 5 5.5 VCC (V) Figure 27-14. Reset Input Threshold Voltage versus VCC (VIL, Reset Pin Read as `0') 2.5 Threshold (V) 2.0 1.5 150C -40C 1.0 0.5 0 2.5 3 3.5 4 4.5 5 5.5 VCC (V) Internal Oscillator Speed Figure 27-15. Watchdog Oscillator Frequency versus VCC 190 170 FRC (kHz) 27.5 150 2.7V 3.0V 5.0V 5.5V 130 110 90 70 -40 -30 -20 -10 0 10 20 30 40 50 60 70 80 90 100 110 120 130 140 150 160 Temperature ATmega88/ATmega168 Automotive [DATASHEET] 9365A-AVR-02/16 265 Figure 27-16. Calibrated 8MHz RC Oscillator Frequency versus Temperature 8.4 5.5V 5.0V 4.5V 3.3V 3.0V 2.7V 8.3 FRC (MHz) 8.2 8.1 8.0 7.9 7.8 7.7 7.6 -40 -30 -20 -10 0 10 20 30 40 50 60 70 80 90 100 110 120 130 140 150 Temperature Figure 27-17. Calibrated 8MHz RC Oscillator Frequency versus VCC 8.4 150C 8.3 125C FRC (MHz) 8.2 85C 8.1 25C 8.0 -40C 7.9 7.8 7.7 7.6 2 2.5 3 3.5 4 4.5 5 5.5 6 VCC (V) Figure 27-18. Calibrated 8MHz RC Oscillator Frequency versus OSCCAL Value 16 150C -40C 14 FRC (MHz) 12 10 8 6 4 2 0 0 16 32 48 64 80 96 112 128 144 160 176 192 208 224 240 256 OSCCAL (X1) 266 ATmega88/ATmega168 Automotive [DATASHEET] 9365A-AVR-02/16 BOD Thresholds and Analog Comparator Offset Figure 27-19. BOD Threshold versus Temperature (BODLEVEL is 4.0V) 4.6 Threshold (V) 4.5 4.4 1 4.3 0 4.2 4.1 4.0 -50 -40 -30 -20 -10 0 10 20 30 40 50 60 70 80 90 100 110 120 130 140 150 160 Temperature (C) Figure 27-20. BOD Threshold versus Temperature (BODLEVEL is 2.7V) 3.0 Threshold (V) 2.9 2.8 1 2.7 0 2.6 2.5 2.4 -50 -40 -30 -20 -10 0 10 20 30 40 50 60 70 80 90 100 110 120 130 140 150 160 Temperature (C) Figure 27-21. Bandgap Voltage versus VCC 1.25 Bandgap Voltage (V) 27.6 1.20 1.15 1.10 150C -40C 1.05 1.00 0.95 2 2.5 3 3.5 4 4.5 5 5.5 VCC (V) ATmega88/ATmega168 Automotive [DATASHEET] 9365A-AVR-02/16 267 27.7 Peripheral Units Figure 27-22. Analog to Digital Converter GAIN versus VCC 0 Error (LSB) -0.5 -1.0 4 IDL -1.5 4 STD -2.0 -2.5 -50 -25 0 25 50 75 100 125 150 Temperature Figure 27-23. Analog to Digital Converter OFFSET versus VCC 2.5 4 IDL Error (LSB) 2.0 4 STD 1.5 1.0 0.5 0 -50 -25 0 25 50 75 100 125 150 Temperature Figure 27-24. Analog to Digital Converter DNL versus VCC 1.0 0.9 Error (LSB) 0.8 0.7 0.6 0.5 0.4 4 IDL 0.3 4 STD 0.2 0.1 0 -50 -25 0 25 50 75 Temperature 268 ATmega88/ATmega168 Automotive [DATASHEET] 9365A-AVR-02/16 100 125 150 Figure 27-25. Analog to Digital Converter INL versus VCC 1.0 0.9 Error (LSB) 0.8 0.7 0.6 4 IDL 0.5 4 STD 0.4 0.3 0.2 0.1 0 -50 -25 0 25 50 75 100 125 150 Temperature Grade 0 Qualification The ATmega88/ATmega168 Automotive has been developed and manufactured according to the most stringent quality assurance requirements of ISO-TS-16949 and verified during product qualification as per AEC-Q100 grade 0. AEC-Q100 qualification relies on temperature accelerated stress testing. High temperature field usage however may result in less significant stress test acceleration. In order to prevent the risk that ATmega88/ATmega168 Automotive lifetime would not satisfy the application end-of-life reliability requirements, Atmel(R) has extended the testing, whenever applicable (High Temperature Operating Life Test, High Temperature Storage Life, Data Retention, Thermal Cycles), far beyond the AECQ100 requirements. Thereby, Atmel verified the ATmega88/ATmega168 Automotive has a long safe lifetime period after the grade 0 qualification acceptance limits. The valid domain calculation depends on the activation energy of the potential failure mechanism that is considered. Examples are given in Figure 27-26. Therefore any temperature mission profile which could exceed the AEC-Q100 equivalence domain shall be submitted to Atmel for a thorough reliability analysis. Figure 27-26. AEC-Q100 Lifetime Equivalence 1000000 100000 10000 Hours 27.8 1000 100 10 1 0 20 40 60 80 100 120 140 160 Temperature (C) HTOL 0.59eV HTSL 0.45eV ATmega88/ATmega168 Automotive [DATASHEET] 9365A-AVR-02/16 269 28. Register Summary Address Name Bit 7 Bit 6 Bit 5 Bit 4 Bit 3 Bit 2 Bit 1 Bit 0 (0xFF) Reserved - - - - - - - - (0xFE) Reserved - - - - - - - - (0xFD) Reserved - - - - - - - - (0xFC) Reserved - - - - - - - - (0xFB) Reserved - - - - - - - - (0xFA) Reserved - - - - - - - - (0xF9) Reserved - - - - - - - - (0xF8) Reserved - - - - - - - - (0xF7) Reserved - - - - - - - - (0xF6) Reserved - - - - - - - - (0xF5) Reserved - - - - - - - - (0xF4) Reserved - - - - - - - - (0xF3) Reserved - - - - - - - - (0xF2) Reserved - - - - - - - - (0xF1) Reserved - - - - - - - - (0xF0) Reserved - - - - - - - - (0xEF) Reserved - - - - - - - - (0xEE) Reserved - - - - - - - - (0xED) Reserved - - - - - - - - (0xEC) Reserved - - - - - - - - (0xEB) Reserved - - - - - - - - (0xEA) Reserved - - - - - - - - (0xE9) Reserved - - - - - - - - (0xE8) Reserved - - - - - - - - (0xE7) Reserved - - - - - - - - (0xE6) Reserved - - - - - - - - (0xE5) Reserved - - - - - - - - (0xE4) Reserved - - - - - - - - (0xE3) Reserved - - - - - - - - (0xE2) Reserved - - - - - - - - (0xE1) Reserved - - - - - - - - (0xE0) Reserved - - - - - - - - (0xDF) Reserved - - - - - - - - (0xDE) Reserved - - - - - - - - (0xDD) Reserved - - - - - - - - (0xDC) Reserved - - - - - - - - Notes: 270 Page 1. For compatibility with future devices, reserved bits should be written to zero if accessed. Reserved I/O memory addresses should never be written. 2. I/O registers within the address range 0x00 - 0x1F are directly bit-accessible using the SBI and CBI instructions. In these registers, the value of single bits can be checked by using the SBIS and SBIC instructions. 3. Some of the status flags are cleared by writing a logical one to them. Note that, unlike most other AVR(R), 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. 4. When using the I/O specific commands IN and OUT, the I/O addresses 0x00 - 0x3F must be used. When addressing I/O registers as data space using LD and ST instructions, 0x20 must be added to these addresses. The ATmega88/168 is a complex microcontroller with more peripheral units than can be supported within the 64 location reserved in opcode for the IN and OUT instructions. For the extended I/O space from 0x60 - 0xFF in SRAM, only the ST/STS/STD and LD/LDS/LDD instructions can be used. 5. Only valid for Atmel(R) ATmega88/168 ATmega88/ATmega168 Automotive [DATASHEET] 9365A-AVR-02/16 28. Register Summary (Continued) Address Name Bit 7 Bit 6 Bit 5 Bit 4 Bit 3 Bit 2 Bit 1 Bit 0 (0xDB) Reserved - - - - - - - - Page (0xDA) Reserved - - - - - - - - (0xD9) Reserved - - - - - - - - (0xD8) Reserved - - - - - - - - (0xD7) Reserved - - - - - - - - (0xD6) Reserved - - - - - - - - (0xD5) Reserved - - - - - - - - (0xD4) Reserved - - - - - - - - (0xD3) Reserved - - - - - - - - (0xD2) Reserved - - - - - - - - (0xD1) Reserved - - - - - - - - (0xD0) Reserved - - - - - - - - (0xCF) Reserved - - - - - - - - (0xCE) Reserved - - - - - - - - (0xCD) Reserved - - - - - - - - (0xCC) Reserved - - - - - - - - (0xCB) Reserved - - - - - - - - (0xCA) Reserved - - - - - - - - (0xC9) Reserved - - - - - - - - (0xC8) Reserved - - - - - - - - (0xC7) Reserved - - - - - - - - (0xC6) UDR0 (0xC5) UBRR0H (0xC4) UBRR0L (0xC3) Reserved - - - - - - - - (0xC2) UCSR0C UMSEL01 UMSEL00 UPM01 UPM00 USBS0 UCSZ01 /UDORD0 UCSZ00 / UCPHA0 UCPOL0 161/171 (0xC1) UCSR0B RXCIE0 TXCIE0 UDRIE0 RXEN0 TXEN0 UCSZ02 RXB80 TXB80 160 (0xC0) UCSR0A RXC0 TXC0 UDRE0 FE0 DOR0 UPE0 U2X0 MPCM0 159 USART I/O data register - - - - 159 USART baud rate register high 162 USART Baud Rate Register Low 162 (0xBF) Reserved - - - - - - - - (0xBE) Reserved - - - - - - - - (0xBD) TWAMR TWAM6 TWAM5 TWAM4 TWAM3 TWAM2 TWAM1 TWAM0 - 183 (0xBC) TWCR TWINT TWEA TWSTA TWSTO TWWC TWEN - TWIE 180 (0xBB) TWDR (0xBA) TWAR TWA6 TWA5 TWA4 TWA3 TWA2 TWA1 TWA0 TWGCE 182 (0xB9) TWSR TWS7 TWS6 TWS5 TWS4 TWS3 - TWPS1 TWPS0 181 Notes: 2-wire serial interface data register 182 1. For compatibility with future devices, reserved bits should be written to zero if accessed. Reserved I/O memory addresses should never be written. 2. I/O registers within the address range 0x00 - 0x1F are directly bit-accessible using the SBI and CBI instructions. In these registers, the value of single bits can be checked by using the SBIS and SBIC instructions. 3. Some of the status flags are cleared by writing a logical one to them. Note that, unlike most other AVR(R), 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. 4. When using the I/O specific commands IN and OUT, the I/O addresses 0x00 - 0x3F must be used. When addressing I/O registers as data space using LD and ST instructions, 0x20 must be added to these addresses. The ATmega88/168 is a complex microcontroller with more peripheral units than can be supported within the 64 location reserved in opcode for the IN and OUT instructions. For the extended I/O space from 0x60 - 0xFF in SRAM, only the ST/STS/STD and LD/LDS/LDD instructions can be used. 5. Only valid for Atmel(R) ATmega88/168 ATmega88/ATmega168 Automotive [DATASHEET] 9365A-AVR-02/16 271 28. Register Summary (Continued) Address Name (0xB8) TWBR Bit 7 Bit 6 Bit 5 Bit 4 (0xB7) Reserved - - - - Bit 3 Bit 2 Bit 1 Bit 0 - - 2-wire serial interface bit rate register - - Page 180 (0xB6) ASSR - EXCLK AS2 TCN2UB (0xB5) Reserved - - - - OCR2AUB OCR2BUB TCR2AUB (0xB4) OCR2B Timer/Counter2 output compare register B 130 (0xB3) OCR2A Timer/Counter2 output compare register A 130 - - - TCR2BUB 133 - (0xB2) TCNT2 (0xB1) TCCR2B FOC2A FOC2B - Timer/Counter2 (8-bit) - WGM22 CS22 CS21 CS20 129 130 (0xB0) TCCR2A COM2A1 COM2A0 COM2B1 COM2B0 - - WGM21 WGM20 127 (0xAF) Reserved - - - - - - - - (0xAE) Reserved - - - - - - - - (0xAD) Reserved - - - - - - - - (0xAC) Reserved - - - - - - - - (0xAB) Reserved - - - - - - - - (0xAA) Reserved - - - - - - - - (0xA9) Reserved - - - - - - - - (0xA8) Reserved - - - - - - - - (0xA7) Reserved - - - - - - - - (0xA6) Reserved - - - - - - - - (0xA5) Reserved - - - - - - - - (0xA4) Reserved - - - - - - - - (0xA3) Reserved - - - - - - - - (0xA2) Reserved - - - - - - - - (0xA1) Reserved - - - - - - - - (0xA0) Reserved - - - - - - - - (0x9F) Reserved - - - - - - - - (0x9E) Reserved - - - - - - - - (0x9D) Reserved - - - - - - - - (0x9C) Reserved - - - - - - - - (0x9B) Reserved - - - - - - - - (0x9A) Reserved - - - - - - - - (0x99) Reserved - - - - - - - - (0x98) Reserved - - - - - - - - (0x97) Reserved - - - - - - - - (0x96) Reserved - - - - - - - - (0x95) Reserved - - - - - - - - Notes: 272 1. For compatibility with future devices, reserved bits should be written to zero if accessed. Reserved I/O memory addresses should never be written. 2. I/O registers within the address range 0x00 - 0x1F are directly bit-accessible using the SBI and CBI instructions. In these registers, the value of single bits can be checked by using the SBIS and SBIC instructions. 3. Some of the status flags are cleared by writing a logical one to them. Note that, unlike most other AVR(R), 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. 4. When using the I/O specific commands IN and OUT, the I/O addresses 0x00 - 0x3F must be used. When addressing I/O registers as data space using LD and ST instructions, 0x20 must be added to these addresses. The ATmega88/168 is a complex microcontroller with more peripheral units than can be supported within the 64 location reserved in opcode for the IN and OUT instructions. For the extended I/O space from 0x60 - 0xFF in SRAM, only the ST/STS/STD and LD/LDS/LDD instructions can be used. 5. Only valid for Atmel(R) ATmega88/168 ATmega88/ATmega168 Automotive [DATASHEET] 9365A-AVR-02/16 28. Register Summary (Continued) Address Name Bit 7 Bit 6 Bit 5 Bit 4 Bit 3 Bit 2 Bit 1 Bit 0 (0x94) Reserved - - - - - - - - (0x93) Reserved - - - - - - - - (0x92) Reserved - - - - - - - - (0x91) Reserved - - - - - - - - (0x90) Reserved - - - - - - - - (0x8F) Reserved - - - - - - - - (0x8E) Reserved - - - - - - - - (0x8D) Reserved - - - - - - - - (0x8C) Reserved - - - - - - - - (0x8B) OCR1BH Page Timer/Counter1 - Output compare register B high byte 115 (0x8A) OCR1BL Timer/Counter1 - Output compare register B low byte 115 (0x89) OCR1AH Timer/Counter1 - Output compare register A high byte 115 (0x88) OCR1AL Timer/Counter1 - Output compare register A low byte 115 (0x87) ICR1H Timer/Counter1 - Input capture register high byte 115 (0x86) ICR1L Timer/Counter1 - Input capture register low byte 115 (0x85) TCNT1H Timer/Counter1 - Counter register high byte 114 (0x84) TCNT1L Timer/Counter1 - Counter register low byte (0x83) Reserved - - - (0x82) TCCR1C FOC1A FOC1B - - - - - - 114 (0x81) TCCR1B ICNC1 ICES1 - WGM13 WGM12 CS12 CS11 CS10 113 (0x80) TCCR1A COM1A1 COM1A0 COM1B1 COM1B0 - - WGM11 WGM10 111 (0x7F) DIDR1 - - - - - - AIN1D AIN0D 203 (0x7E) DIDR0 - - ADC5D ADC4D ADC3D ADC2D ADC1D ADC0D 218 (0x7D) Reserved - - - - - - - - (0x7C) ADMUX REFS1 REFS0 ADLAR - MUX3 MUX2 MUX1 MUX0 215 (0x7B) ADCSRB - ACME - - - ADTS2 ADTS1 ADTS0 218 (0x7A) ADCSRA ADEN ADSC ADATE ADIF ADIE ADPS2 ADPS1 ADPS0 216 (0x79) ADCH ADC data register high byte (0x78) ADCL ADC data register low byte (0x77) Reserved - - - - - - - - (0x76) Reserved - - - - - - - - (0x75) Reserved - - - - - - - - (0x74) Reserved - - - - - - - - (0x73) Reserved - - - - - - - - (0x72) Reserved - - - - - - - - (0x71) Reserved - - - - - - - - Notes: - - - 114 - - 217 217 1. For compatibility with future devices, reserved bits should be written to zero if accessed. Reserved I/O memory addresses should never be written. 2. I/O registers within the address range 0x00 - 0x1F are directly bit-accessible using the SBI and CBI instructions. In these registers, the value of single bits can be checked by using the SBIS and SBIC instructions. 3. Some of the status flags are cleared by writing a logical one to them. Note that, unlike most other AVR(R), 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. 4. When using the I/O specific commands IN and OUT, the I/O addresses 0x00 - 0x3F must be used. When addressing I/O registers as data space using LD and ST instructions, 0x20 must be added to these addresses. The ATmega88/168 is a complex microcontroller with more peripheral units than can be supported within the 64 location reserved in opcode for the IN and OUT instructions. For the extended I/O space from 0x60 - 0xFF in SRAM, only the ST/STS/STD and LD/LDS/LDD instructions can be used. 5. Only valid for Atmel(R) ATmega88/168 ATmega88/ATmega168 Automotive [DATASHEET] 9365A-AVR-02/16 273 28. Register Summary (Continued) Address Name Bit 7 Bit 6 Bit 5 Bit 4 Bit 3 Bit 2 Bit 1 Bit 0 Page (0x70) TIMSK2 - - - - - OCIE2B OCIE2A TOIE2 131 (0x6F) TIMSK1 - - ICIE1 - - OCIE1B OCIE1A TOIE1 115 89 (0x6E) TIMSK0 - - - - - OCIE0B OCIE0A TOIE0 (0x6D) PCMSK2 PCINT23 PCINT22 PCINT21 PCINT20 PCINT19 PCINT18 PCINT17 PCINT16 74 (0x6C) PCMSK1 - PCINT14 PCINT13 PCINT12 PCINT11 PCINT10 PCINT9 PCINT8 74 (0x6B) PCMSK0 PCINT7 PCINT6 PCINT5 PCINT4 PCINT3 PCINT2 PCINT1 PCINT0 74 (0x6A) Reserved - - - - - - - - (0x69) EICRA - - - - ISC11 ISC10 ISC01 ISC00 (0x68) PCICR - - - - - PCIE2 PCIE1 PCIE0 (0x67) Reserved - - - - - - - - Oscillator calibration register 71 (0x66) OSCCAL (0x65) Reserved - - - - - - - - 29 (0x64) PRR PRTWI PRTIM2 PRTIM0 - PRTIM1 PRSPI PRUSART0 PRADC (0x63) Reserved - - - - - - - - (0x62) Reserved - - - - - - - - (0x61) CLKPR CLKPCE - - - CLKPS3 CLKPS2 CLKPS1 CLKPS0 31 (0x60) WDTCSR WDIF WDIE WDP3 WDCE WDE WDP2 WDP1 WDP0 46 0x3F (0x5F) SREG I T H S V N Z C 10 (SP10) (5) 35 0x3E (0x5E) SPH - - - - - SP9 SP8 12 0x3D (0x5D) SPL SP7 SP6 SP5 SP4 SP3 SP2 SP1 SP0 12 0x3C (0x5C) Reserved - - - - - - - - 0x3B (0x5B) Reserved - - - - - - - - 0x3A (0x5A) Reserved - - - - - - - - 0x39 (0x59) Reserved - - - - - - - - 0x38 (0x58) Reserved - - - - - - - - 0x37 (0x57) SPMCSR SPMIE (RWWSB)(5) - (RWWSRE)(5) BLBSET PGWRT PGERS SELFPRGEN 0x36 (0x56) Reserved - - - - - - - - 0x35 (0x55) MCUCR - - - PUD - - IVSEL IVCE 0x34 (0x54) MCUSR - - - - WDRF BORF EXTRF PORF 0x33 (0x53) SMCR - - - - SM2 SM1 SM0 SE 0x32 (0x52) Reserved - - - - - - - - 0x31 (0x51) Reserved - - - - - - - - 0x30 (0x50) ACSR ACD ACBG ACO ACI ACIE ACIC ACIS1 ACIS0 0x2F (0x4F) Reserved - - - - - - - - 0x2E (0x4E) Notes: 274 SPDR SPI Data Register 225 33 202 142 1. For compatibility with future devices, reserved bits should be written to zero if accessed. Reserved I/O memory addresses should never be written. 2. I/O registers within the address range 0x00 - 0x1F are directly bit-accessible using the SBI and CBI instructions. In these registers, the value of single bits can be checked by using the SBIS and SBIC instructions. 3. Some of the status flags are cleared by writing a logical one to them. Note that, unlike most other AVR(R), 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. 4. When using the I/O specific commands IN and OUT, the I/O addresses 0x00 - 0x3F must be used. When addressing I/O registers as data space using LD and ST instructions, 0x20 must be added to these addresses. The ATmega88/168 is a complex microcontroller with more peripheral units than can be supported within the 64 location reserved in opcode for the IN and OUT instructions. For the extended I/O space from 0x60 - 0xFF in SRAM, only the ST/STS/STD and LD/LDS/LDD instructions can be used. 5. Only valid for Atmel(R) ATmega88/168 ATmega88/ATmega168 Automotive [DATASHEET] 9365A-AVR-02/16 28. Register Summary (Continued) Address Name Bit 7 Bit 6 Bit 5 Bit 4 Bit 3 Bit 2 Bit 1 Bit 0 Page 0x2D (0x4D) SPSR SPIF WCOL - - - - - SPI2X 141 0x2C (0x4C) SPCR SPIE SPE DORD MSTR CPOL CPHA SPR1 SPR0 140 0x2B (0x4B) GPIOR2 General purpose I/O register 2 22 0x2A (0x4A) GPIOR1 General purpose I/O register 1 22 0x29 (0x49) Reserved 0x28 (0x48) OCR0B Timer/Counter0 output compare register B 0x27 (0x47) OCR0A Timer/Counter0 output compare register A 0x26 (0x46) TCNT0 0x25 (0x45) TCCR0B FOC0A FOC0B - - WGM02 CS02 CS01 CS00 0x24 (0x44) TCCR0A COM0A1 COM0A0 COM0B1 COM0B0 - - WGM01 WGM00 0x23 (0x43) GTCCR TSM - - - - - PSRASY PSRSYNC 0x22 (0x42) EEARH (EEPROM address register high byte)(5) 17 0x21 (0x41) EEARL EEPROM address register low byte 17 0x20 (0x40) EEDR EEPROM data register 17 - - - - - - - - Timer/Counter0 (8-bit) - - EEPM1 EEPM0 EEMPE EEPE EERE - - INT1 INT0 72 - - - INTF1 INTF0 72 0x1F (0x3F) EECR 0x1E (0x3E) GPIOR0 0x1D (0x3D) EIMSK - - - - 0x1C (0x3C) EIFR - - - EERIE 91/135 General purpose I/O register 0 18 22 0x1B (0x3B) PCIFR - - - - - PCIF2 PCIF1 PCIF0 0x1A (0x3A) Reserved - - - - - - - - 0x19 (0x39) Reserved - - - - - - - - 0x18 (0x38) Reserved - - - - - - - - 0x17 (0x37) TIFR2 - - - - - OCF2B OCF2A TOV2 131 0x16 (0x36) TIFR1 - - ICF1 - - OCF1B OCF1A TOV1 116 0x15 (0x35) TIFR0 - - - - - OCF0B OCF0A TOV0 0x14 (0x34) Reserved - - - - - - - - 0x13 (0x33) Reserved - - - - - - - - 0x12 (0x32) Reserved - - - - - - - - 0x11 (0x31) Reserved - - - - - - - - 0x10 (0x30) Reserved - - - - - - - - 0x0F (0x2F) Reserved - - - - - - - - 0x0E (0x2E) Reserved - - - - - - - - 0x0D (0x2D) Reserved - - - - - - - - 0x0C (0x2C) Reserved - - - - - - - - 0x0B (0x2B) PORTD PORTD7 PORTD6 PORTD5 PORTD4 PORTD3 PORTD2 PORTD1 PORTD0 70 0x0A (0x2A) DDRD DDD7 DDD6 DDD5 DDD4 DDD3 DDD2 DDD1 DDD0 70 Notes: 1. For compatibility with future devices, reserved bits should be written to zero if accessed. Reserved I/O memory addresses should never be written. 2. I/O registers within the address range 0x00 - 0x1F are directly bit-accessible using the SBI and CBI instructions. In these registers, the value of single bits can be checked by using the SBIS and SBIC instructions. 3. Some of the status flags are cleared by writing a logical one to them. Note that, unlike most other AVR(R), 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. 4. When using the I/O specific commands IN and OUT, the I/O addresses 0x00 - 0x3F must be used. When addressing I/O registers as data space using LD and ST instructions, 0x20 must be added to these addresses. The ATmega88/168 is a complex microcontroller with more peripheral units than can be supported within the 64 location reserved in opcode for the IN and OUT instructions. For the extended I/O space from 0x60 - 0xFF in SRAM, only the ST/STS/STD and LD/LDS/LDD instructions can be used. 5. Only valid for Atmel(R) ATmega88/168 ATmega88/ATmega168 Automotive [DATASHEET] 9365A-AVR-02/16 275 28. Register Summary (Continued) Address Name Bit 7 Bit 6 Bit 5 Bit 4 Bit 3 Bit 2 Bit 1 Bit 0 0x09 (0x29) PIND PIND7 PIND6 PIND5 PIND4 PIND3 PIND2 PIND1 PIND0 70 0x08 (0x28) PORTC - PORTC6 PORTC5 PORTC4 PORTC3 PORTC2 PORTC1 PORTC0 69 0x07 (0x27) DDRC - DDC6 DDC5 DDC4 DDC3 DDC2 DDC1 DDC0 70 0x06 (0x26) PINC - PINC6 PINC5 PINC4 PINC3 PINC2 PINC1 PINC0 70 0x05 (0x25) PORTB PORTB7 PORTB6 PORTB5 PORTB4 PORTB3 PORTB2 PORTB1 PORTB0 69 0x04 (0x24) DDRB DDB7 DDB6 DDB5 DDB4 DDB3 DDB2 DDB1 DDB0 69 0x03 (0x23) PINB PINB7 PINB6 PINB5 PINB4 PINB3 PINB2 PINB1 PINB0 69 0x02 (0x22) Reserved - - - - - - - - 0x01 (0x21) Reserved - - - - - - - - 0x0 (0x20) Reserved - - - - - - - - Notes: 276 Page 1. For compatibility with future devices, reserved bits should be written to zero if accessed. Reserved I/O memory addresses should never be written. 2. I/O registers within the address range 0x00 - 0x1F are directly bit-accessible using the SBI and CBI instructions. In these registers, the value of single bits can be checked by using the SBIS and SBIC instructions. 3. Some of the status flags are cleared by writing a logical one to them. Note that, unlike most other AVR(R), 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. 4. When using the I/O specific commands IN and OUT, the I/O addresses 0x00 - 0x3F must be used. When addressing I/O registers as data space using LD and ST instructions, 0x20 must be added to these addresses. The ATmega88/168 is a complex microcontroller with more peripheral units than can be supported within the 64 location reserved in opcode for the IN and OUT instructions. For the extended I/O space from 0x60 - 0xFF in SRAM, only the ST/STS/STD and LD/LDS/LDD instructions can be used. 5. Only valid for Atmel(R) ATmega88/168 ATmega88/ATmega168 Automotive [DATASHEET] 9365A-AVR-02/16 29. Instruction Set Summary Mnemonics Operands Description Operation Flags #Clocks Arithmetic and Logic Instructions ADD Rd, Rr Add two registers Rd Rd + Rr Z,C,N,V,H 1 ADC Rd, Rr Add with carry two registers Rd Rd + Rr + C Z,C,N,V,H 1 ADIW Rdl, K Add immediate to word Rdh:Rdl Rdh:Rdl + K Z,C,N,V,S 2 SUB Rd, Rr Subtract two registers Rd Rd - Rr Z,C,N,V,H 1 SUBI Rd, K Subtract constant from register Rd Rd - K Z,C,N,V,H 1 SBC Rd, Rr Subtract with carry two registers Rd Rd - Rr - C Z,C,N,V,H 1 SBCI Rd, K Subtract with carry constant from reg. Rd Rd - K - C Z,C,N,V,H 1 SBIW Rdl, K Subtract immediate from word Rdh:Rdl Rdh:Rdl - K Z,C,N,V,S 2 AND Rd, Rr Logical AND registers Rd Rd Rr Z,N,V 1 ANDI Rd, K Logical AND register and constant Rd Rd K Z,N,V 1 OR Rd, Rr Logical OR registers Rd Rd v Rr Z,N,V 1 ORI Rd, K Logical OR register and constant Rd Rd v K Z,N,V 1 EOR Rd, Rr Exclusive OR registers Rd Rd Rr Z,N,V 1 COM Rd One's complement Rd 0xFF Rd Z,C,N,V 1 NEG Rd Two's complement Rd 0x00 Rd Z,C,N,V,H 1 SBR Rd, K Set bit(s) in register Rd Rd v K Z,N,V 1 CBR Rd, K Clear bit(s) in register Rd Rd (0xFF - K) Z,N,V 1 INC Rd Increment Rd Rd + 1 Z,N,V 1 DEC Rd Decrement Rd Rd - 1 Z,N,V 1 TST Rd Test for zero or minus Rd Rd Rd Z,N,V 1 CLR Rd Clear register Rd Rd Rd Z,N,V 1 SER Rd Set register Rd 0xFF None 1 MUL Rd, Rr Multiply unsigned R1:R0 Rd x Rr Z,C 2 MULS Rd, Rr Multiply signed R1:R0 Rd x Rr Z,C 2 MULSU Rd, Rr Multiply signed with unsigned R1:R0 Rd x Rr Z,C 2 FMUL Rd, Rr Fractional multiply unsigned R1:R0 (Rd x Rr) << 1 Z,C 2 FMULS Rd, Rr Fractional multiply signed R1:R0 (Rd x Rr) << 1 Z,C 2 FMULSU Rd, Rr Fractional multiply signed with unsigned R1:R0 (Rd x Rr) << 1 Z,C 2 Relative jump PC PC + k + 1 None 2 Indirect jump to (Z) PC Z None 2 Branch Instructions RJMP k IJMP (1) JMP k Direct jump PC k None 3 RCALL k Relative subroutine call PC PC + k + 1 None 3 Indirect call to (Z) PC Z None 3 Direct subroutine call PC k None 4 RET Subroutine return PC STACK None 4 RETI Interrupt return PC STACK I 4 ICALL CALL(1) k CPSE Rd, Rr Compare, skip if equal if (Rd = Rr) PC PC + 2 or 3 None 1/2/3 CP Rd, Rr Compare Rd Rr Z,N,V,C,H 1 Z,N,V,C,H 1 CPC Note: 1. Rd, Rr Compare with carry Rd Rr C These instructions are only available in Atmel(R) ATmega168. ATmega88/ATmega168 Automotive [DATASHEET] 9365A-AVR-02/16 277 29. Instruction Set Summary (Continued) Mnemonics Operands Description Operation Flags #Clocks CPI Rd, K Compare register with immediate Rd K Z,N,V,C,H 1 SBRC Rr, b Skip if bit in register cleared if (Rr(b)=0) PC PC + 2 or 3 None 1/2/3 SBRS Rr, b Skip if bit in register is set if (Rr(b)=1) PC PC + 2 or 3 None 1/2/3 SBIC P, b Skip if bit in I/O register cleared if (P(b)=0) PC PC + 2 or 3 None 1/2/3 SBIS P, b Skip if bit in I/O register is set if (P(b)=1) PC PC + 2 or 3 None 1/2/3 None 1/2 BRBS s, k Branch if status flag set if (SREG(s) = 1) then PC PC + k + 1 BRBC s, k Branch if status flag cleared if (SREG(s) = 0) then PC PC + k + 1 None 1/2 BREQ k Branch if equal if (Z = 1) then PC PC + k + 1 None 1/2 BRNE k Branch if not equal if (Z = 0) then PC PC + k + 1 None 1/2 BRCS k Branch if carry set if (C = 1) then PC PC + k + 1 None 1/2 BRCC k Branch if carry cleared if (C = 0) then PC PC + k + 1 None 1/2 BRSH k Branch if same or higher if (C = 0) then PC PC + k + 1 None 1/2 BRLO k Branch if lower if (C = 1) then PC PC + k + 1 None 1/2 BRMI k Branch if minus if (N = 1) then PC PC + k + 1 None 1/2 BRPL k Branch if plus if (N = 0) then PC PC + k + 1 None 1/2 None 1/2 BRGE k Branch if greater or equal, signed if (N V= 0) then PC PC + k + 1 BRLT k Branch if less than zero, signed if (N V= 1) then PC PC + k + 1 None 1/2 BRHS k Branch if half carry flag set if (H = 1) then PC PC + k + 1 None 1/2 BRHC k Branch if half carry flag cleared if (H = 0) then PC PC + k + 1 None 1/2 BRTS k Branch if T flag set if (T = 1) then PC PC + k + 1 None 1/2 BRTC k Branch if T flag cleared if (T = 0) then PC PC + k + 1 None 1/2 BRVS k Branch if overflow flag is set if (V = 1) then PC PC + k + 1 None 1/2 BRVC k Branch if overflow flag is cleared if (V = 0) then PC PC + k + 1 None 1/2 BRIE k Branch if interrupt enabled if (I = 1) then PC PC + k + 1 None 1/2 BRID k Branch if interrupt disabled if (I = 0) then PC PC + k + 1 None 1/2 Bit and Bit-test Instructions SBI P, b Set bit in I/O register I/O(P,b) 1 None 2 CBI P, b Clear Bit in I/O register I/O(P,b) 0 None 2 LSL Rd Logical shift left Rd(n+1) Rd(n), Rd(0) 0 Z,C,N,V 1 LSR Rd Logical shift right Rd(n) Rd(n+1), Rd(7) 0 Z,C,N,V 1 ROL Rd Rotate left through carry Rd(0) C,Rd(n+1) Rd(n), C Rd(7) Z,C,N,V 1 ROR Rd Rotate right through carry Rd(7) C,Rd(n) Rd(n+1), C Rd(0) Z,C,N,V 1 ASR Rd Arithmetic shift right Rd(n) Rd(n+1), n=0..6 Z,C,N,V 1 SWAP Rd Swap nibbles Rd(3..0) Rd(7..4), Rd(7..4) Rd(3..0) None 1 BSET s Flag set SREG(s) 1 SREG(s) 1 s Flag clear SREG(s) 0 These instructions are only available in Atmel(R) ATmega168. SREG(s) 1 BCLR Note: 1. 278 ATmega88/ATmega168 Automotive [DATASHEET] 9365A-AVR-02/16 29. Instruction Set Summary (Continued) Mnemonics Operands Description Operation Flags #Clocks BST Rr, b Bit store from register to T T Rr(b) T 1 BLD Rd, b Bit load from T to register Rd(b) T None 1 SEC Set carry C1 C 1 CLC Clear carry C0 C 1 SEN Set negative flag N1 N 1 CLN Clear negative flag N0 N 1 SEZ Set zero flag Z1 Z 1 CLZ Clear zero flag Z0 Z 1 SEI Global interrupt enable I1 I 1 CLI Global interrupt disable I 0 I 1 SES Set signed test flag S1 S 1 CLS Clear signed test flag S0 S 1 SEV Set twos complement overflow. V1 V 1 CLV Clear twos complement overflow V0 V 1 SET Set T in SREG T1 T 1 CLT Clear T in SREG T0 T 1 SEH Set half carry flag in SREG H1 H 1 CLH Clear half carry flag in SREG H0 H 1 Data Transfer Instructions MOV Rd, Rr Move between registers Rd Rr None 1 MOVW Rd, Rr Copy register word Rd+1:Rd Rr+1:Rr None 1 LDI Rd, K Load immediate Rd K None 1 LD Rd, X Load indirect Rd (X) None 2 LD Rd, X+ Load indirect and post-inc. Rd (X), X X + 1 None 2 LD Rd, - X Load indirect and pre-dec. X X - 1, Rd (X) None 2 LD Rd, Y Load indirect Rd (Y) None 2 LD Rd, Y+ Load indirect and post-inc. Rd (Y), Y Y + 1 None 2 LD Rd, - Y Load indirect and pre-dec. Y Y - 1, Rd (Y) None 2 LDD Rd, Y+q Load indirect with displacement Rd (Y + q) None 2 LD Rd, Z Load indirect Rd (Z) None 2 LD Rd, Z+ Load indirect and post-inc. Rd (Z), Z Z+1 None 2 LD Rd, -Z Load indirect and pre-dec. Z Z - 1, Rd (Z) None 2 LDD Rd, Z+q Load indirect with displacement Rd (Z + q) None 2 LDS Rd, k Load direct from SRAM Rd (k) None 2 ST X, Rr Store indirect (X) Rr None 2 ST X+, Rr Store indirect and post-inc. (X) Rr, X X + 1 None 2 ST - X, Rr Store indirect and pre-dec. X X - 1, (X) Rr None 2 ST Y, Rr Store indirect (Y) Rr None 2 ST Y+, Rr Store indirect and post-inc. (Y) Rr, Y Y + 1 None 2 ST - Y, Rr Store indirect and pre-dec. Y Y - 1, (Y) Rr None 2 None 2 STD Note: 1. Y+q, Rr Store indirect with displacement (Y + q) Rr These instructions are only available in Atmel(R) ATmega168. ATmega88/ATmega168 Automotive [DATASHEET] 9365A-AVR-02/16 279 29. Instruction Set Summary (Continued) Mnemonics Operands Description Operation Flags #Clocks ST Z, Rr Store indirect (Z) Rr None 2 ST Z+, Rr Store indirect and post-inc. (Z) Rr, Z Z + 1 None 2 ST -Z, Rr Store ndirect and pre-dec. Z Z - 1, (Z) Rr None 2 STD Z+q,Rr Store indirect with displacement (Z + q) Rr None 2 STS k, Rr Store direct to SRAM (k) Rr None 2 Load program memory R0 (Z) None 3 LPM LPM Rd, Z Load program memory Rd (Z) None 3 LPM Rd, Z+ Load program memory and post-inc Rd (Z), Z Z + 1 None 3 Store program memory (Z) R1:R0 None - IN Rd, P In port Rd P None 1 OUT P, Rr Out port P Rr None 1 PUSH Rr Push register on stack STACK Rr None 2 POP Rd Pop register from stack Rd STACK None 2 None 1 SPM MCU Control Instructions NOP No operation SLEEP Sleep (see specific description for sleep function) None 1 WDR Watchdog reset (see specific description for WDR/timer) None 1 None N/A BREAK Note: 1. 280 Break For on-chip debug Only These instructions are only available in Atmel(R) ATmega168. ATmega88/ATmega168 Automotive [DATASHEET] 9365A-AVR-02/16 30. Ordering Information 30.1 ATmega88 Speed (MHz) Power Supply Ordering Code Package(1) Operation Range 16(2) 2.7V to 5.5V ATmega88-15MT2 PN Extended (-40C to +150C) 16(2) 2.7V to 5.5V ATmega88-15AD MA Extended (-40C to +150C) 1. Pb-free packaging, complies to the European Directive for Restriction of Hazardous Substances (RoHS directive). Also halide free and fully green. Notes: 2. 30.2 See Figure 25-1 on page 254. ATmega168 Speed (MHz) Power Supply Ordering Code Package(1) Operation Range 16(2) 2.7V to 5.5V ATmega168-15MD PN Extended (-40C to +150C) 16(2) 2.7V to 5.5V ATmega168-15AD MA Extended (-40C to +150C) 1. Pb-free packaging, complies to the European Directive for Restriction of Hazardous Substances (RoHS directive). Also halide free and fully green. Notes: 2. 30.3 See Figure 25-1 on page 254. Package information Package Information MA 32 - Lead, 7mm 7mm body size, 1.0mm body thickness 0.8mm lead pitch, thin profile plastic quad flat package (TQFP) PN 32-pad, 5 5 1.0mm body, lead pitch 0.50mm, quad flat no-lead/micro lead frame package (QFN/MLF): E2/D2 3.1 0.1mm ATmega88/ATmega168 Automotive [DATASHEET] 9365A-AVR-02/16 281 31. Packaging Information 31.1 MA Drawings not scaled A A2 A1 D1 32 1 E1 e L 0~7 Top View C Side View D COMMON DIMENSIONS (Unit of Measure = mm) Symbol MIN NOM A MAX A1 0.05 A2 0.95 1.00 1.05 D/E 8.75 9.00 9.25 D1/E1 6.90 7.00 7.10 C 0.09 0.20 L 0.45 0.75 b 0.30 0.45 E b Bottom View NOTE 1.20 0.15 e 0.80 TYP. n 32 2 Notes: 1. This drawing is for general information only. Refer to JEDEC Drawing MS-026, Variation ABA. 2. Dimensions D1 and E1 do not include mold protrusion. Allowable protrusion is 0.25mm per side. Dimensions D1 and E1 are maximum plastic body size dimensions including mold mismatch. 3. Lead coplanarity is 0.10mm maximum. 02/29/12 Package Drawing Contact: packagedrawings@atmel.com 282 TITLE GPC DRAWING NO. REV. MA, 32 Lds - 0.80mm Pitch, 7x7x1.00mm Body size Thin Profile Plastic Quad Flat Package (TQFP) AUT MA C ATmega88/ATmega168 Automotive [DATASHEET] 9365A-AVR-02/16 31.2 PN Drawings not scaled A A3 D A1 N 1 0.30 Dia. Typ. Laser Marking E Seating Plane C 0.080 C Top View L Side View D2 COMMON DIMENSIONS b (Unit of Measure = mm) Option A Pin 1# Chamfer (C 0.30) E2 Option B PIN1 ID 1 Pin 1# Notch (C 0.20 R) See Options A, B e Symbol MIN NOM MAX A 0.80 0.85 0.90 A1 A3 0.00 NOTE 0.05 0.20 REF D/E 5.00 BSC D2/E2 3.00 3.10 3.20 L 0.30 0.40 0.50 b 0.18 0.25 0.30 e 0.50 BSC n 32 2 Bottom View Notes: 1. This drawing is for general information only. Refer to JEDEC Drawing MO-220, Variation VHHD-2, for proper dimensions, tolerances, datums, etc. 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. 01/31/12 Package Drawing Contact: packagedrawings@atmel.com TITLE GPC DRAWING NO. REV. PN, 32 Leads - 0.50mm Pitch, 5x5mm Very Thin Quad Flat no Lead Package (VQFN) Sawn ZMF PN I ATmega88/ATmega168 Automotive [DATASHEET] 9365A-AVR-02/16 283 32. Errata ATmega88 The revision letter in this section refers to the revision of the ATmega88 device. 32.1 Rev. G Interrupts may be lost when writing the timer registers in the asynchronous timer 1. Interrupts may be lost when writing the timer registers in the asynchronous timer If one of the timer registers which is synchronized to the asynchronous Timer2 clock is written in the cycle before an overflow interrupt occurs, the interrupt may be lost. Problem Fix/Workaround Always check that the Timer2 Timer/Counter register, TCNT2, does not have the value 0xFF before writing the Timer2 control register, TCCR2, or output compare register, OCR2. 32.2 Rev. E Interrupts may be lost when writing the timer registers in the asynchronous timer POR sensitivity with Vcc ramp up from a very low supply voltage 1. Interrupts may be lost when writing the timer registers in the asynchronous timer If one of the timer registers which is synchronized to the asynchronous Timer2 clock is written in the cycle before an overflow interrupt occurs, the interrupt may be lost. Problem Fix/Workaround Always check that the Timer2 Timer/Counter register, TCNT2, does not have the value 0xFF before writing the Timer2 control register, TCCR2, or output compare register, OCR2. 2. POR sensitivity with Vcc ramp up from a very low supply voltage If Vcc ramp up from a stable 150mV to 300mV plateau, the power on reset (POR) may not reset the device properly. Problem Fix/Workaround None. Note: 284 Please note from datasheet 7530F-AVR-09/07 we introduce a new errata numbering scheme (Errata Rev F of datasheet 7530E-AVR-03/07 is equivalent to Errata Rev E of datasheet 7530F-AVR-09/07) ATmega88/ATmega168 Automotive [DATASHEET] 9365A-AVR-02/16 33. Errata ATmega168 The revision letter in this section refers to the revision of the ATmega168 device. 33.1 Rev. F Interrupts may be lost when writing the timer registers in the asynchronous timer 1. Interrupts may be lost when writing the timer registers in the asynchronous timer If one of the timer registers which is synchronized to the asynchronous Timer2 clock is written in the cycle before an overflow interrupt occurs, the interrupt may be lost. Problem Fix/Workaround Always check that the Timer2 Timer/Counter register, TCNT2, does not have the value 0xFF before writing the Timer2 control register, TCCR2, or output compare register, OCR2. 33.2 Rev. E Interrupts may be lost when writing the timer registers in the asynchronous timer POR sensitivity with Vcc ramp up from a very low supply voltage 1. Interrupts may be lost when writing the timer registers in the asynchronous timer If one of the timer registers which is synchronized to the asynchronous Timer2 clock is written in the cycle before an overflow interrupt occurs, the interrupt may be lost. Problem Fix/Workaround Always check that the Timer2 Timer/Counter register, TCNT2, does not have the value 0xFF before writing the Timer2 control register, TCCR2, or output compare register, OCR2. 2. POR sensitivity with Vcc ramp up from a very low supply voltage If Vcc ramp up from a stable 150mV to 300mV plateau, the power on reset (POR) may not reset the device properly. Problem Fix/Workaround None. Note: Please note from datasheet 7530F-AVR-09/07 we introduce a new errata numbering scheme (Errata Rev F of datasheet 7530E-AVR-03/07 is equivalent to Errata Rev E of datasheet 7530F-AVR-09/07) ATmega88/ATmega168 Automotive [DATASHEET] 9365A-AVR-02/16 285 34. Table of Contents Features . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1 1. Pin Configurations . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 3 2. Overview . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 4 3. About Code Examples . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 8 4. AVR CPU Core . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 8 5. AVR ATmega88/168 Memories . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 15 6. System Clock and Clock Options . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 23 7. Power Management and Sleep Modes . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 33 8. System Control and Reset . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 38 9. Interrupts . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 48 10. I/O-Ports . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 55 11. External Interrupts . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 71 12. 8-bit Timer/Counter0 with PWM . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 75 13. Timer/Counter0 and Timer/Counter1 Prescalers . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 90 14. 16-bit Timer/Counter1 with PWM . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 92 15. 8-bit Timer/Counter2 with PWM and Asynchronous Operation . . . . . . . . . . . . . . . . . 117 16. Serial Peripheral Interface - SPI . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 136 17. USART0 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 144 18. USART in SPI Mode . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 166 19. 2-wire Serial Interface . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 173 20. Analog Comparator . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 201 21. Analog-to-Digital Converter . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 204 22. debugWIRE On-chip Debug System . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 219 23. Boot Loader Support - Read-While-Write Self-Programming, ATmega88 and ATmega168 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 221 286 24. Memory Programming . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 234 25. Electrical Characteristics . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 252 26. 2-wire Serial Interface Characteristics . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 257 27. ATmega88/168 Typical Characteristics . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 261 28. Register Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 270 29. Instruction Set Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 277 30. Ordering Information . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 281 ATmega88/ATmega168 Automotive [DATASHEET] 9365A-AVR-02/16 31. Packaging Information . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 282 32. Errata ATmega88 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 284 33. Errata ATmega168 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 285 34. Table of Contents . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 286 ATmega88/ATmega168 Automotive [DATASHEET] 9365A-AVR-02/16 287 XXXXXX Atmel Corporation 1600 Technology Drive, San Jose, CA 95110 USA T: (+1)(408) 441.0311 F: (+1)(408) 436.4200 | www.atmel.com (c) 2016 Atmel Corporation. / Rev.: 9365A-AVR-02/16 Atmel(R), Atmel logo and combinations thereof, Enabling Unlimited Possibilities(R), AVR(R), AVR Studio(R), 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.