8-bit AVR Microcontrollers ATmega164P/V DATASHEET COMPLETE Introduction (R) (R) The Atmel picoPower ATmega164P 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 ATmega164P achieves throughputs close to 1MIPS per MHz. This empowers system designer to optimize the device for power consumption versus processing speed. Feature (R) (R) High Performance, Low Power Atmel AVR 8-Bit Microcontroller Family * * * Advanced RISC Architecture - 131 Powerful Instructions - Most Single Clock Cycle Execution - 32 x 8 General Purpose Working Registers - Fully Static Operation - Up to 20 MIPS Throughput at 20MHz - On-chip 2-cycle Multiplier High Endurance Non-volatile Memory Segments - 16KBytes of In-System Self-Programmable Flash Program Memory - 512Bytes EEPROM - 1KBytes Internal SRAM - Write/Erase Cycles: 10,000 Flash/100,000 EEPROM - Data Retention: 20 Years at 85C/100 Years at 25C(1) - Optional Boot Code Section with Independent Lock Bits * In-System Programming by On-chip Boot Program * True Read-While-Write Operation - Programming Lock for Software Security Atmel QTouch(R) Library Support - Capacitive Touch Buttons, Sliders and Wheels - QTouch and QMatrix acquisition Atmel-42742B-ATmega164P/V_Datasheet_Complete-08/2016 - * * Up to 64 Sense Channels JTAG (IEEE std. 1149.1 Compliant) Interface - Boundary-scan Capabilities According to the JTAG Standard - Extensive On-chip Debug Support - Programming of Flash, EEPROM, Fuses, and Lock Bits through the JTAG Interface 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 * Differential Mode with Selectable Gain at 1x, 10x or 200x - One Byte-oriented 2-wire Serial Interface (Philips I2C compatible) - Two Programmable Serial USART - One Master/Slave SPI 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 RC Oscillator - External and Internal Interrupt Sources - Six Sleep Modes: Idle, ADC Noise Reduction, Power-save, Power-down, Standby, and Extended Standby I/O and Packages - 32 Programmable I/O Lines - 40-pin PDIP - 44-lead TQFP - 44-pad VQFN/QFN Operating Voltage: - 1.8 - 5.5V for ATmega164PV - 2.7 - 5.5V for ATmega164P Speed Grades - ATmega164PV: * 0 - 4MHz @ 1.8V - 5.5V * 0 - 10MHz @ 2.7V - 5.5V - ATmega164P: * 0 - 10MHz @ 2.7V - 5.5V * 0 - 20MHz @ 4.5 - 5.5V Power Consumption at 1MHz, 1.8V, 25C - Active Mode: 0.4mA - Power-down Mode: 0.1A - Power-save Mode: 0.6A (Including 32kHz RTC) Atmel ATmega164P/V [DATASHEET] Atmel-42742B-ATmega164P/V_Datasheet_Complete-08/2016 2 1. Refer to Data Retention. Related Links Data Retention on page 20 Atmel ATmega164P/V [DATASHEET] Atmel-42742B-ATmega164P/V_Datasheet_Complete-08/2016 3 Table of Contents Introduction......................................................................................................................1 Feature............................................................................................................................ 1 1. Description...............................................................................................................10 2. Configuration Summary........................................................................................... 11 3. Ordering Information ...............................................................................................12 4. Block Diagram......................................................................................................... 14 5. Pin Configurations................................................................................................... 15 5.1. 5.2. Pinout......................................................................................................................................... 15 Pin Descriptions..........................................................................................................................16 6. I/O Multiplexing........................................................................................................ 18 7. General Information................................................................................................. 20 7.1. 7.2. 7.3. 7.4. Resources.................................................................................................................................. 20 Data Retention............................................................................................................................20 About Code Examples................................................................................................................20 Capacitive Touch Sensing.......................................................................................................... 20 8. AVR CPU Core........................................................................................................ 21 8.1. 8.2. 8.3. 8.4. 8.5. 8.6. Overview.....................................................................................................................................21 ALU - Arithmetic Logic Unit........................................................................................................22 Status Register...........................................................................................................................22 General Purpose Register File................................................................................................... 24 Stack Pointer.............................................................................................................................. 25 Accessing 16-bit Registers.........................................................................................................26 8.7. 8.8. Instruction Execution Timing...................................................................................................... 27 Reset and Interrupt Handling..................................................................................................... 27 9. AVR Memories.........................................................................................................30 9.1. Overview.....................................................................................................................................30 9.2. 9.3. 9.4. 9.5. 9.6. In-System Reprogrammable Flash Program Memory................................................................ 30 SRAM Data Memory...................................................................................................................31 EEPROM Data Memory............................................................................................................. 32 I/O Memory.................................................................................................................................33 Register Description................................................................................................................... 34 10. System Clock and Clock Options............................................................................ 43 10.1. Clock Systems and Their Distribution.........................................................................................43 10.2. Clock Sources............................................................................................................................ 44 10.3. Low Power Crystal Oscillator......................................................................................................46 Atmel ATmega164P/V [DATASHEET] Atmel-42742B-ATmega164P/V_Datasheet_Complete-08/2016 4 10.4. Full Swing Crystal Oscillator.......................................................................................................47 10.5. Low Frequency Crystal Oscillator...............................................................................................48 10.6. Calibrated Internal RC Oscillator................................................................................................49 10.7. 128kHz Internal Oscillator.......................................................................................................... 50 10.8. External Clock............................................................................................................................ 51 10.9. Timer/Counter Oscillator.............................................................................................................52 10.10. Clock Output Buffer....................................................................................................................52 10.11. System Clock Prescaler............................................................................................................. 52 10.12. Register Description...................................................................................................................53 11. PM - Power Management and Sleep Modes........................................................... 57 11.1. 11.2. 11.3. 11.4. 11.5. 11.6. 11.7. 11.8. 11.9. 11.10. 11.11. 11.12. Overview.....................................................................................................................................57 Sleep Modes...............................................................................................................................57 BOD Disable...............................................................................................................................58 Idle Mode....................................................................................................................................58 ADC Noise Reduction Mode.......................................................................................................58 Power-Down Mode.....................................................................................................................59 Power-save Mode.......................................................................................................................59 Standby Mode............................................................................................................................ 60 Extended Standby Mode............................................................................................................ 60 Power Reduction Register..........................................................................................................60 Minimizing Power Consumption................................................................................................. 60 Register Description................................................................................................................... 62 12. SCRST - System Control and Reset....................................................................... 68 12.1. 12.2. 12.3. 12.4. 12.5. 12.6. 12.7. 12.8. 12.9. Resetting the AVR...................................................................................................................... 68 Reset Sources............................................................................................................................68 Power-on Reset..........................................................................................................................69 External Reset............................................................................................................................70 Brown-out Detection...................................................................................................................70 Watchdog System Reset............................................................................................................ 71 Internal Voltage Reference.........................................................................................................71 Watchdog Timer......................................................................................................................... 72 Register Description................................................................................................................... 74 13. Interrupts................................................................................................................. 78 13.1. Overview.....................................................................................................................................78 13.2. Interrupt Vectors in ATmega164P...............................................................................................78 13.3. Register Description................................................................................................................... 81 14. External Interrupts................................................................................................... 84 14.1. EXINT - External Interrupts........................................................................................................ 84 15. I/O-Ports.................................................................................................................. 96 15.1. 15.2. 15.3. 15.4. Overview.....................................................................................................................................96 Ports as General Digital I/O........................................................................................................97 Alternate Port Functions...........................................................................................................100 Register Description................................................................................................................. 113 Atmel ATmega164P/V [DATASHEET] Atmel-42742B-ATmega164P/V_Datasheet_Complete-08/2016 5 16. TC0 - 8-bit Timer/Counter0 with PWM...................................................................128 16.1. 16.2. 16.3. 16.4. 16.5. 16.6. 16.7. 16.8. 16.9. Features................................................................................................................................... 128 Overview...................................................................................................................................128 Timer/Counter Clock Sources.................................................................................................. 130 Counter Unit............................................................................................................................. 130 Output Compare Unit................................................................................................................131 Compare Match Output Unit.....................................................................................................133 Modes of Operation..................................................................................................................134 Timer/Counter Timing Diagrams...............................................................................................138 Register Description................................................................................................................. 140 17. TC1 - 16-bit Timer/Counter1 with PWM.................................................................153 17.1. Overview...................................................................................................................................153 17.2. Features................................................................................................................................... 153 17.3. Block Diagram.......................................................................................................................... 153 17.4. Definitions.................................................................................................................................154 17.5. Registers.................................................................................................................................. 155 17.6. Accessing 16-bit Registers.......................................................................................................155 17.7. Timer/Counter Clock Sources.................................................................................................. 158 17.8. Counter Unit............................................................................................................................. 158 17.9. Input Capture Unit.................................................................................................................... 159 17.10. Output Compare Units............................................................................................................. 161 17.11. Compare Match Output Unit.....................................................................................................163 17.12. Modes of Operation..................................................................................................................164 17.13. Timer/Counter Timing Diagrams.............................................................................................. 172 17.14. Register Description.................................................................................................................173 18. Timer/Counter 0, 1 Prescalers...............................................................................186 18.1. 18.2. 18.3. 18.4. Internal Clock Source............................................................................................................... 186 Prescaler Reset........................................................................................................................186 External Clock Source..............................................................................................................186 Register Description................................................................................................................. 187 19. TC2 - 8-bit Timer/Counter2 with PWM and Asynchronous Operation................... 189 19.1. Features................................................................................................................................... 189 19.2. Overview...................................................................................................................................189 19.3. Timer/Counter Clock Sources.................................................................................................. 191 19.4. Counter Unit............................................................................................................................. 191 19.5. Output Compare Unit................................................................................................................192 19.6. Compare Match Output Unit.....................................................................................................194 19.7. Modes of Operation..................................................................................................................195 19.8. Timer/Counter Timing Diagrams...............................................................................................199 19.9. Asynchronous Operation of Timer/Counter2............................................................................ 200 19.10. Timer/Counter Prescaler.......................................................................................................... 202 19.11. Register Description................................................................................................................. 202 20. SPI - Serial Peripheral Interface........................................................................... 215 20.1. Features................................................................................................................................... 215 Atmel ATmega164P/V [DATASHEET] Atmel-42742B-ATmega164P/V_Datasheet_Complete-08/2016 6 20.2. Overview...................................................................................................................................215 20.3. SS Pin Functionality................................................................................................................. 219 20.4. Data Modes.............................................................................................................................. 219 20.5. Register Description................................................................................................................. 220 21. USART - Universal Synchronous Asynchronous Receiver Transceiver................225 21.1. Features................................................................................................................................... 225 21.2. Overview...................................................................................................................................225 21.3. Block Diagram.......................................................................................................................... 225 21.4. Clock Generation......................................................................................................................226 21.5. Frame Formats.........................................................................................................................229 21.6. USART Initialization..................................................................................................................230 21.7. Data Transmission - The USART Transmitter......................................................................... 231 21.8. Data Reception - The USART Receiver.................................................................................. 233 21.9. Asynchronous Data Reception.................................................................................................237 21.10. Multi-Processor Communication Mode.................................................................................... 239 21.11. Examples of Baud Rate Setting............................................................................................... 240 21.12. Register Description.................................................................................................................243 22. USARTSPI - USART in SPI Mode.........................................................................253 22.1. 22.2. 22.3. 22.4. 22.5. 22.6. 22.7. 22.8. Features................................................................................................................................... 253 Overview...................................................................................................................................253 Clock Generation......................................................................................................................253 SPI Data Modes and Timing.....................................................................................................254 Frame Formats.........................................................................................................................254 Data Transfer............................................................................................................................256 AVR USART MSPIM vs. AVR SPI............................................................................................257 Register Description................................................................................................................. 258 23. TWI - 2-wire Serial Interface..................................................................................259 23.1. 23.2. 23.3. 23.4. 23.5. 23.6. 23.7. 23.8. 23.9. Features................................................................................................................................... 259 Two-Wire Serial Interface Bus Definition..................................................................................259 Data Transfer and Frame Format.............................................................................................260 Multi-master Bus Systems, Arbitration, and Synchronization...................................................263 Overview of the TWI Module.................................................................................................... 265 Using the TWI...........................................................................................................................267 Transmission Modes................................................................................................................ 270 Multi-master Systems and Arbitration.......................................................................................288 Register Description................................................................................................................. 290 24. AC - Analog Comparator....................................................................................... 298 24.1. Overview...................................................................................................................................298 24.2. Analog Comparator Multiplexed Input...................................................................................... 298 24.3. Register Description................................................................................................................. 299 25. ADC - Analog to Digital Converter.........................................................................304 25.1. Features................................................................................................................................... 304 25.2. Overview...................................................................................................................................304 25.3. Starting a Conversion...............................................................................................................306 Atmel ATmega164P/V [DATASHEET] Atmel-42742B-ATmega164P/V_Datasheet_Complete-08/2016 7 25.4. Prescaling and Conversion Timing...........................................................................................307 25.5. 25.6. 25.7. 25.8. Changing Channel or Reference Selection.............................................................................. 310 ADC Noise Canceler................................................................................................................ 312 ADC Conversion Result............................................................................................................316 Register Description................................................................................................................. 318 26. JTAG Interface and On-chip Debug System..........................................................328 26.1. Features................................................................................................................................... 328 26.2. Overview...................................................................................................................................328 26.3. TAP - Test Access Port............................................................................................................ 329 26.4. TAP Controller.......................................................................................................................... 330 26.5. Using the Boundary-scan Chain...............................................................................................331 26.6. Using the On-chip Debug System............................................................................................ 331 26.7. On-chip Debug Specific JTAG Instructions.............................................................................. 332 26.8. Using the JTAG Programming Capabilities.............................................................................. 332 26.9. Bibliography..............................................................................................................................333 26.10. IEEE 1149.1 (JTAG) Boundary-scan........................................................................................333 26.11. Data Registers..........................................................................................................................334 26.12. Boundry-scan Specific JTAG Instructions................................................................................ 335 26.13. Boundary-scan Chain...............................................................................................................337 26.14. ATmega164P Boundary-scan Order........................................................................................ 340 26.15. Boundary-scan Description Language Files............................................................................ 342 26.16. Register Description.................................................................................................................342 27. BTLDR - Boot Loader Support - Read-While-Write Self-Programming................ 347 27.1. 27.2. 27.3. 27.4. 27.5. 27.6. 27.7. 27.8. 27.9. Features................................................................................................................................... 347 Overview...................................................................................................................................347 Application and Boot Loader Flash Sections............................................................................347 Read-While-Write and No Read-While-Write Flash Sections...................................................348 Entering the Boot Loader Program...........................................................................................350 Boot Loader Lock Bits.............................................................................................................. 351 Addressing the Flash During Self-Programming...................................................................... 352 Self-Programming the Flash.....................................................................................................353 Register Description................................................................................................................. 361 28. MEMPROG- Memory Programming......................................................................364 28.1. Program And Data Memory Lock Bits...................................................................................... 364 28.2. Fuse Bits...................................................................................................................................365 28.3. Signature Bytes........................................................................................................................ 368 28.4. Calibration Byte........................................................................................................................ 368 28.5. Serial Number...........................................................................................................................368 28.6. Page Size................................................................................................................................. 368 28.7. Parallel Programming Parameters, Pin Mapping, and Commands.......................................... 369 28.8. Parallel Programming...............................................................................................................371 28.9. Serial Downloading...................................................................................................................378 28.10. Programming Via the JTAG Interface.......................................................................................383 29. Electrical Characteristics....................................................................................... 397 29.1. Absolute Maximum Ratings......................................................................................................397 Atmel ATmega164P/V [DATASHEET] Atmel-42742B-ATmega164P/V_Datasheet_Complete-08/2016 8 29.2. DC Characteristics....................................................................................................................397 29.3. 29.4. 29.5. 29.6. 29.7. 29.8. 29.9. Speed Grades.......................................................................................................................... 400 Clock Characteristics................................................................................................................400 System and Reset Characteristics........................................................................................... 401 External interrupts characteristics............................................................................................ 402 SPI Timing Characteristics....................................................................................................... 403 Two-wire Serial Interface Characteristics................................................................................. 404 ADC characteristics..................................................................................................................406 30. Typical Characteristics........................................................................................... 411 30.1. Active Supply Current............................................................................................................... 411 30.2. Idle Supply Current...................................................................................................................413 30.3. Supply Current of I/O Modules................................................................................................. 415 30.4. Power-down Supply Current.....................................................................................................416 30.5. Power-save Supply Current......................................................................................................417 30.6. Standby Supply Current........................................................................................................... 418 30.7. Pin Pull-Up................................................................................................................................418 30.8. Pin Driver Strength................................................................................................................... 421 30.9. Pin Threshold and Hysteresis...................................................................................................423 30.10. BOD Threshold........................................................................................................................ 425 30.11. Internal Oscillator Speed.......................................................................................................... 427 30.12. Current Consumption of Peripheral Units................................................................................ 429 30.13. Current Consumption in Reset and Reset Pulse Width........................................................... 431 31. Register Summary.................................................................................................433 32. Instruction Set Summary....................................................................................... 436 33. Packaging Information...........................................................................................440 33.1. 40-pin PDIP.............................................................................................................................. 440 33.2. 44-pin TQFP.............................................................................................................................441 33.3. 44-pin VQFN.............................................................................................................................442 34. Errata.....................................................................................................................443 34.1. Rev. A....................................................................................................................................... 443 35. Datasheet Revision History................................................................................... 444 35.1. Rev. B - 08/2016...................................................................................................................... 444 35.2. Rev. A - 07/2016...................................................................................................................... 444 Atmel ATmega164P/V [DATASHEET] Atmel-42742B-ATmega164P/V_Datasheet_Complete-08/2016 9 1. Description The Atmel(R) ATmega164P is a low-power CMOS 8-bit microcontroller based on the AVR enhanced RISC architecture. By executing powerful instructions in a single clock cycle, the ATmega164P achieves throughputs close to 1MIPS per MHz. This empowers system designer to optimize the device for power consumption versus processing speed. The Atmel 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 a 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 ATmega164P provides the following features: 16Kbytes of In-System Programmable Flash with Read-While-Write capabilities, 512bytes EEPROM, 1Kbytes SRAM, 32 general purpose I/O lines, 32 general purpose working registers, Real Time Counter (RTC), three flexible Timer/Counters with compare modes and PWM, two serial programmable USARTs , one byte-oriented 2-wire Serial Interface (I2C), a 8channel 10-bit ADC with optional differential input stage with programmable gain, a programmable Watchdog Timer with internal Oscillator, an SPI serial port, IEEE std. 1149.1 compliant JTAG test interface, also used for accessing the On-chip Debug system and programming and six software selectable power saving modes. The Idle mode stops the CPU while allowing the SRAM, Timer/Counters, 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. In Extended Standby mode, both the main oscillator and the asynchronous timer continue to run. Atmel offers the QTouch(R) library for embedding capacitive touch buttons, sliders and wheels functionality into AVR microcontrollers. The patented charge-transfer signal acquisition offers robust sensing and includes fully debounced reporting of touch keys and includes Adjacent Key Suppression(R) (AKSTM) technology for unambiguous detection of key events. The easy-to-use QTouch Suite toolchain allows you to explore, develop and debug your own touch applications. 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 nonvolatile 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 ATmega164P is a powerful microcontroller that provides a highly flexible and cost effective solution to many embedded control applications. The ATmega164P 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. Atmel ATmega164P/V [DATASHEET] Atmel-42742B-ATmega164P/V_Datasheet_Complete-08/2016 10 2. Configuration Summary The table below compares the device series of feature and pin compatible devices, providing a seamless migration path. Table 2-1.Configuration Summary and Device Comparison Features ATmega164/V ATmega324/V ATmega644/V Pin Count 40/44 40/44 40/44 Flash (Bytes) 16K 32K 64K SRAM (Bytes) 1K 2K 4K EEPROM (Bytes) 512 1K 2K General Purpose I/O Lines 32 32 32 SPI 1 1 1 TWI (I2C) 1 1 1 USART 2 2 2 10-bit 15ksps 10-bit 15ksps 10-bit 15ksps ADC Channels 8 8 8 Analog Comparator 1 1 1 8-bit Timer/Counters 2 2 2 16-bit Timer/Counters 1 1 1 PWM channels 6 6 6 PDIP PDIP PDIP TQFP TQFP TQFP VQFN/QFN VQFN/QFN VQFN/QFN ADC Packages Atmel ATmega164P/V [DATASHEET] Atmel-42742B-ATmega164P/V_Datasheet_Complete-08/2016 11 3. Ordering Information Speed [MHz](3) Power Supply [V] 10 1.8 - 5.5 20 10 20 2.7 - 5.5 1.8 - 5.5 2.7 - 5.5 Ordering Code(2) Package(1) Operational Range ATmega164PV-10AU 44A Industrial (-40C to 85C) ATmega164PV-10AUR(4) 44A ATmega164PV-10PU 40P6 ATmega164PV-10MU 44M1 ATmega164PV-10MUR(4) 44M1 ATmega164P-20AU 44A ATmega164P-20AUR(4) 44A ATmega164P-20PU 40P6 ATmega164P-20MU 44M1 ATmega164P-20MUR(4) 44M1 ATmega164PV-10AN 44A ATmega164PV-10ANR(4) 44A ATmega164PV-10PN 40P6 ATmega164P-20AN 44A ATmega164P-20ANR(4) 44A ATmega164P-20PN 40P6 ATmega164P-20MN 44M1 ATmega164P-20MNR(4) 44M1 Industrial (-40C to 85C) Industrial (-40C to 105C) Industrial (-40C to 105C) Note: 1. This device can also be supplied in wafer form. Please contact your local Atmel sales office for detailed ordering information and minimum quantities. 2. Pb-free packaging, complies to the European Directive for Restriction of Hazardous Substances (RoHS directive). Also Halide free and fully Green. 3. Refer to Speed Grades for Speed vs. VCC 4. Tape & Reel. Atmel ATmega164P/V [DATASHEET] Atmel-42742B-ATmega164P/V_Datasheet_Complete-08/2016 12 Package Type 40P6 40-pin, 0.600" Wide, Plastic Dual Inline Package (PDIP) 44A 44-lead, Thin (1.0mm) Plastic Quad Flat Package (TQFP) 44M1 44-pad, 7 x 7 x 1.0mm body, lead pitch 0.50mm, Thermally Enhanced Plastic Very Thin Quad Flat NoLead (VQFN) Related Links Speed Grades on page 400 Atmel ATmega164P/V [DATASHEET] Atmel-42742B-ATmega164P/V_Datasheet_Complete-08/2016 13 4. Block Diagram Figure 4-1.Block Diagram SRAM TCK TMS TDI TDO JTAG CPU OCD Clock generation TOSC1 32.768kHz XOSC TOSC2 XTAL1 16MHz LP XOSC XTAL2 VCC RESET GND 8MHz Calib RC 128kHz int osc External clock Power Supervision POR/BOD & RESET ADC[7:0] AREF PCINT[31:0] INT[2:0] OC1A/B T1 ICP1 OC2A OC2B NVM programming Power management and clock control Watchdog Timer ADC EXTINT TC 1 (16-bit) TC 2 (8-bit async) FLASH D A T A B U S EEPROM EEPROMIF I/O PORTS I N / O U T GPIOR[2:0] D A T A B U S TC 0 (8-bit) SPI AC Internal Reference USART 0 RxD0 TxD0 XCK0 USART 1 RxD1 TxD1 XCK1 TWI PA[7:0] PB[7:0] PC[7:0] PD[7:0] T0 OC0A OC0B MISO MOSI SCK SS AIN0 AIN1 ACO ADCMUX SDA SCL Atmel ATmega164P/V [DATASHEET] Atmel-42742B-ATmega164P/V_Datasheet_Complete-08/2016 14 5. Pin Configurations 5.1. Pinout 5.1.1. PDIP (PCINT8/XCK0/T0) (ADC0/PCINT0) (PCINT9/CLKO/T1) (ADC1/PCINT1) (PCINT10/INT2/AIN0) (ADC2/PCINT2) (PCINT11/OC0A/AIN1) (ADC3/PCINT3) (PCINT12/OC0B/ (ADC4/PCINT4) (PCINT13/MOSI) (ADC5/PCINT5) (PCINT14/MISO) (ADC6/PCINT6) (PCINT15//SCK) (ADC7/PCINT7) XTAL2 (TOSC2/PCINT23) XTAL1 (TOSC1/PCINT22) (PCINT24/RXD0) (TDI/PCINT21) (PCINT25/TXD0) (TDO/PCINT20) (PCINT26/RXD1/INT0) (TMS/PCINT19) (PCINT27/TXD1/INT1) (TCK/PCINT18) (PCINT28/XCK1/OC1B) (SDA/PCINT17) (PCINT29/OC1A) (SCL/PCINT16) (PCINT30/OC2B/ICP1) (OC2A/PCINT31) Power Ground Programming/debug Digital Analog Crystal/Osc Atmel ATmega164P/V [DATASHEET] Atmel-42742B-ATmega164P/V_Datasheet_Complete-08/2016 15 PB1 (T1/CLKO/PCINT9) PB0 (XCK0/T0/PCINT8) GND VCC PA0 (ADC0/PCINT0) PA1 (ADC1/PCINT1) PA2 (ADC2/PCINT2) PA3 (ADC3/PCINT3) 41 40 39 38 37 36 35 34 Crystal/Osc PB2 (AIN0/INT2/PCINT10) Analog 42 Digital PB3 (AIN1/OC0A/PCINT11) Programming/debug 43 Ground PB4 (SS/OC0B/PCINT12) Power 44 TQFN and QFN 28 GND XTAL2 7 27 AVCC XTAL1 8 26 PC7 (TOSC2/PCINT23) (PCINT24/RXD0) PD0 9 25 PC6 (TOSC1/PCINT22) (PCINT25/TXD0) PD1 10 24 PC5 (TDI/PCINT21) (PCINT26/RXD1/INT0) PD2 11 23 PC4 (TDO/PCINT20) (PCINT16/SCL) PC0 22 6 (PCINT19/TMS) PC3 GND 21 AREF (PCINT18/TCK) PC2 29 20 5 (PCINT17/SDA) PC1 VCC 19 PA7 (ADC7/PCINT7) 18 30 GND 4 17 RESET VCC PA6 (ADC6/PCINT6) 16 31 (PCINT31/OC2A) PD7 3 15 (PCINT15/SCK) PB7 (PCINT30/OC2B/ICP1) PD6 PA5 (ADC5/PCINT5) 14 (PCINT14/MISO) PB6 32 (PCINT29/OC1A) PD5 PA4 (ADC4/PCINT4) 2 13 33 (PCINT28/XCK1/OC1B) PD4 1 12 (PCINT13/MOSI) PB5 (PCINT27/TXD1/INT1) PD3 5.1.2. 5.2. Pin Descriptions 5.2.1. VCC Digital supply voltage. 5.2.2. GND Ground. 5.2.3. Port A (PA[7:0]) This port serves as analog inputs to the Analog-to-digital Converter. Atmel ATmega164P/V [DATASHEET] Atmel-42742B-ATmega164P/V_Datasheet_Complete-08/2016 16 This is an 8-bit, bi-directional I/O port with internal pull-up resistors, individually selectable for each bit. The output buffers have symmetrical drive characteristics, with both high sink and source capability. As inputs, the port pins that are externally pulled low will source current if pull-up resistors are activated. Port pins are tri-stated when a reset condition becomes active, even if the clock is not running. 5.2.4. Port B (PB[7:0]) This is an 8-bit, bi-directional I/O port with internal pull-up resistors, individually selectable for each bit. The output buffers have symmetrical drive characteristics, with both high sink and source capability. As inputs, the port pins that are externally pulled low will source current if pull-up resistors are activated. Port pins are tri-stated when a reset condition becomes active, even if the clock is not running. This port also serves the functions of various special features. 5.2.5. Port C (PC[7:0]) This is an 8-bit, bi-directional I/O port with internal pull-up resistors, individually selectable for each bit. The output buffers have symmetrical drive characteristics, with both high sink and source capability. As inputs, the port pins that are externally pulled low will source current if pull-up resistors are activated. Port pins are tri-stated when a reset condition becomes active, even if the clock is not running. This port also serves the functions of the JTAG interface, along with special features. 5.2.6. Port D (PD[7:0]) This is an 8-bit, bi-directional I/O port with internal pull-up resistors, individually selectable for each bit. The output buffers have symmetrical drive characteristics, with both high sink and source capability. As inputs, the port pins that are externally pulled low will source current if pull-up resistors are activated. Port pins are tri-stated when a reset condition becomes active, even if the clock is not running. This port also serves the functions of various special features. 5.2.7. RESET Reset input. A low level on this pin for longer than the minimum pulse length will generate a reset, even if the clock is not running. Shorter pulses are not guaranteed to generate a reset. 5.2.8. XTAL1 Input to the inverting Oscillator amplifier and input to the internal clock operating circuit. 5.2.9. XTAL2 Output from the inverting Oscillator amplifier. 5.2.10. AVCC AVCC is the supply voltage pin for Port A and the Analog-to-digital Converter. 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. 5.2.11. AREF This is the analog reference pin for the Analog-to-digital Converter. Atmel ATmega164P/V [DATASHEET] Atmel-42742B-ATmega164P/V_Datasheet_Complete-08/2016 17 6. I/O Multiplexing Each pin is by default controlled by the PORT as a general purpose I/O and alternatively it can be assigned to one of the peripheral functions. The following table describes the peripheral signals multiplexed to the PORT I/O pins. Table 6-1.PORT Function Multiplexing 32-pin TQFP/ QFN/ MLF Pin # 40-pin PDIP Pin # PAD 1 6 PB[5] EXTINT PCINT PCINT13 ADC/AC OSC T/C # 0 T/C # 1 USART MOSI 2 7 PB[6] PCINT14 MISO 3 8 PB[7] PCINT15 SCK 4 9 RESET 5 10 VCC 6 11 GND 7 12 XTAL2 8 13 XTAL1 9 14 PD[0] 10 15 PD[1] 11 16 PD[2] INT0 12 17 PD[3] INT1 13 18 PD[4] PCINT28 OC1B 14 19 PD[5] PCINT29 OC1A 15 20 PD[6] PCINT30 OC2B 16 21 PD[7] PCINT31 OC2A 17 - VCC RxD2 MISO1 18 - GND TxD2 MOSI1 19 22 PC[0] PCINT16 SCL 20 23 PC[1] PCINT17 SDA 21 24 PC[2] PCINT18 TCK 22 25 PC[3] PCINT19 TMS 23 26 PC[4] PCINT20 TDO 24 27 PC[5] PCINT21 TDI 25 28 PC[6] PCINT22 TOSC1 26 29 PC[7] PCINT23 TOSC2 27 30 AVCC 28 31 GND 29 32 AREF 30 33 PA[7] PCINT7 ADC7 31 34 PA[6] PCINT6 ADC6 32 35 PA[5] PCINT5 ADC5 33 36 PA[4] PCINT4 ADC4 34 37 PA[3] PCINT3 ADC3 PCINT24 RxD0 PCINT25 TxD0 PCINT26 RxD1 PCINT27 TXD1 I2C SPI JTAG XCK1 ICP1 AREF Atmel ATmega164P/V [DATASHEET] Atmel-42742B-ATmega164P/V_Datasheet_Complete-08/2016 18 32-pin TQFP/ QFN/ MLF Pin # 40-pin PDIP Pin # PAD 35 38 36 EXTINT PCINT ADC/AC PA[2] PCINT2 ADC2 39 PA[1] PCINT1 ADC1 37 40 PA[0] PCINT0 ADC0 38 - VCC SDA1 39 - GND SCL1 40 1 PB[0] PCINT8 41 2 PB[1] PCINT9 42 3 PB[2] 43 4 44 INT2 OSC T/C # 0 T/C # 1 T0 CLKO PCINT10 AIN0 PB[3] PCINT11 AIN1 5 PB[4] PCINT12 - - GND - - GND - - GND - - GND - - GND USART I2C SPI JTAG XCK0 T1 OC0A OC0B SS Atmel ATmega164P/V [DATASHEET] Atmel-42742B-ATmega164P/V_Datasheet_Complete-08/2016 19 7. General Information 7.1. Resources A comprehensive set of development tools, application notes, and datasheets are available for download on http://www.atmel.com/avr. 7.2. Data Retention Reliability Qualification results show that the projected data retention failure rate is much less than 1 PPM over 20 years at 85C or 100 years at 25C. 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. Confirm with the C compiler documentation for more details. For I/O Registers located in extended I/O map, "IN", "OUT", "SBIS", "SBIC", "CBI", and "SBI" instructions must be replaced with instructions that allow access to extended I/O. Typically "LDS" and "STS" combined with "SBRS", "SBRC", "SBR", and "CBR". 7.4. Capacitive Touch Sensing 7.4.1. QTouch Library (R) (R) The Atmel QTouch Library provides a simple to use solution to realize touch sensitive interfaces on (R) most Atmel AVR microcontrollers. The QTouch Library includes support for the Atmel QTouch and Atmel (R) QMatrix acquisition methods. Touch sensing can be added to any application by linking the appropriate Atmel QTouch Library for the AVR Microcontroller. This is done by using a simple set of APIs to define the touch channels and sensors, and then calling the touch sensing API's to retrieve the channel information and determine the touch sensor states. The QTouch Library is FREE and downloadable from the Atmel website at the following location: http:// www.atmel.com/technologies/touch/. For implementation details and other information, refer to the Atmel QTouch Library User Guide - also available for download from the Atmel website. Atmel ATmega164P/V [DATASHEET] Atmel-42742B-ATmega164P/V_Datasheet_Complete-08/2016 20 8. AVR CPU Core 8.1. Overview This section discusses the AVR core architecture in general. The main function of the CPU core is to ensure correct program execution. The CPU must therefore be able to access memories, perform calculations, control peripherals, and handle interrupts. Figure 8-1.Block Diagram of the AVR Architecture Register file R31 (ZH) R29 (YH) R27 (XH) R25 R23 R21 R19 R17 R15 R13 R11 R9 R7 R5 R3 R1 R30 (ZL) R28 (YL) R26 (XL) R24 R22 R20 R18 R16 R14 R12 R10 R8 R6 R4 R2 R0 Program counter Flash program memory Instruction register Instruction decode Data memory Stack pointer Status register ALU In order to maximize performance and parallelism, the AVR uses a Harvard architecture - with separate memories and buses for program and data. Instructions in the program memory are executed with a single level pipelining. While one instruction is being executed, the next instruction is pre-fetched from the program memory. This concept enables instructions to be executed in every clock cycle. The program memory is In-System Reprogrammable Flash memory. 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. Atmel ATmega164P/V [DATASHEET] Atmel-42742B-ATmega164P/V_Datasheet_Complete-08/2016 21 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, this device has Extended I/O space from 0x60 - 0xFF in SRAM where only the ST/STS/STD and LD/LDS/LDD instructions can be used. 8.2. 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 Instruction Set Summary section for a detailed description. Related Links Instruction Set Summary on page 436 8.3. 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. 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. Atmel ATmega164P/V [DATASHEET] Atmel-42742B-ATmega164P/V_Datasheet_Complete-08/2016 22 8.3.1. Status Register When addressing I/O Registers as data space using LD and ST instructions, the provided offset must be used. When using the I/O specific commands IN and OUT, the offset is reduced by 0x20, resulting in an I/O address offset within 0x00 - 0x3F. Name: SREG Offset: 0x5F Reset: 0x00 Property: When addressing as I/O Register: address offset is 0x3F Bit Access Reset 7 6 5 4 3 2 1 0 I T H S V N Z C R/W R/W R/W R/W R/W R/W R/W R/W 0 0 0 0 0 0 0 0 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 Ibit 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: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 Flag is useful in BCD arithmetic. See the Instruction Set Description for detailed information. Bit 4 - S:Sign Flag, 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 arithmetic. 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. Atmel ATmega164P/V [DATASHEET] Atmel-42742B-ATmega164P/V_Datasheet_Complete-08/2016 23 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. 8.4. General Purpose Register File The Register File is optimized for the AVR Enhanced RISC instruction set. In order to achieve the required performance and flexibility, the following input/output schemes are supported by the Register File: * * * * One 8-bit output operand and one 8-bit result input Two 8-bit output operands and one 8-bit result input Two 8-bit output operands and one 16-bit result input One 16-bit output operand and one 16-bit result input Figure 8-2.AVR CPU General Purpose Working Registers 7 0 Addr. R0 0x00 R1 0x01 R2 0x02 ... R13 0x0D Ge ne ra l R14 0x0E P urpos e R15 0x0F Working R16 0x10 Re gis te rs R17 0x11 ... R26 0x1A X-re gis te r Low Byte R27 0x1B X-re gis te r High Byte R28 0x1C Y-re gis te r Low Byte R29 0x1D Y-re gis te r High Byte R30 0x1E Z-re gis te r Low Byte R31 0x1F Z-re gis te r 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 the figure, 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. 8.4.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 the figure. Atmel ATmega164P/V [DATASHEET] Atmel-42742B-ATmega164P/V_Datasheet_Complete-08/2016 24 Figure 8-3.The X-, Y-, and Z-registers 15 X-register XH 7 0 15 Y-register 7 R26 YH YL 0 7 R28 ZH ZL 0 0 0 R29 7 0 0 R27 7 15 Z-register XL 7 0 0 R31 R30 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). Related Links Instruction Set Summary on page 436 8.5. 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 is implemented as growing from higher to lower memory locations. The Stack Pointer Register always points to the top of the Stack. The Stack Pointer points to the data SRAM Stack area where the Subroutine and Interrupt Stacks are located. A Stack PUSH command will decrease the Stack Pointer. The Stack in the data SRAM must be defined by the program before any subroutine calls are executed or interrupts are enabled. Initial Stack Pointer value equals the last address of the internal SRAM and the Stack Pointer must be set to point above start of the SRAM. See the table for Stack Pointer details. Table 8-1.Stack Pointer Instructions Instruction Stack pointer Description PUSH Decremented by 1 Data is pushed onto the stack CALL Decremented by 2 Return address is pushed onto the stack with a subroutine call or interrupt ICALL RCALL POP Incremented by 1 Data is popped from the stack RET Incremented by 2 Return address is popped from the stack with return from subroutine or return from interrupt RETI The AVR Stack Pointer is implemented as two 8-bit registers in the I/O space. The number of bits actually used is implementation dependent. Note that the data space in some implementations of the AVR architecture is so small that only SPL is needed. In this case, the SPH Register will not be present. Atmel ATmega164P/V [DATASHEET] Atmel-42742B-ATmega164P/V_Datasheet_Complete-08/2016 25 8.5.1. Stack Pointer Register Low and High byte The SPL and SPH register pair represents the 16-bit value, SP.The low byte [7:0] (suffix L) is accessible at the original offset. The high byte [15:8] (suffix H) can be accessed at offset + 0x01. For more details on reading and writing 16-bit registers, refer to Accessing 16-bit Registers. 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 offset addresses. The device is a complex microcontroller with more peripheral units than can be supported within the 64 locations reserved in Opcode for the IN and OUT instructions. For the Extended I/O space from 0x60 in SRAM, only the ST/STS/STD and LD/LDS/LDD instructions can be used. Name: SPL and SPH Offset: 0x5D Reset: 0x4FF Property: When addressing I/O Registers as data space the offset address is 0x3D Bit 15 14 13 12 11 Access R R R R R Reset 0 0 0 0 0 Bit Access Reset 10 9 8 SP10 SP9 SP8 RW RW RW 1 0 0 7 6 5 4 3 2 1 0 SP7 SP6 SP5 SP4 SP3 SP2 SP1 SP0 RW RW RW RW RW RW RW RW 1 1 1 1 1 1 1 1 Bits 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 - SPn:Stack Pointer Register SPL and SPH are combined into SP. 8.6. Accessing 16-bit Registers The AVR data bus is 8 bits wide, and so accessing 16-bit registers requires atomic operations. These registers must be byte-accessed using two read or write operations. 16-bit registers are connected to the 8-bit bus and a temporary register using a 16-bit bus. For a write operation, the low byte of the 16-bit register must be written before the high byte. The low byte is then written into the temporary register. When the high byte of the 16-bit register is written, the temporary register is copied into the low byte of the 16-bit register in the same clock cycle. For a read operation, the low byte of the 16-bit register must be read before the high byte. When the low byte 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. When the high byte is read, it is then read from the temporary register. This ensures that the low and high bytes of 16-bit registers are always accessed simultaneously when reading or writing the register. Interrupts can corrupt the timed sequence if an interrupt is triggered and accesses the same 16-bit register during an atomic 16-bit read/write operation. To prevent this, interrupts can be disabled when writing or reading 16-bit registers. The temporary registers can also be read and written directly from user software. Atmel ATmega164P/V [DATASHEET] Atmel-42742B-ATmega164P/V_Datasheet_Complete-08/2016 26 8.7. Instruction Execution Timing This section describes the general access timing concepts for instruction execution. The AVR CPU is driven by the CPU clock clkCPU, directly generated from the selected clock source for the chip. No internal clock division is used. The Figure below 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 1 MIPS per MHz with the corresponding unique results for functions per cost, functions per clocks, and functions per power-unit. Figure 8-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 The following Figure 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 8-5.Single Cycle ALU Operation T1 T2 T3 T4 clkCPU Total Execution Time Register Operands Fetch ALU Operation Execute Result Write Back 8.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. The lowest addresses in the program memory space are by default defined as the Reset and Interrupt Vectors. They have determined priority levels: 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). The Reset Vector can also be moved to the start of the Boot Flash section by programming the BOOTRST Fuse. Atmel ATmega164P/V [DATASHEET] Atmel-42742B-ATmega164P/V_Datasheet_Complete-08/2016 27 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. 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 exits from an interrupt, it will always return to the main program and execute one more instruction before any pending interrupt is served. 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(1) in r16, SREG ; store SREG value cli ; disable interrupts during timed sequence sbi EECR, EEMPE ; start EEPROM write sbi EECR, EEPE out SREG, r16 ; restore SREG value (I-bit) C Code Example(1) char cSREG; cSREG = SREG; /* store SREG value */ /* disable interrupts during timed sequence */ _CLI(); EECR |= (1< ... r16,high(RAMEND) SPH,r16 r16,low(RAMEND) SPL,r16 ; Main program start ; Set Stack Pointer to top of RAM xxx Timer1 CompareA Timer1 CompareB Timer1 Overflow Timer0 CompareA Timer0 CompareB Timer0 Overflow SPI Transfer Complete USART RX Complete USART UDR Empty USART TX Complete Analog Comparator ADC Conversion Complete EEPROM Ready 2-wire Serial SPM Ready USART1 RX Complete USART1 UDR Empty USART1 TX Complete ; Enable interrupts ... When the BOOTRST Fuse is unprogrammed, the Boot section size set to 8Kbytes and the MCUCR.IVSEL is set before any interrupts are enabled, the most typical and general program setup for the Reset and Interrupt Vector Addresses is: Address Labels 0x00000 RESET: 0x00001 0x00002 0x00003 0x00004 0x00005 ; .org 0x1F002 0x1F002 0x1F004 ... 0x1FO36 Code ldi r16,high(RAMEND) out SPH,r16 ldi r16,low(RAMEND) out SPL,r16 sei xxx Comments ; Main program start ; Set Stack Pointer to top of RAM jmp jmp ... jmp ; IRQ0 Handler ; IRQ1 Handler ; ; SPM Ready Handler EXT_INT0 EXT_INT1 ... SPM_RDY ; Enable interrupts When the BOOTRST Fuse is programmed and the Boot section size set to 8Kbytes, the most typical and general program setup for the Reset and Interrupt Vector Addresses is: Address Labels .org 0x0002 0x00002 0x00004 ... 0x00036 ; .org 0x1F000 0x1F000 RESET: 0x1F001 0x1F002 0x1F003 0x1F004 0x1F005 Code Comments jmp jmp ... jmp EXT_INT0 EXT_INT1 ... SPM_RDY ; IRQ0 Handler ; IRQ1 Handler ; ; SPM Ready Handler ldi out ldi out sei r16,high(RAMEND) SPH,r16 r16,low(RAMEND) SPL,r16 ; Main program start ; Set Stack Pointer to top of RAM xxx ; Enable interrupts When the BOOTRST Fuse is programmed, the Boot section size set to 8Kbytes and the MCUCR.IVSEL Register is set before any interrupts are enabled, the most typical and general program setup for the Reset and Interrupt Vector Addresses is: Address Labels ; .org 0x1F000 0x1F000 0x1F002 Code jmp jmp Comments RESET EXT_INT0 ; Reset handler ; IRQ0 Handler Atmel ATmega164P/V [DATASHEET] Atmel-42742B-ATmega164P/V_Datasheet_Complete-08/2016 80 0x1F004 ... 0x1F036 ; 0x1F03E 0x1F03F 0x1F040 0x1F041 0x1F042 0x1FO43 RESET: jmp ... jmp EXT_INT1 ... SPM_RDY ; IRQ1 Handler ; ; SPM Ready Handler ldi out ldi out sei r16,high(RAMEND) SPH,r16 r16,low(RAMEND) SPL,r16 ; Main program start ; Set Stack Pointer to top of RAM xxx ; Enable interrupts 13.3. Register Description 13.3.1. Moving Interrupts Between Application and Boot Space The MCU Control Register controls the placement of the Interrupt Vector table. Atmel ATmega164P/V [DATASHEET] Atmel-42742B-ATmega164P/V_Datasheet_Complete-08/2016 81 13.3.2. MCU Control Register When addressing I/O Registers as data space using LD and ST instructions, the provided offset must be used. When using the I/O specific commands IN and OUT, the offset is reduced by 0x20, resulting in an I/O address offset within 0x00 - 0x3F. The device is a complex microcontroller with more peripheral units than can be supported within the 64 locations reserved in Opcode for the IN and OUT instructions. For the Extended I/O space from 0x60 in SRAM, only the ST/STS/STD and LD/LDS/LDD instructions can be used. Name: MCUCR Offset: 0x55 Reset: 0x00 Property: When addressing as I/O Register: address offset is 0x35 Bit Access Reset 7 6 5 4 1 0 JTD BODS BODSE PUD 3 2 IVSEL IVCE R/W R/W R/W R/W R/W R/W 0 0 0 0 0 0 Bit 7 - JTD When this bit is zero, the JTAG interface is enabled if the JTAGEN Fuse is programmed. If this bit is one, the JTAG interface is disabled. In order to avoid unintentional disabling or enabling of the JTAG interface, a timed sequence must be followed when changing this bit: The application software must write this bit to the desired value twice within four cycles to change its value. Note that this bit must not be altered when using the On-chip Debug system. Bit 6 - BODS:BOD Sleep The BODS bit must be written to '1' in order to turn off BOD during sleep. Writing to the BODS bit is controlled by a timed sequence and the enable bit BODSE. To disable BOD in relevant sleep modes, both BODS and BODSE must first be written to '1'. Then, BODS must be written to '1' and BODSE must be written to zero within four clock cycles. The BODS bit is active three clock cycles after it is set. A sleep instruction must be executed while BODS is active in order to turn off the BOD for the actual sleep mode. The BODS bit is automatically cleared after three clock cycles. Bit 5 - BODSE:BOD Sleep Enable BODSE enables setting of BODS control bit, as explained in BODS bit description. BOD disable is controlled by a timed sequence. Bit 4 - PUD:Pull-up Disable When this bit is written to one, the pull-ups in the I/O ports are disabled even if the DDxn and PORTxn Registers are configured to enable the pull-ups ({DDxn, PORTxn} = 0b01). 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. To avoid unintentional changes of Interrupt Vector tables, a special write procedure must be followed to change the IVSEL bit: Atmel ATmega164P/V [DATASHEET] Atmel-42742B-ATmega164P/V_Datasheet_Complete-08/2016 82 1. 2. Write the Interrupt Vector Change Enable (IVCE) bit to one. 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. 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: ; Get MCUCR in r16, MCUCR mov r17, r16 ; Enable change of Interrupt Vectors ori r16, (1< CSn[2:0] > 0x1). 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. 18.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. See also the block diagram of the T1/T0 synchronization and edge detector logic below. 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 (CSn[2:0]=0x7) or negative (CSn[2:0]=0x6) edge it detects. Figure 18-1.T1/T0 Pin Sampling Tn D Q D Q D Tn_sync (To Clock Select Logic) Q LE clk I/O Synchronization Edge Detector Atmel ATmega164P/V [DATASHEET] Atmel-42742B-ATmega164P/V_Datasheet_Complete-08/2016 186 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. 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 (fTn < fclk_I/O/2) given a 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 the tolerances of the oscillator source (crystal, resonator, and capacitors), 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 18-2.Prescaler for Timer/Counter0 and Timer/Counter1(1) clk I/O 10-BIT T/C PRESCALER CK/1024 CK/256 PSR10 CK/64 CK/8 Clear OFF Tn Synchronization CSn0 CSn1 CSn2 TIMER /COUNTERn CLOCK SOURCE clk Tn Note: 1. The synchronization logic on the input pins (T1/T0) is shown in the block diagram above. 18.4. Register Description Atmel ATmega164P/V [DATASHEET] Atmel-42742B-ATmega164P/V_Datasheet_Complete-08/2016 187 18.4.1. General Timer/Counter Control Register When addressing I/O Registers as data space using LD and ST instructions, the provided offset must be used. When using the I/O specific commands IN and OUT, the offset is reduced by 0x20, resulting in an I/O address offset within 0x00 - 0x3F. The device is a complex microcontroller with more peripheral units than can be supported within the 64 locations reserved in Opcode for the IN and OUT instructions. For the Extended I/O space from 0x60 in SRAM, only the ST/STS/STD and LD/LDS/LDD instructions can be used. Name: GTCCR Offset: 0x43 Reset: 0x00 Property: When addressing as I/O Register: address offset is 0x23 Bit Access Reset 1 0 TSM 7 6 5 4 3 2 PSRASY PSRSYNC R/W R/W R/W 0 0 0 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 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. 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. Atmel ATmega164P/V [DATASHEET] Atmel-42742B-ATmega164P/V_Datasheet_Complete-08/2016 188 19. TC2 - 8-bit Timer/Counter2 with PWM and Asynchronous Operation 19.1. Features * * * * * * * 19.2. Single Channel Counter Clear Timer on Compare Match (Auto Reload) 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 Timer/Counter2 (TC2) is a general purpose, single channel, 8-bit Timer/Counter module. A simplified block diagram of the 8-bit Timer/Counter is shown below. 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 following Register Description. For the actual placement of I/O pins, refer to the pinout diagram. The TC2 is enabled when the PRTIM2 bit in the Power Reduction Register (PRR.PRTIM2) is written to '1'. Atmel ATmega164P/V [DATASHEET] Atmel-42742B-ATmega164P/V_Datasheet_Complete-08/2016 189 Figure 19-1.8-bit Timer/Counter Block Diagram Count Clear Direction TOVn (Int.Req.) Control Logic Clock Select clkTn Edge Detector TOP BOTTOM ( From Prescaler ) Timer/Counter TCNTn Tn = =0 OCnA (Int.Req.) Waveform Generation = OCnA OCRnA DATA BUS Fixed TOP Value OCnB (Int.Req.) Waveform Generation = OCnB OCRnB TCCRnA TCCRnB Related Links Pin Configurations on page 15 19.2.1. Definitions Many register and bit references in this section are written in general form: * n=2 represents the Timer/Counter number * x=A,B represents the Output Compare Unit A or B However, when using the register or bit definitions in a program, the precise form must be used, i.e., TCNT2 for accessing Timer/Counter2 counter value. The following definitions are used throughout the section: Atmel ATmega164P/V [DATASHEET] Atmel-42742B-ATmega164P/V_Datasheet_Complete-08/2016 190 Table 19-1.Definitions Constant Description BOTTOM The counter reaches the BOTTOM when it becomes zero (0x00). 19.2.2. 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. 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 Output Compare Unit 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. 19.3. 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/synchronous to the MCU clock, clkI/O. When the Asynchronous TC2 bit in the Asynchronous Status Register (ASSR.AS2) is written to '1', the clock source is taken from the Timer/Counter Oscillator connected to TOSC1 and TOSC2. For details on asynchronous operation, see the description of the ASSR. For details on clock sources and prescaler, see Timer/Counter Prescaler. 19.4. Counter Unit The main part of the 8-bit Timer/Counter is the programmable bi-directional counter unit. Below is the block diagram of the counter and its surroundings. Atmel ATmega164P/V [DATASHEET] Atmel-42742B-ATmega164P/V_Datasheet_Complete-08/2016 191 Figure 19-2.Counter Unit Block Diagram TOVn (Int.Req.) DATA BUS TOSC1 count clear TCNTn clk Tn Control Logic Prescaler T/C Oscillator direction bottom TOSC2 clkI/O top Table 19-2.Signal description (internal signals): Signal name Description 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 (CS2[2:0]). When no clock source is selected (CS2[2:0]=0x0) 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 bit 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 "Modes of Operation". The Timer/Counter Overflow Flag (TOV2) is set according to the mode of operation selected by the TCC2B.WGM2[2:0] bits. TOV2 can be used for generating a CPU interrupt. 19.5. 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 WGM2[2:0] bits and Compare Output mode (COM2x[1:0]) bits. The max and bottom signals are used by the Waveform Generator for handling the special cases of the extreme values in some modes of operation (See Modes of Operation). The following figure shows a block diagram of the Output Compare unit. Atmel ATmega164P/V [DATASHEET] Atmel-42742B-ATmega164P/V_Datasheet_Complete-08/2016 192 Figure 19-3.Output Compare Unit, Block Diagram DATA BUS TCNTn OCRnx = (8-bit Comparator ) OCFnx (Int.Req.) top bottom Waveform Generator OCnx FOCn WGMn[1:0] COMnx[1:0] 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. Related Links Modes of Operation on page 134 19.5.1. Force Output Compare In non-PWM waveform generation modes, the match output of the comparator can be forced by writing a one to the Force Output Compare (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 COM2x[1:0] bits settings define whether the OC2x pin is set, cleared or toggled). 19.5.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. 19.5.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 Atmel ATmega164P/V [DATASHEET] Atmel-42742B-ATmega164P/V_Datasheet_Complete-08/2016 193 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 COM2x[1:0] bits are not double buffered together with the compare value. Changing the COM2x[1:0] bits will take effect immediately. Compare Match Output Unit The Compare Output mode (COM2x[1:0]) bits have two functions. The Waveform Generator uses the COM2x[1:0] bits for defining the Output Compare (OC2x) state at the next compare match. Also, the COM2x[1:0] bits control the OC2x pin output source. The following figure shows a simplified schematic of the logic affected by the COM2x[1: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 COM2x[1:0] bits are shown. When referring to the OC2x state, the reference is for the internal OC2x Register, not the OC2x pin. Figure 19-4.Compare Match Output Unit, Schematic COMnx[1] COMnx[0] FOCnx Waveform Generator D Q 1 OCnx D DATA BUS 19.6. 0 OCnx Pin Q PORT D Q DDR clk I/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 COM2x[1:0] bit settings are reserved for certain modes of operation. See Register Description. Related Links Modes of Operation on page 134 Atmel ATmega164P/V [DATASHEET] Atmel-42742B-ATmega164P/V_Datasheet_Complete-08/2016 194 19.6.1. Compare Output Mode and Waveform Generation The Waveform Generator uses the COM2x[1:0] bits differently in normal, CTC, and PWM modes. For all modes, setting the COM2x[1:0] = 0 tells the Waveform Generator that no action on the OC2x Register is to be performed on the next compare match. Refer also to the descriptions of the output modes. A change of the COM2x[1: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. 19.7. Modes of Operation The mode of operation, i.e., the behavior of the Timer/Counter and the Output Compare pins, is defined by the combination of the Waveform Generation mode (WGM2[2:0]) and Compare Output mode (COM2x[1:0]) bits. The Compare Output mode bits do not affect the counting sequence, while the Waveform Generation mode bits do. The COM2x[1:0] bits control whether the PWM output generated should be inverted or not (inverted or non-inverted PWM). For non-PWM modes the COM2x[1:0] bits control whether the output should be set, cleared, or toggled at a compare match (See Compare Match Output Unit). For detailed timing information refer to Timer/Counter Timing Diagrams. 19.7.1. Normal Mode The simplest mode of operation is the Normal mode (WGM2[2: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. 19.7.2. Clear Timer on Compare Match (CTC) Mode In Clear Timer on Compare or CTC mode (WGM2[2: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 as follows. The counter value (TCNT2) increases until a compare match occurs between TCNT2 and OCR2A, and then counter (TCNT2) is cleared. Atmel ATmega164P/V [DATASHEET] Atmel-42742B-ATmega164P/V_Datasheet_Complete-08/2016 195 Figure 19-5.CTC Mode, Timing Diagram OCnx Interrupt Flag Set TCNTn OCn (Toggle) (COMnx[1:0] = 0x1) Period 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 (COM2A[1: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: OCnx = clk_I/O 2 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. 19.7.3. Fast PWM Mode The fast Pulse Width Modulation or fast PWM mode (WGM2[2:0] = 0x3 or 0x7) 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[2:0] = 0x3, and OCR2A when WGM2[2:0] = 0x7. 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 depicted in the following figure. 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 Atmel ATmega164P/V [DATASHEET] Atmel-42742B-ATmega164P/V_Datasheet_Complete-08/2016 196 small horizontal line marks on the TCNT2 slopes represent compare matches between OCR2x and TCNT2. Figure 19-6.Fast PWM Mode, Timing Diagram OCRnx Interrupt Flag Set OCRnx Update and TOVn Interrupt Flag Set TCNTn OCnx (COMnx[1:0] = 0x2) OCnx (COMnx[1:0] = 0x3) 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 COM2x[1:0] to three. TOP is defined as 0xFF when WGM2[2:0] = 0x3, and OCR2A when MGM2[2:0] = 0x7. 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: OCnxPWM = clk_I/O 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 COM2A[1:0] bits.) 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 (COM2x[1: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. 19.7.4. Phase Correct PWM Mode The phase correct PWM mode (WGM2[2:0] = 0x1 or 0x5) 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 Atmel ATmega164P/V [DATASHEET] Atmel-42742B-ATmega164P/V_Datasheet_Complete-08/2016 197 0xFF when WGM2[2:0] = 0x3, and OCR2A when MGM2[2: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 19-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 19-7.Phase Correct PWM Mode, Timing Diagram OCnx Interrupt Flag Set OCRnx Update TOVn Interrupt Flag Set TCNTn OCnx (COMnx[1:0] = 2) OCnx (COMnx[1: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. In phase correct PWM mode, the compare unit allows generation of PWM waveforms on the OC2x pin. Setting the COM2x[1:0] bits to two will produce a non-inverted PWM. An inverted PWM output can be generated by setting the COM2x[1:0] to three. TOP is defined as 0xFF when WGM2[2:0] = 0x3, and OCR2A when WGM2[2:0] = 7. 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: Atmel ATmega164P/V [DATASHEET] Atmel-42742B-ATmega164P/V_Datasheet_Complete-08/2016 198 OCnxPCPWM = clk_I/O 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 the above figure OC2x 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, as shown in the preceeding figure. When the OCR2A value is MAX the OC2 pin value is the same as the result of a down-counting compare match. To ensure symmetry around BOTTOM the OC2 value at MAX must correspond to the result of an up-counting Compare Match. * The timer starts counting from a value higher than the one in OCR2A, and for that reason misses the Compare Match and hence the OC2 change that would have happened on the way up. 19.8. 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. The following figure 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 19-8.Timer/Counter Timing Diagram, no Prescaling clkI/O clkTn (clkI/O /1) TCNTn MAX - 1 MAX BOTTOM BOTTOM + 1 TOVn The following figure shows the same timing data, but with the prescaler enabled. Atmel ATmega164P/V [DATASHEET] Atmel-42742B-ATmega164P/V_Datasheet_Complete-08/2016 199 Figure 19-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 The following figure shows the setting of OCF2A in all modes except CTC mode. Figure 19-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 The following figure shows the setting of OCF2A and the clearing of TCNT2 in CTC mode. Figure 19-11.Timer/Counter Timing Diagram, Clear Timer on Compare Match mode, with Prescaler (fclk_I/O/8) clkI/O clkTn (clkI/O /8) TCNTn (CTC) TOP - 1 TOP OCRnx BOTTOM BOTTOM + 1 TOP OCFnx 19.9. Asynchronous Operation of Timer/Counter2 When TC2 operates asynchronously, some considerations must be taken: * When switching between asynchronous and synchronous clocking of TC2, the registers TCNT2, OCR2x, and TCCR2x might be corrupted. A safe procedure for switching clock source is: Atmel ATmega164P/V [DATASHEET] Atmel-42742B-ATmega164P/V_Datasheet_Complete-08/2016 200 * * * * * * * 1. Disable the TC2 interrupts by clearing OCIE2x and TOIE2. 2. Select clock source by setting AS2 as appropriate. 3. Write new values to TCNT2, OCR2x, and TCCR2x. 4. To switch to asynchronous operation: Wait for TCN2xUB, OCR2xUB, and TCR2xUB. 5. Clear the TC2 Interrupt Flags. 6. 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 has its individual temporary register, which means that e.g. writing to TCNT2 does not disturb an OCR2x write in progress. The Asynchronous Status Register (ASSR) indicates that a transfer to the destination register has taken place. 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 TC2 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 interrupts 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 TC2 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: If re-entering sleep mode within the TOSC1 cycle, the interrupt will immediately occur and the device wake up again. The result is multiple interrupts and wake-ups within one TOSC1 cycle from the first interrupt. 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: 1. Write a value to TCCR2x, TCNT2, or OCR2x. 2. Wait until the corresponding Update Busy Flag in ASSR returns to zero. 3. Enter Power-save or ADC Noise Reduction mode. When the asynchronous operation is selected, the 32.768kHz oscillator for TC2 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 TC2 after power-up or wake-up from Power-down or Standby mode. The contents of all TC2 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. 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 Atmel ATmega164P/V [DATASHEET] Atmel-42742B-ATmega164P/V_Datasheet_Complete-08/2016 201 * unpredictable, as it depends on the wake-up time. The recommended procedure for reading TCNT2 is thus as follows: 1. Wait for the corresponding Update Busy Flag to be cleared. 2. 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. 19.10. Timer/Counter Prescaler Figure 19-12.Prescaler for TC2 PSRASY clkT2S/1024 clkT2S/256 clkT2S/128 clkT2S/64 AS2 10-BIT T/C PRESCALER Clear clkT2S/32 TOSC1 clkT2S clkT2S/8 clkI/O 0 CS20 CS21 CS22 TIMER/COUNTER2 CLOCK SOURCE clkT2 The clock source for TC2 is named clkT2S. It is by default connected to the main system I/O clock clkI/O. By writing a '1' to the Asynchronous TC2 bit in the Asynchronous Status Register (ASSR.AS2), TC2 is asynchronously clocked from the TOSC1 pin. This enables use of TC2 as a Real Time Counter (RTC). When AS2 is set, pins TOSC1 and TOSC2 are disconnected from Port B. A crystal can then be connected between the TOSC1 and TOSC2 pins to serve as an independent clock source for TC2. The Oscillator is optimized for use with a 32.768kHz crystal. For TC2, 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. The prescaler is reset by writing a '1' to the Prescaler Reset TC2 bit in the General TC2 Control Register (GTCCR.PSRASY). This allows the user to operate with a defined prescaler. 19.11. Register Description Atmel ATmega164P/V [DATASHEET] Atmel-42742B-ATmega164P/V_Datasheet_Complete-08/2016 202 19.11.1. TC2 Control Register A Name: TCCR2A Offset: 0xB0 Reset: 0x00 Property: Bit Access Reset 7 6 5 4 COM2A1 COM2A0 COM2B1 R/W R/W R/W 0 0 0 3 2 1 0 COM2B0 WGM21 WGM20 R/W R/W R/W 0 0 0 Bits 7:6 - COM2An:Compare Output Mode for Channel A [n = 1:0] These bits control the Output Compare pin (OC2A) behavior. If one or both of the COM2A[1: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 COM2A[1:0] bits depends on the WGM2[2:0] bit setting. The table below shows the COM2A[1:0] bit functionality when the WGM2[2:0] bits are set to a normal or CTC mode (non- PWM). Table 19-3.Compare Output Mode, non-PWM COM2A1 COM2A0 Description 0 0 Normal port operation, OC2A disconnected. 0 1 Toggle OC2A on Compare Match. 1 0 Clear OC2A on Compare Match. 1 1 Set OC2A on Compare Match . The table below shows the COM2A[1:0] bit functionality when the WGM2[1:0] bits are set to fast PWM mode. Table 19-4.Compare Output Mode, Fast PWM(1) COM2A1 COM2A0 Description 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, set OC2A at BOTTOM (non-inverting mode) 1 1 Set OC2A on Compare Match, clear OC2A at BOTTOM (inverting mode) Note: 1. 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 BOTTOM. Refer to Fast PWM Mode for details. The table below shows the COM2A[1:0] bit functionality when the WGM2[2:0] bits are set to phase correct PWM mode. Atmel ATmega164P/V [DATASHEET] Atmel-42742B-ATmega164P/V_Datasheet_Complete-08/2016 203 Table 19-5.Compare Output Mode, Phase Correct PWM Mode(1) COM2A1 COM2A0 Description 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 1 Set OC2A on Compare Match when up-counting. Clear OC2A on Compare Match when down-counting. Note: 1. 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. Refer to Phase Correct PWM Mode for details. Bits 5:4 - COM2Bn:Compare Output Mode for Channel B [n = 1:0] These bits control the Output Compare pin (OC2B) behavior. If one or both of the COM2B[1: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 COM2B[1:0] bits depends on the WGM2[2:0] bit setting. The table shows the COM2B[1:0] bit functionality when the WGM2[2:0] bits are set to a normal or CTC mode (non- PWM). Table 19-6.Compare Output Mode, non-PWM COM2B1 COM2B0 Description 0 0 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. The table below shows the COM0B[1:0] bit functionality when the WGM0[2:0] bits are set to fast PWM mode. Table 19-7.Compare Output Mode, Fast PWM(1) COM0B1 COM0B0 Description 0 0 Normal port operation, OC0B disconnected. 0 1 Reserved 1 0 Clear OC0B on Compare Match, set OC0B at BOTTOM, (non-inverting mode) 1 1 Set OC0B on Compare Match, clear OC0B at BOTTOM, (inverting mode) Note: Atmel ATmega164P/V [DATASHEET] Atmel-42742B-ATmega164P/V_Datasheet_Complete-08/2016 204 1. A special case occurs when OCR2B equals TOP and COM2B[1] is set. In this case, the Compare Match is ignored, but the set or clear is done at TOP. Refer to Fast PWM Mode for details. The table below shows the COM2B[1:0] bit functionality when the WGM2[2:0] bits are set to phase correct PWM mode. Table 19-8.Compare Output Mode, Phase Correct PWM Mode(1) COM2B1 COM2B0 Description 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 1 Set OC2B on Compare Match when up-counting. Clear OC2B on Compare Match when down-counting. Note: 1. 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. Refer to Phase Correct PWM Mode for details. Bits 1:0 - WGM2n:Waveform Generation Mode [n = 1:0] 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. Modes of operation supported by the Timer/Counter unit are: Normal mode (counter), Clear Timer on Compare Match (CTC) mode, and two types of Pulse Width Modulation (PWM) modes (see Modes of Operation). Table 19-9.Waveform Generation Mode Bit Description Mode WGM22 WGM21 WGM20 Timer/Counter Mode of Operation TOP Update of OCR0x at 0 0 0 1 0 0 2 0 1 3 0 1 4 1 5 TOV Flag Set on(1) 0 Normal 0xFF Immediate MAX 1 PWM, Phase Correct 0xFF TOP BOTTOM 0 CTC OCRA Immediate MAX 1 Fast PWM 0xFF BOTTOM MAX 0 0 Reserved - - - 1 0 1 PWM, Phase Correct OCRA TOP BOTTOM 6 1 1 0 Reserved - - - 7 1 1 1 Fast PWM OCRA BOTTOM TOP Note: 1. MAX = 0xFF 2. BOTTOM = 0x00 Atmel ATmega164P/V [DATASHEET] Atmel-42742B-ATmega164P/V_Datasheet_Complete-08/2016 205 19.11.2. TC2 Control Register B Name: TCCR2B Offset: 0xB1 Reset: 0x00 Property: Bit Access Reset 7 6 FOC2A FOC2B 5 4 WGM22 3 2 1 0 R/W R/W R/W R/W R/W R/W 0 0 0 0 0 0 CS2[2:0] Bit 7 - FOC2A:Force Output Compare A The FOC2A bit is only active when the WGM bits specify a non-PWM mode. To ensure 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 COM2A[1:0] bits setting. Note that the FOC2A bit is implemented as a strobe. Therefore it is the value present in the COM2A[1: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. To ensure 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 COM2B[1:0] bits setting. Note that the FOC2B bit is implemented as a strobe. Therefore it is the value present in the COM2B[1: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. Bit 3 - WGM22:Waveform Generation Mode Refer to TCCR2A. Bits 2:0 - CS2[2:0]:Clock Select 2 [n = 0..2] The three Clock Select bits select the clock source to be used by the Timer/Counter. Table 19-10.Clock Select Bit Description CA22 CA21 CS20 0 0 0 No clock source (Timer/Counter stopped). 1 clkI/O/1 (No prescaling) 0 clkI/O/8 (From prescaler) 0 0 1 Description Atmel ATmega164P/V [DATASHEET] Atmel-42742B-ATmega164P/V_Datasheet_Complete-08/2016 206 CA22 CA21 CS20 Description 0 1 1 clkI/O/32 (From prescaler) 1 0 0 clkI/O/64 (From prescaler) 1 0 1 clkI/O/128 (From prescaler) 1 1 0 clkI/O/256 (From prescaler) 1 1 1 clkI/O/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. Atmel ATmega164P/V [DATASHEET] Atmel-42742B-ATmega164P/V_Datasheet_Complete-08/2016 207 19.11.3. TC2 Counter Value Register Name: TCNT2 Offset: 0xB2 Reset: 0x00 Property: Bit 7 6 5 4 3 2 1 0 TCNT2[7:0] Access Reset R/W R/W R/W R/W R/W R/W R/W R/W 0 0 0 0 0 0 0 0 Bits 7:0 - TCNT2[7:0]:Timer/Counter 2 Counter Value 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. Atmel ATmega164P/V [DATASHEET] Atmel-42742B-ATmega164P/V_Datasheet_Complete-08/2016 208 19.11.4. TC2 Output Compare Register A Name: OCR2A Offset: 0xB3 Reset: 0x00 Property: Bit 7 6 5 4 3 2 1 0 OCR2A[7:0] Access Reset R/W R/W R/W R/W R/W R/W R/W R/W 0 0 0 0 0 0 0 0 Bits 7:0 - OCR2A[7:0]:Output Compare 2 A 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. Atmel ATmega164P/V [DATASHEET] Atmel-42742B-ATmega164P/V_Datasheet_Complete-08/2016 209 19.11.5. TC2 Output Compare Register B Name: OCR2B Offset: 0xB4 Reset: 0x00 Property: Bit 7 6 5 4 3 2 1 0 OCR2B[7:0] Access Reset R/W R/W R/W R/W R/W R/W R/W R/W 0 0 0 0 0 0 0 0 Bits 7:0 - OCR2B[7:0]:Output Compare 2 B 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. Atmel ATmega164P/V [DATASHEET] Atmel-42742B-ATmega164P/V_Datasheet_Complete-08/2016 210 19.11.6. TC2 Interrupt Mask Register Name: TIMSK2 Offset: 0x70 Reset: 0x00 Property: Bit Access Reset 7 6 5 4 3 2 1 0 OCIEB OCIEA TOIE R/W R/W R/W 0 0 0 Bit 2 - OCIEB:Timer/Counter2, Output Compare B Match Interrupt Enable When the OCIEB bit is written to '1' 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 OCFB bit is set in TIFR2. Bit 1 - OCIEA:Timer/Counter2, Output Compare A Match Interrupt Enable When the OCIEA bit is written to '1' 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 OCFA bit is set in TIFR2. Bit 0 - TOIE:Timer/Counter2, Overflow Interrupt Enable When the TOIE bit is written to '1' 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 TOV bit is set in TIFR2. Atmel ATmega164P/V [DATASHEET] Atmel-42742B-ATmega164P/V_Datasheet_Complete-08/2016 211 19.11.7. TC2 Interrupt Flag Register When addressing I/O Registers as data space using LD and ST instructions, the provided offset must be used. When using the I/O specific commands IN and OUT, the offset is reduced by 0x20, resulting in an I/O address offset within 0x00 - 0x3F. The device is a complex microcontroller with more peripheral units than can be supported within the 64 locations reserved in Opcode for the IN and OUT instructions. For the Extended I/O space from 0x60 in SRAM, only the ST/STS/STD and LD/LDS/LDD instructions can be used. Name: TIFR2 Offset: 0x37 Reset: 0x00 Property: When addressing as I/O Register: address offset is 0x17 Bit Access Reset 7 6 5 4 3 2 1 0 OCFB OCFA TOV R/W R/W R/W 0 0 0 Bit 2 - OCFB:Timer/Counter2, Output Compare B Match Flag The OCFB bit is set (one) when a compare match occurs between the Timer/Counter2 and the data in OCRB - Output Compare Register2. OCFB is cleared by hardware when executing the corresponding interrupt handling vector. Alternatively, OCFB is cleared by writing a logic one to the flag. When the I-bit in SREG, OCIEB (Timer/Counter2 Compare match Interrupt Enable), and OCFB are set (one), the Timer/ Counter2 Compare match Interrupt is executed. Bit 1 - OCFA:Timer/Counter2, Output Compare A Match Flag The OCFA bit is set (one) when a compare match occurs between the Timer/Counter2 and the data in OCRA - Output Compare Register2. OCFA is cleared by hardware when executing the corresponding interrupt handling vector. Alternatively, OCFA is cleared by writing a logic one to the flag. When the I-bit in SREG, OCIEA (Timer/Counter2 Compare match Interrupt Enable), and OCFA are set (one), the Timer/ Counter2 Compare match Interrupt is executed. Bit 0 - TOV:Timer/Counter2, Overflow Flag The TOV bit is set (one) when an overflow occurs in Timer/Counter2. TOV is cleared by hardware when executing the corresponding interrupt handling vector. Alternatively, TOV is cleared by writing a logic one to the flag. When the SREG I-bit, TOIEA (Timer/Counter2 Overflow Interrupt Enable), and TOV 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. Atmel ATmega164P/V [DATASHEET] Atmel-42742B-ATmega164P/V_Datasheet_Complete-08/2016 212 19.11.8. Asynchronous Status Register Name: ASSR Offset: 0xB6 Reset: 0x00 Property: Bit 7 6 5 4 3 2 1 0 EXCLK AS2 TCN2UB OCR2AUB OCR2BUB TCR2AUB TCR2BUB Access R R R R R R R Reset 0 0 0 0 0 0 0 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:Enable External Clock Input 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. 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. Atmel ATmega164P/V [DATASHEET] Atmel-42742B-ATmega164P/V_Datasheet_Complete-08/2016 213 19.11.9. General Timer/Counter Control Register When addressing I/O Registers as data space using LD and ST instructions, the provided offset must be used. When using the I/O specific commands IN and OUT, the offset is reduced by 0x20, resulting in an I/O address offset within 0x00 - 0x3F. The device is a complex microcontroller with more peripheral units than can be supported within the 64 locations reserved in Opcode for the IN and OUT instructions. For the Extended I/O space from 0x60 in SRAM, only the ST/STS/STD and LD/LDS/LDD instructions can be used. Name: GTCCR Offset: 0x43 Reset: 0x00 Property: When addressing as I/O Register: address offset is 0x23 Bit Access Reset 1 0 TSM 7 6 5 4 3 2 PSRASY PSRSYNC R/W R/W R/W 0 0 0 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 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. 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. Atmel ATmega164P/V [DATASHEET] Atmel-42742B-ATmega164P/V_Datasheet_Complete-08/2016 214 20. SPI - Serial Peripheral Interface 20.1. Features * * * * * * * * 20.2. 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 Overview The Serial Peripheral Interface (SPI) allows high-speed synchronous data transfer between the device and peripheral units, or between several AVR devices. The USART can also be used in Master SPI mode, please refer to USART in SPI Mode chapter. To enable the SPI module, Power Reduction Serial Peripheral Interface bit in the Power Reduction Register (0.PRSPI0) must be written to '0'. Atmel ATmega164P/V [DATASHEET] Atmel-42742B-ATmega164P/V_Datasheet_Complete-08/2016 215 Figure 20-1.SPI Block Diagram SPI2X SPI2X DIVIDER /2/4/8/16/32/64/128 Note: Refer to the pin-out description and the IO Port description for SPI pin placement. The interconnection between Master and Slave CPUs with SPI is shown in the figure below. 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 Atmel ATmega164P/V [DATASHEET] Atmel-42742B-ATmega164P/V_Datasheet_Complete-08/2016 216 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 20-2.SPI Master-slave Interconnection SHIFT ENABLE 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 minimum low and high periods should be longer than two CPU clock cycles. When the SPI is enabled, the data direction of the MOSI, MISO, SCK, and SS pins is overridden according to the table below. For more details on automatic port overrides, refer to the IO Port description. Table 20-1.SPI Pin Overrides Pin Direction, Master SPI Direction, Slave SPI MOSI User Defined Input MISO Input User Defined SCK User Defined Input SS User Defined Input Note: 1. See the IO Port description for how to define the SPI pin directions. 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 SPI_MasterInit: ; Set MOSI and SCK output, all others input ldi r17,(1<>8); UBRR0L = (unsigned char)ubrr; Enable receiver and transmitter */ UCSR0B = (1<> 1) & 0x01; return ((resh << 8) | resl); 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. Related Links About Code Examples on page 20 21.8.3. Receive Compete Flag and Interrupt The USART Receiver has one flag that indicates the Receiver state. The Receive Complete (RXC) Flag indicates if there are unread data present in the receive buffer. This flag is one when unreaddata 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 (RXEN = 0), the receive buffer will be flushed and consequently the RXCn bit will become zero. When the Receive Complete Interrupt Enable (RXCIE) in UCSRnB is set, the USART Receive Complete interrupt will be executed as long as the RXC 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 UDR in order to clear the RXC Flag, otherwise a new interrupt will occur once the interrupt routine terminates. 21.8.4. Receiver Error Flags The USART Receiver has three Error Flags: Frame Error (FE), Data OverRun (DOR) and Parity Error (UPE). 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 (FE) Flag indicates the state of the first stop bit of the next readable frame stored in the receive buffer. The FE Flag is zero when the stop bit was correctly read as '1', and the FE 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 FE Flag is not affected by the setting of the USBS 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 (DOR) Flag indicates data loss due to a receiver buffer full condition. A Data OverRun occurs when the receive buffer is full (two characters), a new character is waiting in the Receive Shift Register, and a new start bit is detected. If the DOR Flag is set, one or more serial frames were lost between the last frame read from UDR, and the next frame read from UDR. For compatibility with future Atmel ATmega164P/V [DATASHEET] Atmel-42742B-ATmega164P/V_Datasheet_Complete-08/2016 235 devices, always write this bit to zero when writing to UCSRnA. The DOR Flag is cleared when the frame received was successfully moved from the Shift Register to the receive buffer. The Parity Error (UPE) Flag indicates that the next frame in the receive buffer had a Parity Error when received. If Parity Check is not enabled the UPE bit will always read '0'. For compatibility with future devices, always set this bit to zero when writing to UCSRnA. For more details see Parity Bit Calculation and 'Parity Checker' below. 21.8.5. Parity Checker The Parity Checker is active when the high USART Parity Mode bit 1 in the USART Control and Status Register n C (UCSRnC.UPM[1]) is written to '1'. The type of Parity Check to be performed (odd or even) is selected by the UCSRnC.UPM[0] 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 USART Parity Error Flag in the USART Control and Status Register n A (UCSRnA.UPE) 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 (UPM[1] = 1). This bit is valid until the receive buffer (UDRn) is read. 21.8.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., UCSRnB.RXEN is written 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. 21.8.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 shows how to flush the receive buffer of USART0. Assembly Code Example USART_Flush: in r16, UCSR0A sbrs r16, RXC ret in r16, UDR0 rjmp USART_Flush C Code Example void USART_Flush( void ) { unsigned char dummy; while ( UCSR0A & (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 Serial Programming Pin Mapping Table 28-17.Pin Mapping Serial Programming Symbol Pins I/O Description MOSI PB5 I Serial Data in MISO PB6 O Serial Data out SCK PB7 I Serial Clock Note: The pin mapping for SPI programming is listed. Not all parts use the SPI pins dedicated for the internal SPI interface. 28.9.2. Serial Programming Algorithm When writing serial data to the device, data is clocked on the rising edge of SCK. When reading data from the device, data is clocked on the falling edge of SCK. Please refer to the figure, Serial Programming Waveforms in SPI Serial Programming Characteristics section for timing details. Atmel ATmega164P/V [DATASHEET] Atmel-42742B-ATmega164P/V_Datasheet_Complete-08/2016 379 To program and verify the device in the serial programming mode, the following sequence is recommended (See Serial Programming Instruction set in Table 28-19: 1. 2. 3. 4. 5. 6. 7. 8. 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". Wait for at least 20ms and enable serial programming by sending the Programming Enable serial instruction to pin MOSI. 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. 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 7 MSB of the address. If polling (RDY/BSY) is not used, the user must wait at least tWD_FLASH before issuing the next page . Accessing the serial programming interface before the Flash write operation completes can result in incorrect programming. A: 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 (RDY/BSY) is not used, the user must wait at least tWD_EEPROM before issuing the next byte. In a chip erased device, no 0xFFs in the data file(s) need to be programmed. B: The EEPROM array 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 EEPROM Memory Page instruction. The EEPROM Memory Page is stored by loading the Write EEPROM Memory Page Instruction with the 7 MSB of the address. When using EEPROM page access only byte locations loaded with the Load EEPROM Memory Page instruction is altered. The remaining locations remain unchanged. If polling (RDY/BSY) is not used, the used must wait at least tWD_EEPROM before issuing the next byte. In a chip erased device, no 0xFF in the data file(s) need to be programmed. Any memory location can be verified by using the Read instruction which returns the content at the selected address at serial output MISO. At the end of the programming session, RESET can be set high to commence normal operation. Power-off sequence (if needed): Set RESET to "1". Turn VCC power off. Table 28-18.Typical Wait Delay Before Writing the Next Flash or EEPROM Location Symbol Minimum Wait Delay tWD_FLASH 2.6ms tWD_EEPROM 3.6ms Atmel ATmega164P/V [DATASHEET] Atmel-42742B-ATmega164P/V_Datasheet_Complete-08/2016 380 28.9.3. Symbol Minimum Wait Delay tWD_ERASE 10.5ms tWD_FUSE 4.5ms Serial Programming Instruction Set This section describes the Instruction Set. Table 28-19.Serial Programming Instruction Set (Hexadecimal values) Instruction/Operation Instruction Format Byte 1 Byte 2 Byte 3 Byte 4 Programming Enable 0xAC 0x53 0x00 0x00 Chip Erase (Program Memory/EEPROM) 0xAC 0x80 0x00 0x00 Poll RDY/BSY 0xF0 0x00 0x00 data byte out Load Extended Address byte(1) 0x4D 0x00 Extended adr 0x00 Load Program Memory Page, High byte 0x48 0x00 adr LSB high data byte in Load Program Memory Page, Low byte 0x40 0x00 adr LSB low data byte in Load EEPROM Memory Page (page access) 0xC1 0x00 0000 000aa data byte in Read Program Memory, High byte 0x28 adr MSB adr LSB high data byte out Read Program Memory, Low byte 0x20 adr MSB adr LSB low data byte out Read EEPROM Memory 0xA0 0000 00aa aaaa aaaa data byte out Read Lock bits 0x58 0x00 0x00 data byte out Read Signature Byte 0x30 0x00 0000 000aa data byte out Read Fuse bits 0x50 0x00 0x00 data byte out Read Fuse High bits 0x58 0x08 0x00 data byte out Read Extended Fuse Bits 0x50 0x08 0x00 data byte out Read Calibration Byte 0x38 0x00 0x00 data byte out Write Program Memory Page 0x4C adr MSB(8) adr LSB(8) 0x00 Write EEPROM Memory 0xC0 0000 00aa aaaa aaaa data byte in Write EEPROM Memory Page (page access) 0xC2 0000 00aa aaaa aa00 0x00 Write Lock bits 0xAC 0xE0 0x00 data byte in Write Fuse bits 0xAC 0xA0 0x00 data byte in Load Instructions Read Instructions Write Instructions(6) Atmel ATmega164P/V [DATASHEET] Atmel-42742B-ATmega164P/V_Datasheet_Complete-08/2016 381 Instruction/Operation Instruction Format Byte 1 Byte 2 Byte 3 Byte 4 Write Fuse High bits 0xAC 0xA8 0x00 data byte in Write Extended Fuse Bits 0xAC 0xA4 0x00 data byte in Note: 1. Not all instructions are applicable for all parts. 2. a = address. 3. Bits are programmed `0', unprogrammed `1'. 4. To ensure future compatibility, unused Fuses and Lock bits should be unprogrammed (`1') . 5. Refer to the corresponding section for Fuse and Lock bits, Calibration and Signature bytes and Page size. 6. Instructions accessing program memory use a word address. This address may be random within the page range. 7. See http://www.atmel.com/avr for Application Notes regarding programming and programmers. 8. WORDS. If the LSB in RDY/BSY data byte out is `1', a programming operation is still pending. Wait until this bit returns `0' before the next instruction is carried out. Within the same page, the low data byte must be loaded prior to the high data byte. After data is loaded to the page buffer, program the EEPROM page, Please refer to the following figure. Figure 28-7.Serial Programming Instruction example Serial Programming Instruction Load Program Memory Page (High/Low Byte)/ Load EEPROM Memory Page (page access) Byte 1 Byte 2 Adr MSB Bit 15 B Byte 3 Write Program Memory Page/ Write EEPROM Memory Page Byte 1 Byte 4 Byte 2 Adr LSB Adr MSB Bit 15 B 0 Byte 3 Byte 4 Adr LSB 0 Page Buffer Page Offset Page 0 Page 1 Page 2 Page Number Page N-1 Program Memory/ EEPROM Memory Atmel ATmega164P/V [DATASHEET] Atmel-42742B-ATmega164P/V_Datasheet_Complete-08/2016 382 28.9.4. SPI Serial Programming Characteristics Figure 28-8.Serial Programming Waveforms SERIAL DATA INPUT (MOSI) MSB LSB SERIAL DATA OUTPUT (MISO) MSB LSB SERIAL CLOCK INPUT (SCK) SAMPLE 28.10. Programming Via the JTAG Interface Programming through the JTAG interface requires control of the four JTAG specific pins: TCK, TMS, TDI, and TDO. Control of the Reset and clock pins is not required. To be able to use the JTAG interface, the JTAGEN fuse must be programmed. The device is default shipped with the Fuse programmed. In addition, the JTD bit in MCUCSR must be cleared. Alternatively, if the JTD bit is set, the external reset can be forced low. Then, the JTD bit will be cleared after two chip clocks, and the JTAG pins are available for programming. This provides a means of using the JTAG pins as normal port pins in running mode while still allowing In-System Programming via the JTAG interface. Note that this technique can not be used when using the JTAG pins for Boundary-scan or On-chip Debug. In these cases the JTAG pins must be dedicated for this purpose. As a definition in this data sheet, the LSB is shifted in and out first of all Shift Registers. 28.10.1. Programming Specific JTAG Instructions The instruction register is 4-bit wide, supporting up to 16 instructions. The JTAG instructions useful for Programming are listed below. The OPCODE for each instruction is shown behind the instruction name in hex format. The text describes which data register is selected as path between TDI and TDO for each instruction. The Run-Test/Idle state of the TAP controller is used to generate internal clocks. It can also be used as an idle state between JTAG sequences. The state machine sequence for changing the instruction word is shown in the figure below. Atmel ATmega164P/V [DATASHEET] Atmel-42742B-ATmega164P/V_Datasheet_Complete-08/2016 383 Figure 28-9.State Machine Sequence for Changing the Instruction Word 1 Te s t-Logic-Re s e t 0 0 Run-Te s t/Idle 1 1 S e le ct-DR S ca n S e le ct-IR S ca n 0 0 1 1 Ca pture -DR Ca pture -IR 0 0 S hift-DR S hift-IR 0 1 Exit1-DR 1 Exit1-IR 0 0 Pa us e -DR 0 0 Pa us e -IR 1 1 0 Exit2-DR Exit2-IR 1 1 Upda te -DR 1 0 1 1 0 1 Upda te -IR 1 0 0 28.10.2. AVR_RESET (0xC) The AVR specific public JTAG instruction for setting the AVR device in the Reset mode or taking the device out from the Reset mode. The TAP controller is not reset by this instruction. The one bit Reset Register is selected as Data Register. Note that the reset will be active as long as there is a logic 'one' in the Reset Chain. The output from this chain is not latched. The active states are: * Shift-DR: The Reset Register is shifted by the TCK input. Atmel ATmega164P/V [DATASHEET] Atmel-42742B-ATmega164P/V_Datasheet_Complete-08/2016 384 28.10.3. PROG_ENABLE (0x4) The AVR specific public JTAG instruction for enabling programming via the JTAG port. The 16-bit Programming Enable Register is selected as data register. The active states are the following: * * Shift-DR: the programming enable signature is shifted into the data register. Update-DR: the programming enable signature is compared to the correct value, and Programming mode is entered if the signature is valid. 28.10.4. PROG_COMMANDS (0x5) The AVR specific public JTAG instruction for entering programming commands via the JTAG port. The 15bit Programming Command Register is selected as data register. The active states are the following: * * * * Capture-DR: the result of the previous command is loaded into the data register. Shift-DR: the data register is shifted by the TCK input, shifting out the result of the previous command and shifting in the new command. Update-DR: the programming command is applied to the Flash inputs. Run-Test/Idle: one clock cycle is generated, executing the applied command. 28.10.5. PROG_PAGELOAD (0x6) The AVR specific public JTAG instruction to directly load the Flash data page via the JTAG port. The 2048-bit Virtual Flash Page Load Register is selected as data register. This is a virtual scan chain with length equal to the number of bits in one Flash page. Internally the Shift Register is 8-bit. Unlike most JTAG instructions, the Update-DR state is not used to transfer data from the Shift Register. The data are automatically transferred to the Flash page buffer byte by byte in the Shift-DR state by an internal state machine. This is the only active state: * Shift-DR: Flash page data are shifted in from TDI by the TCK input, and automatically loaded into the Flash page one byte at a time. Note: 1. The JTAG instruction PROG_PAGELOAD can only be used if the AVR device is the first device in JTAG scan chain. If the AVR cannot be the first device in the scan chain, the byte-wise programming algorithm must be used. 28.10.6. PROG_PAGEREAD (0x7) The AVR specific public JTAG instruction to read one full Flash data page via the JTAG port. The 2056-bit Virtual Flash Page Read Register is selected as data register. This is a virtual scan chain with length equal to the number of bits in one Flash page plus 8. Internally the Shift Register is 8-bit. Unlike most JTAG instructions, the Capture-DR state is not used to transfer data to the Shift Register. The data are automatically transferred from the Flash page buffer byte by byte in the Shift-DR state by an internal state machine. This is the only active state: * Shift-DR: Flash data are automatically read one byte at a time and shifted out on TDO by the TCK input. The TDI input is ignored. Note: 1. The JTAG instruction PROG_PAGEREAD can only be used if the AVR device is the first device in JTAG scan chain. If the AVR cannot be the first device in the scan chain, the byte-wise programming algorithm must be used. 28.10.7. Data Registers The data registers are selected by the JTAG instruction registers described in section Programming Specific JTAG Instructions. The data registers relevant for programming operations are: * Reset Register Atmel ATmega164P/V [DATASHEET] Atmel-42742B-ATmega164P/V_Datasheet_Complete-08/2016 385 * * * * Programming Enable Register Programming Command Register Virtual Flash Page Load Register Virtual Flash Page Read Register 28.10.8. Reset Register The Reset Register is a Test Data Register used to reset the part during programming. It is required to reset the part before entering programming mode. A high value in the Reset Register corresponds to pulling the external Reset low. The part is reset as long as there is a high value present in the Reset Register. Depending on the Fuse settings for the clock options, the part will remain reset for a Reset Time-Out Period (refer to Clock Sources) after releasing the Reset Register. The output from this Data Register is not latched, so the reset will take place immediately, as shown in figure Reset Register. 28.10.9. Programming Enable Register The Programming Enable Register is a 16-bit register. The contents of this register is compared to the programming enable signature, binary code 1010_0011_0111_0000. When the contents of the register is equal to the programming enable signature, programming via the JTAG port is enabled. The Register is reset to 0 on Power-on Reset, and should always be reset when leaving Programming mode. Figure 28-10.Programming Enable Register TDI D A T A $A370 = D Q P rogra mming e na ble ClockDR & P ROG_ENABLE TDO 28.10.10. Programming Command Register The Programming Command Register is a 15-bit register. This register is used to serially shift in programming commands, and to serially shift out the result of the previous command, if any. The JTAG Programming Instruction Set is shown in the following table. The state sequence when shifting in the programming commands is illustrated in State Machine Sequence for Changing/Reading the Data Word further down in this section. Atmel ATmega164P/V [DATASHEET] Atmel-42742B-ATmega164P/V_Datasheet_Complete-08/2016 386 Figure 28-11.Programming Command Register TDI S T R O B E S Fla s h EEP ROM Fus e s Lock Bits A D D R E S S / D A T A TDO Table 28-20.JTAG Programming Instruction Set 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 Instruction TDI sequence TDO sequence 1a. Chip erase 0100011_10000000 0110001_10000000 xxxxxxx_xxxxxxxx xxxxxxx_xxxxxxxx 0110011_10000000 xxxxxxx_xxxxxxxx 0110011_10000000 xxxxxxx_xxxxxxxx 1b. Poll for chip erase complete 0110011_10000000 xxxxxox_xxxxxxxx 2a. Enter Flash Write 0100011_00010000 xxxxxxx_xxxxxxxx 2b. Load Address High Byte 0000111_aaaaaaaa xxxxxxx_xxxxxxxx 2c. Load Address Low Byte 0000011_bbbbbbbb xxxxxxx_xxxxxxxx 2d. Load Data Low Byte 0010011_iiiiiiii xxxxxxx_xxxxxxxx 2e. Load Data High Byte 0010111_iiiiiiii xxxxxxx_xxxxxxxx 2f. Latch Data 0110111_00000000 1110111_00000000 xxxxxxx_xxxxxxxx xxxxxxx_xxxxxxxx 0110111_00000000 xxxxxxx_xxxxxxxx Notes (2) (9) (1) Atmel ATmega164P/V [DATASHEET] Atmel-42742B-ATmega164P/V_Datasheet_Complete-08/2016 387 Instruction TDI sequence TDO sequence Notes 2g. Write Flash Page 0110111_00000000 0110101_00000000 xxxxxxx_xxxxxxxx xxxxxxx_xxxxxxxx (1) 0110111_00000000 xxxxxxx_xxxxxxxx 0110111_00000000 xxxxxxx_xxxxxxxx 2h. Poll for Page Write complete 0110111_00000000 xxxxxox_xxxxxxxx 3a. Enter Flash Read 0100011_00000010 xxxxxxx_xxxxxxxx 3b. Load Address High Byte 0000111_aaaaaaaa xxxxxxx_xxxxxxxx 3c. Load Address Low Byte 0000011_bbbbbbbb xxxxxxx_xxxxxxxx 3d. Read Data Low and High Byte 0110010_00000000 0110110_00000000 xxxxxxx_xxxxxxxx xxxxxxx_oooooooo 0110111_00000000 xxxxxxx_oooooooo 4a. Enter EEPROM Write 0100011_00010001 xxxxxxx_xxxxxxxx 4b. Load Address High Byte 0000111_aaaaaaaa xxxxxxx_xxxxxxxx 4c. Load Address Low Byte 0000011_bbbbbbbb xxxxxxx_xxxxxxxx 4d. Load Data Byte 0010011_iiiiiiii xxxxxxx_xxxxxxxx 4e. Latch Data 0110111_00000000 1110111_00000000 xxxxxxx_xxxxxxxx xxxxxxx_xxxxxxxx 0110111_00000000 xxxxxxx_xxxxxxxx 0110011_00000000 0110001_00000000 xxxxxxx_xxxxxxxx xxxxxxx_xxxxxxxx 0110011_00000000 xxxxxxx_xxxxxxxx 0110011_00000000 xxxxxxx_xxxxxxxx 4g. Poll for Page Write complete 0110011_00000000 xxxxxox_xxxxxxxx 5a. Enter EEPROM Read 0100011_00000011 xxxxxxx_xxxxxxxx 5b. Load Address High Byte 0000111_aaaaaaaa xxxxxxx_xxxxxxxx 5c. Load Address Low Byte 0000011_bbbbbbbb xxxxxxx_xxxxxxxx 5d. Read Data Byte 0110011_bbbbbbbb 0110010_00000000 xxxxxxx_xxxxxxxx xxxxxxx_xxxxxxxx 0110011_00000000 xxxxxxx_oooooooo 6a. Enter Fuse Write 0100011_01000000 xxxxxxx_xxxxxxxx 6b. Load Data Low Byte(6) 0010011_iiiiiiii xxxxxxx_xxxxxxxx 4f. Write EEPROM Page (2) (9) low byte high byte (9) (1) (1) (2) (9) (3) Atmel ATmega164P/V [DATASHEET] Atmel-42742B-ATmega164P/V_Datasheet_Complete-08/2016 388 Instruction TDI sequence TDO sequence Notes 6c. Write Fuse Extended byte 0111011_00000000 0111001_00000000 xxxxxxx_xxxxxxxx xxxxxxx_xxxxxxxx (1) 0111011_00000000 xxxxxxx_xxxxxxxx 0111011_00000000 xxxxxxx_xxxxxxxx 6d. Poll for Fuse Write complete 0110111_00000000 xxxxxox_xxxxxxxx (2) 6e. Load Data Low Byte(7) 0010011_iiiiiiii xxxxxxx_xxxxxxxx (3) 6f. Write Fuse High byte 0110111_00000000 0110101_00000000 xxxxxxx_xxxxxxxx xxxxxxx_xxxxxxxx (1) 0110111_00000000 xxxxxxx_xxxxxxxx 0110111_00000000 xxxxxxx_xxxxxxxx 6g. Poll for Fuse Write complete 0110111_00000000 xxxxxox_xxxxxxxx (2) 6h. Load Data Low Byte(7) 0010011_iiiiiiii xxxxxxx_xxxxxxxx (3) 6i. Write Fuse Low byte 0110011_00000000 0110001_00000000 xxxxxxx_xxxxxxxx xxxxxxx_xxxxxxxx (1) 0110011_00000000 xxxxxxx_xxxxxxxx 0110011_00000000 xxxxxxx_xxxxxxxx 6j. Poll for Fuse Write complete 0110011_00000000 xxxxxox_xxxxxxxx 7a. Enter Lock bit Write 0100011_00100000 xxxxxxx_xxxxxxxx 7b. Load Data Byte(9) 0010011_11iiiiii xxxxxxx_xxxxxxxx (4) 7c. Write Lock bits 0110011_00000000 0110001_00000000 xxxxxxx_xxxxxxxx xxxxxxx_xxxxxxxx (1) 0110011_00000000 xxxxxxx_xxxxxxxx 0110011_00000000 xxxxxxx_xxxxxxxx 7d. Poll for Lock bit Write complete 0110011_00000000 xxxxxox_xxxxxxxx 8a. Enter Fuse/Lock bit Read 0100011_00000100 xxxxxxx_xxxxxxxx 8b. Read Extended Fuse Byte(6) 0111010_00000000 0111011_00000000 xxxxxxx_xxxxxxxx xxxxxxx_oooooooo 8c. Read Fuse High Byte(7) 0111110_00000000 0111111_00000000 xxxxxxx_xxxxxxxx xxxxxxx_oooooooo 8d. Read Fuse Low Byte(8) 0110010_00000000 0110011_00000000 xxxxxxx_xxxxxxxx xxxxxxx_oooooooo 8e. Read Lock bits(9) 0110110_00000000 0110111_00000000 xxxxxxx_xxxxxxxx xxxxxxx_xxoooooo (2) (2) (5) Atmel ATmega164P/V [DATASHEET] Atmel-42742B-ATmega164P/V_Datasheet_Complete-08/2016 389 Instruction TDI sequence TDO sequence Notes 8f. Read Fuses and Lock bits 0111010_00000000 0111110_00000000 xxxxxxx_xxxxxxxx xxxxxxx_oooooooo (5) fuse ext. byte 0110010_00000000 xxxxxxx_oooooooo fuse high byte 0110110_00000000 xxxxxxx_oooooooo fuse low byte 0110111_00000000 xxxxxxx_oooooooo lock bits 9a. Enter Signature Byte Read 0100011_00001000 xxxxxxx_xxxxxxxx 9b. Load Address Byte 0000011_bbbbbbbb xxxxxxx_xxxxxxxx 9c. Read Signature Byte 0110010_00000000 0110011_00000000 xxxxxxx_xxxxxxxx xxxxxxx_oooooooo 10a. Enter Calibration Byte Read 0100011_00001000 xxxxxxx_xxxxxxxx 10b. Load Address Byte 0000011_bbbbbbbb xxxxxxx_xxxxxxxx 10c. Read Calibration Byte 0110110_00000000 0110111_00000000 xxxxxxx_xxxxxxxx xxxxxxx_oooooooo 11a. Load No Operation Command 0100011_00000000 0110011_00000000 xxxxxxx_xxxxxxxx xxxxxxx_xxxxxxxx Note: 1. This command sequence is not required if the seven MSB are correctly set by the previous command sequence (which is normally the case). 2. Repeat until o = "1". 3. Set bits to "0" to program the corresponding fuse, "1" to unprogram the Fuse. 4. Set bits to "0" to program the corresponding lock bit, "1" to leave the Lock bit unchanged. 5. "0" = programmed, "1" = unprogrammed. 6. The bit mapping for Fuses Extended byte is listed in Extended Fuse Byte table of Fuse Bits section. 7. The bit mapping for Fuses High byte is listed in Fuse High Byte table of Fuse Bits section. 8. The bit mapping for Fuses Low byte is listed in Fuse Low Byte table of Fuse Bits section. 9. The bit mapping for Lock bits byte is listed in Lock Bit Byte table of Program and Data Memory Lock Bits section. 10. Address bits exceeding PCMSB and EEAMSB (Command Byte Bit Coding in Signal Names section and Page Size section) are don't care Atmel ATmega164P/V [DATASHEET] Atmel-42742B-ATmega164P/V_Datasheet_Complete-08/2016 390 Figure 28-12.State Machine Sequence for Changing/Reading the Data Word 1 Te s t-Logic-Re s e t 0 0 Run-Te s t/Idle 1 S e le ct-DR S ca n 1 S e le ct-IR S ca n 0 0 1 1 Ca pture -DR Ca pture -IR 0 0 S hift-DR S hift-IR 0 1 Exit1-DR 1 Exit1-IR 0 0 Pa us e -DR 0 0 Pa us e -IR 1 1 0 Exit2-DR Exit2-IR 1 1 Upda te -DR 1 0 1 1 0 1 Upda te -IR 0 1 0 28.10.11. Virtual Flash Page Load Register The Virtual Flash Page Load Register is a virtual scan chain with length equal to the number of bits in one Flash page. Internally the Shift Register is 8-bit, and the data are automatically transferred to the Flash page buffer byte by byte. Shift in all instruction words in the page, starting with the LSB of the first instruction in the page and ending with the MSB of the last instruction in the page. This provides an efficient way to load the entire Flash page buffer before executing Page Write. Atmel ATmega164P/V [DATASHEET] Atmel-42742B-ATmega164P/V_Datasheet_Complete-08/2016 391 Figure 28-13.Virtual Flash Page Load Register S TROBES TDI S ta te ma chine ADDRES S Fla s h EEP ROM Fus e s Lock Bits D A T A TDO 28.10.12. Virtual Flash Page Read Register The Virtual Flash Page Read Register is a virtual scan chain with length equal to the number of bits in one Flash page plus 8. Internally the Shift Register is 8-bit, and the data are automatically transferred from the Flash data page byte by byte. The first eight cycles are used to transfer the first byte to the internal Shift Register, and the bits that are shifted out during these 8 cycles should be ignored. Following this initialization, data are shifted out starting with the LSB of the first instruction in the page and ending with the MSB of the last instruction in the page. This provides an efficient way to read one full Flash page to verify programming. Atmel ATmega164P/V [DATASHEET] Atmel-42742B-ATmega164P/V_Datasheet_Complete-08/2016 392 Figure 28-14.Virtual Flash Page Read Register S TROBES TDI S ta te ma chine ADDRES S Fla s h EEP ROM Fus e s Lock Bits D A T A TDO 28.10.13. Programming Algorithm All references below of type "1a", "1b", and so on, refer to Table 28-20. 28.10.14. Entering Programming Mode 1. 2. Enter JTAG instruction AVR_RESET and shift 1 in the Reset Register. Enter instruction PROG_ENABLE and shift 1010_0011_0111_0000 in the Programming Enable Register. 28.10.15. Leaving Programming Mode 1. 2. 3. 4. Enter JTAG instruction PROG_COMMANDS. Disable all programming instructions by using no operation instruction 11a. Enter instruction PROG_ENABLE and shift 0000_0000_0000_0000 in the programming Enable Register. Enter JTAG instruction AVR_RESET and shift 0 in the Reset Register. 28.10.16. Performing Chip Erase 1. 2. 3. Enter JTAG instruction PROG_COMMANDS. Start chip erase using programming instruction 1a. Poll for chip erase complete using programming instruction 1b, or wait for tWLRH_CE (refer to table Command Byte Bit Coding in section Parallel Programming Parameters, Pin Mapping, and Commands). 28.10.17. Programming the Flash Before programming the Flash a Chip Erase must be performed. See Performing Chip Erase. 1. 2. 3. Enter JTAG instruction PROG_COMMANDS. Enable Flash write using programming instruction 2a. Load address high byte using programming instruction 2b. Atmel ATmega164P/V [DATASHEET] Atmel-42742B-ATmega164P/V_Datasheet_Complete-08/2016 393 4. 5. 6. 7. 8. 9. Load address low byte using programming instruction 2c. Load data using programming instructions 2d, 2e and 2f. Repeat steps 4 and 5 for all instruction words in the page. Write the page using programming instruction 2g. Poll for Flash write complete using programming instruction 2h, or wait for tWLRH (refer to table Parallel Programming Characteristics, VCC = 5V 10% in chapter Parallel Programming Characteristics). Repeat steps 3 to 7 until all data have been programmed. A more efficient data transfer can be achieved using the PROG_PAGELOAD instruction: 1. 2. 3. 4. 5. 6. 7. 8. 9. Enter JTAG instruction PROG_COMMANDS. Enable Flash write using programming instruction 2a. Load the page address using programming instructions 2b and 2c. PCWORD (refer to Command Byte Bit Coding table in Signal Names section) is used to address within one page and must be written as 0. Enter JTAG instruction PROG_PAGELOAD. Load the entire page by shifting in all instruction words in the page, starting with the LSB of the first instruction in the page and ending with the MSB of the last instruction in the page. Enter JTAG instruction PROG_COMMANDS. Write the page using programming instruction 2g. Poll for Flash write complete using programming instruction 2h, or wait for tWLRH (refer to table Parallel Programming Characteristics, VCC = 5V 10% in chapter Parallel Programming Characteristics). Repeat steps 3 to 8 until all data have been programmed. 28.10.18. Reading the Flash 1. 2. 3. 4. 5. Enter JTAG instruction PROG_COMMANDS. Enable Flash read using programming instruction 3a. Load address using programming instructions 3b and 3c. Read data using programming instruction 3d. Repeat steps 3 and 4 until all data have been read. A more efficient data transfer can be achieved using the PROG_PAGEREAD instruction: 1. 2. 3. 4. 5. 6. 7. Enter JTAG instruction PROG_COMMANDS. Enable Flash read using programming instruction 3a. Load the page address using programming instructions 3b and 3c. PCWORD (refer to table Command Byte Bit Coding in section Parallel Programming Parameters, Pin Mapping, and Commands) is used to address within one page and must be written as 0. Enter JTAG instruction PROG_PAGEREAD. Read the entire page by shifting out all instruction words in the page, starting with the LSB of the first instruction in the page and ending with the MSB of the last instruction in the page. Remember that the first 8 bits shifted out should be ignored. Enter JTAG instruction PROG_COMMANDS. Repeat steps 3 to 6 until all data have been read. 28.10.19. Programming the EEPROM Before programming the EEPROM a Chip Erase must be performed. See Performing Chip Erase. Atmel ATmega164P/V [DATASHEET] Atmel-42742B-ATmega164P/V_Datasheet_Complete-08/2016 394 1. 2. 3. 4. 5. 6. 7. 8. 9. Enter JTAG instruction PROG_COMMANDS. Enable EEPROM write using programming instruction 4a. Load address high byte using programming instruction 4b. Load address low byte using programming instruction 4c. Load data using programming instructions 4d and 4e. Repeat steps 4 and 5 for all data bytes in the page. Write the data using programming instruction 4f. Poll for EEPROM write complete using programming instruction 4g, or wait for tWLRH (refer to table Parallel Programming Characteristics, VCC = 5V 10% in chapter Parallel Programming Characteristics). Repeat steps 3 to 8 until all data have been programmed. Note that the PROG_PAGELOAD instruction can not be used when programming the EEPROM 28.10.20. Reading the EEPROM 1. 2. 3. 4. 5. Enter JTAG instruction PROG_COMMANDS. Enable EEPROM read using programming instruction 5a. Load address using programming instructions 5b and 5c. Read data using programming instruction 5d. Repeat steps 3 and 4 until all data have been read. Note that the PROG_PAGEREAD instruction can not be used when reading the EEPROM 28.10.21. Programming the Fuses 1. 2. 3. Enter JTAG instruction PROG_COMMANDS. Enable Fuse write using programming instruction 6a. Load data byte using programming instructions 6b. A bit value of "0" will program the corresponding fuse, a "1" will unprogram the fuse. 4. Write Extended Fuse byte using programming instruction 6c. 5. Poll for Fuse write complete using programming instruction 6d, or wait for tWLRH (refer to table Parallel Programming Characteristics, VCC = 5V 10% in chapter Parallel Programming Characteristics). 6. Load data byte using programming instructions 6e. A bit value of "0" will program the corresponding fuse, a "1" will unprogram the fuse. 7. Write Fuse high byte using programming instruction 6f. 8. Poll for Fuse write complete using programming instruction 6g, or wait for tWLRH (refer to table Parallel Programming Characteristics, VCC = 5V 10% in chapter Parallel Programming Characteristics). 9. Load data byte using programming instructions 6h. A "0" will program the fuse, a "1" will unprogram the fuse. 10. Write Fuse low byte using programming instruction 6i. 11. Poll for Fuse write complete using programming instruction 6j, or wait for tWLRH (refer to table Parallel Programming Characteristics, VCC = 5V 10% in chapter Parallel Programming Characteristics). 28.10.22. Programming the Lock Bits 1. 2. Enter JTAG instruction PROG_COMMANDS. Enable Lock bit write using programming instruction 7a. Atmel ATmega164P/V [DATASHEET] Atmel-42742B-ATmega164P/V_Datasheet_Complete-08/2016 395 3. 4. 5. Load data using programming instructions 7b. A bit value of "0" will program the corresponding lock bit, a "1" will leave the lock bit unchanged. Write Lock bits using programming instruction 7c. Poll for Lock bit write complete using programming instruction 7d, or wait for tWLRH (refer to table Parallel Programming Characteristics, VCC = 5V 10% in chapter Parallel Programming Characteristics). 28.10.23. Reading the Fuses and Lock Bits 1. 2. 3. Enter JTAG instruction PROG_COMMANDS. Enable Fuse/Lock bit read using programming instruction 8a. - To read all Fuses and Lock bits, use programming instruction 8f. - To only read Extended Fuse byte, use programming instruction 8b. - To only read Fuse high byte, use programming instruction 8c. - To only read Fuse low byte, use programming instruction 8d. - To only read Lock bits, use programming instruction 8e. 28.10.24. Reading the Signature Bytes 1. 2. 3. 4. 5. Enter JTAG instruction PROG_COMMANDS. Enable Signature byte read using programming instruction 9a. Load address 0x00 using programming instruction 9b. Read first signature byte using programming instruction 9c. Repeat steps 3 and 4 with address 0x01 and address 0x02 to read the second and third signature bytes, respectively. 28.10.25. Reading the Calibration Byte 1. 2. 3. 4. Enter JTAG instruction PROG_COMMANDS. Enable Calibration byte read using programming instruction 10a. Load address 0x00 using programming instruction 10b. Read the calibration byte using programming instruction 10c. Atmel ATmega164P/V [DATASHEET] Atmel-42742B-ATmega164P/V_Datasheet_Complete-08/2016 396 29. Electrical Characteristics 29.1. Absolute Maximum Ratings Table 29-1.Absolute Maximum Ratings Operating Temperature -55C to +125C Storage Temperature -65C to +150C Voltage on any Pin except RESET with respect to Ground -0.5V to VCC+0.5V Voltage on RESET with respect to Ground -0.5V to +13.0V Maximum Operating Voltage 6.0V DC Current per I/O Pin 40.0mA DC Current VCC and GND Pins 200.0mA Note: 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 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. 29.2. DC Characteristics Table 29-2. Common DC characteristics TA = -40C to 105C, VCC = 1.8V to 5.5V (unless otherwise noted) Symbol Parameter Condition Min. Typ. Max. Units V Input Low Voltage, except XTAL1 VCC = 1.8V - 2.4V and RESET pin VCC = 2.4V - 5.5V -0.5 0.2VCC(1) -0.5 0.3VCC(1) VIL1 Input Low Voltage, XTAL1 pin VCC = 1.8V - 5.5V -0.5 0.1VCC(1) V VIL2 Input Low Voltage, RESET pin VCC = 1.8V - 5.5V -0.5 0.1VCC(1) V VIH Input High Voltage, except XTAL1 and RESET pins VCC = 1.8V - 2.4V 0.7VCC(2) VCC + 0.5 V VCC = 2.4V - 5.5V 0.6VCC(2) VCC + 0.5 Input High Voltage, XTAL1 pin VCC = 1.8V - 2.4V 0.8VCC(2) VCC + 0.5 VCC = 2.4V - 5.5V 0.7VCC(2) VCC + 0.5 Input High Voltage, RESET pin VCC = 1.8V - 5.5V 0.9VCC(2) VCC + 0.5 VIL VIH1 VIH2 Atmel ATmega164P/V [DATASHEET] Atmel-42742B-ATmega164P/V_Datasheet_Complete-08/2016 V V 397 Symbol Parameter VOL Output Low Voltage(4) except RESET pin Condition IOL = 20mA, VCC = 5V IOL = 10mA, VCC = 3V VOH Output High Voltage(3) except Reset pin Min. Max. Units TA=85C 0.9 V TA=105C 1.0 TA=85C 0.6 TA=105C 0.7 IOH = -20mA, TA=85C TA=105C VCC = 5V 4.2 IOH = -10mA, TA=85C TA=105C VCC = 3V 2.3 Typ. 4.0 2.1 V IIL Input Leakage Current I/O Pin VCC = 5.5V, pin low (absolute value) 1 A IIH Input Leakage Current I/O Pin VCC = 5.5V, pin high (absolute value) 1 A RRST Reset Pull-up Resistor 30 60 k RPU I/O Pin Pull-up Resistor 20 50 k VACIO Analog Comparator Input Offset Voltage 40 mV 50 nA <10 VCC = 5V, Vin = VCC/2 IACLK Analog Comparator Input Leakage Current VCC=5V , -50 Vin = VCC/2 tACID Analog Comparator Propagation Delay VCC = 2.7V 750 VCC = 4.0V 500 ns Note: 1. "Max." means the highest value where the pin is guaranteed to be read as low. 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, 10mA at VCC = 3V) under steady state conditions (non-transient), the following must be observed: 3.1. The sum of all IOL, for ports PB0-PB7, XTAL2, PD0-PD7 should not exceed 100mA. 3.2. The sum of all IOH, for ports PA0-PA3, PC0-PC7 should not exceed 100mA. 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, 10mA at VCC = 3V) under steady state conditions (non-transient), the following must be observed: 4.1. The sum of all IOL, for ports PB0-PB7, XTAL2, PD0-PD7 should not exceed 100mA. Atmel ATmega164P/V [DATASHEET] Atmel-42742B-ATmega164P/V_Datasheet_Complete-08/2016 398 4.2. The sum of all IOH, for ports PA0-PA3, PC0-PC7 should not exceed 100mA. 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. Related Links Minimizing Power Consumption on page 60 29.2.1. Power Consumption Table 29-3.ATmega164P DC Characteristics - TA = -40C to 85C, VCC = 1.8V to 5.5V (unless otherwise noted) Symbol Parameter ICC Power Supply Current(1) Power-save mode(3) Power-down mode(3) Condition Min. Typ.(2) Max. Units Active 1MHz, VCC = 2V - 0.4 0.5 Active 4MHz, VCC = 3V - 2.1 2.7 Active 8MHz, VCC = 5V - 7.4 9.0 Idle 1MHz, VCC = 2V - 0.1 0.15 Idle 4MHz, VCC = 3V - 0.5 0.7 Idle 8MHz, VCC = 5V - 1.9 5.0 32 kHz TOSC enabled, VCC = 1.8V - 0.5 - 32 kHz TOSC enabled, VCC = 3V - 0.6 - WDT enabled, VCC = 3V - 4.3 8.0 WDT disabled, VCC = 3V - 0.2 2.0 mA A Note: 1. All bits set in the "PRR - Power Reduction Register ". 2. Typical values at 25C. Maximum values are test limits in production. 3. The current consumption values include input leakage current. Table 29-4.ATmega164P DC Characteristics - TA = -40C to 105C, VCC = 1.8V to 5.5V (unless otherwise noted) Symbol Parameter Condition Min. Typ. Max. Units ICC Power Supply Current(1) Active 1MHz, VCC = 2V - - TBD mA Active 4MHz, VCC = 3V - - TBD Active 8MHz, VCC = 5V - - TBD Idle 1MHz, VCC = 2V - - TBD Idle 4MHz, VCC = 3V - - TBD Idle 8MHz, VCC = 5V - - TBD WDT enabled, VCC = 3V - - TBD WDT disabled, VCC = 3V - - TBD Power-down mode(2) A Note: 1. All bits set in the "PRR - Power Reduction Register " 2. The current consumption values include input leakage current. Atmel ATmega164P/V [DATASHEET] Atmel-42742B-ATmega164P/V_Datasheet_Complete-08/2016 399 29.3. Speed Grades Maximum frequency is depending on VCC. The Maximum Frequency vs. VCC curve is linear between 1.8V < VCC < 2.7V and between 2.7V < VCC < 4.5V. Figure 29-1.Maximum Frequency vs. VCC, ATmega164PV 10 MHz S a fe Ope ra ting Are a 4 MHz 2.7V 1.8V 5.5V Figure 29-2.Maximum Frequency vs. VCC, ATmega164P 20 MHz 10 MHz S a fe Ope ra ting Are a 2.7V 29.4. 4.5V 5.5V Clock Characteristics Related Links Calibrated Internal RC Oscillator on page 49 29.4.1. Calibration Accuracy of Internal RC Oscillator Table 29-5.Calibration Accuracy of Internal RC Oscillator. Frequency VCC Temperature Calibration accuracy Factory calibration 8.0MHz 3V 25C 10% User calibration 7.3 - 8.1MHz 1.8 - 5.5V(1) -40C - 85C 1% 2.7 - 5.5(2) 1. 2. Voltage range for ATmega164PV. Voltage range for ATmega164P. Atmel ATmega164P/V [DATASHEET] Atmel-42742B-ATmega164P/V_Datasheet_Complete-08/2016 400 29.4.2. External Clock Drive Waveforms Figure 29-3.External Clock Drive Waveforms VIH1 VIL1 29.4.3. External Clock Drive Table 29-6.External Clock Drive Symbol Parameter 29.5. VCC= 1.8 - 5.5V VCC= 2.7 - 5.5V VCC= 4.5 - 5.5V Units Min. Max. Min. Max. Min. Max. 1/tCLCL Oscillator Frequency 0 4 0 10 0 20 MHz tCLCL Clock Period 250 - 100 - 50 - ns tCHCX High Time 100 - 40 - 20 - ns tCLCX Low Time 100 - 40 - 20 - ns tCLCH Rise Time - 2.0 - 1.6 - 0.5 s tCHCL Fall Time - 2.0 - 1.6 - 0.5 s tCLCL Change in period from one clock cycle to the next - 2 - 2 - 2 % System and Reset Characteristics Table 29-7.Reset, Brown-out and Internal Voltage Characteristics Symbol Parameter Min. Typ Max Units Power-on Reset Threshold Voltage (rising) 0.7 1.0 1.4 V Power-on Reset Threshold Voltage (falling)(2) 0.6 0.9 1.9 V VRST RESET Pin Threshold Voltage 0.2 VCC - 0.9 VCC V tRST Minimum pulse width on RESET Pin - - 2.5 s VHYST Brown-out Detector Hysteresis - 50 - mV tBOD Min. Pulse Width on Brown-out Reset - 2 - s VBG Bandgap reference voltage VCC=2.7 , TA=25C 1.0 1.1 1.2 V tBG Bandgap reference start-up time VCC=2.7 , TA=25C - 40 70 s IBG Bandgap reference current consumption VCC=2.7 , TA=25C - 10 - A VPOT(1) Condition Atmel ATmega164P/V [DATASHEET] Atmel-42742B-ATmega164P/V_Datasheet_Complete-08/2016 401 1. 2. Values are guidelines only. The Power-on Reset will not work unless the supply voltage has been below VPOT (falling) Table 29-8.BODLEVEL Fuse Coding BODLEVEL [2:0] Fuses Min. VBOT Typ VBOT Max VBOT Units 111 BOD Disabled 110 1.7 1.8 2.0 V 101 2.5 2.7 2.9 100 4.1 4.3 4.5 011 Reserved 010 001 000 1. 29.6. VBOT may be below nominal minimum operating voltage for some devices. For devices where this is the case, the device is tested down to VCC = VBOT during the production test. This guarantees that a Brown-Out Reset will occur before VCC drops to a voltage where correct operation of the microcontroller is no longer guaranteed. The test is performed using BODLEVEL = 110 and 101 . External interrupts characteristics Table 29-9.Asynchronous external interrupt characteristics. Symbol Parameter Min. Typ. Max. Units tINT Minimum pulse width for asynchronous external interrupt - 50 - ns Atmel ATmega164P/V [DATASHEET] Atmel-42742B-ATmega164P/V_Datasheet_Complete-08/2016 402 29.7. SPI Timing Characteristics Table 29-10.SPI Timing Parameters Description Mode SCK period Master - See Table. Relationship Between SCK and the Oscillator Frequency in "SPCR - SPI Control Register" SCK high/low Master - 50% duty cycle - Rise/Fall time Master - 3.6 - Setup Master - 10 - Hold Master - 10 - Out to SCK Master - 0.5 * tsck - SCK to out Master - 10 - SCK to out high Master - 10 - SS low to out Slave - 15 - SCK period Slave 4 * tck - - SCK high/low(1) Slave 2 * tck - - Rise/Fall time Slave - - 1600 Setup Slave 10 - - Hold Slave tck - - SCK to out Slave - 15 - SCK to SS high Slave 20 - - SS high to tri-state Slave - 10 - SS low to SCK 2 * tck - Slave Min. Typ Max Units ns - Note: 1. In SPI Programming mode the minimum SCK high/low period is: * * 2 tCLCL for fCK < 12MHz 3 tCLCL for fCK > 12MHz Atmel ATmega164P/V [DATASHEET] Atmel-42742B-ATmega164P/V_Datasheet_Complete-08/2016 403 Figure 29-4.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 7 MOSI (Data Output) 8 MSB ... LSB Figure 29-5.SPI Interface Timing Requirements (Slave Mode) SS 10 9 16 SCK (CPOL = 0) 11 11 SCK (CPOL = 1) 13 MOSI (Data Input) 14 12 MSB ... LSB 15 MISO (Data Output) 29.8. 17 MSB ... LSB X Two-wire Serial Interface Characteristics The table in this section describes the requirements for devices connected to the 2-wire Serial Bus. The 2-wire Serial Interface meets or exceeds these requirements under the noted conditions. The timing symbols refers to Figure 29-6. Table 29-11.Two-wire Serial Bus Requirements Symbol Parameter Condition Min. Max Units V VIL Input Low-voltage -0.5 0.3 VCC VIH Input High-voltage 0.7 VCC VCC + 0.5 V Vhys(1) Hysteresis of Schmitt Trigger Inputs 0.05 VCC(2) - Atmel ATmega164P/V [DATASHEET] Atmel-42742B-ATmega164P/V_Datasheet_Complete-08/2016 V 404 Symbol Parameter Condition Min. Max Units VOL(1) Output Low-voltage 3mA sink current 0 0.4 V tr(1) Rise Time for both SDA and SCL tof(1) Output Fall Time from VIHmin to VILmax tSP(1) Spikes Suppressed by Input Filter Ii Input Current each I/O Pin Ci(1) Capacitance for each I/O Pin fSCL SCL Clock Frequency Rp Value of Pull-up resistor tHD;STA tLOW tHIGH tSU;STA tHD;DAT tSU;DAT tSU;STO tBUF Hold Time (repeated) START Condition Low Period of the SCL Clock High period of the SCL clock Set-up time for a repeated START condition Data hold time Data setup time Setup time for STOP condition Bus free time between a STOP and START condition 10pF < Cb < 400pF(3) 20 + 0.1Cb(3)(2) 300 ns 20 + 0.1Cb(3)(2) 250 ns 0 50(2) ns -10 10 A - 10 pF fCK(4) > max(16fSCL, 250kHz)(5) 0 400 kHz fSCL 100kHz 1000ns fSCL > 100kHz CC - 0.4V 3mA 0.1VCC < Vi < 0.9VCC fSCL 100kHz CC - 0.4V 3mA 4.0 300ns - s fSCL > 100kHz 0.6 - s fSCL 100kHz 4.7 - s fSCL > 100kHz 1.3 - s fSCL 100kHz 4.0 - s fSCL > 100kHz 0.6 - s fSCL 100kHz 4.7 - s fSCL > 100kHz 0.6 - s fSCL 100kHz 0 3.45 s fSCL > 100kHz 0 0.9 s fSCL 100kHz 250 - ns fSCL > 100kHz 100 - ns fSCL 100kHz 4.0 - s fSCL > 100kHz 0.6 - s fSCL 100kHz 4.7 - s fSCL > 100kHz 1.3 - s Note: 1. This parameter is characterized and not 100% tested. 2. Required only for fSCL > 100kHz. 3. Cb = capacitance of one bus line in pF. Atmel ATmega164P/V [DATASHEET] Atmel-42742B-ATmega164P/V_Datasheet_Complete-08/2016 405 4. 5. fCK = CPU clock frequency. This requirement applies to all 2-wire Serial Interface operation. Other devices connected to the 2wire Serial Bus need only obey the general fSCL requirement. Figure 29-6.Two-wire Serial Bus Timing t of t HIGH t LOW tr t LOW SCL t SU;STA t HD;STA t HD;DAT t SU;DAT t SU;STO SDA t BUF 29.9. ADC characteristics Table 29-12.ADC Characteristics, Single Ended Channel Symbol Parameter Min.(1) Typ Max Units - 10 - Bits - 3 - LSB VREF = 4V, VCC = 4V, ADC clock = 1MHz - 3.5 - LSB VREF = 4V, VCC = 4V, - 2.75 - LSB - 3.5 - LSB - 1.5 - LSB - 0.3 - LSB - 2.5 - LSB - 2.5 - LSB Condition Resolution Absolute accuracy (Including INL, DNL, quantization error, gain and offset error) VREF = 4V, VCC = 4V, ADC clock = 200kHz ADC clock = 200kHz Noise Reduction Mode VREF = 4V, VCC = 4V, ADC clock = 1MHz Noise Reduction Mode Integral Non-Linearity (INL) VREF = 4V, VCC = 4V, ADC clock = 200kHz Differential Non-Linearity (DNL) VREF = 4V, VCC = 4V, ADC clock = 200kHz Gain Error VREF = 4V, VCC = 4V, ADC clock = 200kHz Offset Error VREF = 4V, VCC = 4V, ADC clock = 200kHz Atmel ATmega164P/V [DATASHEET] Atmel-42742B-ATmega164P/V_Datasheet_Complete-08/2016 406 Symbol Parameter Min.(1) Condition Typ Max Units Conversion Time Free Running Conversion 13 - 260 s Clock Frequency 50 - 1000 kHz AVCC(1) Analog Supply Voltage VCC - 0.3 - VCC + 0.3 V VREF Reference Voltage 1.0 - AVCC V VIN Input Voltage GND - VREF V Input Bandwidth - 38.5 - kHz V VINT1 Internal Voltage Reference 1.1V 1.0 1.1 VINT2 Internal Voltage Reference 2.56V, VCC > 2.7V 2.33 2.56 2.79 V RREF Reference Input Resistance - 32 k RAIN Analog Input Resistance - 100 - 1. 1.2 - M Values are guidelines only. Table 29-13.ADC Characteristics, Differential Channels Symbol Parameter Condition Min(1) Typ(1) Max(1) Units Resolution Gain = 1x - 10 - Bits Gain = 10x - 10 - Gain = 200x - 7 - Gain = 1x, - 19.5 - - 20.5 - 8.5 - Absolute Accuracy (Including INL, DNL Quantization Error and Offset Error) LSB VCC = 5V, VREF = 4V ADC clock = 200 kHz Gain = 10x, VCC = 5 V, VREF = 4V ADC clock = 200 kHz Gain = 200x, VCC = 5V, VREF = 4V ADC clock = 200 kHz Atmel ATmega164P/V [DATASHEET] Atmel-42742B-ATmega164P/V_Datasheet_Complete-08/2016 407 Symbol Parameter Min(1) Typ(1) Max(1) Units - 2.25 - LSB - 4.25 - Gain = 200x, - 11.5 - - 0.75 - - 0.75 - 9.5 - Condition Integral Non- Gain = 1x, linearity (INL) VCC = 5V, VREF = 4V ADC clock = 200 kHz Gain = 10x, VCC = 5V, VREF = 4V ADC clock = 200 kHz VCC = 5V, VREF = 4V ADC clock = 200 kHz Differential Non-linearity (DNL) Gain = 1x, LSB VCC = 5V, VREF = 4V ADC clock = 200 kHz Gain = 10x, VCC = 5V, VREF = 4V ADC clock = 200 kHz Gain = 200x, VCC = 5V, VREF = 4V ADC clock = 200 kHz Atmel ATmega164P/V [DATASHEET] Atmel-42742B-ATmega164P/V_Datasheet_Complete-08/2016 408 Symbol Parameter Condition Min(1) Typ(1) Max(1) Units Gain Error Gain = 1x, - 19.5 - LSB - 19.5 - 6.5 - - 1 - - 1.25 - 2.5 - VCC = 5V, VREF = 4V ADC clock = 200 kHz Gain = 10x, VCC = 5V, VREF = 4V ADC clock = 200 kHz Gain = 200x, VCC = 5V, VREF = 4V ADC clock = 200 kHz Gain = 1x, LSB VCC = 5V, VREF = 4V ADC clock = 200 kHz Gain = 10x, VCC = 5V, VREF = 4V ADC clock = 200 kHz Gain = 200x, VCC = 5V, VREF = 4V ADC clock = 200 kHz Conversion Time 13 - 260 s Clock Frequency 50 - 1000 kHz Atmel ATmega164P/V [DATASHEET] Atmel-42742B-ATmega164P/V_Datasheet_Complete-08/2016 409 Min(1) Typ(1) Max(1) Units Analog Supply Voltage VCC - 0.3 - VCC + 0.3 V VREF Reference Voltage 2.0 - AVCC - 0.5 VIN Input Differential Voltage 0 - AVCC ADC Conversion Output -511 - 511 LSB Input Bandwidth - 4 - kHz V Symbol Parameter AVCC Condition VINT1 Internal Voltage Reference 1.1V 1.0 1.1 1.2 VINT2 Internal Voltage Reference 2.56V 2.33 2.56 2.79 RREF Reference Input Resistance - 32 - k Note: 1. Values are guidelines only. Atmel ATmega164P/V [DATASHEET] Atmel-42742B-ATmega164P/V_Datasheet_Complete-08/2016 410 30. Typical Characteristics The following charts show typical behavior. These figures are not tested during manufacturing. All current consumption measurements are performed with all I/O pins configured as inputs and with internal pull-ups enabled. A sine wave generator with rail-to-rail output is used as clock source. All Active- and Idle current consumption measurements are done with all bits in the PRR registers set and thus, the corresponding I/O modules are turned off. Also the Analog Comparator is disabled during these measurements. The power consumption in Power-down mode is independent of clock selection. The current consumption is a function of several factors such as: operating voltage, operating frequency, loading of I/O pins, switching rate of I/O pins, code executed and ambient temperature. The dominating factors are operating voltage and frequency. The current drawn from capacitive loaded pins may be estimated (for one pin) as CL x VCC x f where CL = load capacitance, VCC = operating voltage and f = average switching frequency of I/O pin. The parts are characterized at frequencies higher than test limits. Parts are not guaranteed to function properly at frequencies higher than the ordering code indicates. The difference between current consumption in Power-down mode with Watchdog Timer enabled and Power-down mode with Watchdog Timer disabled represents the differential current drawn by the Watchdog Timer. Active Supply Current Figure 30-1.Active Supply Current vs. Low Frequency (0.1 - 1.0MHz) 0,4 ICC (mA) 30.1. 0,35 5.5 V 0,3 5.0 V 4.5 V 0,25 4.0 V 0,2 3.3 V 0,15 2.7 V 0,1 1.8 V 0,05 0 0 0,1 0,2 0,3 0,4 0,5 0,6 0,7 0,8 0,9 1 Frequency (MHz) Atmel ATmega164P/V [DATASHEET] Atmel-42742B-ATmega164P/V_Datasheet_Complete-08/2016 411 Figure 30-2.Active Supply Current vs. Frequency (1 - 20MHz) 20 5.5V ICC (mA) 18 16 5.0V 14 4.5V 12 10 4.0V 8 6 3.3V 4 2.7V 2 1.8V 0 0 2 4 6 8 10 12 14 16 18 20 Frequency (MHz) Figure 30-3.Active Supply Current vs. VCC (Internal RC Oscillator, 8 MHz) 6 105C 85 C 25 C -40 C 5 ICC (mA) 4 3 2 1 0 1.5 2 2.5 3 3.5 4 4.5 5 5.5 VCC (V) Figure 30-4.Active Supply Current vs. VCC (Internal RC Oscillator, 1 MHz) 1.6 1.4 105C 85 C 25 C -40 C 1.2 ICC (mA) 1 0.8 0.6 0.4 0.2 0 1.5 2 2.5 3 3.5 4 4.5 5 5.5 VCC (V) Atmel ATmega164P/V [DATASHEET] Atmel-42742B-ATmega164P/V_Datasheet_Complete-08/2016 412 Figure 30-5.Active Supply Current vs. VCC (Internal RC Oscillator, 128 kHz) 1.4 1.2 -40 C 105C 25 C 85 C ICC (mA) 1 0.8 0.6 0.4 0.2 0 1.5 2 2.5 3 3.5 4 4.5 5 5.5 VCC (V) Idle Supply Current Figure 30-6.Idle Supply Current vs. Low Frequency (0.1 - 1.0MHz) 0,4 ICC (mA) 30.2. 0,35 5.5 V 0,3 5.0 V 4.5 V 0,25 4.0 V 0,2 3.3 V 0,15 2.7 V 0,1 1.8 V 0,05 0 0 0,1 0,2 0,3 0,4 0,5 0,6 0,7 0,8 0,9 1 Frequency (MHz) Atmel ATmega164P/V [DATASHEET] Atmel-42742B-ATmega164P/V_Datasheet_Complete-08/2016 413 Figure 30-7.Idle Supply Current vs. Frequency (1 - 20MHz) 6 5.5 V 5 5.0 V ICC (mA) 4 4.5 V 3 4.0 V 2 3.3 V 1 2.7 V 1.8 V 0 0 2 4 6 8 10 12 14 16 18 20 Frequency (MHz) Figure 30-8.Idle Supply Current vs. VCC (Internal RC Oscillator, 8 MHz) 1.4 105C 85 C 25 C -40 C 1.2 1 ICC (mA) 0.8 0.6 0.4 0.2 0 1.5 2 2.5 3 3.5 4 4.5 5 5.5 VCC (V) Figure 30-9.Idle Supply Current vs. VCC (Internal RC Oscillator, 1 MHz) 0.4 105C -40 C 85 C 25 C ICC (mA) 0.35 0.3 0.25 0.2 0.15 0.1 1.5 2 2.5 3 3.5 4 4.5 5 5.5 VCC (V) Atmel ATmega164P/V [DATASHEET] Atmel-42742B-ATmega164P/V_Datasheet_Complete-08/2016 414 Figure 30-10.Idle Supply Current vs. VCC (Internal RC Oscillator, 128 kHz) 0.14 0.12 -40 C 105C 25 C 85 C ICC (mA) 0.1 0.08 0.06 0.04 0.02 0 1.5 2 2.5 3 3.5 4 4.5 5 5.5 VCC (V) 30.3. Supply Current of I/O Modules The tables and formulas below can be used to calculate the additional current consumption for the different I/O modules in Active and Idle mode. The enabling or disabling of the I/O modules are controlled by the Power Reduction Register. See "PRR - Power Reduction Register" for details. Table 30-1.Additional Current Consumption for the different I/O modules (absolute values) PRR bit Typical numbers (A) VCC = 2V, F = 1MHz VCC = 3V, F = 4MHz VCC = 5V, F = 8MHz PRUSART1 6.0 38.5 150.0 PRUSART0 7.9 50.3 197.0 PRTWI 16.9 116.2 489.3 PRTIM2 14.4 95.8 393.2 PRTIM1 9.0 57.3 234.8 PRTIM0 5.1 33.3 132.5 PRADC 18.1 86.3 335.3 PRSPI 11.1 70.5 285.0 Table 30-2.Additional Current Consumption (percentage) in Active and Idle mode PRR bit Additional Current consumption compared to Active with external clock (See Figure 30-1 and Figure 30-2) Additional Current consumption compared to Idle with external clock (See Figure 30-6 and Figure 30-7) PRUSART1 1.8% 6.9% PRUSART0 2.4% 9.1% PRTWI 5.4% 21.2% Atmel ATmega164P/V [DATASHEET] Atmel-42742B-ATmega164P/V_Datasheet_Complete-08/2016 415 PRR bit Additional Current consumption compared to Active with external clock (See Figure 30-1 and Figure 30-2) Additional Current consumption compared to Idle with external clock (See Figure 30-6 and Figure 30-7) PRTIM2 4.6% 17.4% PRTIM1 2.7% 10.5% PRTIM0 1.6% 6.0% PRADC 4.5% 16.8% PRSPI 3.3% 12.9% It is possible to calculate the typical current consumption based on the numbers from Table 30-2 for other VCC and frequency settings than listed in Table 30-1. Calculate the expected current consumption in idle mode with TIMER1, ADC, and SPI enabled at VCC = 2.0V and F = 1MHz. From Table 30-2, third column, we see that we need to add 10.5% for the TIMER1, 16.8% for the ADC, and 12.9% for the SPI module. Reading from Figure 30-6, we find that the idle current consumption is ~0.115 mA at VCC = 2.0V and F = 1MHz. The total current consumption in idle mode with TIMER1, ADC, and SPI enabled, gives: 0.115 1 + 0.105 + 0.168 + 0.129 0.161 Power-down Supply Current Figure 30-11.Power-down Supply Current vs. VCC (Watchdog Timer Disabled) 4.5 105C 4 3.5 3 ICC (uA) 30.4. 2.5 2 85 C 1.5 1 0.5 25 C -40 C 0 1.5 2 2.5 3 3.5 4 4.5 5 5.5 VCC (V) Atmel ATmega164P/V [DATASHEET] Atmel-42742B-ATmega164P/V_Datasheet_Complete-08/2016 416 Figure 30-12.Power-down Supply Current vs. VCC (Watchdog Timer Enabled) 14 12 105C ICC (uA) 10 85 C 25 C -40 C 8 6 4 2 1.5 2 2.5 3 3.5 4 4.5 5 5.5 VCC (V) Power-save Supply Current Figure 30-13.Power-save Supply Current vs. VCC (Watchdog Timer Disabled and 32kHz crystal oscillator running) 1,4 1,2 25 C 1 I CC (uA) 30.5. 0,8 0,6 0,4 0,2 0 1,5 2 2,5 3 3,5 4 4,5 5 5,5 VCC (V) Atmel ATmega164P/V [DATASHEET] Atmel-42742B-ATmega164P/V_Datasheet_Complete-08/2016 417 30.6. Standby Supply Current Figure 30-14.Standby Supply Current vs. VCC (Watchdog Timer Disabled) 0,18 6MHz_xtal 0,16 6MHz_res 0,14 4MHz_xtal 4MHz_res ICC (mA) 0,12 0,1 2MHz_res 2MHz_xtal 1MHz_res 450kHz_res 0,08 0,06 0,04 0,02 0 1,5 2 2,5 3 3,5 4 4,5 5 5,5 VCC (V) Pin Pull-Up Figure 30-15.I/O Pin Pull-up Resistor Current vs. Input Voltage (VCC = 1.8V) 50 45 40 35 IOP(uA) 30.7. 30 25 20 15 10 25 C 85 C -40 C 105 C 5 0 0 0.2 0.4 0.6 0.8 1 1.2 1.4 1.6 1.8 VOP(V) Atmel ATmega164P/V [DATASHEET] Atmel-42742B-ATmega164P/V_Datasheet_Complete-08/2016 418 Figure 30-16.I/O Pin Pull-up Resistor Current vs. Input Voltage (VCC = 2.7V) 80 70 60 IOP (uA) 50 40 30 20 25C 85C 105C -40C 10 0 0 0.5 1 1.5 2 2.5 VOP (V) Figure 30-17.I/O Pin Pull-up Resistor Current vs. Input Voltage (VCC = 5V) 140 120 IOP (uA) 100 80 60 40 25C 85C 105C -40C 20 0 0 0.5 1 1.5 2 2.5 3 3.5 4 4.5 5 VOP (V) Figure 30-18.Reset Pull-up Resistor Current vs. Reset Pin Voltage (VCC = 1.8V) 40 35 30 IRESET (uA) 25 20 15 10 105C 85C 25C -40C 5 0 0 0.2 0.4 0.6 0.8 1 1.2 1.4 1.6 1.8 VRESET (V) Atmel ATmega164P/V [DATASHEET] Atmel-42742B-ATmega164P/V_Datasheet_Complete-08/2016 419 Figure 30-19.Reset Pull-up Resistor Current vs. Reset Pin Voltage (VCC = 2.7V) 60 50 IRESET (uA) 40 30 20 105C 85C 25C -40C 10 0 0 0.5 1 1.5 2 2.5 VRESET (V) Figure 30-20.Reset Pull-up Resistor Current vs. Reset Pin Voltage (VCC = 5V) 120 IRESET (uA) 100 80 60 40 25C 105C 85C -40C 20 0 0 0.5 1 1.5 2 2.5 3 3.5 4 4.5 5 VRESET (V) Atmel ATmega164P/V [DATASHEET] Atmel-42742B-ATmega164P/V_Datasheet_Complete-08/2016 420 Pin Driver Strength Figure 30-21.I/O Pin Output Voltage vs. Sink Current (VCC = 3V) 1 105C 85 C 0.9 0.8 0.7 25 C VOL (V) 0.6 -40 C 0.5 0.4 0.3 0.2 0.1 0 0 2 4 6 8 10 12 14 16 18 20 IOL (mA) Figure 30-22.I/O Pin Output Voltage vs. Sink Current (VCC = 5V) 0.7 0.6 105C 85 C 0.5 25 C VOL (V) 30.8. 0.4 -40 C 0.3 0.2 0.1 0 0 2 4 6 8 10 12 14 16 18 20 IOL (mA) Atmel ATmega164P/V [DATASHEET] Atmel-42742B-ATmega164P/V_Datasheet_Complete-08/2016 421 Figure 30-23.I/O Pin Output Voltage vs. Source Current (VCC = 3V) 3.5 3 VOH (V) 2.5 -40 C 25 C 85 C 105C 2 1.5 1 0.5 0 0 2 4 6 8 10 12 14 16 18 20 IOH (mA) Figure 30-24.I/O Pin Output Voltage vs. Source Current (VCC = 5V) 5.1 5 4.9 VOH (V) 4.8 4.7 4.6 -40 C 4.5 25 C 4.4 85 C 105C 4.3 0 2 4 6 8 10 12 14 16 18 20 IOH (mA) Atmel ATmega164P/V [DATASHEET] Atmel-42742B-ATmega164P/V_Datasheet_Complete-08/2016 422 Pin Threshold and Hysteresis Figure 30-25.I/O Pin Input Threshold vs. VCC (VIH , I/O Pin Read as `1') 3 105C 85C 25C -40C 2.5 2 Threshold (V) 1.5 1 0.5 0 1.5 2 2.5 3 3.5 4 4.5 5 VCC (V) Figure 30-26.I/O Pin Input Threshold vs. VCC (VIL, I/O Pin Read as `0') 2.5 105C 85C -40C 25C 2 Threshold (V) 30.9. 1.5 1 0.5 0 1.5 2 2.5 3 3.5 4 4.5 5 5.5 VCC (V) Atmel ATmega164P/V [DATASHEET] Atmel-42742B-ATmega164P/V_Datasheet_Complete-08/2016 423 Figure 30-27.I/O Pin Input Hysteresis vs. VCC 0.7 0.65 Input Hysteresis (mV) 0.6 85C -40C 105C 25C 0.55 0.5 0.45 0.4 0.35 0.3 0.25 0.2 1.5 2 2.5 3 3.5 4 4.5 5 5.5 VCC (V) Figure 30-28.Reset Pin Input Threshold vs. VCC (VIH , I/O Pin Read as `1') 2.5 -40C 105C 85C 25C Threshold (V) 2 1.5 1 0.5 0 1.7 2.2 2.7 3.2 3.7 4.2 4.7 5.2 VCC (V) Figure 30-29.Reset Pin Input Threshold vs. VCC (VIL, I/O Pin Read as `0') 2.5 -40C 105C 85C 25C Threshold (V) 2 1.5 1 0.5 0 1.5 2 2.5 3 3.5 4 4.5 5 5.5 VCC (V) Atmel ATmega164P/V [DATASHEET] Atmel-42742B-ATmega164P/V_Datasheet_Complete-08/2016 424 Figure 30-30.Reset Pin Input Hysteresis vs. VCC 0.7 0.6 Input Hysteresis (mV) 0.5 0.4 0.3 0.2 -40C 105C 25C 85C 0.1 0 1.5 2 2.5 3 3.5 4 4.5 5 5.5 VCC (V) 30.10. BOD Threshold Figure 30-31.BOD Threshold vs. Temperature (VCC = 4.3V) 4.4 Threshold (V) 4.35 Rising Vcc 4.3 Falling Vcc 4.25 4.2 -50 -40 -30 -20 -10 0 10 20 30 40 Temperature (C) 50 60 70 80 90 100 110 Atmel ATmega164P/V [DATASHEET] Atmel-42742B-ATmega164P/V_Datasheet_Complete-08/2016 425 Figure 30-32.BOD Threshold vs. Temperature (VCC = 2.7V) 2.85 2.8 Threshold (V) Rising Vcc 2.75 2.7 Falling Vcc 2.65 2.6 -50 -30 -10 10 30 50 70 90 110 50 70 90 110 Temperature (C) Figure 30-33.BOD Threshold vs. Temperature (VCC = 1.8V) 1.87 Rising Vcc Threshold (V) 1.85 1.83 Falling Vcc 1.81 1.79 1.77 -50 -30 -10 10 30 Temperature (C) Atmel ATmega164P/V [DATASHEET] Atmel-42742B-ATmega164P/V_Datasheet_Complete-08/2016 426 30.11. Internal Oscillator Speed Figure 30-34.Watchdog Oscillator Frequency vs. Temperature 128 126 124 FRC (kHz) 122 120 1.8 V 118 2.7 V 3.3 V 4.0 V 5.5 V 116 114 112 -60 -40 -20 0 20 40 60 80 100 120 Temperature (C) Figure 30-35.Watchdog Oscillator Frequency vs. VCC 129 127 -40C 125 FRC (kHz) 123 25C 121 119 117 85C 115 105C 113 1.5 2 2.5 3 3.5 4 4.5 5 5.5 VCC (V) Atmel ATmega164P/V [DATASHEET] Atmel-42742B-ATmega164P/V_Datasheet_Complete-08/2016 427 Figure 30-36.Calibrated 8MHz RC Oscillator vs. VCC 8.3 105C 85 C 8.2 8.1 25 C FRC (MHz) 8 7.9 7.8 -40 C 7.7 7.6 7.5 1.5 2 2.5 3 3.5 4 4.5 5 5.5 VCC (V) Figure 30-37.Calibrated 8MHz RC Oscillator vs. Temperature 8.75 5.0 V 8.7 3.0 V FRC (MHz) 8.65 8.6 8.55 8.5 8.45 8.4 -40 -30 -20 -10 0 10 20 30 40 50 60 70 80 90 100 110 Temperature Figure 30-38.Calibrated 8MHz RC Oscillator vs. OSCCAL Value 16 25 C 85 C 105C -40 C 14 FRC (MHz) 12 10 8 6 4 0 16 32 48 64 80 96 112 128 144 160 176 192 208 224 240 OSCCAL(X1) Atmel ATmega164P/V [DATASHEET] Atmel-42742B-ATmega164P/V_Datasheet_Complete-08/2016 428 30.12. Current Consumption of Peripheral Units Figure 30-39.ADC Current vs. VCC (AREF = AVCC) 350 -40C 105C 85C 25C 300 200 ICC (uA) ICC (uA) 250 150 100 1.5 2 2.5 3 3.5 4 4.5 5 5.5 VCC (V) Figure 30-40.Analog Comparator Current vs. VCC 100 -40C 25C 105C 85C ICC(uA) 80 60 40 20 1.5 2 2.5 3 3.5 4 4.5 5 5.5 VCC(V) Atmel ATmega164P/V [DATASHEET] Atmel-42742B-ATmega164P/V_Datasheet_Complete-08/2016 429 Figure 30-41.AREF External Reference Current vs. VCC 200 105C 85 C 25 C -40 C 180 160 ICC (uA) 140 120 100 80 60 40 1.5 2 2.5 3 3.5 4 4.5 5 5.5 VCC (V) Figure 30-42.Brownout Detector Current vs. VCC 30 105C 85 C 25 C -40 C 25 ICC (uA) 20 15 10 5 0 1.5 2 2.5 3 3.5 4 4.5 5 5.5 VCC (V) Figure 30-43.Programming Current vs. VCC ICC (mA) 12 10 25 C -40 C 8 85 C 105C 6 4 2 0 1.5 2 2.5 3 3.5 4 4.5 5 5.5 VCC (V) Atmel ATmega164P/V [DATASHEET] Atmel-42742B-ATmega164P/V_Datasheet_Complete-08/2016 430 Figure 30-44.Watchdog Timer Current vs. VCC 10 9 8 -40C 7 25C 85C 105C ICC (uA) 6 5 4 3 2 1 0 1.5 2 2.5 3 3.5 4 4.5 5 5.5 VCC (V) 30.13. Current Consumption in Reset and Reset Pulse Width Figure 30-45.Reset supply current vs. Low Frequency (0.1 - 1.0Mhz) 0.1 5.5 V ICC (mA) 0.09 0.08 5.0 V 0.07 4.5 V 0.06 4.0 V 0.05 0.04 3.3 V 0.03 2.7 V 0.02 1.8 V 0.01 0 0 0.1 0.2 0.3 0.4 0.5 0.6 0.7 0.8 0.9 1 Frequency (MHz) Atmel ATmega164P/V [DATASHEET] Atmel-42742B-ATmega164P/V_Datasheet_Complete-08/2016 431 Figure 30-46.Reset supply current vs. Frequency (1 - 20Mhz) 2 1.8 5.5 V ICC (mA) 1.6 1.4 5.0 V 1.2 4.5 V 1 4.0 V 0.8 0.6 3.3 V 0.4 2.7 V 0.2 1.8 V 0 0 2 4 6 8 10 12 14 16 18 20 Frequency (MHz) Figure 30-47.Minimum Reset Pulsewidth vs. VCC 1800 1600 1400 Pulsewidth (ns) 1200 1000 800 600 400 105 C 85 C 25 C -40 C 200 0 1 1.5 2 2.5 3 3.5 4 4.5 5 5.5 VCC (V) Atmel ATmega164P/V [DATASHEET] Atmel-42742B-ATmega164P/V_Datasheet_Complete-08/2016 432 31. Register Summary Offset Name Bit Pos. 0x20 PINA 7:0 0x21 DDRA 7:0 DDRA7 DDRA6 DDRA5 DDRA4 DDRA3 DDRA2 DDRA1 DDRA0 0x22 PORTA 7:0 PORTA7 PORTA6 PORTA5 PORTA4 PORTA3 PORTA2 PORTA1 PORTA0 0x23 PINB 7:0 PINB7 PINB6 PINB5 PINB4 PINB3 PINB2 PINB1 PINB0 0x24 DDRB 7:0 DDRB7 DDRB6 DDRB5 DDRB4 DDRB3 DDRB2 DDRB1 DDRB0 0x25 PORTB 7:0 PORTB7 PORTB6 PORTB5 PORTB4 PORTB3 PORTB2 PORTB1 PORTB0 0x26 PINC 7:0 PINC7 PINC6 PINC5 PINC4 PINC3 PINC2 PINC1 PINC0 0x27 DDRC 7:0 DDRC7 DDRC6 DDRC5 DDRC4 DDRC3 DDRC2 DDRC1 DDRC0 0x28 PORTC 7:0 PORTC7 PORTC6 PORTC5 PORTC4 PORTC3 PORTC2 PORTC1 PORTC0 PINA7 PINA6 PINA5 PINA4 PINA3 PINA2 PINA1 PINA0 0x29 PIND 7:0 PIND7 PIND6 PIND5 PIND4 PIND3 PIND2 PIND1 PIND0 0x2A DDRD 7:0 DDRD7 DDRD6 DDRD5 DDRD4 DDRD3 DDRD2 DDRD1 DDRD0 0x2B PORTD 7:0 PORTD7 PORTD6 PORTD5 PORTD4 PORTD3 PORTD2 PORTD1 PORTD0 OCFB OCFA TOV OCFB OCFA TOV OCFB OCFA TOV PCIF2 PCIF1 PCIF0 INTF2 INTF1 INTF0 INT2 INT1 INT0 EEMPE EEPE EERE EEAR2 EEAR1 EEAR0 0x2C ... Reserved 0x34 0x35 TIFR0 7:0 0x36 TIFR1 7:0 0x37 TIFR2 7:0 ICF 0x38 ... Reserved 0x3A 0x3B PCIFR 7:0 0x3C EIFR 7:0 0x3D EIMSK 7:0 0x3E GPIOR0 7:0 0x3F EECR 7:0 0x40 EEDR 7:0 0x41 0x42 EEARL and EEARH 7:0 PCIF3 GPIOR0[7:0] EEPM1 EEPM0 EEAR5 EEAR4 EERIE EEDR[7:0] EEAR7 EEAR6 EEAR3 15:8 EEAR8 0x43 GTCCR 7:0 TSM 0x44 TCCR0A 7:0 COM0A1 COM0A0 0x45 TCCR0B 7:0 FOC0A FOC0B 0x46 TCNT0 7:0 TCNT0[7:0] 0x47 OCR0A 7:0 OCR0A[7:0] 0x48 OCR0B 7:0 OCR0B[7:0] 0x49 Reserved 0x4A GPIOR1 7:0 GPIOR1[7:0] 0x4B GPIOR2 7:0 0x4C SPCR0 7:0 SPIE0 SPE0 0x4D SPSR0 7:0 SPIF0 WCOL0 0x4E SPDR0 7:0 0x4F Reserved COM0B1 COM0B0 WGM02 PSRASY PSRSYNC WGM01 WGM00 CS0[2:0] GPIOR2[7:0] DORD0 MSTR0 CPOL0 CPHA0 SPR01 SPR00 SPI2X0 SPID[7:0] 0x50 ACSR 7:0 ACD ACBG ACO ACI ACIE ACIC ACIS1 ACIS0 0x51 OCDR 7:0 IDRD/OCDR7 OCDR6 OCDR5 OCDR4 OCDR3 OCDR2 OCDR1 OCDR0 Atmel ATmega164P/V [DATASHEET] Atmel-42742B-ATmega164P/V_Datasheet_Complete-08/2016 433 Offset Name 0x52 Reserved Bit Pos. 0x53 SMCR 7:0 0x54 MCUSR 7:0 0x55 MCUCR 7:0 JTD BODS BODSE PUD 0x56 Reserved 0x57 SPMCSR 7:0 SPMIE RWWSB SIGRD RWWSRE BLBSET 7:0 SP7 SP6 SP5 SP4 SP3 JTRF SM2 SM1 SM0 SE WDRF BORF EXTRF PORF IVSEL IVCE PGWRT PGERS SPMEN SP2 SP1 SP0 SP10 SP9 SP8 Z C 0x58 ... Reserved 0x5C 0x5D 0x5E SPL and SPH 15:8 0x5F SREG 7:0 I T H S V 0x60 WDTCSR 7:0 WDIF WDIE WDP[3] WDCE WDE 0x61 CLKPR 7:0 CLKPCE 7:0 PRTWI PRTIM2 PRTIM0 7:0 CAL7 CAL6 N WDP[2:0] CLKPS3 CLKPS2 CLKPS1 CLKPS0 PRUSART1 PRTIM1 PRSPI0 PRUSART0 PRADC CAL5 CAL4 CAL3 CAL2 CAL1 CAL0 PCIE3 PCIE2 PCIE1 PCIE0 ISC21 ISC20 ISC11 ISC10 ISC01 ISC00 PCINT3 PCINT2 PCINT1 PCINT0 0x62 ... Reserved 0x63 0x64 PRR0 0x65 Reserved 0x66 OSCCAL 0x67 Reserved 0x68 PCICR 7:0 0x69 EICRA 7:0 0x6A Reserved 0x6B PCMSK0 7:0 PCINT7 PCINT6 PCINT5 PCINT4 0x6C PCMSK1 7:0 PCINT15 PCINT14 PCINT13 PCINT12 PCINT11 PCINT10 PCINT9 PCINT8 0x6D PCMSK2 7:0 PCINT23 PCINT22 PCINT21 PCINT20 PCINT19 PCINT18 PCINT17 PCINT16 0x6E TIMSK0 7:0 OCIEB OCIEA TOIE 0x6F TIMSK1 7:0 OCIEB OCIEA TOIE 0x70 TIMSK2 7:0 OCIEB OCIEA TOIE ICIE 0x71 ... Reserved 0x72 0x73 PCMSK3 7:0 PCINT31 PCINT30 PCINT29 PCINT28 PCINT27 PCINT26 PCINT25 PCINT24 7:0 ADC7 ADC6 ADC5 ADC4 ADC3 ADC2 ADC1 ADC0 0x74 ... Reserved 0x77 0x78 0x79 ADCL and ADCH 15:8 0x7A ADCSRA 7:0 ADEN ADSC REFS1 REFS0 ADATE ADIF ADIE ADLAR MUX4 MUX3 ACME ADC9 ADC8 ADPS2 ADPS1 ADPS0 ADTS2 ADTS1 ADTS0 MUX2 MUX1 MUX0 0x7B ADCSRB 7:0 0x7C ADMUX 7:0 0x7D Reserved 0x7E DIDR0 7:0 ADC7D ADC6D ADC5D ADC4D ADC3D ADC2D ADC1D ADC0D 0x7F DIDR1 7:0 Reserved5 Reserved4 Reserved3 Reserved2 Reserved1 Reserved0 AIN1D AIN0D 0x80 TCCR1A 7:0 COM1 COM1 COM1 WGM11 WGM10 0x81 TCCR1B 7:0 ICNC1 ICES1 WGM12 CS12 CS11 CS10 COM1 WGM13 Atmel ATmega164P/V [DATASHEET] Atmel-42742B-ATmega164P/V_Datasheet_Complete-08/2016 434 Offset Name Bit Pos. 0x82 TCCR1C 7:0 0x83 Reserved 0x84 TCNT1L and 7:0 TCNT1[7:0] 0x85 TCNT1H 15:8 TCNT1[15:8] 7:0 ICR1[7:0] 0x86 0x87 ICR1L and ICR1H FOC1A FOC1B 15:8 ICR1[15:8] 0x88 OCR1AL and 7:0 OCR1A[7:0] 0x89 OCR1AH 15:8 OCR1A[15:8] 0x8A OCR1BL and 7:0 OCR1B[7:0] 0x8B OCR1BH 15:8 OCR1B[15:8] 0x8C ... Reserved 0xAF 0xB0 TCCR2A 7:0 COM2A1 COM2A0 0xB1 TCCR2B 7:0 FOC2A FOC2B 0xB2 TCNT2 7:0 TCNT2[7:0] 0xB3 OCR2A 7:0 OCR2A[7:0] 0xB4 OCR2B 7:0 OCR2B[7:0] 0xB5 Reserved 0xB6 ASSR 0xB7 Reserved 7:0 0xB8 TWBR 7:0 COM2B1 COM2B0 WGM21 WGM22 WGM20 CS2[2:0] EXCLK AS2 TCN2UB OCR2AUB OCR2BUB TCR2AUB TCR2BUB TWBR7 TWBR6 TWBR5 TWBR4 TWBR3 TWBR2 TWBR1 TWBR0 0xB9 TWSR 7:0 TWS7 TWS6 TWS5 TWS4 TWS3 0xBA TWAR 7:0 TWA6 TWA5 TWA4 TWA3 TWA2 TWA1 TWA0 TWPS[1:0] TWGCE 0xBB TWDR 7:0 TWD7 TWD6 TWD5 TWD4 TWD3 TWD2 TWD1 TWD0 0xBC TWCR 7:0 TWINT TWEA TWSTA TWSTO TWWC TWEN 0xBD TWAMR 7:0 TWAM6 TWAM5 TWAM4 TWAM3 TWAM2 TWAM1 TWAM0 UPE U2X MPCM TXB8 TWIE 0xBE ... Reserved 0xBF 0xC0 UCSR0A 7:0 RXC TXC UDRE FE DOR 0xC1 UCSR0B 7:0 RXCIE TXCIE UDRIE RXEN TXEN 0xC2 UCSR0C 7:0 UMSEL[1:0] UPM[1:0] USBS UCSZ2 RXB8 UCSZ1 / UCSZ0 / UDORD UCPHA UCPOL 0xC3 Reserved 0xC4 UBRR0L and 7:0 0xC5 UBRR0H 15:8 0xC6 UDR0 7:0 0xC7 Reserved 0xC8 UCSR1A 7:0 RXC TXC UDRE FE DOR UPE U2X MPCM 0xC9 UCSR1B 7:0 RXCIE TXCIE UDRIE RXEN TXEN UCSZ2 RXB8 TXB8 UCSZ1 / UCSZ0 / UDORD UCPHA 0xCA UCSR1C 7:0 0xCB Reserved 0xCC UBRR1L and 7:0 0xCD UBRR1H 15:8 0xCE UDR1 7:0 UBRR[7:0] UBRR[11:8] TXB / RXB[7:0] UMSEL[1:0] UPM[1:0] USBS UCPOL UBRR[7:0] UBRR[11:8] TXB / RXB[7:0] Atmel ATmega164P/V [DATASHEET] Atmel-42742B-ATmega164P/V_Datasheet_Complete-08/2016 435 32. Instruction Set Summary ARITHMETIC AND LOGIC INSTRUCTIONS Mnemonics Operands Description Operation Flags #Clocks ADD Rd, Rr Add two Registers without Carry Rd Rd + Rr Z,C,N,V,H 1 ADC Rd, Rr Add two Registers with Carry 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 two Registers with Carry Rd Rd - Rr - C Z,C,N,V,H 1 SBCI Rd, K Subtract Constant from Reg with Carry. 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 BRANCH INSTRUCTIONS Mnemonics Operands Description Operation Flags #Clocks RJMP k Relative Jump PC PC + k + 1 None 2 Indirect Jump to (Z) PC Z None 2 IJMP JMP(1) k Direct Jump PC k None 3 RCALL k Relative Subroutine Call PC PC + k + 1 None 3 Atmel ATmega164P/V [DATASHEET] Atmel-42742B-ATmega164P/V_Datasheet_Complete-08/2016 436 BRANCH INSTRUCTIONS Mnemonics Operands Description Operation Flags #Clocks 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 CPC Rd,Rr Compare with Carry Rd - Rr - C Z, N,V,C,H 1 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 A, b Skip if Bit in I/O Register Cleared if (I/O(A,b)=1) PC PC + 2 or 3 None 1/2/3 SBIS A, b Skip if Bit in I/O Register is Set if (I/O(A,b)=1) PC PC + 2 or 3 None 1/2/3 BRBS s, k Branch if Status Flag Set if (SREG(s) = 1) then PCPC+k + 1 None 1/2 BRBC s, k Branch if Status Flag Cleared if (SREG(s) = 0) then PCPC+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 BRGE k Branch if Greater or Equal, Signed if (N V= 0) then PC PC + k + 1 None 1/2 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 Mnemonics Operands Description Operation Flags #Clocks 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 Atmel ATmega164P/V [DATASHEET] Atmel-42742B-ATmega164P/V_Datasheet_Complete-08/2016 437 BIT AND BIT-TEST INSTRUCTIONS Mnemonics Operands Description Operation Flags #Clocks 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),CRd(7) Z,C,N,V 1 ROR Rd Rotate Right Through Carry Rd(7)C,Rd(n) Rd(n+1),CRd(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 BCLR s Flag Clear SREG(s) 0 SREG(s) 1 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 I0 I 1 SES Set Signed Test Flag S1 S 1 CLS Clear Signed Test Flag S0 S 1 SEV Set Two's Complement Overflow. V1 V 1 CLV Clear Two's 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 Mnemonics Operands Description Operation Flags #Clocks 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-Increment Rd (X), X X + 1 None 2 LD Rd, - X Load Indirect and Pre-Decrement X X - 1, Rd (X) None 2 LD Rd, Y Load Indirect Rd (Y) None 2 LD Rd, Y+ Load Indirect and Post-Increment Rd (Y), Y Y + 1 None 2 Atmel ATmega164P/V [DATASHEET] Atmel-42742B-ATmega164P/V_Datasheet_Complete-08/2016 438 DATA TRANSFER INSTRUCTIONS Mnemonics Operands Description Operation Flags #Clocks LD Rd, - Y Load Indirect and Pre-Decrement 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-Increment Rd (Z), Z Z+1 None 2 LD Rd, -Z Load Indirect and Pre-Decrement 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-Increment (X) Rr, X X + 1 None 2 ST - X, Rr Store Indirect and Pre-Decrement X X - 1, (X) Rr None 2 ST Y, Rr Store Indirect (Y) Rr None 2 ST Y+, Rr Store Indirect and Post-Increment (Y) Rr, Y Y + 1 None 2 ST - Y, Rr Store Indirect and Pre-Decrement Y Y - 1, (Y) Rr None 2 STD Y+q,Rr Store Indirect with Displacement (Y + q) Rr None 2 ST Z, Rr Store Indirect (Z) Rr None 2 ST Z+, Rr Store Indirect and Post-Increment (Z) Rr, Z Z + 1 None 2 ST -Z, Rr Store Indirect and Pre-Decrement 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 - SPM IN Rd, A In from I/O Location Rd I/O (A) None 1 OUT A, Rr Out to I/O Location I/O (A) Rr None 1 PUSH Rr Push Register on Stack STACK Rr None 2 POP Rd Pop Register from Stack Rd STACK None 2 MCU CONTROL INSTRUCTIONS Mnemonics Operands Description Operation Flags #Clocks NOP No Operation No Operation None 1 SLEEP Sleep (see specific descr. for Sleep function) None 1 WDR Watchdog Reset (see specific descr. for WDR/timer) None 1 BREAK Break For On-chip Debug Only None N/A Atmel ATmega164P/V [DATASHEET] Atmel-42742B-ATmega164P/V_Datasheet_Complete-08/2016 439 33. Packaging Information 33.1. 40-pin PDIP D PIN 1 E1 A SEATING PLANE A1 L B B1 e E 0 ~ 15 C COMMON DIMENSIONS (Unit of Measure = mm) REF eB Notes: 1. This package conforms to JEDEC reference MS-011, Variation AC. 2. Dimensions D and E1 do not include mold Flash or Protrusion. Mold Flash or Protrusion shall not exceed 0.25mm (0.010"). SYMBOL MIN NOM MAX A - - 4.826 A1 0.381 - - D 52.070 - 52.578 E 15.240 - 15.875 E1 13.462 - 13.970 B 0.356 - 0.559 B1 1.041 - 1.651 L 3.048 - 3.556 C 0.203 - 0.381 eB 15.494 - 17.526 e NOTE Note 2 Note 2 2.540 TYP 13/02/2014 40P6, 40-lead (0.600"/15.24mm Wide) Plastic Dual Inline Package (PDIP) C 40P6 Atmel ATmega164P/V [DATASHEET] Atmel-42742B-ATmega164P/V_Datasheet_Complete-08/2016 440 33.2. 44-pin TQFP P IN 1 IDENTIFIER P IN 1 B e E1 E A1 A2 D1 D C 0~7 L A COMMON DIMENS IONS (Unit of Me a s ure = mm) Note s : 1. This pa cka ge conforms to J EDEC re fe re nce MS -026, Va ria tion ACB. 2. Dime ns ions D1 a nd E1 do not include mold protrus ion. Allowa ble protrus ion is 0.25mm pe r s ide . Dime ns ions D1 a nd E1 a re ma ximum pla s tic body s ize dime ns ions including mold mis ma tch. 3. Le a d copla na rity is 0.10mm ma ximum. S YMBOL MIN NOM MAX A - - 1.20 A1 0.05 - 0.15 A2 0.95 1.00 1.05 D 11.75 12.00 12.25 D1 9.90 10.00 10.10 E 11.75 12.00 12.25 E1 9.90 10.00 10.10 B 0.30 0.37 0.45 C 0.09 (0.17) 0.20 L 0.45 0.60 0.75 e NOTE Note 2 Note 2 0.80 TYP 06/02/2014 44A, 44-le a d, 10 x 10mm body s ize , 1.0mm body thickne s s , 0.8 mm le a d pitch, thin profile pla s tic qua d fla t pa cka ge (TQFP ) 44A Atmel ATmega164P/V [DATASHEET] Atmel-42742B-ATmega164P/V_Datasheet_Complete-08/2016 C 441 33.3. 44-pin VQFN D Marked Pin# 1 I D E SE ATING PLANE A1 TOP VIEW A3 A K L Pin #1 Co rne r D2 1 2 3 Option A SIDE VIEW Pin #1 Triangl e COMMON DIMENSIONS (Unit of Measure = mm) MIN NOM MAX A 0.80 0.90 1.00 A1 - 0.02 0.05 SYMBOL E2 Option B K Option C b e Pin #1 Cham fe r (C 0.30) Pin #1 Notch (0.20 R) BOTTOM VIEW A3 0.20 REF b 0.18 0.23 D 6.90 7.00 7.10 D2 5.00 5.20 5.40 E 6.90 7.00 7.10 E2 5.00 5.20 5.40 e Note : JEDEC Standard MO-220, Fig . 1 (S AW Singulation) VKKD-3 . NOTE 0.30 0.50 BSC L 0.59 0.64 0.69 K 0.20 0.26 0.41 9/26/08 Package Drawing Contact: avr@atmel.com TITLE 44M1, 44-pad, 7 x 7 x 1.0mm body, lead pitch 0.50mm, 5.20mm exposed pad, thermally enhanced plastic very thin quad flat no lead package (VQFN) GPC ZWS DRAWING NO. REV. 44M1 H Atmel ATmega164P/V [DATASHEET] Atmel-42742B-ATmega164P/V_Datasheet_Complete-08/2016 442 34. Errata 34.1. Rev. A No known errata. Atmel ATmega164P/V [DATASHEET] Atmel-42742B-ATmega164P/V_Datasheet_Complete-08/2016 443 35. Datasheet Revision History Please note that the referring page numbers in this section are referred to this document. The referring revision in this section are referring to the document revision. 35.1. Rev. B - 08/2016 * 35.2. Pinout: Added PDIP and TQFP/QFN pinout figures. Rev. A - 07/2016 Initial Document Release: Based on the Atmel-8011R-AVR-09/2015 datasheet which was a common datasheet for following 8-bit AVR microcontrollers: ATmega164P/V, ATmega324P/V and ATmega644P/V. The Atmel-8011RAVR-09/2015 is now split into separate datasheets for each of these microcontrollers. Atmel ATmega164P/V [DATASHEET] Atmel-42742B-ATmega164P/V_Datasheet_Complete-08/2016 444 Atmel Corporation (c) 1600 Technology Drive, San Jose, CA 95110 USA T: (+1)(408) 441.0311 F: (+1)(408) 436.4200 | www.atmel.com 2016 Atmel Corporation. / Rev.: Atmel-42742B-ATmega164P/V_Datasheet_Complete-08/2016 (R) (R) (R) Atmel , Atmel logo and combinations thereof, Enabling Unlimited Possibilities , AVR , 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.