link to page 7 Features • High-performance, Low-power Atmel ® AVR ® 8-bit Microcontroller • Advanced RISC Architecture– 130 Powerful Instructions – Most Single-clock Cycle Execution – 32 × 8 General Purpose Working Registers – Fully Static Operation – Up to 16MIPS Throughput at 16MHz – On-chip 2-cycle Multiplier • High Endurance Non-volatile Memory segments– 8Kbytes of In-System Self-programmable Flash program memory – 512Bytes EEPROM8-bit Atmel with– 1Kbyte Internal SRAM – Write/Erase Cycles: 10,000 Flash/100,000 EEPROM8KBytes In-– Data retention: 20 years at 85°C/100 years at 25°C (1) – Optional Boot Code Section with Independent Lock BitsSystemIn-System Programming by On-chip Boot ProgramTrue Read-While-Write Operation– Programming Lock for Software SecurityProgrammable • Peripheral Features– Two 8-bit Timer/Counters with Separate Prescaler, one Compare ModeFlash– One 16-bit Timer/Counter with Separate Prescaler, Compare Mode, and CaptureMode– Real Time Counter with Separate Oscillator – Three PWM ChannelsATmega8– 8-channel ADC in TQFP and QFN/MLF packageEight Channels 10-bit AccuracyATmega8L– 6-channel ADC in PDIP packageSix Channels 10-bit Accuracy– Byte-oriented Two-wire Serial Interface – Programmable Serial USART – Master/Slave SPI Serial Interface – Programmable Watchdog Timer with Separate On-chip Oscillator – On-chip Analog Comparator • Special Microcontroller Features– Power-on Reset and Programmable Brown-out Detection – Internal Calibrated RC Oscillator – External and Internal Interrupt Sources – Five Sleep Modes: Idle, ADC Noise Reduction, Power-save, Power-down, andStandby • I/O and Packages– 23 Programmable I/O Lines – 28-lead PDIP, 32-lead TQFP, and 32-pad QFN/MLF • Operating Voltages– 2.7V - 5.5V (ATmega8L) – 4.5V - 5.5V (ATmega8) • Speed Grades– 0 - 8MHz (ATmega8L) – 0 - 16MHz (ATmega8) • Power Consumption at 4Mhz, 3V, 25C– Active: 3.6mA – Idle Mode: 1.0mA – Power-down Mode: 0.5µA Rev.2486AA–AVR–02/2013 Document Outline Features Pin Configurations Overview Block Diagram Disclaimer Pin Descriptions VCC GND Port B (PB7..PB0) XTAL1/XTAL2/TOSC1/ TOSC2 Port C (PC5..PC0) PC6/RESET Port D (PD7..PD0) RESET AVCC AREF ADC7..6 (TQFP and QFN/MLF Package Only) Resources Data Retention About Code Examples Atmel AVR CPU Core Introduction Architectural Overview Arithmetic Logic Unit – ALU Status Register General Purpose Register File The X-register, Y- register and Z-register Stack Pointer Instruction Execution Timing Reset and Interrupt Handling Interrupt Response Time AVR ATmega8 Memories In-System Reprogrammable Flash Program Memory SRAM Data Memory Data Memory Access Times EEPROM Data Memory EEPROM Read/Write Access The EEPROM Address Register – EEARH and EEARL The EEPROM Data Register – EEDR The EEPROM Control Register – EECR EEPROM Write during Power-down Sleep Mode Preventing EEPROM Corruption I/O Memory System Clock and Clock Options Clock Systems and their Distribution CPU Clock – clkCPU I/O Clock – clkI/O Flash Clock – clkFLASH Asynchronous Timer Clock – clkASY ADC Clock – clkADC Clock Sources Crystal Oscillator Low-frequency Crystal Oscillator External RC Oscillator Calibrated Internal RC Oscillator Oscillator Calibration Register – OSCCAL External Clock Timer/Counter Oscillator Power Management and Sleep Modes MCU Control Register – MCUCR Idle Mode ADC Noise Reduction Mode Power-down Mode Power-save Mode Standby Mode Minimizing Power Consumption Analog-to-Digital Converter (ADC) Analog Comparator Brown-out Detector Internal Voltage Reference Watchdog Timer Port Pins System Control and Reset Resetting the AVR Reset Sources Power-on Reset External Reset Brown-out Detection Watchdog Reset MCU Control and Status Register – MCUCSR Internal Voltage Reference Voltage Reference Enable Signals and Start-up Time Watchdog Timer Watchdog Timer Control Register – WDTCR Timed Sequences for Changing the Configuration of the Watchdog Timer Safety Level 1 (WDTON Fuse Unprogrammed) Safety Level 2 (WDTON Fuse Programmed) Interrupts Interrupt Vectors in ATmega8 Moving Interrupts Between Application and Boot Space General Interrupt Control Register – GICR I/O Ports Introduction Ports as General Digital I/O Configuring the Pin Reading the Pin Value Digital Input Enable and Sleep Modes Unconnected pins Alternate Port Functions Special Function IO Register – SFIOR Alternate Functions of Port B Alternate Functions of Port C Alternate Functions of Port D Register Description for I/O Ports The Port B Data Register – PORTB The Port B Data Direction Register – DDRB The Port B Input Pins Address – PINB The Port C Data Register – PORTC The Port C Data Direction Register – DDRC The Port C Input Pins Address – PINC The Port D Data Register – PORTD The Port D Data Direction Register – DDRD The Port D Input Pins Address – PIND External Interrupts MCU Control Register – MCUCR General Interrupt Control Register – GICR General Interrupt Flag Register – GIFR 8-bit Timer/Counter0 Overview Registers Definitions Timer/Counter Clock Sources Counter Unit Operation Timer/Counter Timing Diagrams 8-bit Timer/Counter Register Description Timer/Counter Control Register – TCCR0 Timer/Counter Register – TCNT0 Timer/Counter Interrupt Mask Register – TIMSK Timer/Counter Interrupt Flag Register – TIFR Timer/Counter0 and Timer/Counter1 Prescalers Internal Clock Source Prescaler Reset External Clock Source Special Function IO Register – SFIOR 16-bit Timer/Counter1 Overview Registers Definitions Compatibility Accessing 16-bit Registers Reusing the Temporary High Byte Register Timer/Counter Clock Sources Counter Unit Input Capture Unit Input Capture Pin Source Noise Canceler Using the Input Capture Unit Output Compare Units Force Output Compare Compare Match Blocking by TCNT1 Write Using the Output Compare Unit Compare Match Output Unit Compare Output Mode and Waveform Generation Modes of Operation Normal Mode Clear Timer on Compare Match (CTC) Mode Fast PWM Mode Phase Correct PWM Mode Phase and Frequency Correct PWM Mode Timer/Counter Timing Diagrams 16-bit Timer/Counter Register Description Timer/Counter 1 Control Register A – TCCR1A Timer/Counter 1 Control Register B – TCCR1B Timer/Counter 1 – TCNT1H and TCNT1L Output Compare Register 1 A – OCR1AH and OCR1AL Output Compare Register 1 B – OCR1BH and OCR1BL Input Capture Register 1 – ICR1H and ICR1L Timer/Counter Interrupt Mask Register – TIMSK(1) Timer/Counter Interrupt Flag Register – TIFR(1) 8-bit Timer/Counter2 with PWM and Asynchronous Operation Overview Registers Definitions Timer/Counter Clock Sources Counter Unit Output Compare Unit Force Output Compare Compare Match Blocking by TCNT2 Write Using the Output Compare Unit Compare Match Output Unit Compare Output Mode and Waveform Generation Modes of Operation Normal Mode Clear Timer on Compare Match (CTC) Mode Fast PWM Mode Phase Correct PWM Mode Timer/Counter Timing Diagrams 8-bit Timer/Counter Register Description Timer/Counter Control Register – TCCR2 Timer/Counter Register – TCNT2 Output Compare Register – OCR2 Asynchronous Operation of the Timer/Counter Asynchronous Status Register – ASSR Asynchronous Operation of Timer/Counter2 Timer/Counter Interrupt Mask Register – TIMSK Timer/Counter Interrupt Flag Register – TIFR Timer/Counter Prescaler Special Function IO Register – SFIOR Serial Peripheral Interface – SPI SS Pin Functionality Slave Mode Master Mode SPI Control Register – SPCR SPI Status Register – SPSR SPI Data Register – SPDR Data Modes USART Overview AVR USART vs. AVR UART – Compatibility Clock Generation Internal Clock Generation – The Baud Rate Generator Double Speed Operation (U2X) External Clock Synchronous Clock Operation Frame Formats Parity Bit Calculation USART Initialization Data Transmission – The USART Transmitter Sending Frames with 5 to 8 Data Bits Sending Frames with 9 Data Bits Transmitter Flags and Interrupts Parity Generator Disabling the Transmitter Data Reception – The USART Receiver Receiving Frames with 5 to 8 Data Bits Receiving Frames with 9 Data Bits Receive Compete Flag and Interrupt Receiver Error Flags Parity Checker Disabling the Receiver Flushing the Receive Buffer Asynchronous Data Reception Asynchronous Clock Recovery Asynchronous Data Recovery Asynchronous Operational Range Multi-processor Communication Mode Using MPCM Accessing UBRRH/UCSRC Registers Write Access Read Access USART Register Description USART I/O Data Register – UDR USART Control and Status Register A – UCSRA USART Control and Status Register B – UCSRB USART Control and Status Register C – UCSRC USART Baud Rate Registers – UBRRL and UBRRHs Examples of Baud Rate Setting Two-wire Serial Interface Features Two-wire Serial Interface Bus Definition TWI Terminology Electrical Interconnection Data Transfer and Frame Format Transferring Bits START and STOP Conditions Address Packet Format Data Packet Format Combining Address and Data Packets into a Transmission Multi-master Bus Systems, Arbitration and Synchronization Overview of the TWI Module SCL and SDA Pins Bit Rate Generator Unit Bus Interface Unit Address Match Unit Control Unit TWI Register Description TWI Bit Rate Register – TWBR TWI Control Register – TWCR TWI Status Register – TWSR TWI Data Register – TWDR TWI (Slave) Address Register – TWAR Using the TWI Transmission Modes Master Transmitter Mode Master Receiver Mode Slave Receiver Mode Slave Transmitter Mode Miscellaneous States Combining Several TWI Modes Multi-master Systems and Arbitration Analog Comparator Special Function IO Register – SFIOR Analog Comparator Control and Status Register – ACSR Analog Comparator Multiplexed Input Analog-to- Digital Converter Features Starting a Conversion Prescaling and Conversion Timing Changing Channel or Reference Selection ADC Input Channels ADC Voltage Reference ADC Noise Canceler Analog Input Circuitry Analog Noise Canceling Techniques ADC Accuracy Definitions ADC Conversion Result ADC Multiplexer Selection Register – ADMUX ADC Control and Status Register A – ADCSRA The ADC Data Register – ADCL and ADCH Boot Loader Support – Read- While-Write Self- Programming Boot Loader Features Application and Boot Loader Flash Sections Application Section BLS – Boot Loader Section Read-While-Write and No Read- While-Write Flash Sections RWW – Read-While- Write Section NRWW – No Read- While-Write Section Boot Loader Lock Bits Entering the Boot Loader Program Store Program Memory Control Register – SPMCR Addressing the Flash During Self- Programming Self-Programming the Flash Performing Page Erase by SPM Filling the Temporary Buffer (Page Loading) Performing a Page Write Using the SPM Interrupt Consideration While Updating BLS Prevent Reading the RWW Section During Self-Programming Setting the Boot Loader Lock Bits by SPM EEPROM Write Prevents Writing to SPMCR Reading the Fuse and Lock Bits from Software Preventing Flash Corruption Programming Time for Flash when using SPM Simple Assembly Code Example for a Boot Loader ATmega8 Boot Loader Parameters Memory Programming Program And Data Memory Lock Bits Fuse Bits Latching of Fuses Signature Bytes Calibration Byte Page Size Parallel Programming Parameters, Pin Mapping, and Commands Signal Names Parallel Programming Enter Programming Mode Considerations for Efficient Programming Chip Erase Programming the Flash Programming the EEPROM Reading the Flash Reading the EEPROM Programming the Fuse Low Bits Programming the Fuse High Bits Programming the Lock Bits Reading the Fuse and Lock Bits Reading the Signature Bytes Reading the Calibration Byte Parallel Programming Characteristics Serial Downloading Serial Programming Pin Mapping Serial Programming Algorithm Data Polling Flash Data Polling EEPROM SPI Serial Programming Characteristics Electrical Characteristics – TA = -40°C to 85°C Absolute Maximum Ratings* DC Characteristics External Clock Drive Waveforms External Clock Drive Two-wire Serial Interface Characteristics SPI Timing Characteristics ADC Characteristics Electrical Characteristics – TA = -40°C to 105°C Absolute Maximum Ratings* DC Characteristics TA = -40°C to 105°C, VCC = 2.7V to 5.5V (unless otherwise noted) ATmega8 Typical Characteristics – TA = -40°C to 85°C Active Supply Current Idle Supply Current Power-down Supply Current Power-save Supply Current Standby Supply Current Pin Pull-up Pin Driver Strength Pin Thresholds and Hysteresis Bod Thresholds and Analog Comparator Offset Internal Oscillator Speed Current Consumption of Peripheral Units Current Consumption in Reset and Reset Pulsewidth ATmega8 Typical Characteristics – TA = -40°C to 105°C Active Supply Current Idle Supply Current Power-down Supply Current Pin Pull-up Pin Driver Strength Pin Thresholds and Hysteresis Bod Thresholds and Analog Comparator Offset Internal Oscillator Speed Current Consumption of Peripheral Units Current Consumption in Reset and Reset Pulsewidth Register Summary Instruction Set Summary Ordering Information Packaging Information 32A 28P3 32M1-A Errata ATmega8 Rev. D to I, M Datasheet Revision History Changes from Rev. 2486Z- 02/11 to Rev. 2486AA- 02/2013 Changes from Rev. 2486Y- 10/10 to Rev. 2486Z- 02/11 Changes from Rev. 2486X- 06/10 to Rev. 2486Y- 10/10 Changes from Rev. 2486W- 02/10 to Rev. 2486X- 06/10 Changes from Rev. 2486V- 05/09 to Rev. 2486W- 02/10 Changes from Rev. 2486U- 08/08 to Rev. 2486V- 05/09 Changes from Rev. 2486T- 05/08 to Rev. 2486U- 08/08 Changes from Rev. 2486S- 08/07 to Rev. 2486T- 05/08 Changes from Rev. 2486R- 07/07 to Rev. 2486S- 08/07 Changes from Rev. 2486Q- 10/06 to Rev. 2486R- 07/07 Changes from Rev. 2486P- 02/06 to Rev. 2486Q- 10/06 Changes from Rev. 2486O-10/04 to Rev. 2486P- 02/06 Changes from Rev. 2486N-09/04 to Rev. 2486O-10/04 Changes from Rev. 2486M-12/03 to Rev. 2486N-09/04 Changes from Rev. 2486L-10/03 to Rev. 2486M-12/03 Changes from Rev. 2486K-08/03 to Rev. 2486L-10/03 Changes from Rev. 2486J-02/03 to Rev. 2486K-08/03 Changes from Rev. 2486I-12/02 to Rev. 2486J-02/03 Changes from Rev. 2486H-09/02 to Rev. 2486I-12/02 Changes from Rev. 2486G-09/02 to Rev. 2486H-09/02 Changes from Rev. 2486F-07/02 to Rev. 2486G-09/02 Changes from Rev. 2486E-06/02 to Rev. 2486F-07/02 Changes from Rev. 2486D-03/02 to Rev. 2486E-06/02 Changes from Rev. 2486C-03/02 to Rev. 2486D-03/02 Changes from Rev. 2486B-12/01 to Rev. 2486C-03/02 Table of Contents