ATmega328/P
AVR
®
Microcontroller with picoPower
®
Technology
Introduction
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
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 85°C/100 years at 25°C
(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
©
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 I
2
C 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:
– -40°C to 105°C
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, 25°C
– Active mode: 0.2 mA
– Power-Down mode: 0.1 μA
– Power-Save mode: 0.75 μA (Including 32 kHz RTC)
•
•
•
•
©
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
©
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
©
2018 Microchip Technology Inc.
Datasheet Complete
DS40001984A-page 4
ATmega328/P
18.1. Overview.................................................................................................................................... 99
18.2. Ports as General Digital I/O......................................................................................................100
18.3. Alternate Port Functions...........................................................................................................103
18.4. 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.
20.2.
20.3.
20.4.
20.5.
Overview.................................................................................................................................. 152
Features................................................................................................................................... 152
Block Diagram.......................................................................................................................... 152
Definitions.................................................................................................................................153
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
©
2018 Microchip Technology Inc.
Datasheet Complete
DS40001984A-page 5