ATmega328/P AVR(R) Microcontroller with picoPower(R) Technology Introduction (R) (R) The picoPower ATmega328/P 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 ATmega328/P achieves throughputs close to 1 MIPS per MHz. This empowers system designers to optimize the device for power consumption versus processing speed. Feature (R) High Performance, Low-Power 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 20 MHz - On-chip 2-cycle multiplier * High Endurance Nonvolatile Memory Segments - 32K Bytes of in-system self-programmable Flash program memory - 1K Bytes EEPROM - 2K Bytes 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 * QTouch Library Support - Capacitive touch buttons, sliders and wheels - QTouch and QMatrix acquisition - Up to 64 sense channels * 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 (c) 2018 Microchip Technology Inc. Datasheet Complete DS40001984A-page 1 ATmega328/P - - - - - - * * * * * * 8-channel 10-bit ADC in TQFP and QFN/MLF package * Temperature measurement 6-channel 10-bit ADC in PDIP package * Temperature measurement Two master/slave SPI serial interface One programmable serial USART One byte-oriented 2-wire serial interface (Philips I2C compatible) Programmable watchdog timer with separate on-chip oscillator - One on-chip analog comparator - Interrupt and wake-up on pin change Special Microcontroller Features - Power-on Reset and programmable Brown-out Detection - Internal calibrated oscillator - External and internal interrupt sources - Six sleep modes: idle, ADC noise reduction, power-save, power-down, standby, and extended standby I/O and Packages - 23 Programmable I/O lines - 28-pin PDIP, 32-lead TQFP, 28-pad QFN/MLF and 32-pad QFN/MLF Operating Voltage: - 1.8 - 5.5V Temperature Range: - -40C to 105C Speed Grade: - ATmega328/P: 0 - 4 MHz @ 1.8 - 5.5V, 0 - 10 MHz @ 2.7 - 5.5V, 0 - 20 MHz @ 4.5 - 5.5V Power Consumption at 1 MHz, 1.8V, 25C - Active mode: 0.2 mA - Power-Down mode: 0.1 A - Power-Save mode: 0.75 A (Including 32 kHz RTC) (c) 2018 Microchip Technology Inc. Datasheet Complete DS40001984A-page 2 Table of Contents Introduction......................................................................................................................1 Feature............................................................................................................................ 1 1. Description.................................................................................................................9 2. Configuration Summary...........................................................................................10 3. Ordering Information ............................................................................................... 11 3.1. 3.2. ATmega328 ............................................................................................................................... 11 ATmega328P .............................................................................................................................11 4. Block Diagram......................................................................................................... 13 5. Pin Configurations................................................................................................... 14 5.1. 5.2. Pinout......................................................................................................................................... 14 Pin Descriptions......................................................................................................................... 17 6. I/O Multiplexing........................................................................................................19 7. Resources............................................................................................................... 21 8. Data Retention.........................................................................................................22 9. About Code Examples.............................................................................................23 10. Capacitive Touch Sensing....................................................................................... 24 10.1. QTouch Library........................................................................................................................... 24 11. AVR CPU Core........................................................................................................ 25 11.1. 11.2. 11.3. 11.4. 11.5. 11.6. Overview.................................................................................................................................... 25 Arithmetic Logic Unit (ALU)........................................................................................................ 26 Status Register...........................................................................................................................26 General Purpose Register File................................................................................................... 29 Stack Pointer.............................................................................................................................. 30 Instruction Execution Timing...................................................................................................... 32 11.7. Reset and Interrupt Handling..................................................................................................... 33 12. AVR Memories.........................................................................................................36 12.1. 12.2. 12.3. 12.4. 12.5. 12.6. Overview.................................................................................................................................... 36 In-System Reprogrammable Flash Program Memory................................................................36 SRAM Data Memory.................................................................................................................. 37 EEPROM Data Memory............................................................................................................. 38 I/O Memory.................................................................................................................................39 Register Description................................................................................................................... 40 (c) 2018 Microchip Technology Inc. Datasheet Complete DS40001984A-page 3 ATmega328/P 13. System Clock and Clock Options............................................................................ 49 13.1. Clock Systems and Their Distribution........................................................................................ 49 13.2. Clock Sources............................................................................................................................ 50 13.3. Low-Power Crystal Oscillator..................................................................................................... 52 13.4. Full Swing Crystal Oscillator.......................................................................................................54 13.5. Low-Frequency Crystal Oscillator.............................................................................................. 55 13.6. Calibrated Internal RC Oscillator................................................................................................56 13.7. 128 kHz Internal Oscillator......................................................................................................... 57 13.8. External Clock............................................................................................................................ 58 13.9. Timer/Counter Oscillator.............................................................................................................59 13.10. Clock Output Buffer....................................................................................................................59 13.11. System Clock Prescaler............................................................................................................. 59 13.12. Register Description...................................................................................................................60 14. Power Management and Sleep Modes................................................................... 64 14.1. Overview.................................................................................................................................... 64 14.2. Sleep Modes.............................................................................................................................. 64 14.3. BOD Disable...............................................................................................................................65 14.4. Idle Mode....................................................................................................................................65 14.5. ADC Noise Reduction Mode...................................................................................................... 65 14.6. Power-Down Mode.....................................................................................................................66 14.7. Power-Save Mode......................................................................................................................66 14.8. Standby Mode............................................................................................................................ 67 14.9. Extended Standby Mode............................................................................................................ 67 14.10. Power Reduction Register......................................................................................................... 67 14.11. Minimizing Power Consumption................................................................................................. 67 14.12. Register Description...................................................................................................................69 15. System Control and Reset.......................................................................................74 15.1. 15.2. 15.3. 15.4. 15.5. 15.6. 15.7. 15.8. 15.9. Resetting the AVR...................................................................................................................... 74 Reset Sources............................................................................................................................74 Power-on Reset..........................................................................................................................75 External Reset............................................................................................................................76 Brown-out Detection...................................................................................................................76 Watchdog System Reset............................................................................................................ 77 Internal Voltage Reference.........................................................................................................77 Watchdog Timer......................................................................................................................... 78 Register Description................................................................................................................... 80 16. Interrupts................................................................................................................. 84 16.1. Interrupt Vectors in ATmega328/P............................................................................................. 84 16.2. Register Description................................................................................................................... 86 17. EXTINT - External Interrupts................................................................................... 89 17.1. Pin Change Interrupt Timing.......................................................................................................89 17.2. Register Description................................................................................................................... 90 18. I/O-Ports.................................................................................................................. 99 (c) 2018 Microchip Technology Inc. Datasheet Complete DS40001984A-page 4 ATmega328/P 18.1. 18.2. 18.3. 18.4. Overview.................................................................................................................................... 99 Ports as General Digital I/O......................................................................................................100 Alternate Port Functions...........................................................................................................103 Register Description................................................................................................................. 115 19. 8-bit Timer/Counter0 (TC0) with PWM.................................................................. 127 19.1. 19.2. 19.3. 19.4. 19.5. 19.6. 19.7. 19.8. 19.9. Features................................................................................................................................... 127 Overview.................................................................................................................................. 127 Timer/Counter Clock Sources.................................................................................................. 129 Counter Unit............................................................................................................................. 129 Output Compare Unit............................................................................................................... 130 Compare Match Output Unit.....................................................................................................132 Modes of Operation..................................................................................................................134 Timer/Counter Timing Diagrams.............................................................................................. 138 Register Description................................................................................................................. 140 20. 16-bit Timer/Counter1 (TC1) with PWM................................................................ 152 20.1. Overview.................................................................................................................................. 152 20.2. Features................................................................................................................................... 152 20.3. Block Diagram.......................................................................................................................... 152 20.4. Definitions.................................................................................................................................153 20.5. Registers.................................................................................................................................. 154 20.6. Accessing 16-bit Timer/Counter Registers............................................................................... 154 20.7. Timer/Counter Clock Sources.................................................................................................. 157 20.8. Counter Unit............................................................................................................................. 157 20.9. Input Capture Unit.................................................................................................................... 158 20.10. Output Compare Units............................................................................................................. 160 20.11. Compare Match Output Unit.....................................................................................................162 20.12. Modes of Operation..................................................................................................................163 20.13. Timer/Counter 0, 1 Prescalers................................................................................................. 171 20.14. Timer/Counter Timing Diagrams.............................................................................................. 171 20.15. Register Description.................................................................................................................173 21. Timer/Counter 0, 1 Prescalers...............................................................................186 21.1. 21.2. 21.3. 21.4. Internal Clock Source............................................................................................................... 186 Prescaler Reset........................................................................................................................186 External Clock Source..............................................................................................................186 Register Description................................................................................................................. 188 22. 8-bit Timer/Counter2 (TC2) with PWM and Asynchronous Operation...................190 22.1. 22.2. 22.3. 22.4. 22.5. 22.6. 22.7. 22.8. Features................................................................................................................................... 190 Overview.................................................................................................................................. 190 Timer/Counter Clock Sources.................................................................................................. 192 Counter Unit............................................................................................................................. 192 Output Compare Unit............................................................................................................... 193 Compare Match Output Unit.....................................................................................................195 Modes of Operation..................................................................................................................196 Timer/Counter Timing Diagrams.............................................................................................. 200 (c) 2018 Microchip Technology Inc. Datasheet Complete DS40001984A-page 5 ATmega328/P 22.9. Asynchronous Operation of Timer/Counter2............................................................................ 201 22.10. Timer/Counter Prescaler.......................................................................................................... 203 22.11. Register Description................................................................................................................. 203 23. Serial Peripheral Interface (SPI)............................................................................218 23.1. 23.2. 23.3. 23.4. 23.5. Features................................................................................................................................... 218 Overview.................................................................................................................................. 218 SS Pin Functionality................................................................................................................. 222 Data Modes.............................................................................................................................. 222 Register Description................................................................................................................. 223 24. Universal Synchronous Asynchronous Receiver Transceiver (USART)............... 228 24.1. Features................................................................................................................................... 228 24.2. Overview.................................................................................................................................. 228 24.3. Block Diagram.......................................................................................................................... 228 24.4. Clock Generation......................................................................................................................229 24.5. Frame Formats.........................................................................................................................232 24.6. USART Initialization................................................................................................................. 233 24.7. Data Transmission - The USART Transmitter......................................................................... 234 24.8. Data Reception - The USART Receiver.................................................................................. 236 24.9. Asynchronous Data Reception.................................................................................................240 24.10. Multi-Processor Communication Mode.................................................................................... 243 24.11. Examples of Baud Rate Setting............................................................................................... 243 24.12. Register Description.................................................................................................................246 25. USART in SPI (USARTSPI) Mode.........................................................................256 25.1. 25.2. 25.3. 25.4. 25.5. 25.6. 25.7. 25.8. Features................................................................................................................................... 256 Overview.................................................................................................................................. 256 Clock Generation......................................................................................................................256 SPI Data Modes and Timing.....................................................................................................257 Frame Formats.........................................................................................................................257 Data Transfer............................................................................................................................259 AVR USART MSPIM vs. AVR SPI............................................................................................260 Register Description................................................................................................................. 261 26. Two-Wire Serial Interface (TWI)............................................................................ 262 26.1. 26.2. 26.3. 26.4. 26.5. 26.6. 26.7. 26.8. 26.9. Features................................................................................................................................... 262 Two-Wire Serial Interface Bus Definition..................................................................................262 Data Transfer and Frame Format.............................................................................................263 Multi-Master Bus Systems, Arbitration, and Synchronization...................................................266 Overview of the TWI Module.................................................................................................... 268 Using the TWI...........................................................................................................................270 Transmission Modes................................................................................................................ 273 Multi-Master Systems and Arbitration...................................................................................... 291 Register Description................................................................................................................. 292 27. Analog Comparator (AC)....................................................................................... 300 27.1. Overview.................................................................................................................................. 300 (c) 2018 Microchip Technology Inc. Datasheet Complete DS40001984A-page 6 ATmega328/P 27.2. Analog Comparator Multiplexed Input...................................................................................... 300 27.3. Register Description................................................................................................................. 301 28. Analog-to-Digital Converter (ADC)........................................................................ 305 28.1. 28.2. 28.3. 28.4. 28.5. 28.6. 28.7. 28.8. 28.9. Features................................................................................................................................... 305 Overview.................................................................................................................................. 305 Starting a Conversion...............................................................................................................307 Prescaling and Conversion Timing...........................................................................................308 Changing Channel or Reference Selection.............................................................................. 310 ADC Noise Canceler................................................................................................................ 312 ADC Conversion Result........................................................................................................... 315 Temperature Measurement...................................................................................................... 316 Register Description................................................................................................................. 316 29. debugWIRE On-chip Debug System..................................................................... 325 29.1. 29.2. 29.3. 29.4. 29.5. 29.6. Features................................................................................................................................... 325 Overview.................................................................................................................................. 325 Physical Interface..................................................................................................................... 325 Software Breakpoints............................................................................................................... 326 Limitations of debugWIRE........................................................................................................326 Register Description................................................................................................................. 326 30. Boot Loader Support - Read-While-Write Self-programming (BTLDR)................ 328 30.1. 30.2. 30.3. 30.4. 30.5. 30.6. 30.7. 30.8. 30.9. Features................................................................................................................................... 328 Overview.................................................................................................................................. 328 Application and Boot Loader Flash Sections............................................................................328 Read-While-Write and No Read-While-Write Flash Sections...................................................329 Boot Loader Lock Bits.............................................................................................................. 331 Entering the Boot Loader Program...........................................................................................332 Addressing the Flash During Self-Programming...................................................................... 333 Self-Programming the Flash.....................................................................................................334 Register Description................................................................................................................. 342 31. Memory Programming (MEMPROG).....................................................................345 31.1. 31.2. 31.3. 31.4. 31.5. 31.6. 31.7. 31.8. 31.9. Program And Data Memory Lock Bits...................................................................................... 345 Fuse Bits.................................................................................................................................. 346 Signature Bytes........................................................................................................................ 348 Calibration Byte........................................................................................................................ 349 Serial Number.......................................................................................................................... 349 Page Size................................................................................................................................. 349 Parallel Programming Parameters, Pin Mapping, and Commands..........................................349 Parallel Programming...............................................................................................................351 Serial Downloading.................................................................................................................. 359 32. Electrical Characteristics....................................................................................... 364 32.1. Absolute Maximum Ratings......................................................................................................364 32.2. Common DC Characteristics....................................................................................................364 32.3. Speed Grades.......................................................................................................................... 367 (c) 2018 Microchip Technology Inc. Datasheet Complete DS40001984A-page 7 ATmega328/P 32.4. 32.5. 32.6. 32.7. 32.8. 32.9. Clock Characteristics................................................................................................................368 System and Reset Characteristics........................................................................................... 369 SPI Timing Characteristics....................................................................................................... 370 Two-Wire Serial Interface Characteristics................................................................................ 371 ADC Characteristics................................................................................................................. 373 Parallel Programming Characteristics...................................................................................... 374 33. Typical Characteristics (TA = -40C to 85C).........................................................377 33.1. ATmega328 Typical Characteristics......................................................................................... 377 34. Typical Characteristics (TA = -40C to 105C).......................................................402 34.1. ATmega328P Typical Characteristics.......................................................................................402 35. Register Summary.................................................................................................427 35.1. Note..........................................................................................................................................429 36. Instruction Set Summary....................................................................................... 431 37. Packaging Information...........................................................................................436 37.1. 37.2. 37.3. 37.4. 32-pin 32A................................................................................................................................ 436 32-pin 32M1-A..........................................................................................................................437 28-pin 28M1............................................................................................................................. 438 28-pin 28P3.............................................................................................................................. 438 38. Errata.....................................................................................................................440 38.1. Errata ATmega328/P................................................................................................................440 39. Datasheet Revision History................................................................................... 441 39.1. Rev. A - 2/2018........................................................................................................................441 39.2. Pre Microchip Revisions...........................................................................................................441 The Microchip Web Site.............................................................................................. 442 Customer Change Notification Service........................................................................442 Customer Support....................................................................................................... 442 Microchip Devices Code Protection Feature............................................................... 442 Legal Notice.................................................................................................................443 Trademarks................................................................................................................. 443 Quality Management System Certified by DNV...........................................................444 Worldwide Sales and Service......................................................................................445 (c) 2018 Microchip Technology Inc. Datasheet Complete DS40001984A-page 8 ATmega328/P Description 1. Description (R) The AVR core combines a rich instruction set with 32 general purpose working registers. All the 32 registers are directly connected to the Arithmetic Logic Unit (ALU), allowing two independent 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 ATmega328/P provides the following features: 32Kbytes of in-system programmable Flash with readwhile-write capabilities, 1Kbytes EEPROM, 2Kbytes SRAM, 23 general purpose I/O lines, 32 general purpose working registers, Real Time Counter (RTC), three flexible timer/counters with Compare modes and PWM, 1 serial programmable USARTs , 1 byte-oriented 2-wire Serial Interface (I2C), a 6-channel 10bit ADC (8 channels in TQFP and QFN/MLF packages), a programmable watchdog timer with internal oscillator, an SPI serial port, 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. (R) Microchip offers the QTouch 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 SuppressionTM (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 Microchip's high density nonvolatile 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 selfprogrammable Flash on a monolithic chip, the ATmega328/P is a powerful microcontroller that provides a highly flexible and cost effective solution to many embedded control applications. The ATmega328/P 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. (c) 2018 Microchip Technology Inc. Datasheet Complete DS40001984A-page 9 ATmega328/P Configuration Summary 2. Configuration Summary Features ATmega328/P Pin Count 28/32 Flash (Bytes) 32K SRAM (Bytes) 2K EEPROM (Bytes) 1K General Purpose I/O Lines 23 SPI 2 TWI (I2C) 1 USART 1 ADC 10-bit 15 kSPS ADC Channels 8 8-bit Timer/Counters 2 16-bit Timer/Counters 1 (c) 2018 Microchip Technology Inc. Datasheet Complete DS40001984A-page 10 ATmega328/P Ordering Information 3. Ordering Information 3.1 ATmega328 Speed [MHz](3) Power Supply [V] Ordering Code(2) Package(1) Operational Range 20 1.8 - 5.5 ATmega328-AU ATmega328-AUR(5) ATmega328-MMH(4) ATmega328-MMHR(4)(5) ATmega328-MU ATmega328-MUR(5) ATmega328-PU 32A 32A 28M1 28M1 32M1-A 32M1-A 28P3 Industrial (-40C to 85C) Note: 1. This device can also be supplied in wafer form. Please contact your local Microchip 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. Please refer to Speed Grades for Speed vs. VCC 4. Tape & Reel. 5. NiPdAu Lead Finish. Package Type 28M1 28-pad, 4 x 4 x 1.0 body, Lead Pitch 0.45mm Quad Flat No-Lead/Micro Lead Frame Package (QFN/ MLF) 28P3 28-lead, 0.300" Wide, Plastic Dual Inline Package (PDIP) 32M1-A 32-pad, 5 x 5 x 1.0 body, Lead Pitch 0.50mm Quad Flat No-Lead/Micro Lead Frame Package (QFN/ MLF) 32A 3.2 32-lead, Thin (1.0mm) Plastic Quad Flat Package (TQFP) ATmega328P Speed [MHz](3) Power Supply [V] Ordering Code(2) Package(1) Operational Range 20 1.8 - 5.5 ATmega328P-AU ATmega328P-AUR(5) ATmega328P-MMH(4) ATmega328P-MMHR(4)(5) ATmega328P-MU 32A 32A 28M1 28M1 32M1-A Industrial (-40C to 85C) (c) 2018 Microchip Technology Inc. Datasheet Complete DS40001984A-page 11 ATmega328/P Ordering Information Speed [MHz](3) Power Supply [V] Ordering Code(2) Package(1) ATmega328P-MUR(5) ATmega328P-PU 32M1-A 28P3 ATmega328P-AN ATmega328P-ANR(5) ATmega328P-MN ATmega328P-MNR(5) ATmega328P-PN 32A 32A 32M1-A 32M1-A 28P3 Operational Range Industrial (-40C to 105C) Note: 1. This device can also be supplied in wafer form. Please contact your local Microchip 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. Please refer to Speed Grades for Speed vs. VCC 4. Tape & Reel. 5. NiPdAu Lead Finish. Package Type 28M1 28-pad, 4 x 4 x 1.0 body, Lead Pitch 0.45mm Quad Flat No-Lead/Micro Lead Frame Package (QFN/ MLF) 28P3 28-lead, 0.300" Wide, Plastic Dual Inline Package (PDIP) 32M1-A 32-pad, 5 x 5 x 1.0 body, Lead Pitch 0.50mm Quad Flat No-Lead/Micro Lead Frame Package (QFN/ MLF) 32A 32-lead, Thin (1.0mm) Plastic Quad Flat Package (TQFP) (c) 2018 Microchip Technology Inc. Datasheet Complete DS40001984A-page 12 ATmega328/P Block Diagram 4. Block Diagram Figure 4-1. Block Diagram SRAM debugWire CPU OCD Clock generation XTAL1 / TOSC1 XTAL2 / TOSC2 32.768kHz XOSC 8MHz Calib RC External clock 16MHz LP XOSC VCC 128kHz int osc Power Supervision POR/BOD & RESET RESET GND ADC6,ADC7,PC[5:0] AREF ADC[7:0] AREF NVM programming Power management and clock control Watchdog Timer ADC FLASH D A T A B U S EEPROM EEPROMIF I/O PORTS I N / O U T PB[7:0] PC[6:0] PD[7:0] GPIOR[2:0] TC 0 D A T A B U S (8-bit) SPI AC Internal Reference T0 OC0A OC0B PD4 PD6 PD5 MISO0 MOSI0 SCK0 SS0 PB4 PB3 PB5 PB2 AIN0 AIN1 PD6 PD7 ADC6, ADC7 PC[5:0] ADCMUX PD[7:0], PC[6:0], PB[7:0] PD3, PD2 PCINT[23:0] INT[1:0] PB1, PB2 PD5 PB0 OC1A/B T1 ICP1 PB3 PD3 OC2A OC2B (c) 2018 Microchip Technology Inc. EXTINT USART RxD0 TxD0 XCK0 PD0 PD1 PD4 TWI SDA0 SCL0 PC4 PC5 TC 1 (16-bit) TC 2 (8-bit async) Datasheet Complete DS40001984A-page 13 ATmega328/P Pin Configurations 5. Pin Configurations 5.1 Pinout Figure 5-1. 28-pin PDIP (PCINT14/RESET) PC6 1 28 PC5 (ADC5/SCL/PCINT13) (PCINT16/RXD) PD0 2 27 PC4 (ADC4/SDA/PCINT12) (PCINT17/TXD) PD1 3 26 PC3 (ADC3/PCINT11) (PCINT18/INT0) PD2 4 25 PC2 (ADC2/PCINT10) (PCINT19/OC2B/INT1) PD3 5 24 PC1 (ADC1/PCINT9) (PCINT20/XCK/T0) PD4 6 23 PC0 (ADC0/PCINT8) VCC 7 22 GND GND 8 21 AREF (PCINT6/XTAL1/TOSC1) PB6 9 20 AVCC (PCINT7/XTAL2/TOSC2) PB7 10 19 PB5 (SCK/PCINT5) (PCINT21/OC0B/T1) PD5 11 18 PB4 (MISO/PCINT4) (PCINT22/OC0A/AIN0) PD6 12 17 PB3 (MOSI/OC2A/PCINT3) (PCINT23/AIN1) PD7 13 16 PB2 (SS/OC1B/PCINT2) (PCINT0/CLKO/ICP1) PB0 14 15 PB1 (OC1A/PCINT1) (c) 2018 Microchip Technology Inc. Datasheet Complete Power Ground Programming/debug Digital Analog Crystal/Osc DS40001984A-page 14 ATmega328/P Pin Configurations PD2 (INT0/PCINT18) PD1 (TXD/PCINT17) PD0 (RXD/PCINT16) PC6 (RESET/PCINT14) PC5 (ADC5/SCL/PCINT13) PC4 (ADC4/SDA/PCINT12) PC3 (ADC3/PCINT11) 28 27 26 25 24 23 22 Figure 5-2. 28-pin MLF Top View Power Ground Programming/debug Digital Analog Crystal/CLK 4 18 GND (PCINT6/XTAL1/TOSC1) PB6 5 17 AREF (PCINT7/XTAL2/TOSC2) PB7 6 16 AVCC (PCINT21/OC0B/T1) PD5 7 15 PB5 (SCK/PCINT5) (c) 2018 Microchip Technology Inc. (PCINT4/MISO) PB4 Bottom pad should be soldered to ground 14 GND 13 PC0 (ADC0/PCINT8) (PCINT3/OC2A/MOSI) PB3 19 12 3 (PCINT2/SS/OC1B) PB2 VCC 11 PC1 (ADC1/PCINT9) (PCINT1/OC1A) PB1 20 10 2 (PCINT0/CLKO/ICP1) PB0 (PCINT20/XCK/T0) PD4 (PCINT23/AIN1) PD7 PC2 (ADC2/PCINT10) 9 21 8 1 (PCINT22/OC0A/AIN0) PD6 (PCINT19/OC2B/INT1) PD3 Datasheet Complete DS40001984A-page 15 ATmega328/P Pin Configurations PC6 (RESET/PCINT14) PC5 (ADC5/SCL/PCINT13) PC4 (ADC4/SDA/PCINT12) 29 28 27 Digital Analog Crystal/CLK PC2 (ADC2/PCINT10) PD0 (RXD/PCINT16) 30 Programming/debug 25 PD1 (TXD/PCINT17) 31 Ground 26 PD2 (INT0/PCINT18) 32 Power PC3 (ADC3/PCINT11) Figure 5-3. 32-pin TQFP Top View GND GND 5 20 AREF VCC 6 19 ADC6 (PCINT6/XTAL1/TOSC1) PB6 7 18 AVCC (PCINT7/XTAL2/TOSC2) PB7 8 17 PB5 (SCK/PCINT5) (PCINT21/OC0B/T1) PD5 (c) 2018 Microchip Technology Inc. 16 21 (PCINT4/MISO) PB4 4 15 VCC (PCINT3/OC2A/MOSI) PB3 ADC7 14 22 (PCINT2/SS/OC1B) PB2 3 13 GND (PCINT1/OC1A) PB1 PC0 (ADC0/PCINT8) 12 23 (PCINT0/CLKO/ICP1) PB0 2 11 (PCINT20/XCK/T0) PD4 (PCINT23/AIN1) PD7 PC1 (ADC1/PCINT9) 10 24 (PCINT22/OC0A/AIN0) PD6 1 9 (PCINT19/OC2B/INT1) PD3 Datasheet Complete DS40001984A-page 16 ATmega328/P Pin Configurations PD2 (INT0/PCINT18) PD1 (TXD/PCINT17) PD0 (RXD/PCINT16) PC6 (RESET/PCINT14) PC5 (ADC5/SCL/PCINT13) PC4 (ADC4/SDA/PCINT12) PC3 (ADC3/PCINT11) PC2 (ADC2/PCINT10) 32 31 30 29 28 27 26 25 Figure 5-4. 32-pin MLF Top View Power Ground Programming/debug Digital Analog Crystal/CLK GND GND 5 20 AREF VCC 6 19 ADC6 (PCINT6/XTAL1/TOSC1) PB6 7 18 AVCC (PCINT7/XTAL2/TOSC2) PB7 8 17 PB5 (SCK/PCINT5) 16 21 (PCINT4/MISO) PB4 4 15 VCC (PCINT3/OC2A/MOSI) PB3 ADC7 14 22 (PCINT2/SS/OC1B) PB2 3 13 GND (PCINT1/OC1A) PB1 PC0 (ADC0/PCINT8) 12 23 (PCINT0/CLKO/ICP1) PB0 2 11 (PCINT20/XCK/T0) PD4 (PCINT23/AIN1) PD7 PC1 (ADC1/PCINT9) 10 24 (PCINT22/OC0A/AIN0) PD6 1 9 (PCINT19/OC2B/INT1) PD3 (PCINT21/OC0B/T1) PD5 Bottom pad should be soldered to ground 5.2 Pin Descriptions 5.2.1 VCC Digital supply voltage pin. 5.2.2 GND Ground. 5.2.3 Port B (PB[7:0]) XTAL1/XTAL2/TOSC1/TOSC2 Port B is an 8-bit bi-directional I/O port with internal pull-up resistors (selected for each pin). The Port B output buffers have symmetrical drive characteristics with both high sink and source capability. As inputs, Port B pins that are externally pulled low will source current if the pull-up resistors are activated. The Port B pins are tri-stated during a Reset condition even if the clock is not running. (c) 2018 Microchip Technology Inc. Datasheet Complete DS40001984A-page 17 ATmega328/P Pin Configurations Depending on the clock selection fuse settings, PB6 can be used as input to the inverting oscillator amplifier and input to the internal clock operating circuit. Depending on the clock selection fuse settings, PB7 can be used as output from the inverting oscillator amplifier. If the internal calibrated RC oscillator is used as chip clock source, PB[7:6] is used as TOSC[2:1] input for the asynchronous timer/counter2 if the AS2 bit in ASSR is set. 5.2.4 Port C (PC[5:0]) Port C is a 7-bit bi-directional I/O port with internal pull-up resistors (selected for each pin). The PC[5:0] output buffers have symmetrical drive characteristics with both high sink and source capability. As inputs, Port C pins that are externally pulled low will source current if the pull-up resistors are activated. The Port C pins are tri-stated during a Reset condition even if the clock is not running. 5.2.5 PC6/RESET If the RSTDISBL fuse is programmed, PC6 is used as an I/O pin. Note that the electrical characteristics of PC6 differ from those of the other pins of Port C. If the RSTDISBL fuse is unprogrammed, PC6 is used as a Reset input. A low level on this pin for longer than the minimum pulse length will generate a Reset, even if the clock is not running. Shorter pulses are not guaranteed to generate a Reset. The various special features of Port C are elaborated in the Alternate Functions of Port C section. 5.2.6 Port D (PD[7:0]) Port D is an 8-bit bi-directional I/O port with internal pull-up resistors (selected for each pin). The Port D output buffers have symmetrical drive characteristics with both high sink and source capability. As inputs, Port D pins that are externally pulled low will source current if the pull-up resistors are activated. The Port D pins are tri-stated during a Reset condition even if the clock is not running. 5.2.7 AVCC AVCC is the supply voltage pin for the A/D Converter (ADC), PC[3:0], and PE[3:2]. It should be externally connected to VCC, even if the ADC is not used. If the ADC is used, it should be connected to VCC through a low-pass filter. Note that PC[6:4] use digital supply voltage, VCC. 5.2.8 AREF AREF is the analog reference pin for the A/D Converter. 5.2.9 ADC[7:6] In the TQFP and VFQFN package, ADC[7:6] serve as analog inputs to the A/D converter. These pins are powered by the analog supply and serve as 10-bit ADC channels. (c) 2018 Microchip Technology Inc. Datasheet Complete DS40001984A-page 18 ATmega328/P I/O Multiplexing 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 USART 0 I2C 0 (32-pin MLF/TQFP) Pin# (28-pin MLF) Pin# (28-pin PIPD) Pin# PAD EXTINT PCINT 1 1 5 PD3 INT1 2 2 6 PD4 4 3 7 VCC 3 4 8 GND 6 - - VCC 5 - - GND 7 5 9 PB6 PCINT6 XTAL1/ TOSC1 8 6 10 PB7 PCINT7 XTAL2/ TOSC2 9 7 11 PD5 PCINT21 OC0B 10 8 12 PD6 PCINT22 AIN0 OC0A 11 9 13 PD7 PCINT23 AIN1 12 10 14 PB0 PCINT0 13 11 15 PB1 PCINT1 OC1A 14 12 16 PB2 PCINT2 OC1B SS0 15 13 17 PB3 PCINT3 OC2A MOSI0 16 14 18 PB4 PCINT4 MISO0 17 15 19 PB5 PCINT5 SCK0 18 16 20 AVCC 19 - - ADC6 20 17 21 AREF 21 18 22 GND 22 - - ADC7 23 19 13 PC0 PCINT8 ADC0 24 20 24 PC1 PCINT9 ADC1 25 21 25 PC2 PCINT10 ADC2 26 22 26 PC3 PCINT11 ADC3 27 23 27 PC4 PCINT12 ADC4 SDA0 28 24 28 PC5 PCINT13 ADC5 SCL0 (c) 2018 Microchip Technology Inc. ADC/AC OSC T/C #0 T/C #1 PCINT19 OC2B PCINT20 T0 CLKO SPI 0 XCK0 T1 ICP1 ADC6 ADC7 Datasheet Complete DS40001984A-page 19 ATmega328/P I/O Multiplexing (28-pin MLF) Pin# (28-pin PIPD) Pin# PAD 29 25 1 PC6/RESET PCINT14 30 26 2 PD0 PCINT16 RXD0 31 27 3 PD1 PCINT17 TXD0 32 28 4 PD2 (c) 2018 Microchip Technology Inc. EXTINT PCINT INT0 ADC/AC OSC T/C #0 T/C #1 USART 0 I2C 0 (32-pin MLF/TQFP) Pin# SPI 0 PCINT18 Datasheet Complete DS40001984A-page 20 ATmega328/P Resources 7. Resources A comprehensive set of development tools, application notes, and datasheets are available for download on http://www.microchip.com/design-centers/8-bit/microchip-avr-mcus. (c) 2018 Microchip Technology Inc. Datasheet Complete DS40001984A-page 21 ATmega328/P Data Retention 8. Data Retention Reliability qualification results show that the projected data retention failure rate is much less than 1 PPM over 20 years at 85C. (c) 2018 Microchip Technology Inc. Datasheet Complete DS40001984A-page 22 ATmega328/P About Code Examples 9. 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. (c) 2018 Microchip Technology Inc. Datasheet Complete DS40001984A-page 23 ATmega328/P Capacitive Touch Sensing 10. 10.1 Capacitive Touch Sensing QTouch Library (R) (R) The QTouch library provides a simple to use solution to realize touch sensitive interfaces on most AVR TM microcontrollers. The QTouch library includes support for the QTouch and QMatrix acquisition methods. Touch sensing can be added to any application by linking the appropriate 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 QTouch Library . For implementation details and other information, refer to the QTouch Library User Guide, also available for download from the Microchip website. (c) 2018 Microchip Technology Inc. Datasheet Complete DS40001984A-page 24 ATmega328/P AVR CPU Core 11. AVR CPU Core 11.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 11-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 these address pointers can be used as an (c) 2018 Microchip Technology Inc. Datasheet Complete DS40001984A-page 25 ATmega328/P AVR CPU Core address pointer for lookup tables in Flash program memory. These added function registers are the 16-bit X-, Y-, and Z-register, described later in this section. The ALU supports arithmetic and logic operations between registers or between a constant and a register. Single register operations can also be executed in the ALU. After an arithmetic operation, the Status register is updated to reflect information about the result of the operation. Program flow is provided by conditional and unconditional jump and call instructions, able to directly address the whole address space. Most AVR instructions have a single 16-bit word format. Every program memory address contains a 16- or 32-bit instruction. Program Flash memory space is divided into 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 Stack Pointer (SP) in the Reset routine (before subroutines or interrupts are executed). The SP is read/write accessible in the I/O space. The data SRAM can easily be accessed through the five different addressing modes supported in the AVR architecture. The memory spaces in the 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. 11.2 Arithmetic Logic Unit (ALU) 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 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 11.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. (c) 2018 Microchip Technology Inc. Datasheet Complete DS40001984A-page 26 ATmega328/P AVR CPU Core 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. (c) 2018 Microchip Technology Inc. Datasheet Complete DS40001984A-page 27 ATmega328/P AVR CPU Core 11.3.1 Status Register Name: Offset: Reset: Property: SREG 0x5F 0x00 When addressing as I/O Register: address offset is 0x3F 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. 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 I-bit is cleared by hardware after an interrupt has occurred, and is set by the RETI instruction to enable subsequent interrupts. The I-bit can also be set and cleared by the application with the SEI and CLI instructions, as described in the instruction set reference. Bit 6 - T 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. (c) 2018 Microchip Technology Inc. Datasheet Complete DS40001984A-page 28 ATmega328/P AVR CPU Core 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. 11.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 11-2. AVR CPU General Purpose Working Registers 7 0 Addr. R0 0x00 R1 0x01 R2 0x02 ... R13 0x0D General R14 0x0E Purpose R15 0x0F Working R16 0x10 Registers R17 0x11 ... R26 0x1A X-register Low Byte R27 0x1B X-register High Byte R28 0x1C Y-register Low Byte R29 0x1D Y-register High Byte R30 0x1E Z-register Low Byte R31 0x1F Z-register High Byte Most of the instructions operating on the register file have direct access to all registers, and most of them are single cycle instructions. As shown in 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. 11.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. (c) 2018 Microchip Technology Inc. Datasheet Complete DS40001984A-page 29 ATmega328/P AVR CPU Core Figure 11-3. The X-, Y-, and Z-registers 15 X-register 0 7 R26 YH YL 0 7 R28 ZH ZL 0 7 R31 0 0 R29 7 0 0 R27 7 15 Z-register XL 7 15 Y-register XH 0 0 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 11.5 Stack Pointer The stack is mainly used for storing temporary data, local variables, and 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 11-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 (c) 2018 Microchip Technology Inc. Datasheet Complete DS40001984A-page 30 ATmega328/P AVR CPU Core 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. (c) 2018 Microchip Technology Inc. Datasheet Complete DS40001984A-page 31 ATmega328/P AVR CPU Core 11.5.1 Stack Pointer Register Low and High byte Name: Offset: Reset: Property: SPL and SPH 0x5D 0x4FF When addressing I/O registers as data space the offset address is 0x3D 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 Timer/Counter 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. Bit 15 14 13 12 11 10 9 8 SP11 SP10 SP9 SP8 Access R R R R RW RW RW RW Reset 0 0 0 0 0 1 0 0 Bit Access Reset 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, 11 - SP Stack Pointer Register SPL and SPH are combined into SP. Related Links Accessing 16-bit Timer/Counter Registers 11.6 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. (c) 2018 Microchip Technology Inc. Datasheet Complete DS40001984A-page 32 ATmega328/P AVR CPU Core Figure 11-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 11-5. Single Cycle ALU Operation T1 T2 T3 T4 clkCPU Total Execution Time Register Operands Fetch ALU Operation Execute Result Write Back 11.7 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 be moved to the start of the boot Flash section by programming the BOOTRST Fuse. 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 (c) 2018 Microchip Technology Inc. Datasheet Complete DS40001984A-page 33 ATmega328/P AVR CPU Core hardware clears the corresponding interrupt flag. Interrupt flags can 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< ... ; Enable interrupts xxx ... When the BOOTRST fuse is unprogrammed, the Boot section size set to 2K bytes 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 0x0000 RESET: 0x0001 0x0002 0x0003 0x0004 0x0005 ; .org 0x3C02 0x3C02 0x3C04 ... 0x3C32 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 2K bytes, the most typical and general program setup for the Reset and Interrupt Vector addresses is: Address Labels .org 0x0002 0x0002 0x0004 ... 0x0032 ; .org 0x3C00 0x3C00 RESET: 0x3C01 0x3C02 0x3C03 0x3C04 0x3C05 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 2K bytes 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 ; .org 0x3C00 0x3C00 0x3C02 0x3C04 ... 0x3C32 ; 0x3C34 RESET: 0x3C35 0x3C36 0x3C37 0x3C38 0x3C39 Code Comments jmp jmp jmp ... jmp RESET EXT_INT0 EXT_INT1 ... SPM_RDY ; Reset handler ; 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 16.2 Register Description 16.2.1 Moving Interrupts Between Application and Boot Space The MCU Control register controls the placement of the interrupt vector table. (c) 2018 Microchip Technology Inc. Datasheet Complete DS40001984A-page 86 ATmega328/P Interrupts 16.2.2 MCU Control Register Name: Offset: Reset: Property: MCUCR 0x55 0x00 When addressing as I/O register: address offset is 0x35 The MCU Control register controls the placement of the interrupt vector table in order to move interrupts between application and boot space. 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. Bit 7 Access Reset 6 5 4 1 0 BODS BODSE PUD 3 2 IVSEL IVCE R/W R/W R/W R/W R/W 0 0 0 0 0 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. Note: BOD disable is only available for ATmega328P. 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. Note: BOD disable is only available for ATmega328P. 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: 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 same cycle as IVCE is written, and interrupts remain disabled until after the instruction following the write (c) 2018 Microchip Technology Inc. Datasheet Complete DS40001984A-page 87 ATmega328/P Interrupts 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 programmed, 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 the 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] > 0x01). 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. 21.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 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. (c) 2018 Microchip Technology Inc. Datasheet Complete DS40001984A-page 186 ATmega328/P Timer/Counter 0, 1 Prescalers Figure 21-1. T1/T0 Pin Sampling D Tn Q D Q D Tn_sync (To Clock Select Logic) Q LE clk I/O Synchronization Edge Detector The synchronization and edge detector logic introduces a delay of 2.5 to 3.5 system clock cycles from an edge has been applied to the T1/T0 pin to the counter is updated. Enabling and disabling of the clock input must be done when T1/T0 has been stable for at least one system clock cycle, otherwise it is a risk that a false timer/counter clock pulse is generated. 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 cannot be prescaled. Figure 21-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. (c) 2018 Microchip Technology Inc. Datasheet Complete DS40001984A-page 187 ATmega328/P Timer/Counter 0, 1 Prescalers 21.4 Register Description (c) 2018 Microchip Technology Inc. Datasheet Complete DS40001984A-page 188 ATmega328/P Timer/Counter 0, 1 Prescalers 21.4.1 General Timer/Counter Control Register Name: Offset: Reset: Property: GTCCR 0x43 0x00 When addressing as I/O register: address offset is 0x23 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. 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/counter 0, 1 prescaler will be Reset. This bit is normally cleared immediately by hardware, except if the TSM bit is set. Note that timer/counter 0, 1 share the same prescaler and a Reset of this prescaler will affect the mentioned timers. (c) 2018 Microchip Technology Inc. Datasheet Complete DS40001984A-page 189 ATmega328/P 8-bit Timer/Counter2 (TC2) with PWM and A... 22. 8-bit Timer/Counter2 (TC2) with PWM and Asynchronous Operation 22.1 Features * * * * * * * 22.2 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 32 kHz Watch Crystal Independent of the I/O Clock Overview Timer/Counter2 (TC2) is a general purpose, 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'. (c) 2018 Microchip Technology Inc. Datasheet Complete DS40001984A-page 190 ATmega328/P 8-bit Timer/Counter2 (TC2) with PWM and A... Figure 22-1. 8-bit Timer/Counter Block Diagram Count Clear Direction TOVn (Int.Req.) Control Logic clkTn Clock Select 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 Pin Descriptions 22.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: (c) 2018 Microchip Technology Inc. Datasheet Complete DS40001984A-page 191 ATmega328/P 8-bit Timer/Counter2 (TC2) with PWM and A... Table 22-1. Definitions Constant Description BOTTOM The counter reaches the BOTTOM when it becomes zero (0x00). 22.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 the timer/ counter uses to increment (or decrement) its value. The timer/counter is inactive when no clock source is selected. The output from the clock select logic is referred to as the timer clock (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. 22.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. 22.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. (c) 2018 Microchip Technology Inc. Datasheet Complete DS40001984A-page 192 ATmega328/P 8-bit Timer/Counter2 (TC2) with PWM and A... Figure 22-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 top clkI/O Table 22-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. 22.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). (c) 2018 Microchip Technology Inc. Datasheet Complete DS40001984A-page 193 ATmega328/P 8-bit Timer/Counter2 (TC2) with PWM and A... The following figure shows a block diagram of the output compare unit. Figure 22-3. Output Compare Unit, Block Diagram DATA BUS OCRnx TCNTn =(8-bit Comparator ) OCFnx (Int.Req.) top bottom Waveform Generator OCnx FOCn WGMn[1:0] COMnx[1:0] The OCR2x 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 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 access may seem complex, but this is not the 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 22.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). 22.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. (c) 2018 Microchip Technology Inc. Datasheet Complete DS40001984A-page 194 ATmega328/P 8-bit Timer/Counter2 (TC2) with PWM and A... 22.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 compare match will be missed, resulting in incorrect waveform generation. Similarly, do not write the TCNT2 value equal to BOTTOM when the counter is counting down. 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 22-4. Compare Match Output Unit, Schematic COMnx[1] COMnx[0] FOCnx Waveform Generator D Q 1 OCnx D DATA BUS 22.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 DDR 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. (c) 2018 Microchip Technology Inc. Datasheet Complete DS40001984A-page 195 ATmega328/P 8-bit Timer/Counter2 (TC2) with PWM and A... 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 22.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 an immediate effect by using the FOC2x strobe bits. 22.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. 22.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. 22.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. (c) 2018 Microchip Technology Inc. Datasheet Complete DS40001984A-page 196 ATmega328/P 8-bit Timer/Counter2 (TC2) with PWM and A... Figure 22-5. CTC Mode, Timing Diagram 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. 22.7.3 Fast PWM Mode The fast Pulse-Width Modulation (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 (c) 2018 Microchip Technology Inc. Datasheet Complete DS40001984A-page 197 ATmega328/P 8-bit Timer/Counter2 (TC2) with PWM and A... illustrating the single-slope operation. The diagram includes non-inverted and inverted PWM outputs. The small horizontal line marks on the TCNT2 slopes represent compare matches between OCR2x and TCNT2. Figure 22-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. (c) 2018 Microchip Technology Inc. Datasheet Complete DS40001984A-page 198 ATmega328/P 8-bit Timer/Counter2 (TC2) with PWM and A... 22.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 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 counting up, and set on the compare match while down-counting. In inverting Output Compare mode, the operation is inverted. The dual-slope operation has lower maximum operation frequency than singleslope 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 in Figure 22-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 22-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 (c) 2018 Microchip Technology Inc. Datasheet Complete DS40001984A-page 199 ATmega328/P 8-bit Timer/Counter2 (TC2) with PWM and A... 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: 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 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 preceding 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. * 22.8 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. 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 22-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. (c) 2018 Microchip Technology Inc. Datasheet Complete DS40001984A-page 200 ATmega328/P 8-bit Timer/Counter2 (TC2) with PWM and A... Figure 22-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 22-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 22-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 22.9 Asynchronous Operation of Timer/Counter2 When TC2 operates asynchronously, some considerations must be taken: (c) 2018 Microchip Technology Inc. Datasheet Complete DS40001984A-page 201 ATmega328/P 8-bit Timer/Counter2 (TC2) with PWM and A... * * * * * * * * 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: 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 wakes 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.768 kHz 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 (c) 2018 Microchip Technology Inc. Datasheet Complete DS40001984A-page 202 ATmega328/P 8-bit Timer/Counter2 (TC2) with PWM and A... * 22.10 rising TOSC1 edge. When waking up from Power-Save mode, and the I/O clock (clkI/O) again becomes active, TCNT2 will read as the previous value (before entering sleep) until the next rising TOSC1 edge. The phase of the TOSC clock after waking up from Power-Save mode is essentially unpredictable, as it depends on the wake-up time. The recommended procedure for reading TCNT2 is thus as follows: 8.1. Wait for the corresponding update busy flag to be cleared. 8.2. Read TCNT2. During asynchronous operation, the synchronization of the interrupt flags for the asynchronous timer takes three 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. Timer/Counter Prescaler Figure 22-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 clk I/O 0 CS20 CS21 CS22 TIMER/COUNTER2 CLOCK SOURCE clk T2 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 the 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.768 kHz 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. 22.11 Register Description (c) 2018 Microchip Technology Inc. Datasheet Complete DS40001984A-page 203 ATmega328/P 8-bit Timer/Counter2 (TC2) with PWM and A... 22.11.1 TC2 Control Register A Name: Offset: Reset: Property: Bit TCCR2A 0xB0 0x00 - 7 6 5 COM2A[1:0] Access Reset 4 3 2 1 COM2B[1:0] 0 WGM2[1:0] R/W R/W R/W R/W R/W R/W 0 0 0 0 0 0 Bits 7:6 - COM2A[1:0] Compare Output Mode for Channel A 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 22-3. Compare Output Mode, Non-PWM COM2A[1] COM2A[0] 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 22-4. Compare Output Mode, Fast PWM(1) COM2A[1] COM2A[0] Description 0 0 Normal port operation, OC2A disconnected. 0 1 WGM2[2:0]: Normal port operation, OC2A disconnected WGM2[2: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 COM2A[1] 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. (c) 2018 Microchip Technology Inc. Datasheet Complete DS40001984A-page 204 ATmega328/P 8-bit Timer/Counter2 (TC2) with PWM and A... The table below shows the COM2A[1:0] bit functionality when the WGM2[2:0] bits are set to phase correct PWM mode. Table 22-5. Compare Output Mode, Phase Correct PWM Mode(1) COM2A[1] COM2A[0] Description 0 0 Normal port operation, OC2A disconnected. 0 1 WGM2[2 :0]: Normal port operation, OC2A disconnected. WGM2[2: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 - COM2B[1:0] Compare Output Mode for Channel B 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 22-6. Compare Output Mode, Non-PWM COM2B[1] COM2B[0] 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 22-7. Compare Output Mode, Fast PWM(1) COM2B[1] COM2B[0] Description 0 0 Normal port operation, OC0B disconnected. 0 1 Reserved (c) 2018 Microchip Technology Inc. Datasheet Complete DS40001984A-page 205 ATmega328/P 8-bit Timer/Counter2 (TC2) with PWM and A... COM2B[1] COM2B[0] Description 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: 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 22-8. Compare Output Mode, Phase Correct PWM Mode(1) COM2B[1] COM2B[0] 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 COM2B[1] 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 - WGM2[1:0] Waveform Generation Mode Combined with the WGM2[2] 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 22-9. Waveform Generation Mode Bit Description Mode WGM2[2] WGM2[1] WGM2[0] Timer/Counter Mode of Operation TOP Update of OCR0x at TOV Flag Set on(1) 0 0 0 0 Normal 0xFF Immediate MAX 1 0 0 1 PWM, Phase Correct 0xFF TOP BOTTOM 2 0 1 0 CTC OCR2A Immediate MAX 3 0 1 1 Fast PWM 0xFF BOTTOM MAX 4 1 0 0 Reserved - - - 5 1 0 1 PWM, Phase Correct OCR2A TOP BOTTOM 6 1 1 0 Reserved - - - 7 1 1 1 Fast PWM OCR2A BOTTOM TOP Note: (c) 2018 Microchip Technology Inc. Datasheet Complete DS40001984A-page 206 ATmega328/P 8-bit Timer/Counter2 (TC2) with PWM and A... 1. 2. MAX = 0xFF BOTTOM = 0x00 (c) 2018 Microchip Technology Inc. Datasheet Complete DS40001984A-page 207 ATmega328/P 8-bit Timer/Counter2 (TC2) with PWM and A... 22.11.2 TC2 Control Register B Name: Offset: Reset: Property: Bit Access Reset TCCR2B 0xB1 0x00 - 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 22-10. Clock Select Bit Description CS22 CS21 CS20 0 0 0 No clock source (Timer/counter stopped). 0 0 1 clkI/O/1 (No prescaling) (c) 2018 Microchip Technology Inc. Description Datasheet Complete DS40001984A-page 208 ATmega328/P 8-bit Timer/Counter2 (TC2) with PWM and A... CS22 CS21 CS20 Description 0 1 0 clkI/O/8 (From prescaler) 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. (c) 2018 Microchip Technology Inc. Datasheet Complete DS40001984A-page 209 ATmega328/P 8-bit Timer/Counter2 (TC2) with PWM and A... 22.11.3 TC2 Counter Value Register Name: Offset: Reset: Property: Bit 7 TCNT2 0xB2 0x00 - 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. (c) 2018 Microchip Technology Inc. Datasheet Complete DS40001984A-page 210 ATmega328/P 8-bit Timer/Counter2 (TC2) with PWM and A... 22.11.4 TC2 Output Compare Register A Name: Offset: Reset: Property: Bit 7 OCR2A 0xB3 0x00 - 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. (c) 2018 Microchip Technology Inc. Datasheet Complete DS40001984A-page 211 ATmega328/P 8-bit Timer/Counter2 (TC2) with PWM and A... 22.11.5 TC2 Output Compare Register B Name: Offset: Reset: Property: Bit 7 OCR2B 0xB4 0x00 - 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. Related Links Timer/Counter Oscillator (c) 2018 Microchip Technology Inc. Datasheet Complete DS40001984A-page 212 ATmega328/P 8-bit Timer/Counter2 (TC2) with PWM and A... 22.11.6 TC2 Interrupt Mask Register Name: Offset: Reset: Property: Bit 7 TIMSK2 0x70 0x00 - 6 5 4 3 Access Reset 2 1 0 OCIE2B OCIE2A TOIE2 R/W R/W R/W 0 0 0 Bit 2 - OCIE2B Timer/Counter 2, Output Compare B Match Interrupt Enable When the OCIE2B 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/Counter 2 occurs, i.e., when the OCF2B bit is set in TIFR2. Bit 1 - OCIE2A Timer/Counter 2, Output Compare A Match Interrupt Enable When the OCIE2A 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/Counter 2 occurs, i.e., when the OCF2A bit is set in TIFR2. Bit 0 - TOIE2 Timer/Counter 2, Overflow Interrupt Enable When the TOIE2 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/Counter 2 occurs, i.e., when the TOV2 bit is set in TIFR2. (c) 2018 Microchip Technology Inc. Datasheet Complete DS40001984A-page 213 ATmega328/P 8-bit Timer/Counter2 (TC2) with PWM and A... 22.11.7 TC2 Interrupt Flag Register Name: Offset: Reset: Property: TIFR2 0x37 0x00 When addressing as I/O Register: address offset is 0x17 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. Bit 7 6 5 4 3 Access Reset 2 1 0 OCF2B OCF2A TOV2 R/W R/W R/W 0 0 0 Bit 2 - OCF2B Timer/Counter 2, Output Compare B Match Flag The OCF2B bit is set (one) when a compare match occurs between the timer/counter2 and the data in Output Compare Register2 (OCR2B). OCF2B is cleared by hardware when executing the corresponding interrupt handling vector. Alternatively, OCF2B is cleared by writing a logic one to the flag. When the I-bit in SREG, OCIE2B (timer/counter2 compare match interrupt enable), and OCF2B are set (one), the timer/ counter2 compare match interrupt is executed. Bit 1 - OCF2A Timer/Counter 2, Output Compare A Match Flag The OCF2A bit is set (one) when a compare match occurs between the timer/counter2 and the data in Output Compare Register2 (OCRA). OCF2A is cleared by hardware when executing the corresponding interrupt handling vector. Alternatively, OCF2A is cleared by writing a logic one to the flag. When the I-bit in SREG, OCIE2A (timer/counter2 compare match interrupt enable), and OCF2A are set (one), the timer/ counter 2 compare match interrupt is executed. Bit 0 - TOV2 Timer/Counter 2, Overflow Flag The TOV2 bit is set (one) when an overflow occurs in Timer/Counter 2. TOV2 is cleared by hardware when executing the corresponding interrupt handling vector. Alternatively, TOV2 is cleared by writing a logic one to the flag. When the SREG I-bit, TOIE2A (timer/counter 2 overflow interrupt enable), and TOV2 are set (one), the timer/counter 2 overflow interrupt is executed. In PWM mode, this bit is set when timer/ counter 2 changes counting direction at 0x00. (c) 2018 Microchip Technology Inc. Datasheet Complete DS40001984A-page 214 ATmega328/P 8-bit Timer/Counter2 (TC2) with PWM and A... 22.11.8 Asynchronous Status Register Name: Offset: Reset: Property: Bit Access Reset 7 ASSR 0xB6 0x00 - 6 5 4 3 2 1 0 EXCLK AS2 TCN2UB OCR2AUB OCR2BUB TCR2AUB TCR2BUB R/W R/W R R R R R 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 32 kHz crystal. Writing to EXCLK should be done before asynchronous operation is selected. Note that the crystal oscillator will run only when this bit is zero. Bit 5 - AS2 Asynchronous Timer/Counter2 When AS2 is written to zero, timer/counter2 is clocked from the I/O clock, clkI/O. When AS2 is written to one, timer/counter2 is clocked from a crystal oscillator connected to the timer oscillator 1 (TOSC1) pin. When the value of AS2 is changed, the contents of TCNT2, OCR2A, OCR2B, TCCR2A, and TCCR2B might be corrupted. Bit 4 - TCN2UB Timer/Counter2 Update Busy When timer/counter2 operates asynchronously and TCNT2 is written, this bit becomes set. When TCNT2 has been updated from the temporary storage register, this bit is cleared by hardware. A logical zero in this bit indicates that TCNT2 is ready to be updated with a new value. Bit 3 - OCR2AUB Output Compare Register2A Update Busy When timer/counter2 operates asynchronously and OCR2A is written, this bit becomes set. When OCR2A has been updated from the temporary storage register, this bit is cleared by hardware. A logical zero in this bit indicates that OCR2A is ready to be updated with a new value. Bit 2 - OCR2BUB Output Compare Register2B 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. (c) 2018 Microchip Technology Inc. Datasheet Complete DS40001984A-page 215 ATmega328/P 8-bit Timer/Counter2 (TC2) with PWM and A... 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. Related Links Timer/Counter Oscillator (c) 2018 Microchip Technology Inc. Datasheet Complete DS40001984A-page 216 ATmega328/P 8-bit Timer/Counter2 (TC2) with PWM and A... 22.11.9 General Timer/Counter Control Register Name: Offset: Reset: Property: GTCCR 0x43 0x00 When addressing as I/O register: address offset is 0x23 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. 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/counter 0, 1 prescaler will be Reset. This bit is normally cleared immediately by hardware, except if the TSM bit is set. Note that timer/counter 0, 1 share the same prescaler and a Reset of this prescaler will affect the mentioned timers. (c) 2018 Microchip Technology Inc. Datasheet Complete DS40001984A-page 217 ATmega328/P Serial Peripheral Interface (SPI) 23. Serial Peripheral Interface (SPI) 23.1 Features * * * * * * * * 23.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 be used in Master SPI mode, refer to chapter USART in SPI Mode. To enable the SPI module, Power Reduction Serial Peripheral Interface bit in the Power Reduction Register (PRR.PRSPI0) must be written to '0'. (c) 2018 Microchip Technology Inc. Datasheet Complete DS40001984A-page 218 ATmega328/P Serial Peripheral Interface (SPI) Figure 23-1. SPI Block Diagram SPI2X SPI2X DIVIDER /2/4/8/16/32/64/128 Note: Refer to the pin-out description and the I/O 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, the software may update the contents of the 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 SPIE in the SPCR register is set, (c) 2018 Microchip Technology Inc. Datasheet Complete DS40001984A-page 219 ATmega328/P Serial Peripheral Interface (SPI) an interrupt is requested. The slave may continue to place new data to be sent to SPDR before reading the incoming data. The last incoming byte will be kept in the Buffer register for later use. Figure 23-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 I/O Ports description. Table 23-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 I/O Ports 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, for example, 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 24.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 unread data exist in the receive buffer, and zero when the receive buffer is empty (i.e., does not contain any unread data). If the Receiver is disabled (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. 24.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 cannot 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 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 (c) 2018 Microchip Technology Inc. Datasheet Complete DS40001984A-page 238 ATmega328/P Universal Synchronous Asynchronous Receiver ... 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 DOR flag indicates data loss due to a receiver buffer full condition. A DOR 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 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 UPE 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. 24.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. 24.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. 24.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 ) { (c) 2018 Microchip Technology Inc. Datasheet Complete DS40001984A-page 239 ATmega328/P Universal Synchronous Asynchronous Receiver ... } unsigned char dummy; while ( UCSR0A & (1< 2 CPU clock cycles for fck < 12 MHz, 3 CPU clock cycles for fck 12 MHz High: > 2 CPU clock cycles for fck < 12 MHz, 3 CPU clock cycles for fck 12 MHz (c) 2018 Microchip Technology Inc. Datasheet Complete DS40001984A-page 359 ATmega328/P Memory Programming (MEMPROG) 31.9.1 Serial Programming Pin Mapping Table 31-15. Pin Mapping Serial Programming Symbol Pins I/O Description MOSI PB3 I Serial Data in MISO PB4 O Serial Data out SCK PB5 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. 31.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. To program and verify the device in the Serial Programming mode, the following sequence is recommended (See serial programming instruction set in Table 31-17: 1. 2. 3. 4. 5. Power-up sequence: Apply power between VCC and GND while RESET and SCK are set to "0". In some systems, the programmer can not assure 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 (c) 2018 Microchip Technology Inc. Datasheet Complete DS40001984A-page 360 ATmega328/P Memory Programming (MEMPROG) 6. 7. 8. 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 31-16. Typical Wait Delay Before Writing the Next Flash or EEPROM Location 31.9.3 Symbol Minimum Wait Delay tWD_FLASH 4.5 ms tWD_EEPROM 3.6 ms tWD_ERASE 10.5 ms tWD_FUSE 4.5 ms Serial Programming Instruction Set This section describes the instruction set. Table 31-17. 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 Instructions Load EEPROM Memory Page (page access) 0xC1 0x00 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 0000 000aa(2) data byte in Instructions(5) Read EEPROM Memory Read Lock bits(3) 0xA0 0000 00aa(2) aaaa aaaa(2) data byte out 0x58 0x00 0x00 data byte out Read Signature Byte 0x30 0x00 0000 000aa(2) data byte out Read Fuse bits(3) 0x50 0x00 0x00 data byte out 0x58 0x08 0x00 data byte out 0x50 0x08 0x00 data byte out 0x38 0x00 0x00 data byte out Read Fuse High bits(3) Read Extended Fuse Bits(3) Read Calibration Byte Write Instructions(5) (c) 2018 Microchip Technology Inc. Datasheet Complete DS40001984A-page 361 ATmega328/P Memory Programming (MEMPROG) Instruction/Operation Instruction Format Byte 1 Write Program Memory Page(6) Write EEPROM Memory Write EEPROM Memory Page (page access) 0x4C 0xC0 0xC2 Byte 2 adr MSB(8) 0000 00aa(2) 0000 00aa(2) Byte 3 adr LSB(8) Byte 4 0x00 aaaa aaaa(2) data byte in aaaa aa00(2) 0x00 bits(3)(4) 0xAC 0xE0 0x00 data byte in Write Fuse bits(3)(4) 0xAC 0xA0 0x00 data byte in 0xAC 0xA8 0x00 data byte in 0xAC 0xA4 0x00 data byte in Write Lock Write Fuse High bits(3)(4) Write Extended Fuse Bits(3)(4) 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. Note: See http://www.microchip.com/design-centers/8-bit/microchip-avr-mcus for application notes regarding programming and programmers. If the LSB in RDY/BSY data byte out is `1', a programming operation is still pending. Wait until this bit returns `0' before the next instruction is carried out. Within the same page, the low data byte must be loaded prior to the high data byte. After data is loaded into the page buffer, program the EEPROM page. Refer to the following figure. Within the same moisture group, the user should not configure all the sensors to the single multi-touch group. (c) 2018 Microchip Technology Inc. Datasheet Complete DS40001984A-page 362 ATmega328/P Memory Programming (MEMPROG) Figure 31-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 Byte 3 Adr MSB Bit 15 B Write Program Memory Page/ Write EEPROM Memory Page Byte 1 Byte 4 Byte 2 Adr LSB Adr MSB Byte 3 Adr LSB Bit 15 B 0 Byte 4 0 Page Buffer Page Offset Page 0 Page 1 Page 2 Page Number Page N-1 Program Memory/ EEPROM Memory 31.9.4 SPI Serial Programming Characteristics Figure 31-8. Serial Programming Waveforms SERIAL DATA INPUT (MOSI) MSB LSB SERIAL DATA OUTPUT (MISO) MSB LSB SERIAL CLOCK INPUT (SCK) SAMPLE (c) 2018 Microchip Technology Inc. Datasheet Complete DS40001984A-page 363 ATmega328/P Electrical Characteristics 32. Electrical Characteristics 32.1 Absolute Maximum Ratings Table 32-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. 32.2 Common DC Characteristics Table 32-2. Common DC characteristics TA = -40C to 105C, VCC = 1.8V to 5.5V (unless otherwise noted) Symbol Parameter Condition Min. VIL Input Low Voltage, except XTAL1 and RESET pin VCC = 1.8V - 2.4V -0.5 0.2VCC(1) V VCC = 2.4V - 5.5V -0.5 0.3VCC(1) 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 VIL1 Input Low Voltage, XTAL1 pin VCC = 1.8V - 5.5V -0.5 0.1VCC(1) V VIH1 Input High Voltage, XTAL1 pin VCC = 1.8V - 2.4V 0.8VCC(2) VCC + 0.5 V VCC = 2.4V - 5.5V 0.7VCC(2) VCC + 0.5 VIL2 Input Low Voltage, RESET pin VCC = 1.8V - 5.5V -0.5 0.1VCC(1) V VIH2 Input High Voltage, RESET pin VCC = 1.8V - 5.5V 0.9VCC(2) VCC + 0.5 V VIL3 Input Low Voltage, RESET pin as I/O VCC = 1.8V - 2.4V -0.5 0.2VCC(1) V VCC = 2.4V - 5.5V -0.5 0.3VCC(1) VIH (c) 2018 Microchip Technology Inc. Datasheet Complete Typ. Max. Units DS40001984A-page 364 ATmega328/P Electrical Characteristics Symbol Parameter Condition Min. VIH3 VCC = 1.8V - 2.4V 0.7VCC(2) VCC + 0.5 V VCC = 2.4V - 5.5V 0.6VCC(2) VCC + 0.5 VOL Input High Voltage, RESET pin as I/O Output Low Voltage(4) except RESET pin IOL = 20mA, VCC = 5V IOL = 10mA, VCC = 3V VOH Output High Voltage(3) except Reset pin Typ. Max. Units TA=85C 0.9 V TA=105C(5) 1.0 V TA=85C 0.6 V TA=105C(5) 0.7 V 4.2 IOH = -20mA, TA=85C TA=105C(5) 4.1 VCC = 5V V 2.3 IOH = -10mA, TA=85C TA=105C(5) 2.1 VCC = 3V V V 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 VCC = 5V <10 40 mV Analog Comparator Input Leakage Current VCC = 5V 50 nA Analog Comparator Propagation Delay VCC = 2.7V 750 VCC = 4.0V 500 IACLK tACPD Vin = VCC/2 -50 Vin = VCC/2 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 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: 3.1. The sum of all IOH, for ports C0 - C5, D0- D4, ADC7, RESET should not exceed 100mA. 3.2. The sum of all IOH, for ports B0 - B5, D5 - D7, ADC6, XTAL1, XTAL2 should not exceed 100mA. (c) 2018 Microchip Technology Inc. Datasheet Complete DS40001984A-page 365 ATmega328/P Electrical Characteristics 4. 5. If IIOH exceeds the test condition, VOH may exceed the related specification. Pins are not guaranteed to source current greater than the listed test condition. 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: 4.1. The sum of all IOL, for ports C0 - C5, ADC7, ADC6 should not exceed 100mA. 4.2. The sum of all IOL, for ports B0 - B5, D5 - D7, XTAL1, XTAL2 should not exceed 100mA. 4.3. The sum of all IOL, for ports D0 - D4, RESET 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. Only for ATmega328P Related Links Minimizing Power Consumption 32.2.1 ATmega328 DC Characteristics - Current Consumption Table 32-3. DC characteristics - TA = -40C to 85C, VCC = 1.8V to 5.5V (unless otherwise noted) Typ.(2) Max. Units T = 85C 0.3 0.5 mA Active 4MHz, VCC = 3V T = 85C 1.7 3.5 Active 8MHz, VCC = 5V T = 85C 5.2 12 Idle 1MHz, VCC = 2V T = 85C 0.04 0.5 Idle 4MHz, VCC = 3V T = 85C 0.3 1.5 Idle 8MHz,VCC = 5V T = 85C 1.2 5.5 32kHz TOSC enabled, VCC = 1.8V T = 85C 0.8 32kHz TOSC enabled, VCC = 3V T = 85C 0.9 WDT enabled, VCC = 3V T = 85C 4.2 15 WDT disabled, VCC = 3V T = 85C 0.1 2 Symbol Parameter Condition ICC Power Supply Current(1) Active 1MHz, VCC = 2V Power-save mode(3) Power-down mode(3) Min. A Note: 1. Values with Minimizing Power Consumption enabled (0xFF). 2. Typical values at 25C. Maximum values are test limits in production. 3. The current consumption values include input leakage current. 32.2.2 ATmega328P DC Characteristics - Current Consumption Table 32-4. ATmega328P DC characteristics - TA = -40C to 85/105C, VCC = 1.8V to 5.5V (unless otherwise noted) Symbol Parameter Condition ICC Power Supply Current(1) Active 1MHz, VCC = 2V (c) 2018 Microchip Technology Inc. Typ.(2) Max. Units T = 85C 0.3 0.5 mA T = 105C 0.3 0.5 Min. Datasheet Complete DS40001984A-page 366 ATmega328/P Electrical Characteristics Symbol Parameter Typ.(2) Max. T = 85C 1.7 2.5 T = 105C 1.7 2.5 T = 85C 5.2 9.0 T = 105C 5.2 9.0 T = 85C 0.04 0.15 T = 105C 0.04 0.15 T = 85C 0.3 0.7 T = 105C 0.3 0.7 T = 85C 1.2 2.7 T = 105C 1.2 2.7 32kHz TOSC enabled, VCC = 1.8V T = 85C 0.8 T = 105C 0.8 32kHz TOSC enabled, VCC = 3V T = 85C 0.9 T = 105C 0.9 WDT enabled, VCC = 3V T = 85C 4.2 8 T = 105C 4.2 10 T = 85C 0.1 2 T = 105C 0.1 5 Condition Min. Active 4MHz, VCC = 3V Active 8MHz, VCC = 5V Idle 1MHz, VCC = 2V Idle 4MHz, VCC = 3V Idle 8MHz,VCC = 5V Power-save mode(3) Power-down mode(3)(4) WDT disabled, VCC = 3V Units A Note: 1. Values with Minimizing Power Consumption enabled (0xFF). 2. Typical values at 25C. Maximum values are test limits in production. 3. The current consumption values include input leakage current. 4. No clock is applied to the pad during power-down mode. 32.3 Speed Grades Maximum frequency is dependent on VCC. As shown in the figure Maximum Frequency vs. VCC, where the curve is linear between 1.8V < VCC < 2.7V and between 2.7V < VCC < 4.5V. (c) 2018 Microchip Technology Inc. Datasheet Complete DS40001984A-page 367 ATmega328/P Electrical Characteristics Figure 32-1. Maximum Frequency vs. VCC 20MHz 10MHz Safe Operating Area 4MHz 1.8V 32.4 2.7V 4.5V 5.5V Clock Characteristics Related Links Calibrated Internal RC Oscillator OSCCAL 32.4.1 Calibrated Internal RC Oscillator Accuracy Table 32-5. Calibration Accuracy of Internal RC Oscillator Frequency VCC Temperature Calibration Accuracy Factory 8.0 MHz Calibration 3.0V 25C 10% User Fixed frequency within: Calibration 7.3 - 8.1 MHz Fixed voltage within: 1.8V - 5.5V Fixed temperature within: -40C to - 85C 1% Related Links OSCCAL 32.4.2 External Clock Drive Waveforms Figure 32-2. External Clock Drive Waveforms V IH1 V IL1 Related Links OSCCAL (c) 2018 Microchip Technology Inc. Datasheet Complete DS40001984A-page 368 ATmega328/P Electrical Characteristics 32.4.3 External Clock Drive Table 32-6. External Clock Drive Symbol Parameter 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 % Related Links OSCCAL 32.5 System and Reset Characteristics Table 32-7. Reset, Brown-out and Internal Voltage Characteristics(1) Symbol Parameter VPOT Condition Min. Typ Max Units Power-on Reset Threshold Voltage (rising) 1.1 1.5 1.7 V Power-on Reset Threshold Voltage (falling)(2) 0.6 1.0 1.7 V SRON Power-on Slope Rate 0.01 - 10 V/ms 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 Note: 1. Values are guidelines only. 2. The Power-on Reset will not work unless the supply voltage has been below VPOT (falling) (c) 2018 Microchip Technology Inc. Datasheet Complete DS40001984A-page 369 ATmega328/P Electrical Characteristics Table 32-8. BODLEVEL Fuse Coding(1)(2) 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 - 000 Reserved Note: 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 assures that a Brown-Out Reset will occur before VCC drops to a voltage where correct operation of the microcontroller is no longer certain. The test is performed using BODLEVEL = 110, 101 and 100. Note: VBOT tested at 25C and 85C in production 32.6 SPI Timing Characteristics Table 32-9. SPI Timing Parameters Description Mode 1 SCK period 2 Min. Typ Max Units Master - See Table. Relationship Between SCK and the Oscillator Frequency in "SPCR - SPI Control Register" - SCK high/low Master - 50% duty cycle - 3 Rise/Fall time Master - 3.6 - 4 Setup Master - 10 - 5 Hold Master - 10 - 6 Out to SCK Master - 0.5 * tsck - 7 SCK to out Master - 10 - 8 SCK to out high Master - 10 - 9 SS low to out Slave - 15 - 10 SCK period Slave 4 * tck - - 11 SCK high/low(1) Slave 2 * tck - - 12 Rise/Fall time Slave - - 1600 13 Setup Slave 10 - - 14 Hold Slave tck - - 15 SCK to out Slave - 15 - 16 SCK to SS high Slave 20 - - (c) 2018 Microchip Technology Inc. Datasheet Complete ns DS40001984A-page 370 ATmega328/P Electrical Characteristics Description Mode Min. 17 SS high to tri-state Slave 18 SS low to SCK Slave Typ Max Units 10 - 2 * tck - - Note: In SPI Programming mode the minimum SCK high/low period is: * 2 * tCLCLCL for fCK < 12MHz * 3 * tCLCL for fCK > 12MHz Figure 32-3. 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 32-4. 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 17 15 MISO (Data Output) 32.7 MSB ... LSB X Two-Wire Serial Interface Characteristics Table in this section describes the requirements for devices connected to the two-wire serial bus. The two-wire serial interface meets or exceeds these requirements under the noted conditions. Timing symbols refer to Figure 32-5. (c) 2018 Microchip Technology Inc. Datasheet Complete DS40001984A-page 371 ATmega328/P Electrical Characteristics Table 32-10. 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) - V VOL(1) Output Low-voltage 0 0.4 V tr(1) Rise Time for both SDA and SCL 20 + 0.1Cb(3)(2) 300 ns tof(1) Output Fall Time from VIHmin to 10 pF < Cb < 400 pF(3) VILmax 20 + 0.1Cb(3)(2) 250 ns tSP(1) Spikes Suppressed by Input Filter 0 50(2) ns Ii Input Current each I/O Pin -10 10 A Ci(1) Capacitance for each I/O Pin - 10 pF fSCL SCL Clock Frequency fCK(4) > max(16fSCL, 250 kHz)(5) 0 400 kHz Rp Value of Pull-up resistor fSCL 100 kHz 1000ns fSCL > 100 kHz CC - 0.4V 3mA Hold Time (repeated) START Condition Low Period of the SCL Clock tHD;STA tLOW tHIGH tSU;STA tHD;DAT tSU;DAT tSU;STO High period of the SCL clock Setup time for a repeated START condition Data hold time Data setup time Setup time for STOP condition (c) 2018 Microchip Technology Inc. 3mA sink current 0.1VCC < Vi < 0.9VCC 4.0 300ns fSCL 100 kHz CC - 0.4V 3mA - s fSCL > 100 kHz 0.6 - s fSCL 100 kHz 4.7 - s fSCL > 100 kHz 1.3 - s fSCL 100 kHz 4.0 - s fSCL > 100 kHz 0.6 - s fSCL 100 kHz 4.7 - s fSCL > 100 kHz 0.6 - s fSCL 100 kHz 0 3.45 s fSCL > 100 kHz 0 0.9 s fSCL 100 kHz 250 - ns fSCL > 100 kHz 100 - ns fSCL 100 kHz 4.0 - s fSCL > 100 kHz 0.6 - s Datasheet Complete DS40001984A-page 372 ATmega328/P Electrical Characteristics Symbol Parameter tBUF Condition Bus free time between a STOP fSCL 100 kHz and START condition fSCL > 100 kHz Min. Max Units 4.7 - s 1.3 - s Note: 1. This parameter is characterized and not 100% tested. 2. Required only for fSCL > 100 kHz. 3. Cb = capacitance of one bus line in pF. 4. fCK = CPU clock frequency. 5. This requirement applies to all two-wire serial interface operation. Other devices connected to the two-wire serial bus need only obey the general fSCL requirement. Figure 32-5. Two-Wire Serial Bus Timing t of t HIGH tr t LOW t LOW SCL t SU;STA t HD;STA t HD;DAT t SU;DAT t SU;STO SDA t BUF 32.8 ADC Characteristics Table 32-11. ADC Characteristics Symbol Parameter Condition Min. Typ Max Units - 10 - Bits VREF = 4V, VCC = 4V, ADC clock = 200 kHz - 2 - LSB VREF = 4V, VCC = 4V, ADC clock = 1 MHz - 4 - LSB VREF = 4V, VCC = 4V, ADC clock = 200 kHz Noise Reduction Mode - 2 - LSB VREF = 4V, VCC = 4V, ADC clock = 1 MHz Noise Reduction Mode - 4 - LSB Integral Non-Linearity (INL) VREF = 4V, VCC = 4V, ADC clock = 200 kHz - 0.5 - LSB Differential Non-Linearity (DNL) VREF = 4V, VCC = 4V, ADC clock = 200 kHz - 0.25 - LSB Resolution Absolute accuracy (Including INL, DNL, quantization error, gain and offset error) (c) 2018 Microchip Technology Inc. Datasheet Complete DS40001984A-page 373 ATmega328/P Electrical Characteristics Symbol Parameter Condition Min. Typ Max Units Gain Error VREF = 4V, VCC = 4V, ADC clock = 200 kHz - 2 - LSB Offset Error VREF = 4V, VCC = 4V, ADC clock = 200 kHz - 2 - LSB 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 VINT Internal Voltage Reference 1.0 1.1 1.2 V RREF Reference Input Resistance - 50 - k RAIN Analog Input Resistance - 100 - kHz M Note: 1. AVCC absolute min./max: 1.8V/5.5V 32.9 Parallel Programming Characteristics Table 32-12. Parallel Programming Characteristics, VCC = 5V 10% Symbol Parameter Min. Max Units VPP Programming Enable Voltage 11.5 12.5 V IPP Programming Enable Current - 250 A tDVXH Data and Control Valid before XTAL1 High 67 - ns tXLXH XTAL1 Low to XTAL1 High 200 - ns tXHXL XTAL1 Pulse Width High 150 - ns tXLDX Data and Control Hold after XTAL1 Low 67 - ns tXLWL XTAL1 Low to WR Low 0 - ns tXLPH XTAL1 Low to PAGEL high 0 - ns tPLXH PAGEL low to XTAL1 high 150 - ns tBVPH BS1 Valid before PAGEL High 67 - ns tPHPL PAGEL Pulse Width High 150 - ns tPLBX BS1 Hold after PAGEL Low 67 - ns tWLBX BS2/1 Hold after RDY/BSY high 67 - ns (c) 2018 Microchip Technology Inc. Datasheet Complete DS40001984A-page 374 ATmega328/P Electrical Characteristics Symbol Parameter Min. Max Units tPLWL PAGEL Low to WR Low 67 - ns tBVWL BS1 Valid to WR Low 67 - ns tWLWH WR Pulse Width Low 150 - ns tWLRL WR Low to RDY/BSY Low 0 1 s tWLRH WR Low to RDY/BSY High(1) 3.2 3.4 ms tWLRH_CE WR Low to RDY/BSY High for Chip Erase(2) 9.8 10.5 ms tXLOL XTAL1 Low to OE Low 0 - ns tBVDV BS1 Valid to DATA valid 0 350 ns tOLDV OE Low to DATA Valid - 350 ns tOHDZ OE High to DATA Tri-stated - 250 ns Note: 1. tWLRH is valid for the Write Flash, Write EEPROM, Write Fuse bits and Write Lock bits commands. 2. tWLRH_CE is valid for the Chip Erase command. Figure 32-6. Parallel Programming Timing, Including some General Timing Requirements tXLWL tXHXL XTAL1 Data & Contol (DATA, XA0/1, BS1, BS2) PAGEL tDVXH tXLDX tBVPH tPLBX t BVWL tWLBX tPHPL tWLWH WR tPLWL WLRL RDY/BSY tWLRH Figure 32-7. Parallel Programming Timing, Loading Sequence with Timing Requirements LOAD ADDRESS (LOW BYTE) LOAD DATA LOAD DATA (HIGH BYTE) LOAD DATA (LOW BYTE) tXLPH t XLXH LOAD ADDRESS (LOW BYTE) tPLXH XTAL1 BS1 PAGEL DATA ADDR0 (Low Byte) DATA (Low Byte) DATA (High Byte) ADDR1 (Low Byte) XA0 XA1 (c) 2018 Microchip Technology Inc. Datasheet Complete DS40001984A-page 375 ATmega328/P Electrical Characteristics Note: The timing requirements shown in Parallel Programming Characteristics (i.e., tDVXH, tXHXL, and tXLDX) also apply to loading operation Figure 32-8. Parallel Programming Timing, Reading Sequence (within the Same Page) with Timing Requirements LOAD ADDRESS (LOW BYTE) READ DATA (LOW BYTE) READ DATA (HIGH BYTE) LOAD ADDRESS (LOW BYTE) tXLOL XTAL1 tBVDV BS1 tOLDV OE DATA tOHDZ ADDR0 (Low Byte) DATA (Low Byte) DATA (High Byte) ADDR1 (Low Byte) XA0 XA1 Note: The timing requirements shown in Parallel Programming Characteristics (i.e., tDVXH, tXHXL, and tXLDX) also apply to reading operation. (c) 2018 Microchip Technology Inc. Datasheet Complete DS40001984A-page 376 ATmega328/P Typical Characteristics (TA = -40C to 85C) 33. Typical Characteristics (TA = -40C to 85C) 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 pullups enabled. A sine wave generator with rail-to-rail output is used as clock source. 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*VCC*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. 33.1 ATmega328 Typical Characteristics 33.1.1 Active Supply Current Figure 33-1. ATmega328: Active Supply Current vs. Low Frequency (0.1MHz - 1.0MHz) 1.2 5.5 V 1 5.0 V ICC (mA) 0.8 4.5 V 4.0 V 0.6 3.3 V 0.4 2.7 V 1.8 V 0.2 0 0 0.1 0.2 0.3 0.4 0.5 0.6 0.7 0.8 0.9 1 Frequency (MHz) (c) 2018 Microchip Technology Inc. Datasheet Complete DS40001984A-page 377 ATmega328/P Typical Characteristics (TA = -40C to 85C) Figure 33-2. ATmega328: Active Supply Current vs. Frequency (1MHz - 20MHz) ICC (mA) 14 5.5V 12 5.0V 10 4.5V 8 4.0 V 6 3.3 V 4 2.7 V 2 1.8 V 0 0 2 4 6 8 10 12 14 16 18 20 Frequency (MHz) Figure 33-3. Active Supply Current vs. VCC (Internal RC Oscillator, 128kHz) 0.16 85 C 25 C -40 C ICC (mA) 0.12 0.08 0.04 0 1.5 2 2.5 3 3.5 4 4.5 5 5.5 VCC (V) (c) 2018 Microchip Technology Inc. Datasheet Complete DS40001984A-page 378 ATmega328/P Typical Characteristics (TA = -40C to 85C) Figure 33-4. Active Supply Current vs. VCC (Internal RC Oscillator, 1MHz) 1.4 85 C 25 C 1.2 -40 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) Figure 33-5. Active Supply Current vs. VCC (Internal RC Oscillator, 8MHz) 8 7 85 C 25 C 6 -40 C ICC (mA) 5 4 3 2 1 0 1.5 2 2.5 3 3.5 4 4.5 5 5.5 VCC (V) (c) 2018 Microchip Technology Inc. Datasheet Complete DS40001984A-page 379 ATmega328/P Typical Characteristics (TA = -40C to 85C) Idle Supply Current Figure 33-6. ATmega328: Idle Supply Current vs. Low Frequency (0.1MHz - 1.0MHz) 0.2 5.5 V ICC (mA) 0.16 5.0 V 4.5 V 0.12 4.0 V 3.3 V 0.08 2.7 V 0.04 1.8 V 0 0 0.1 0.2 0.3 0.4 0.5 0.6 0.7 0.8 0.9 1 Frequency (MHz) Figure 33-7. ATmega328: Idle Supply Current vs. Frequency (1MHz - 20MHz) 4 3.5 I CC (mA) 33.1.2 5.5 V 3 5.0 V 2.5 4.5 V 2 4.0 V 1.5 3.3 V 1 2.7 V 0.5 1.8 V 0 0 2 4 6 8 10 12 14 16 18 20 Frequency (MHz) (c) 2018 Microchip Technology Inc. Datasheet Complete DS40001984A-page 380 ATmega328/P Typical Characteristics (TA = -40C to 85C) Figure 33-8. ATmega328: Idle Supply Current vs. VCC (Internal RC Oscillator, 128kHz) CC 0.06 ICC (mA) 0.05 0.04 85 C 0.03 25 C -40 C 0.02 0.01 0 1.5 2 2.5 3 3.5 4 4.5 5 5.5 VCC (V) Figure 33-9. ATmega328: Idle Supply Current vs. VCC (Internal RC Oscillator, 1MHz) 0.4 85 C 0.35 25 C 0.3 -40 C ICC (mA) 0.25 0.2 0.15 0.1 0.05 0 1.5 2 2.5 3 3.5 4 4.5 5 5.5 VCC (V) (c) 2018 Microchip Technology Inc. Datasheet Complete DS40001984A-page 381 ATmega328/P Typical Characteristics (TA = -40C to 85C) Figure 33-10. ATmega328: Idle Supply Current vs. VCC (Internal RC Oscillator, 8MHz) 2 85 C 1.6 ICC (mA) 25 C -40 C 1.2 0.8 0.4 0 1.5 2 2.5 3 3.5 4 4.5 5 5.5 VCC (V) 33.1.3 Supply Current of IO 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 "Power Reduction Register" for details. Table 33-1. ATmega328: 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 PRUSART0 3.20 22.17 100.25 PRTWI 7.34 46.55 199.25 PRTIM2 7.34 50.79 224.25 PRTIM1 6.19 41.25 176.25 PRTIM0 1.89 14.28 61.13 PRSPI 6.94 43.84 186.50 PRADC 8.66 61.80 295.38 Table 33-2. ATmega328: Additional Current Consumption (percentage) in Active and Idle mode PRR bit Additional Current consumption compared to Active with external clock (see Figure 33-1 and Figure 33-2) Additional Current consumption compared to Idle with external clock (see Figure 33-6 and Figure 33-7) PRUSART0 1.4% 7.8% PRTWI 3.0% 16.6% PRTIM2 3.3% 17.8% (c) 2018 Microchip Technology Inc. Datasheet Complete DS40001984A-page 382 ATmega328/P Typical Characteristics (TA = -40C to 85C) PRR bit Additional Current consumption compared to Active with external clock (see Figure 33-1 and Figure 33-2) Additional Current consumption compared to Idle with external clock (see Figure 33-6 and Figure 33-7) PRTIM1 2.7% 14.5% PRTIM0 0.9% 4.8% PRSPI 2.9% 15.7% PRADC 4.1% 22.1% It is possible to calculate the typical current consumption based on the numbers from the above table for other VCC and frequency settings. Related Links PRR 33.1.3.1 Example Calculate the expected current consumption in idle mode with TIMER1, ADC, and SPI enabled at VCC = 2.0V and F = 1MHz. From Table Additional Current Consumption (percentage) in Active and Idle mode in the previous section, third column, we see that we need to add 14.5% for the TIMER1, 22.1% for the ADC, and 15.7% for the SPI module. Reading from Figure Idle Supply Current vs. Low Frequency (0.1-1.0MHz), we find that the idle current consumption is ~0.045mA at VCC = 2.0V and F = 1MHz. The total current consumption in idle mode with TIMER1, ADC, and SPI enabled, gives: Power-down Supply Current Figure 33-11. ATmega328: Power-Down Supply Current vs. VCC (Watchdog Timer Disabled) CC 1.2 85 C 1 0.8 ICC (uA) 33.1.4 ICCtotal 0.045 mA(1 + 0.145 + 0.221 + 0.157) 0.069 mA 0.6 0.4 0.2 25 C -40 C 0 1.5 2 2.5 3 3.5 4 4.5 5 5.5 VCC (V) (c) 2018 Microchip Technology Inc. Datasheet Complete DS40001984A-page 383 ATmega328/P Typical Characteristics (TA = -40C to 85C) Figure 33-12. ATmega328: Power-Down Supply Current vs. VCC (Watchdog Timer Enabled) 10 9 -40 C 85 C 25 C 8 7 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) Power-save Supply Current Figure 33-13. ATmega328: Power-Save Supply Current vs. VCC (Watchdog Timer Disabled and 32kHz Crystal Oscillator Running) 2 1.8 1.6 25 C 1.4 1.2 ICC (uA) 33.1.5 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) (c) 2018 Microchip Technology Inc. Datasheet Complete DS40001984A-page 384 ATmega328/P Typical Characteristics (TA = -40C to 85C) 33.1.6 Standby Supply Current Figure 33-14. ATmega328: Standby Supply Current vs. Vcc (Watchdog Timer Disabled) 0.16 6MH z _r es 6MHz_xtal 0.14 0.12 4MH z _r es 4MHz_xtal ICC (mA) 0.1 0.08 2MH z _r es 2MHz_xtal 0.06 1MHz_res 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 33-15. ATmega328: I/O Pin Pull-up Resistor Current vs. Input Voltage (VCC = 1.8V) 60 50 40 IOP (uA) 33.1.7 30 20 10 25 C 0 85 C -40 C 0 0.2 0.4 0.6 0.8 1 1.2 1.4 1.6 1.8 2 VOP (V) (c) 2018 Microchip Technology Inc. Datasheet Complete DS40001984A-page 385 ATmega328/P Typical Characteristics (TA = -40C to 85C) Figure 33-16. ATmega328: I/O Pin Pull-up Resistor Current vs. Input Voltage (VCC = 2.7V) 90 80 70 IOP (uA) 60 50 40 30 20 25 C 10 85 C -40 C 0 0 0.5 1 1.5 2 2.5 3 VOP (V) Figure 33-17. ATmega328: I/O Pin Pull-up Resistor Current vs. Input Voltage (VCC = 5V) 160 140 120 IOP (uA) 100 80 60 40 25 C 20 85 C -40 C 0 0 1 2 3 4 5 6 VOP (V) (c) 2018 Microchip Technology Inc. Datasheet Complete DS40001984A-page 386 ATmega328/P Typical Characteristics (TA = -40C to 85C) Figure 33-18. ATmega328: Reset Pull-up Resistor Current vs. Reset Pin Voltage (VCC = 1.8V) 40 35 30 IRESET (uA) 25 20 15 10 25 C 5 85 C -40 C 0 0 0.2 0.4 0.6 0.8 1 1.2 1.4 1.6 1.8 2 VRESET(V) Figure 33-19. ATmega328: Reset Pull-up Resistor Current vs. Reset Pin Voltage (VCC = 2.7V) 70 60 IRESET (uA) 50 40 30 20 25 C 10 85 C -40 C 0 0 0.5 1 1.5 2 2.5 3 VRESET(V) (c) 2018 Microchip Technology Inc. Datasheet Complete DS40001984A-page 387 ATmega328/P Typical Characteristics (TA = -40C to 85C) Figure 33-20. ATmega328: Reset Pull-up Resistor Current vs. Reset Pin Voltage (VCC = 5V) 120 100 IRESET(uA) 80 60 40 25 C 20 85 C -40 C 0 0 1 2 3 4 5 6 VRESET(V) Pin Driver Strength Figure 33-21. I/O Pin Output Voltage vs. Sink Current (VCC = 3V) 1 85 C 0.8 25 C 0.6 V OL (V) 33.1.8 -40 C 0.4 0.2 0 0 5 10 15 20 25 IOL (mA) (c) 2018 Microchip Technology Inc. Datasheet Complete DS40001984A-page 388 ATmega328/P Typical Characteristics (TA = -40C to 85C) Figure 33-22. I/O Pin Output Voltage vs. Sink Current (VCC = 5V) 0.6 85 C 0.5 25 C V OL (V) 0.4 -40 C 0.3 0.2 0.1 0 0 5 10 15 20 25 IOL (mA) Figure 33-23. I/O Pin Output Voltage vs. Source Current (Vcc = 3V) 3.5 3 V OH (V) 2.5 -40 C 25 C 85 C 2 1.5 1 0.5 0 0 5 10 15 20 25 IOH (mA) (c) 2018 Microchip Technology Inc. Datasheet Complete DS40001984A-page 389 ATmega328/P Typical Characteristics (TA = -40C to 85C) Figure 33-24. I/O Pin Output Voltage vs. Source Current (VCC = 5V) 5.1 5 4.9 V OH (V) 4.8 4.7 4.6 -40 C 4.5 25 C 4.4 85 C 4.3 0 5 10 15 20 25 IOH (mA) Pin Threshold and Hysteresis Figure 33-25. I/O Pin Input Threshold Voltage vs. VCC (VIH, I/O Pin read as `1') 4 3.5 -40 C 25 C 85 C 3 Threshold (V) 33.1.9 2.5 2 1.5 1 0.5 0 1.5 2 2.5 3 3.5 4 4.5 5 5.5 VCC (V) (c) 2018 Microchip Technology Inc. Datasheet Complete DS40001984A-page 390 ATmega328/P Typical Characteristics (TA = -40C to 85C) Figure 33-26. I/O Pin Input Threshold Voltage vs. VCC (VIL, I/O Pin read as `0') I/O PIN INPUT THRESHOLD VOLTAGE vs. VCC VIL, IO PIN READ AS '0' 2.5 85 C 25 C -40 C 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) Figure 33-27. I/O Pin Input Hysteresis vs. VCC 0.7 -40 C 25 C 85 C 0.6 Input Hysteresis (V) 0.5 0.4 0.3 0.2 0.1 0 1.5 2 2.5 3 3.5 4 4.5 5 5.5 VCC (V) (c) 2018 Microchip Technology Inc. Datasheet Complete DS40001984A-page 391 ATmega328/P Typical Characteristics (TA = -40C to 85C) Figure 33-28. Reset Input Threshold Voltage vs. VCC (VIH, I/O Pin read as `1') 2.5 -40 C 25 C Threshold (V) 2 85 C 1.5 1 0.5 0 1.5 2 2.5 3 3.5 4 4.5 5 5.5 VCC (V) Figure 33-29. Reset Input Threshold Voltage vs. VCC (VIL, I/O Pin read as `0') 2.5 85 C 25 C Threshold (V) 2 -40 C 1.5 1 0.5 0 1.5 2 2.5 3 3.5 4 4.5 5 5.5 VCC (V) Figure 33-30. Reset Pin Input Hysteresis vs. VCC 0.7 0.6 Input Hysteresis (V) 0.5 0.4 0.3 0.2 -40 C 0.1 25 C 85 C 0 1.5 2 2.5 3 3.5 4 4.5 5 5.5 VCC (V) (c) 2018 Microchip Technology Inc. Datasheet Complete DS40001984A-page 392 ATmega328/P Typical Characteristics (TA = -40C to 85C) 33.1.10 BOD Threshold Figure 33-31. BOD Thresholds vs. Temperature (BODLEVEL is 1.8V) 1.85 1.83 Threshold (V) 1 1.81 0 1.79 1.77 1.75 -60 -40 -20 0 20 40 60 80 100 Temperature (C) Figure 33-32. BOD Thresholds vs. Temperature (BODLEVEL is 2.7V) 2.78 2.76 1 Threshold (V) 2.74 2.72 2.7 2.68 0 2.66 -60 -40 -20 0 20 40 60 80 100 Temperature (C) (c) 2018 Microchip Technology Inc. Datasheet Complete DS40001984A-page 393 ATmega328/P Typical Characteristics (TA = -40C to 85C) Figure 33-33. BOD Thresholds vs. Temperature (BODLEVEL is 4.3V) 4.4 Threshold (V) 4.35 1 4.3 0 4.25 -60 -40 -20 0 20 40 60 80 100 Temperature (C) Figure 33-34. Calibrated Bandgap Voltage vs. Vcc 1.138 Bandgap Voltage (V) 1.136 1.134 25 C 1.132 1.13 1.128 85 C -40 C 1.126 1.124 1.5 2 2.5 3 3.5 4 4.5 5 5.5 Vcc (V) (c) 2018 Microchip Technology Inc. Datasheet Complete DS40001984A-page 394 ATmega328/P Typical Characteristics (TA = -40C to 85C) 33.1.11 Internal Oscillator Speed Figure 33-35. Watchdog Oscillator Frequency vs. Temperature 119 118 117 F RC (kHz) 116 115 114 113 112 2.7 V 111 3.3 V 110 4.0 V 5.5 V 109 -60 -40 -20 0 20 40 60 80 100 Temperature (C) Figure 33-36. Watchdog Oscillator Frequency vs. VCC 120 118 -40 C F RC (kHz) 116 25 C 114 112 110 85 C 108 1.5 2 2.5 3 3.5 4 4.5 5 5.5 VCC (V) (c) 2018 Microchip Technology Inc. Datasheet Complete DS40001984A-page 395 ATmega328/P Typical Characteristics (TA = -40C to 85C) Figure 33-37. Calibrated 8 MHz RC Oscillator Frequency vs. VCC 8.4 85 C 8.2 F RC (MHz) 25 C 8 -40 C 7.8 7.6 7.4 1.5 2 2.5 3 3.5 4 4.5 5 5.5 VCC (V) Figure 33-38. Calibrated 8MHz RC Oscillator Frequency vs. Temperature 8.4 8.3 5.0 V 8.2 3.0 V F RC (MHz) 8.1 8 7.9 7.8 7.7 7.6 -50 -40 -30 -20 -10 0 10 20 30 40 50 60 70 80 90 100 Temperature (C) (c) 2018 Microchip Technology Inc. Datasheet Complete DS40001984A-page 396 ATmega328/P Typical Characteristics (TA = -40C to 85C) Figure 33-39. Calibrated 8MHz RC Oscillator Frequency vs. OSCCAL Value 16 14 85 C 25 C 12 -40 C F RC (MHz) 10 8 6 4 2 0 0 16 32 48 64 80 96 112 128 144 160 176 192 208 224 240 256 OSCCAL (X1) 33.1.12 Current Consumption of Peripheral Units Figure 33-40. ADC Current vs. VCC (AREF = AVCC) 350 -40 C 25 C 85 C 300 ICC (uA) 250 200 150 100 50 0 1.5 2 2.5 3 3.5 4 4.5 5 5.5 VCC (V) (c) 2018 Microchip Technology Inc. Datasheet Complete DS40001984A-page 397 ATmega328/P Typical Characteristics (TA = -40C to 85C) Figure 33-41. Analog Comparator Current vs. VCC 120 100 -40 C 25 C 85 C ICC (uA) 80 60 40 20 0 1.5 2 2.5 3 3.5 4 4.5 5 5.5 VCC (V) Figure 33-42. AREF External Reference Current vs. VCC 180 85 C 25 C -40 C 160 140 ICC (uA) 120 100 80 60 40 20 0 1.5 2 2.5 3 3.5 4 4.5 5 5.5 VCC (V) (c) 2018 Microchip Technology Inc. Datasheet Complete DS40001984A-page 398 ATmega328/P Typical Characteristics (TA = -40C to 85C) Figure 33-43. Brownout Detector Current vs. VCC 30 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 33-44. Programming Current vs. VCC 10 9 25 C 85 C -40 C 8 ICC (mA) 7 6 5 4 3 2 1 0 1.5 2 2.5 3 3.5 4 4.5 5 5.5 VCC (V) (c) 2018 Microchip Technology Inc. Datasheet Complete DS40001984A-page 399 ATmega328/P Typical Characteristics (TA = -40C to 85C) 33.1.13 Current Consumption in Reset and Reset Pulsewidth Figure 33-45. Reset Supply Current vs. Low Frequency (0.1MHz - 1.0MHz) 0.15 5.5 V 5.0 V 4.5 V 0.1 ICC (mA) 4.0 V 3.3 V 2.7 V 0.05 1.8 V 0 0 0.1 0.2 0.3 0.4 0.5 0.6 0.7 0.8 0.9 1 Frequency (MHz) Figure 33-46. Reset Supply Current vs. Frequency (1MHz - 20MHz) 3 5.5 V 2.5 5.0 V 4.5 V ICC (mA) 2 4.0 V 1.5 1 3.3 V 2.7 V 0.5 1.8 V 0 0 2 4 6 8 10 12 14 16 18 20 Frequency (MHz) (c) 2018 Microchip Technology Inc. Datasheet Complete DS40001984A-page 400 ATmega328/P Typical Characteristics (TA = -40C to 85C) Figure 33-47. Minimum Reset Pulse Width vs. Vcc 1800 1600 1400 Pulsewidth (ns) 1200 1000 800 600 400 85 C 25 C -40 C 200 0 1.5 2 2.5 3 3.5 4 4.5 5 5.5 VCC (V) (c) 2018 Microchip Technology Inc. Datasheet Complete DS40001984A-page 401 ATmega328/P Typical Characteristics (TA = -40C to 105C) 34. Typical Characteristics (TA = -40C to 105C) 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 pullups enabled. A sine wave generator with rail-to-rail output is used as clock source. 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*VCC*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 recommended 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. 34.1 ATmega328P Typical Characteristics 34.1.1 Active Supply Current ATmega328P: Active Supply Current vs. Low Frequency (0.1MHz - 1.0MHz) Figure 34-1. ATmega328P: Active Supply Current vs. Low Frequency (0.1MHz - 1.0MHz) 1.2 5.5V 1 ICC (mA) 5.0V 0.8 4.5V 0.6 4.0V 3.6V 0.4 2.7V 1.8V 0.2 0 0.1 0.2 0.3 0.4 0.5 0.6 0.7 0.8 0.9 1 Frequency (MHz) (c) 2018 Microchip Technology Inc. Datasheet Complete DS40001984A-page 402 ATmega328/P Typical Characteristics (TA = -40C to 105C) ATmega328P: Active Supply Current vs. Frequency (1MHz - 20MHz) ICC (mA) Figure 34-2. ATmega328P: Active Supply Current vs. Frequency (1MHz - 20MHz) 14 5.5V 12 5.0V 10 4.5V 8 4.0V 6 3.6V 4 2.7V 2 1.8V 0 0 2 4 6 8 10 12 14 16 18 20 Frequency (MHz) (Internal RC Oscillator, 128kHz) ATmega328P: ActiveSupply Supply Current Current vs. CC Figure 34-3. ATmega328P: Active vs.VV CC (Internal RC Oscillator, 128kHz) 0.16 105C 85C -40C 25C 0.14 0.12 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) (c) 2018 Microchip Technology Inc. Datasheet Complete DS40001984A-page 403 ATmega328/P Typical Characteristics (TA = -40C to 105C) ATmega328P: Active SupplyCurrent Current vs. vs. VVCC RCRC Oscillator, 1MHz) Figure 34-4. ATmega328P: Active Supply (Internal Oscillator, 1MHz) CC(Internal 1.4 105C 85C 25C -40C 1.2 ICC (mA) 1 0.8 0.6 0.4 0.2 1.5 2 2.5 3 3.5 4 4.5 5 5.5 VCC (V) Figure 34-5. ATmega328P: Active Supply vs.VVCCCC(Internal (Internal Oscillator, 8MHz) RCRC Oscillator, 8MHz) ATmega328P: Active SupplyCurrent Current vs. 7 105C 85C 25C -40C 6 ICC (mA) 5 4 3 2 1 1.5 2 2.5 3 3.5 4 4.5 5 5.5 VCC (V) (c) 2018 Microchip Technology Inc. Datasheet Complete DS40001984A-page 404 ATmega328/P Typical Characteristics (TA = -40C to 105C) Idle Supply Current Figure 34-6. ATmega328P: Idle Supply Current vs. Low Frequency (0.1MHz - 1.0MHz) 0.2 5.5V 0.18 0.16 5.0V ICC (mA) 0.14 4.5V 0.12 4.0V 3.6V 0.1 0.08 0.06 2.7V 0.04 1.8V 0.02 0 0 0.2 0.4 0.6 1 0.8 Frequency (MHz) Figure 34-7. ATmega328P: Idle Supply Current vs. Frequency (1MHz - 20MHz) ICC (mA) 34.1.2 3.5 5.5V 3 5.0V 2.5 4.5V 2 4.0V 1.5 3.6V 1 2.7V 0.5 1.8V 0 0 2 4 6 8 10 12 14 16 18 20 Frequency (MHz) (c) 2018 Microchip Technology Inc. Datasheet Complete DS40001984A-page 405 ATmega328/P Typical Characteristics (TA = -40C to 105C) Figure 34-8. ATmega328P: Idle Supply Current vs. VCC (Internal RC Oscillator, 128kHz) 0.05 105C 0.045 85C 0.04 ICC (mA) 0.035 25C -40C 0.03 0.025 0.02 0.015 0.01 0.005 1.5 2 3 2.5 3.5 4 4.5 5 5.5 VCC (V) Figure 34-9. ATmega328P: Idle Supply Current vs. VCC (Internal RC Oscillator, 1MHz) 0.4 105C 85C 25C -40C 0.35 ICC (mA) 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) (c) 2018 Microchip Technology Inc. Datasheet Complete DS40001984A-page 406 ATmega328/P Typical Characteristics (TA = -40C to 105C) Figure 34-10. ATmega328P: Idle Supply Current vs. VCC (Internal RC Oscillator, 8MHz) 1.8 105C 85C 25C -40C 1.6 1.4 ICC (mA) 1.2 1 0.8 0.6 0.4 0.2 1.5 2 2.5 3 3.5 4 4.5 5 5.5 VCC (V) 34.1.3 Supply Current of IO 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 "Power Reduction Register" for details. Table 34-1. ATmega328P: 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 PRUSART0 3.20 22.17 100.25 PRTWI 7.34 46.55 199.25 PRTIM2 7.34 50.79 224.25 PRTIM1 6.19 41.25 176.25 PRTIM0 1.89 14.28 61.13 PRSPI 6.94 43.84 186.50 PRADC 8.66 61.80 295.38 Table 34-2. ATmega328P: Additional Current Consumption (percentage) in Active and Idle mode PRR bit Additional Current consumption compared to Active with external clock (see Figure 34-1 and Figure 34-2) Additional Current consumption compared to Idle with external clock (see Figure 34-6 and Figure 34-7) PRUSART0 1.4% 7.8% PRTWI 3.0% 16.6% PRTIM2 3.3% 17.8% PRTIM1 2.7% 14.5% (c) 2018 Microchip Technology Inc. Datasheet Complete DS40001984A-page 407 ATmega328/P Typical Characteristics (TA = -40C to 105C) PRR bit Additional Current consumption compared to Active with external clock (see Figure 34-1 and Figure 34-2) Additional Current consumption compared to Idle with external clock (see Figure 34-6 and Figure 34-7) PRTIM0 0.9% 4.8% PRSPI 2.9% 15.7% PRADC 4.1% 22.1% It is possible to calculate the typical current consumption based on the numbers from the above table for other VCC and frequency settings. Related Links PRR 34.1.3.1 Example Calculate the expected current consumption in idle mode with TIMER1, ADC, and SPI enabled at VCC = 2.0V and F = 1MHz. From Table Additional Current Consumption (percentage) in Active and Idle mode in the previous section, third column, we see that we need to add 14.5% for the TIMER1, 22.1% for the ADC, and 15.7% for the SPI module. Reading from Figure Idle Supply Current vs. Low Frequency (0.1-1.0MHz), we find that the idle current consumption is ~0.045mA at VCC = 2.0V and F = 1MHz. The total current consumption in idle mode with TIMER1, ADC, and SPI enabled, gives: Power-down Supply Current ATmega328P: Power-Down Supply Current vs. Vvs. Timer Disabled) Figure 34-11. ATmega328P: Power-Down Supply Current VCC (Watchdog Timer Disabled) CC (Watchdog 3 105C 2.5 2 ICC (A) 34.1.4 ICCtotal 0.045 mA(1 + 0.145 + 0.221 + 0.157) 0.069 mA 1.5 85C 1 0.5 25C -40C 0 1.5 2 2.5 3 3.5 4 4.5 5 5.5 VCC (V) (c) 2018 Microchip Technology Inc. Datasheet Complete DS40001984A-page 408 ATmega328/P Typical Characteristics (TA = -40C to 105C) Figure 34-12. ATmega328P: Supply Current vs. VCC (Watchdog Enabled) ATmega328P: Power-Down Power-Down Supply Current vs. V Timer Timer Enabled) CC (Watchdog 10 105C 9 -40C 85C 25C 8 ICC (A) 7 6 5 4 3 2 1.5 2 2.5 3 3.5 4 4.5 5 5.5 VCC (V) Power-save Supply Current Figure 34-13. ATmega328P: Power-Save Supply Current vs. VCC (Watchdog Timer Disabled and 32kHz Crystal Oscillator Running) 4 3.5 105C 3 2.5 ICC (A) 34.1.5 2 85C 1.5 1 25C -40C 0.5 0 1.5 2 2.5 3 3.5 4 4.5 5 5.5 VCC (V) (c) 2018 Microchip Technology Inc. Datasheet Complete DS40001984A-page 409 ATmega328/P Typical Characteristics (TA = -40C to 105C) 34.1.6 Standby Supply Current Figure 34-14. ATmega328P: StandbySupply Supply Current vs. (Watchdog VCC (Watchdog Timer Disabled) ATmega328P: Standby Current vs. Vcc Timer Disabled) 0.15 6 MHz_res 6 MHz_xtal 0.14 0.13 0.12 4 MHz_res 4 MHz_xtal 0.11 ICC (mA) 0.1 0.09 2 MHz_res 2 MHz_xtal 0.08 0.07 1 MHz_res 0.06 0.05 0.04 0.03 0.02 2 2.5 3 3.5 4 4.5 5 5.5 VCC (V) Pin Pull-Up Figure 34-15. ATmega328P: Pull-upResistor Resistor Current vs. Input Voltage CC = 1.8V) ATmega328P: I/O I/O Pin Pin Pull-up Current vs. Input Voltage (VCC =(V 1.8V) 50 45 40 35 IOP (A) 34.1.7 30 25 20 15 25C -40C 85C 105C 10 5 0 0 0.2 0.4 0.6 0.8 1 1.2 1.4 1.6 1.8 2 VOP (V) (c) 2018 Microchip Technology Inc. Datasheet Complete DS40001984A-page 410 ATmega328/P Typical Characteristics (TA = -40C to 105C) Figure 34-16. ATmega328P: Resistor Current vs. Input Voltage ATmega328P:I/O I/OPin Pin Pull-up Pull-up Resistor Current vs. Input Voltage (VCC =(V 2.7V) CC = 2.7V) 80 70 60 IOP (A) 50 40 30 25C 85C -40C 105C 20 10 0 0 0.5 1 1.5 2 2.5 3 VOP (V) Figure 34-17. ATmega328P: I/OI/O Pin Current Input Voltage ATmega328P: PinPull-up Pull-up Resistor Resistor Current vs.vs. Input Voltage (VCC(V = CC 5V)= 5V) 160 140 120 IOP (A) 100 80 60 25C 85C 105C -40C 40 20 0 0 1 2 3 4 5 VOP (V) (c) 2018 Microchip Technology Inc. Datasheet Complete DS40001984A-page 411 ATmega328/P Typical Characteristics (TA = -40C to 105C) Figure 34-18. ATmega328P: Reset Pull-up Resistor Current vs. Reset Pin Voltage (VCC = 1.8V) ATmega328P: Reset Pull-up Resistor Current vs. Reset Pin Voltage (VCC = 1.8V) 40 35 IRESET (A) 30 25 20 15 25C -40C 85C 105C 10 5 0 0 0.2 0.4 0.6 0.8 1 1.2 1.4 1.6 1.8 VRESET (V) Figure 34-19. ATmega328P: Reset Pull-up Resistor Current vs. Reset Pin Voltage (VCC = 2.7V) ATmega328P: Reset Pull-up Resistor Current vs. Reset Pin Voltage (VCC = 2.7V) 60 50 IRESET (A) 40 30 20 25C -40C 85C 105C 10 0 0 0.3 0.6 0.9 1.2 1.5 1.8 2.1 2.4 2.7 VRESET (V) (c) 2018 Microchip Technology Inc. Datasheet Complete DS40001984A-page 412 ATmega328/P Typical Characteristics (TA = -40C to 105C) Figure 34-20. ATmega328P: ResetPull-up Pull-up Resistor Current vs. Reset Pin Voltage (VCC = 5V) ATmega328P: Reset Resistor Current vs. Reset Pin Voltage (V = 5V) CC 120 100 IRESET (A) 80 60 40 25C -40C 85C 105C 20 0 0 0.5 1 1.5 2 2.5 3 3.5 4 4.5 5 VRESET (V) Pin Driver Strength ATmega328P: I/OOutput Pin Output Voltage Sink Current(V (VCC = 3V) 3V) Figure 34-21. ATmega328P: I/O Pin Voltage vs.vs. Sink Current CC = 1 105C 85C 0.9 0.8 25C 0.7 VOL (V) 34.1.8 0.6 -40C 0.5 0.4 0.3 0.2 0.1 0 0 5 10 15 20 IOL (mA) (c) 2018 Microchip Technology Inc. Datasheet Complete DS40001984A-page 413 ATmega328/P Typical Characteristics (TA = -40C to 105C) ATmega328P: I/OOutput Pin Output Voltage SinkCurrent Current (V (VCC Figure 34-22. ATmega328P: I/O Pin Voltage vs.vs. Sink 5V) CC = 5V) 0.7 105C 85C 0.6 VOL (V) 0.5 25C 0.4 -40C 0.3 0.2 0.1 0 0 5 10 15 20 IOL (mA) Figure 34-23. ATmega328P: I/O Pin Output Voltage vs. (Vcc= =3V) 3V) ATmega328P: I/O Pin Output Voltage vs.Source Source Current Current (Vcc 3.1 2.9 VOH (V) 2.7 2.5 -40C 2.3 25C 2.1 85C 105C 1.9 0 5 10 15 20 IOH (mA) (c) 2018 Microchip Technology Inc. Datasheet Complete DS40001984A-page 414 ATmega328/P Typical Characteristics (TA = -40C to 105C) Figure 34-24. ATmega328P: I/O Pin Output Voltage Current(V(V 5V) ATmega328P: I/O Pin Output Voltagevs. vs.Source Source Current == 5V) CCCC 5.1 5 4.9 VOH (V) 4.8 4.7 4.6 -40C 4.5 25C 4.4 85C 105C 4.3 0 5 10 15 20 IOH (mA) Pin Threshold and Hysteresis Figure 34-25. ATmega328P: PinInput InputThreshold Threshold Voltage V IH, I/O (VIHPin , I/O Pinasread ATmega328P: I/O I/O Pin Voltage vs. Vvs. read `1') as `1') CC (VCC 3.1 105C 85C 25C -40C 2.8 2.5 Threshold (V) 34.1.9 2.2 1.9 1.6 1.3 1 1.5 2 2.5 3 3.5 4 4.5 5 5.5 VCC (V) (c) 2018 Microchip Technology Inc. Datasheet Complete DS40001984A-page 415 ATmega328/P Typical Characteristics (TA = -40C to 105C) ATmega328P:I/O I/O Pin Pin Input Voltage vs. V (VCC I/OILPin `0' as `0' Figure 34-26. ATmega328P: InputThreshold Threshold Voltage vs. , I/Oread Pinas read CC V IL, (V Threshold (V) 2.6 105C 2.3 85C 2 25C -40C 1.7 1.4 1.1 0.8 0.5 0.2 1.5 2 2.5 3 3.5 4 4.5 5 5.5 VCC (V) ATmega328P: I/OInput Pin Input Hysteresis vs.VV Figure 34-27. ATmega328P: I/O Pin Hysteresis vs. CC CC 0.8 Input Hysteresis (mV) 0.7 -40C 25C 85C 105C 0.6 0.5 0.4 0.3 0.2 1.8 2.3 2.8 3.3 3.8 4.3 4.8 5.3 VCC (V) (c) 2018 Microchip Technology Inc. Datasheet Complete DS40001984A-page 416 ATmega328/P Typical Characteristics (TA = -40C to 105C) Figure 34-28. ATmega328P: Reset Input Voltage vs. , I/OasPin ATmega328P: Reset Input Threshold Threshold Voltage vs. VCC (VIHV, CC I/O (V PinIHread `1') read as `1') 2.6 -40C Threshold (V) 2.4 25C 2.2 85C 2 105C 1.8 1.6 1.4 1.2 1 0.8 1.5 2 2.5 3 3.5 4 4.5 5 5.5 VCC (V) Figure 34-29. ATmega328P: Reset Input Voltage , I/OasPin (V V, CC I/O (V PinILread `0') read as `0') ATmega328P: Reset Input Threshold Threshold Voltage vs. V vs. CC IL 2.5 105C 85C 25C -40C 2.3 2.1 Threshold (V) 1.9 1.7 1.5 1.3 1.1 0.9 0.7 0.5 1.5 2 2.5 3 3.5 4 4.5 5 5.5 VCC (V) Figure 34-30. ATmega328P: Reset PinReset InputPinHysteresis vs.vs. VCC ATmega328P: Input Hysteresis VCC 0.7 -40C 0.6 Input Hysteresis (V) 0.5 25C 0.4 0.3 85C 0.2 105C 0.1 0 1.5 2 2.5 3 3.5 4 4.5 5 5.5 VCC (V) (c) 2018 Microchip Technology Inc. Datasheet Complete DS40001984A-page 417 ATmega328/P Typical Characteristics (TA = -40C to 105C) 34.1.10 BOD Threshold Figure 34-31. ATmega328P: BOD Thresholds vs.vs. Temperature (BODLEVEL is 1.8V) ATmega328P: BOD Thresholds Temperature (BODLEVEL is 1.8V) 1.84 Rising Vcc Threshold (V) 1.83 1.82 1.81 1.8 Falling Vcc 1.79 1.78 -50 -40 -30 -20 -10 0 10 20 30 40 50 60 70 80 90 100 110 Temperature (C) Figure 34-32. ATmega328P: BODBOD Thresholds vs.vs. Temperature 2.7V) ATmega328P Thresholds Temperature(BODLEVEL (BODLEVEL isis2.7V) 2.78 2.77 Rising Vcc 2.76 Threshold (V) 2.75 2.74 2.73 2.72 2.71 2.7 Falling Vcc 2.69 2.68 2.67 2.66 -40 -30 -20 -10 0 10 20 30 40 50 60 70 80 90 100 110 Temperature (C) (c) 2018 Microchip Technology Inc. Datasheet Complete DS40001984A-page 418 ATmega328/P Typical Characteristics (TA = -40C to 105C) Figure 34-33. ATmega328P: BODBOD Thresholds vs.vs. Temperature 4.3V) ATmega328P Thresholds Temperature (BODLEVEL (BODLEVEL isis4.3V) 4.38 4.36 Rising Vcc Threshold (V) 4.34 4.32 4.3 4.28 Falling Vcc 4.26 4.24 -40 -30 -20 -10 0 10 20 30 40 50 60 70 80 90 100 110 Temperature (C) Figure 34-34. ATmega328P: Calibrated Bandgap Voltage vs. vs. VCC ATmega328P: Calibrated Bandgap Voltage Vcc 1.139 Bandgap Voltage (V) 1.136 25C 1.133 1.13 1.127 85C -40C 1.124 105C 1.121 1.5 2 2.5 3 3.5 4 4.5 5 5.5 Vcc (V) (c) 2018 Microchip Technology Inc. Datasheet Complete DS40001984A-page 419 ATmega328/P Typical Characteristics (TA = -40C to 105C) 34.1.11 Internal Oscillator Speed Figure 34-35. ATmega328P: Watchdog Oscillator Frequency ATmega328P: Watchdog Oscillator Frequencyvs. vs.Temperature Temperature 120 118 FRC (kHz) 116 114 112 2.7V 3.6V 4.0V 5.5V 110 108 106 -40 -30 -20 -10 0 10 20 30 40 50 60 70 80 90 100 110 Temperature (C) ATmega328P C Watchdog Oscillator Frequency vs. Figure 34-36. ATmega328P: Watchdog Oscillator Frequency vs. V CCVCC 120 118 -40C FRC (kHz) 116 25C 114 112 110 85C 108 105C 106 2 2.5 3 3.5 4 4.5 5 5.5 VCC (V) (c) 2018 Microchip Technology Inc. Datasheet Complete DS40001984A-page 420 ATmega328/P Typical Characteristics (TA = -40C to 105C) ATmega328P: Calibrated 8 MHz RC Oscillator Frequencyvs. vs.VVCC CC Figure 34-37. ATmega328P: Calibrated 8 MHz RC Oscillator Frequency 8.6 105C 85C 8.4 FRC (MHz) 8.2 25C 8 -40C 7.8 7.6 7.4 2 2.5 3 3.5 4 4.5 5 5.5 VCC (V) Figure 34-38. ATmega328P: Calibrated Frequency Temperature ATmega328P: Calibrated8MHz 8MHz RC RC Oscillator Oscillator Frequency vs. vs. Temperature 5.5V 5.0V 4.5V 4.0V 3.6V 2.7V 8.4 8.3 8.2 FRC (MHz) 8.1 1.8V 8 7.9 7.8 7.7 7.6 7.5 -40 -30 -20 -10 0 10 20 30 40 50 60 70 80 90 100 110 Temperature (C) (c) 2018 Microchip Technology Inc. Datasheet Complete DS40001984A-page 421 ATmega328/P Typical Characteristics (TA = -40C to 105C) Figure 34-39. ATmega328P: 8MHz RC RCOscillator Oscillator Frequency vs. OSCCAL ATmega328PCalibrated Calibrated 8MHz Frequency vs. OSCCAL ValueValue 14 105C 85C 25C -40C 13 12 FRC (MHz) 11 10 9 8 7 6 5 4 0 16 32 48 64 80 96 112 128 144 160 176 192 208 224 240 OSCCAL (X1) 34.1.12 Current Consumption of Peripheral Units ATmega328P: ADC vs. VCC=(AREF Figure 34-40. ATmega328P: ADC Current vs.Current VCC (AREF AVCC) = AVCC) 160 85C 105C 25C -40C 140 ICC (A) 120 100 80 60 40 1.5 2 2.5 3 3.5 4 4.5 5 5.5 VCC (V) (c) 2018 Microchip Technology Inc. Datasheet Complete DS40001984A-page 422 ATmega328/P Typical Characteristics (TA = -40C to 105C) Figure 34-41. ATmega328P: Analog Comparator Current ATmega328P: Analog Comparator Currentvs. vs.VVCC CC ICC (A) 100 90 -40C 80 25C 85C 105C 70 60 50 40 30 1.5 2 2.5 3 3.5 4 4.5 5 5.5 VCC (V) ATmega328P: External Reference Current VCC Figure 34-42. ATmega328P: AREF AREF External Reference Current vs.vs. VCC 180 85C 105C 25C -40C 160 ICC (A) 140 120 100 80 60 40 1.5 2 2.5 3 3.5 4 4.5 5 5.5 VCC (V) (c) 2018 Microchip Technology Inc. Datasheet Complete DS40001984A-page 423 ATmega328/P Typical Characteristics (TA = -40C to 105C) ATmega328P: Brownout Detector Current Figure 34-43. ATmega328P: Brownout Detector Current vs. vs. VCCVCC 30 105C 85C ICC (A) 25 25C -40C 20 15 10 1.5 2 2.5 3 3.5 4 4.5 5 5.5 VCC (V) ATmega328P: Programming Current vs. VCC Figure 34-44. ATmega328P: Programming Current vs. V CC 10 9 25C 85C 105C -40C 8 ICC (mA) 7 6 5 4 3 2 1 1.5 2 2.5 3 3.5 4 4.5 5 5.5 VCC (V) (c) 2018 Microchip Technology Inc. Datasheet Complete DS40001984A-page 424 ATmega328/P Typical Characteristics (TA = -40C to 105C) 34.1.13 Current Consumption in Reset and Reset Pulsewidth Figure 34-45. ATmega328P: Reset vs.Low Low Frequency (0.1MHz - 1.0MHz) ATmega328P: ResetSupply Supply Current Current vs. Frequency (0.1MHz - 1.0MHz) 0.16 5.5V ICC (mA) 0.14 0.12 5.0V 0.1 4.5V 4.0V 3.6V 0.08 0.06 2.7V 0.04 1.8V 0.02 0 0.1 0.2 0.3 0.4 0.5 0.6 0.7 0.8 0.9 1 Frequency (MHz) Figure 34-46. ATmega328P: Reset Supply Current (1MHz- -20MHz) 20MHz) ATmega328P Reset Supply Currentvs. vs.Frequency Frequency (1MHz 3 5.5V 2.5 5.0V 4.5V ICC (mA) 2 4.0V 1.5 3.6V 1 2.7V 0.5 1.8V 0 0 2 4 6 8 10 12 14 16 18 20 Frequency (MHz) (c) 2018 Microchip Technology Inc. Datasheet Complete DS40001984A-page 425 ATmega328/P Typical Characteristics (TA = -40C to 105C) ATmega328P: Reset Width Pulse Width Vcc Figure 34-47. ATmega328P: MinimumMinimum Reset Pulse vs. Vvs. cc 1800 1600 Pulsewidth (ns) 1400 1200 1000 800 600 105C 85C 25C -40C 400 200 0 1.5 2 2.5 3 3.5 4 4.5 5 5.5 VCC (V) (c) 2018 Microchip Technology Inc. Datasheet Complete DS40001984A-page 426 ATmega328/P Register Summary 35. Register Summary Offset Name Bit Pos. 0x23 PINB 7:0 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 PINC6 PINC5 PINC4 PINC3 PINC2 PINC1 PINC0 0x27 DDRC 7:0 DDRC6 DDRC5 DDRC4 DDRC3 DDRC2 DDRC1 DDRC0 0x28 PORTC 7:0 PORTC6 PORTC5 PORTC4 PORTC3 PORTC2 PORTC1 PORTC0 PINB7 PINB6 PINB5 PINB4 PINB3 PINB2 PINB1 PINB0 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 OCF0B OCF0A TOV0 OCF1B OCF1A TOV1 0x2C ... Reserved 0x34 0x35 TIFR0 7:0 0x36 TIFR1 7:0 0x37 TIFR2 7:0 OCF2B OCF2A TOV2 PCIF2 ICF1 0x38 ... Reserved 0x3A 0x3B PCIFR 7:0 PCIF1 PCIF0 0x3C EIFR 7:0 INTF1 INTF0 0x3D EIMSK 7:0 INT1 INT0 0x3E GPIOR0 7:0 0x3F EECR 7:0 EEPE EERE 0x40 EEDR 7:0 EEDR[7:0] 0x41 EEARL and EEARH 7:0 EEAR[7:0] GPIOR0[7:0] EEPM[1:0] EERIE EEMPE 15:8 EEAR[9:8] 0x43 GTCCR 7:0 0x44 TCCR0A 7:0 0x45 TCCR0B 7:0 0x46 TCNT0 7:0 TCNT0[7:0] 0x47 OCR0A 7:0 OCR0A[7:0] 0x48 OCR0B 7:0 OCR0B[7:0] GPIOR1[7:0] 0x49 Reserved 0x4A GPIOR1 7:0 TSM PSRASY COM0A[1:0] FOC0A COM0B[1:0] FOC0B 0x4B GPIOR2 7:0 0x4C SPCR0 7:0 SPIE0 SPE0 0x4D SPSR0 7:0 SPIF0 WCOL0 0x4E SPDR0 7:0 0x4F Reserved 0x50 ACSR 7:0 0x51 DWDR 7:0 0x52 Reserved 0x53 SMCR PSRSYNC WGM0[1:0] WGM02 CS0[2:0] GPIOR2[7:0] DORD0 MSTR0 CPOL0 CPHA0 SPR0[1:0] SPI2X0 SPID[7:0] ACD ACBG ACO ACI ACIE ACIS[1:0] DWDR[7:0] 7:0 (c) 2018 Microchip Technology Inc. ACIC SM[2:0] Datasheet Complete SE DS40001984A-page 427 ATmega328/P Register Summary Offset Name Bit Pos. 0x54 MCUSR 7:0 0x55 MCUCR 7:0 0x56 Reserved 0x57 SPMCSR WDRF BODS BODSE PUD 7:0 SPMIE RWWSB SIGRD RWWSRE 7:0 SP7 SP6 SP5 SP4 BLBSET BORF EXTRF PORF IVSEL IVCE PGWRT PGERS SPMEN 0x58 ... Reserved 0x5C 0x5D SPL and SPH 15:8 SP3 SP2 SP1 SP0 SP11 SP10 SP9 SP8 N Z C 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 PRTWI0 WDP[2:0] CLKPS[3:0] 0x62 ... Reserved 0x63 0x64 PRR 0x65 Reserved 0x66 OSCCAL 0x67 Reserved 0x68 PCICR 7:0 7:0 PRTIM2 PRTIM0 PRTIM1 7:0 0x69 EICRA 0x6A Reserved 0x6B PCMSK0 7:0 0x6C PCMSK1 7:0 0x6D PCMSK2 7:0 0x6E TIMSK0 7:0 0x6F TIMSK1 7:0 0x70 TIMSK2 7:0 PRSPI0 PRUSART0 PRADC PCIE2 PCIE1 PCIE0 CAL[7:0] ISC1[1:0] ISC0[1:0] PCINT[7:0] PCINT[14:8] PCINT[23:16] ICIE1 OCIE0B OCIE0A TOIE0 OCIE1B OCIE1A TOIE1 OCIE2B OCIE2A TOIE2 0x71 ... Reserved 0x77 0x78 0x78 ADCL and ADCH 7:0 ADC[7:0] 15:8 ADCL and ADCH 7:0 ADC[9:8] ADC[1:0] (ADLAR = 1) 15:8 0x7A ADCSRA 7:0 0x7B ADCSRB 7:0 0x7C ADMUX 7:0 0x7D Reserved 0x7E DIDR0 7:0 0x7F DIDR1 7:0 0x80 TCCR1A 7:0 0x81 TCCR1B 7:0 ICNC1 ICES1 0x82 TCCR1C 7:0 FOC1A FOC1B 0x83 Reserved ADC[9:2] ADEN ADSC ADATE ADIF ADIE ADPS [2:0] ACME REFS[1:0] ADTS[2:0] ADLAR ADC5D COM1A[1:0] (c) 2018 Microchip Technology Inc. MUX[3:0] ADC4D ADC3D COM1B[1:0] WGM13 ADC2D ADC1D ADC0D AIN1D AIN0D WGM1[1:0] WGM12 Datasheet Complete CS1[2:0] DS40001984A-page 428 ATmega328/P Register Summary Offset 0x84 0x86 0x88 0x8A Name Bit Pos. TCNT1L and 7:0 TCNT1[7:0] TCNT1H 15:8 TCNT1[15:8] 7:0 ICR1[7:0] ICR1L and ICR1H 15:8 ICR1[15:8] OCR1AL and 7:0 OCR1A[7:0] OCR1AH 15:8 OCR1A[15:8] OCR1BL and 7:0 OCR1B[7:0] OCR1BH 15:8 OCR1B[15:8] 0x8C ... Reserved 0xAF 0xB0 TCCR2A 7:0 0xB1 TCCR2B 7:0 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 0xB8 TWBR 7:0 0xB9 TWSR 7:0 0xBA TWAR 7:0 COM2A[1:0] FOC2A 7:0 COM2B[1:0] WGM2[1:0] FOC2B WGM22 EXCLK AS2 TCN2UB TWS6 TWS5 TWS4 OCR2AUB CS2[2:0] OCR2BUB TCR2AUB TCR2BUB TWBR [7:0] TWS7 TWS3 TWPS[1:0] TWA[6:0] TWGCE 0xBB TWDR 7:0 0xBC TWCR 7:0 TWINT TWEA TWSTA TWSTO TWD[7:0] TWWC TWEN 0xBD TWAMR 7:0 TWAM6 TWAM5 TWAM4 TWAM3 TWAM2 TWAM1 TWIE TWAM0 0xBE ... Reserved 0xBF 0xC0 UCSR0A 7:0 RXC0 TXC0 UDRE0 FE0 DOR0 UPE0 U2X0 MPCM0 0xC1 UCSR0B 7:0 RXCIE0 TXCIE0 UDRIE0 RXEN0 TXEN0 UCSZ02 RXB80 TXB80 UCSZ01 / UCSZ00 / UDORD0 UCPHA0 0xC2 0xC3 0xC4 UCSR0C UMSEL0 [1:0] UPM0 [1:0] USBS0 UCPOL0 Reserved UBRR0L and 7:0 UBRR0H 15:8 UDR0 7:0 0xC6 35.1 7:0 UBRR0[7:0] UBRR0[11:8] TXB / RXB[7:0] Note 1. 2. 3. For compatibility with future devices, reserved bits should be written to zero if accessed. Reserved I/O memory addresses should never be written. I/O registers within the address range 0x00 - 0x1F are directly bit-accessible using the SBI and CBI instructions. In these registers, the value of single bits can be checked by using the SBIS and SBIC instructions. Some of the Status flags are cleared by writing a logical one to them. Note that, unlike most other AVRs, the CBI and SBI instructions will only operate on the specified bit, and can therefore be used (c) 2018 Microchip Technology Inc. Datasheet Complete DS40001984A-page 429 ATmega328/P Register Summary 4. on registers containing such Status flags. The CBI and SBI instructions work with registers 0x00 to 0x1F only. When using the I/O specific commands IN and OUT, the I/O addresses 0x00 - 0x3F must be used. When addressing I/O registers as data space using LD and ST instructions, 0x20 must be added to these addresses. The ATmega328/P is a complex microcontroller with more peripheral units than can be supported within the 64 location reserved in Opcode for the IN and OUT instructions. For the extended I/O space from 0x60 - 0xFF in SRAM, only the ST/STS/STD and LD/LDS/LDD instructions can be used. (c) 2018 Microchip Technology Inc. Datasheet Complete DS40001984A-page 430 ATmega328/P Instruction Set Summary 36. 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 Direct Jump PC k None 3 IJMP JMP(1) k (c) 2018 Microchip Technology Inc. Datasheet Complete DS40001984A-page 431 ATmega328/P Instruction Set Summary BRANCH INSTRUCTIONS Mnemonics Operands Description Operation Flags #Clocks RCALL k Relative Subroutine Call PC PC + k + 1 None 3 Indirect Call to (Z) PC Z None 3 Direct Subroutine Call PC k None 4 RET Subroutine Return PC STACK None 4 RETI Interrupt Return PC STACK I 4 ICALL CALL(1) k CPSE Rd,Rr Compare, Skip if Equal if (Rd = Rr) PC PC + 2 or 3 None 1/2/3 CP Rd,Rr Compare Rd - Rr Z, N,V,C,H 1 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 (c) 2018 Microchip Technology Inc. Datasheet Complete DS40001984A-page 432 ATmega328/P Instruction Set Summary 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 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 (c) 2018 Microchip Technology Inc. Datasheet Complete DS40001984A-page 433 ATmega328/P Instruction Set Summary DATA TRANSFER INSTRUCTIONS Mnemonics Operands Description Operation Flags #Clocks 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 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 (c) 2018 Microchip Technology Inc. Datasheet Complete DS40001984A-page 434 ATmega328/P Instruction Set Summary MCU CONTROL INSTRUCTIONS Mnemonics Operands Description Operation Flags #Clocks WDR Watchdog Reset (see specific descr. for WDR/timer) None 1 BREAK Break For On-chip Debug Only None N/A (c) 2018 Microchip Technology Inc. Datasheet Complete DS40001984A-page 435 ATmega328/P Packaging Information 37. Packaging Information 37.1 32-pin 32A Note: Note: For the most current package drawings, see the Microchip Packaging Specification located at http:// www.microchip.com/packaging PIN 1 IDENTIFIER PIN 1 e B E1 E D1 D C 0~7 L A1 A2 A COMMON DIMENSIONS (Unit of measure = mm) SYMBOL Notes: 1. This package conforms to JEDEC reference MS-026, Variation ABA. 2. Dimensions D1 and E1 do not include mold protrusion. Allowable protrusion is 0.25mm per side. Dimensions D1 and E1 are maximum plastic body size dimensions including mold mismatch. 3. Lead coplanarity is 0.10mm maximum. MIN NOM MAX A - - 1.20 A1 0.05 - 0.15 A2 0.95 1.00 1.05 D 8.75 9.00 9.25 D1 6.90 7.00 7.10 E 8.75 9.00 9.25 E1 6.90 7.00 7.10 - 0.45 B 0.30 C 0.09 - 0.20 L 0.45 - 0.75 e NOTE Note 2 Note 2 0.80 TYP 2010-10-20 TITLE 32A, 32-lead, 7 x 7mm body size, 1.0mm body thickness, 0.8mm lead pitch, thin profile plastic quad flat package (TQFP) (c) 2018 Microchip Technology Inc. Datasheet Complete DRAWING NO. REV. 32A C DS40001984A-page 436 ATmega328/P Packaging Information 37.2 32-pin 32M1-A Note: Note: For the most current package drawings, see the Microchip Packaging Specification located at http:// www.microchip.com/packaging D D1 1 2 3 0 Pin 1 ID E1 SIDE VIEW E TOP VIEW A2 A3 A1 A K 0.08 C P D2 SYMBOL MIN NOM MAX A 0.80 0.90 1.00 A1 - 0.02 0.05 A2 - 0.65 1.00 1 2 3 P Pin #1 Notch (0.20 R) A3 E2 K e b COMMON DIMENSIONS (Unit of Measure = mm) L BOTTOM VIEW 0.20 REF b 0.18 0.23 0.30 D 4.90 5.00 5.10 D1 4.70 4.75 4.80 D2 2.95 3.10 3.25 E 4.90 5.00 5.10 E1 4.70 4.75 4.80 E2 2.95 3.10 3.25 e Note: JEDEC Standard MO-220, Fig. 2 (Anvil Singulation), VHHD-2. NOTE 0.50 BSC L 0.30 0.40 0.50 P - - 0 - - 0.60 o 12 K 0.20 - - 03/14/2014 32M1-A , 32-pad, 5 x 5 x 1.0mm Body, Lead Pitch 0.50mm, 3.10mm Exposed Pad, Micro Lead Frame Package (MLF) (c) 2018 Microchip Technology Inc. Datasheet Complete 32M1-A F DS40001984A-page 437 ATmega328/P Packaging Information 37.3 28-pin 28M1 Note: For the most current package drawings, see the Microchip Packaging Specification located at http:// www.microchip.com/packaging D C 1 2 Pin 1 ID 3 E SIDEVIEW A1 TOP VIEW A y D2 K 1 0.45 2 R0.20 3 E2 b L e 0.4 Ref (4x) BOTTOM VIEW i nal #1 ID is a Laser -m a r ked Feat ur e . Note: The ter m COMMON DIMENSIONS (Unit of Measure = mm) SYMBOL MIN A A1 b C D D2 E E2 e L y K 0.80 0.00 0.17 3.95 2.35 3.95 2.35 0.35 0.00 0.20 NOM MAX NOTE 0.90 1.00 0.02 0.05 0.22 0.27 0.20 REF 4.00 4.05 2.40 2.45 4.00 4.05 2.40 2.45 0.45 0.40 0.45 - 0.08 - - 10/24/08 Package Drawing Contact: packagedrawings@atmel.com 37.4 TITLE 28M1,28-pad,4 x 4 x 1.0mm Body, Lead Pitch 0.45mm, 2.4 x 2.4mm Exposed Pad, Thermally Enhanced Plastic Very Thin Quad Flat No Lead Package (VQFN) GPC ZBV DRAWING NO. 28M1 REV. B 28-pin 28P3 Note: (c) 2018 Microchip Technology Inc. Datasheet Complete DS40001984A-page 438 ATmega328/P Packaging Information Note: For the most current package drawings, see the Microchip Packaging Specification located at http:// www.microchip.com/packaging D PIN 1 E1 A SEATING PLANE L B2 B1 A1 B (4 PLACES) 0 ~ 15 REF e E C COMMON DIMENSIONS (Unit of Measure = mm) Note: NOM MAX - 4.5724 A1 0.508 - - D 34.544 - 34.798 E 7.620 - 8.255 E1 7.112 - 7.493 B 0.381 - 0.533 B1 1.143 - 1.397 B2 0.762 - 1.143 L 3.175 - 3.429 C 0.203 - 0.356 eB - - 10.160 A 1. Dimensions D and E1 do not include mold Flash or Protrusion. Mold Flash or Protrusion shall not exceed 0.25mm (0.010"). MIN - SYMBOL eB e NOTE Note 1 Note 1 2.540 TYP 09/28/01 TITLE 2325 Orchard Parkway 28P3, 28-lead (0.300"/7.62mm Wide) Plastic Dual San Jose, CA 95131 Inline Package (PDIP) (c) 2018 Microchip Technology Inc. Datasheet Complete DRAWING NO. REV. 28P3 B DS40001984A-page 439 ATmega328/P Errata 38. Errata 38.1 Errata ATmega328/P The revision letter in this section refers to the revision of the ATmega328/P device. 38.1.1 Rev. D 1 - Analog MUX can be turned off when setting ACME bit If the ACME (Analog Comparator Multiplexer Enabled) bit in ADCSRB is set while MUX3 in ADMUX is '1' (ADMUX[3:0]=1xxx), all MUXes are turned off until the ACME bit is cleared. Fix/Workaround: Clear the MUX3 bit before setting the ACME bit. 2 - TWI Data setup time can be too short When running the device as a TWI slave with a system clock above 2MHz, the data setup time for the first bit after ACK may in some cases be too short. This may cause a false start or stop condition on the TWI line. Fix/Workaround: Insert a delay between setting TWDR and TWCR. 38.1.2 Rev. C Not sampled. 38.1.3 Rev. B 1 - Analog MUX can be turned off when setting ACME bit If the ACME (Analog Comparator Multiplexer Enabled) bit in ADCSRB is set while MUX3 in ADMUX is '1' (ADMUX[3:0]=1xxx), all MUXes are turned off until the ACME bit is cleared. Fix/Workaround: Clear the MUX3 bit before setting the ACME bit. 2 - Unstable 32kHz Oscillator The 32kHz oscillator does not work as system clock. The 32kHz oscillator used as asynchronous timer is inaccurate. Fix/Workaround: None. 38.1.4 Rev. A 1 - Unstable 32kHz Oscillator The 32kHz oscillator does not work as system clock. The 32kHz oscillator used as asynchronous timer is inaccurate. Fix/Workaround: None. (c) 2018 Microchip Technology Inc. Datasheet Complete DS40001984A-page 440 ATmega328/P Datasheet Revision History 39. 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. 39.1 Rev. A - 2/2018 Section Changes Full data sheet * * * Change of document style New Microchip document number DS40001984A replaces Atmel 42735B Added bit numbering in registers where this was missing SPL and SPH * Added SP11 System Clock and Clock Options * Added input to the Clock Multiplexer from the Watchdog Oscillator in Figure 13-1 Added warning not to use Watchdog Oscillator (128 kHz Internal Oscillator) as both Watchdog Timer and System Oscillator at the same time Corrected capacitances in Table 13-8 * * 39.2 Pre Microchip Revisions 39.2.1 Rev. B - 11/2016 1. 2. Update I/O Multiplexing Errata section updated - ATmega328P: Removed die revision E to K: - * Die revision E to J was not sampled. * Die revision K was not released to production. ATmega328: Removed die revision E to K: * * 39.2.2 Die revision E to J was not sampled. Die revision K was not released to production. Rev. A - 06/2016 Initial document release. (c) 2018 Microchip Technology Inc. Datasheet Complete DS40001984A-page 441 ATmega328/P The Microchip Web Site Microchip provides online support via our web site at http://www.microchip.com/. This web site is used as a means to make files and information easily available to customers. Accessible by using your favorite Internet browser, the web site contains the following information: * * * Product Support - Data sheets and errata, application notes and sample programs, design resources, user's guides and hardware support documents, latest software releases and archived software General Technical Support - Frequently Asked Questions (FAQ), technical support requests, online discussion groups, Microchip consultant program member listing Business of Microchip - Product selector and ordering guides, latest Microchip press releases, listing of seminars and events, listings of Microchip sales offices, distributors and factory representatives Customer Change Notification Service Microchip's customer notification service helps keep customers current on Microchip products. Subscribers will receive e-mail notification whenever there are changes, updates, revisions or errata related to a specified product family or development tool of interest. To register, access the Microchip web site at http://www.microchip.com/. Under "Support", click on "Customer Change Notification" and follow the registration instructions. Customer Support Users of Microchip products can receive assistance through several channels: * * * * Distributor or Representative Local Sales Office Field Application Engineer (FAE) Technical Support Customers should contact their distributor, representative or Field Application Engineer (FAE) for support. Local sales offices are also available to help customers. A listing of sales offices and locations is included in the back of this document. Technical support is available through the web site at: http://www.microchip.com/support Microchip Devices Code Protection Feature Note the following details of the code protection feature on Microchip devices: * * * * Microchip products meet the specification contained in their particular Microchip Data Sheet. Microchip believes that its family of products is one of the most secure families of its kind on the market today, when used in the intended manner and under normal conditions. There are dishonest and possibly illegal methods used to breach the code protection feature. All of these methods, to our knowledge, require using the Microchip products in a manner outside the operating specifications contained in Microchip's Data Sheets. Most likely, the person doing so is engaged in theft of intellectual property. Microchip is willing to work with the customer who is concerned about the integrity of their code. (c) 2018 Microchip Technology Inc. Datasheet Complete DS40001984A-page 442 ATmega328/P * Neither Microchip nor any other semiconductor manufacturer can guarantee the security of their code. Code protection does not mean that we are guaranteeing the product as "unbreakable." Code protection is constantly evolving. We at Microchip are committed to continuously improving the code protection features of our products. Attempts to break Microchip's code protection feature may be a violation of the Digital Millennium Copyright Act. If such acts allow unauthorized access to your software or other copyrighted work, you may have a right to sue for relief under that Act. Legal Notice Information contained in this publication regarding device applications and the like is provided only for your convenience and may be superseded by updates. It is your responsibility to ensure that your application meets with your specifications. MICROCHIP MAKES NO REPRESENTATIONS OR WARRANTIES OF ANY KIND WHETHER EXPRESS OR IMPLIED, WRITTEN OR ORAL, STATUTORY OR OTHERWISE, RELATED TO THE INFORMATION, INCLUDING BUT NOT LIMITED TO ITS CONDITION, QUALITY, PERFORMANCE, MERCHANTABILITY OR FITNESS FOR PURPOSE. Microchip disclaims all liability arising from this information and its use. Use of Microchip devices in life support and/or safety applications is entirely at the buyer's risk, and the buyer agrees to defend, indemnify and hold harmless Microchip from any and all damages, claims, suits, or expenses resulting from such use. No licenses are conveyed, implicitly or otherwise, under any Microchip intellectual property rights unless otherwise stated. Trademarks The Microchip name and logo, the Microchip logo, AnyRate, AVR, AVR logo, AVR Freaks, BeaconThings, BitCloud, CryptoMemory, CryptoRF, dsPIC, FlashFlex, flexPWR, Heldo, JukeBlox, KeeLoq, KeeLoq logo, Kleer, LANCheck, LINK MD, maXStylus, maXTouch, MediaLB, megaAVR, MOST, MOST logo, MPLAB, OptoLyzer, PIC, picoPower, PICSTART, PIC32 logo, Prochip Designer, QTouch, RightTouch, SAM-BA, SpyNIC, SST, SST Logo, SuperFlash, tinyAVR, UNI/O, and XMEGA are registered trademarks of Microchip Technology Incorporated in the U.S.A. and other countries. ClockWorks, The Embedded Control Solutions Company, EtherSynch, Hyper Speed Control, HyperLight Load, IntelliMOS, mTouch, Precision Edge, and Quiet-Wire are registered trademarks of Microchip Technology Incorporated in the U.S.A. Adjacent Key Suppression, AKS, Analog-for-the-Digital Age, Any Capacitor, AnyIn, AnyOut, BodyCom, chipKIT, chipKIT logo, CodeGuard, CryptoAuthentication, CryptoCompanion, CryptoController, dsPICDEM, dsPICDEM.net, Dynamic Average Matching, DAM, ECAN, EtherGREEN, In-Circuit Serial Programming, ICSP, Inter-Chip Connectivity, JitterBlocker, KleerNet, KleerNet logo, Mindi, MiWi, motorBench, MPASM, MPF, MPLAB Certified logo, MPLIB, MPLINK, MultiTRAK, NetDetach, Omniscient Code Generation, PICDEM, PICDEM.net, PICkit, PICtail, PureSilicon, QMatrix, RightTouch logo, REAL ICE, Ripple Blocker, SAM-ICE, Serial Quad I/O, SMART-I.S., SQI, SuperSwitcher, SuperSwitcher II, Total Endurance, TSHARC, USBCheck, VariSense, ViewSpan, WiperLock, Wireless DNA, and ZENA are trademarks of Microchip Technology Incorporated in the U.S.A. and other countries. SQTP is a service mark of Microchip Technology Incorporated in the U.S.A. Silicon Storage Technology is a registered trademark of Microchip Technology Inc. in other countries. GestIC is a registered trademark of Microchip Technology Germany II GmbH & Co. KG, a subsidiary of Microchip Technology Inc., in other countries. All other trademarks mentioned herein are property of their respective companies. (c) 2018 Microchip Technology Inc. Datasheet Complete DS40001984A-page 443 ATmega328/P (c) 2018, Microchip Technology Incorporated, Printed in the U.S.A., All Rights Reserved. ISBN: 978-1-5224-2686-8 Quality Management System Certified by DNV ISO/TS 16949 Microchip received ISO/TS-16949:2009 certification for its worldwide headquarters, design and wafer fabrication facilities in Chandler and Tempe, Arizona; Gresham, Oregon and design centers in California (R) (R) and India. The Company's quality system processes and procedures are for its PIC MCUs and dsPIC (R) DSCs, KEELOQ code hopping devices, Serial EEPROMs, microperipherals, nonvolatile memory and analog products. In addition, Microchip's quality system for the design and manufacture of development systems is ISO 9001:2000 certified. (c) 2018 Microchip Technology Inc. Datasheet Complete DS40001984A-page 444 Worldwide Sales and Service AMERICAS ASIA/PACIFIC ASIA/PACIFIC EUROPE Corporate Office 2355 West Chandler Blvd. Chandler, AZ 85224-6199 Tel: 480-792-7200 Fax: 480-792-7277 Technical Support: http://www.microchip.com/ support Web Address: www.microchip.com Atlanta Duluth, GA Tel: 678-957-9614 Fax: 678-957-1455 Austin, TX Tel: 512-257-3370 Boston Westborough, MA Tel: 774-760-0087 Fax: 774-760-0088 Chicago Itasca, IL Tel: 630-285-0071 Fax: 630-285-0075 Dallas Addison, TX Tel: 972-818-7423 Fax: 972-818-2924 Detroit Novi, MI Tel: 248-848-4000 Houston, TX Tel: 281-894-5983 Indianapolis Noblesville, IN Tel: 317-773-8323 Fax: 317-773-5453 Tel: 317-536-2380 Los Angeles Mission Viejo, CA Tel: 949-462-9523 Fax: 949-462-9608 Tel: 951-273-7800 Raleigh, NC Tel: 919-844-7510 New York, NY Tel: 631-435-6000 San Jose, CA Tel: 408-735-9110 Tel: 408-436-4270 Canada - Toronto Tel: 905-695-1980 Fax: 905-695-2078 Australia - Sydney Tel: 61-2-9868-6733 China - Beijing Tel: 86-10-8569-7000 China - Chengdu Tel: 86-28-8665-5511 China - Chongqing Tel: 86-23-8980-9588 China - Dongguan Tel: 86-769-8702-9880 China - Guangzhou Tel: 86-20-8755-8029 China - Hangzhou Tel: 86-571-8792-8115 China - Hong Kong SAR Tel: 852-2943-5100 China - Nanjing Tel: 86-25-8473-2460 China - Qingdao Tel: 86-532-8502-7355 China - Shanghai Tel: 86-21-3326-8000 China - Shenyang Tel: 86-24-2334-2829 China - Shenzhen Tel: 86-755-8864-2200 China - Suzhou Tel: 86-186-6233-1526 China - Wuhan Tel: 86-27-5980-5300 China - Xian Tel: 86-29-8833-7252 China - Xiamen Tel: 86-592-2388138 China - Zhuhai Tel: 86-756-3210040 India - Bangalore Tel: 91-80-3090-4444 India - New Delhi Tel: 91-11-4160-8631 India - Pune Tel: 91-20-4121-0141 Japan - Osaka Tel: 81-6-6152-7160 Japan - Tokyo Tel: 81-3-6880- 3770 Korea - Daegu Tel: 82-53-744-4301 Korea - Seoul Tel: 82-2-554-7200 Malaysia - Kuala Lumpur Tel: 60-3-7651-7906 Malaysia - Penang Tel: 60-4-227-8870 Philippines - Manila Tel: 63-2-634-9065 Singapore Tel: 65-6334-8870 Taiwan - Hsin Chu Tel: 886-3-577-8366 Taiwan - Kaohsiung Tel: 886-7-213-7830 Taiwan - Taipei Tel: 886-2-2508-8600 Thailand - Bangkok Tel: 66-2-694-1351 Vietnam - Ho Chi Minh Tel: 84-28-5448-2100 Austria - Wels Tel: 43-7242-2244-39 Fax: 43-7242-2244-393 Denmark - Copenhagen Tel: 45-4450-2828 Fax: 45-4485-2829 Finland - Espoo Tel: 358-9-4520-820 France - Paris Tel: 33-1-69-53-63-20 Fax: 33-1-69-30-90-79 Germany - Garching Tel: 49-8931-9700 Germany - Haan Tel: 49-2129-3766400 Germany - Heilbronn Tel: 49-7131-67-3636 Germany - Karlsruhe Tel: 49-721-625370 Germany - Munich Tel: 49-89-627-144-0 Fax: 49-89-627-144-44 Germany - Rosenheim Tel: 49-8031-354-560 Israel - Ra'anana Tel: 972-9-744-7705 Italy - Milan Tel: 39-0331-742611 Fax: 39-0331-466781 Italy - Padova Tel: 39-049-7625286 Netherlands - Drunen Tel: 31-416-690399 Fax: 31-416-690340 Norway - Trondheim Tel: 47-7289-7561 Poland - Warsaw Tel: 48-22-3325737 Romania - Bucharest Tel: 40-21-407-87-50 Spain - Madrid Tel: 34-91-708-08-90 Fax: 34-91-708-08-91 Sweden - Gothenberg Tel: 46-31-704-60-40 Sweden - Stockholm Tel: 46-8-5090-4654 UK - Wokingham Tel: 44-118-921-5800 Fax: 44-118-921-5820 (c) 2018 Microchip Technology Inc. Datasheet Complete DS40001984A-page 445