- •Features
- •Pin Configurations
- •Overview
- •Block Diagram
- •Pin Descriptions
- •Port A (PA7..PA0)
- •Port B (PB7..PB0)
- •Port C (PC7..PC0)
- •Port D (PD7..PD0)
- •Port E (PE7..PE0)
- •Port F (PF7..PF0)
- •Port G (PG4..PG0)
- •RESET
- •XTAL1
- •XTAL2
- •AVCC
- •AREF
- •AVR CPU Core
- •Introduction
- •Architectural Overview
- •Status Register
- •Stack Pointer
- •Interrupt Response Time
- •SRAM Data Memory
- •Data Memory Access Times
- •EEPROM Data Memory
- •EEPROM Read/Write Access
- •I/O Memory
- •Overview
- •ATmega103 Compatibility
- •Address Latch Requirements
- •Pull-up and Bus-keeper
- •Timing
- •XMEM Register Description
- •Using all 64KB Locations of External Memory
- •Clock Systems and their Distribution
- •CPU Clock – clkCPU
- •I/O Clock – clkI/O
- •Flash Clock – clkFLASH
- •ADC Clock – clkADC
- •Clock Sources
- •Crystal Oscillator
- •External RC Oscillator
- •External Clock
- •Timer/Counter Oscillator
- •Idle Mode
- •Power-down Mode
- •Power-save Mode
- •Standby Mode
- •Extended Standby Mode
- •Analog to Digital Converter
- •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
- •Watchdog Timer
- •Timed Sequences for Changing the Configuration of the Watch Dog Timer
- •Safety Level 0
- •Safety Level 1
- •Safety Level 2
- •Interrupts
- •I/O-Ports
- •Introduction
- •Configuring the Pin
- •Reading the Pin Value
- •Alternate Port Functions
- •Alternate Functions of Port A
- •Alternate Functions of Port B
- •Alternate Functions of Port C
- •Alternate Functions of Port D
- •Alternate Functions of Port E
- •Alternate Functions of Port F
- •Alternate Functions of Port G
- •Port A Data Register – PORTA
- •Port B Data Register – PORTB
- •Port C Data Register – PORTC
- •Port D Data Register – PORTD
- •Port E Data Register – PORTE
- •Port F Data Register – PORTF
- •Port G Data Register – PORTG
- •External Interrupts
- •8-bit Timer/Counter0 with PWM and Asynchronous Operation
- •Overview
- •Registers
- •Definitions
- •Counter Unit
- •Output Compare Unit
- •Force Output Compare
- •Modes of Operation
- •Normal Mode
- •Fast PWM Mode
- •Phase Correct PWM Mode
- •Timer/Counter Prescaler
- •16-bit Timer/Counter (Timer/Counter1 and Timer/Counter3)
- •Overview
- •Registers
- •Definitions
- •Compatibility
- •Counter Unit
- •Input Capture Unit
- •Input Capture Trigger Source
- •Noise Canceler
- •Using the Input Capture Unit
- •Output Compare Units
- •Force Output Compare
- •Modes of Operation
- •Normal Mode
- •Fast PWM Mode
- •Phase Correct PWM Mode
- •16-bit Timer/Counter Register Description
- •Internal Clock Source
- •Prescaler Reset
- •External Clock Source
- •8-bit Timer/Counter2 with PWM
- •Overview
- •Registers
- •Definitions
- •Counter Unit
- •Output Compare Unit
- •Force Output Compare
- •Modes of Operation
- •Normal Mode
- •Fast PWM Mode
- •Phase Correct PWM Mode
- •Overview
- •Description
- •Timing Example
- •Slave Mode
- •Master Mode
- •SPI Control Register – SPCR
- •SPI Status Register – SPSR
- •SPI Data Register – SPDR
- •Data Modes
- •USART
- •Dual USART
- •Overview
- •AVR USART vs. AVR UART – Compatibility
- •Clock Generation
- •External Clock
- •Synchronous Clock Operation
- •Frame Formats
- •Parity Bit Calculation
- •USART Initialization
- •Sending Frames with 5 to 8 Data Bit
- •Sending Frames with 9 Data Bit
- •Parity Generator
- •Disabling the Transmitter
- •Receiving Frames with 5 to 8 Data Bits
- •Receiving Frames with 9 Data Bits
- •Receiver Error Flags
- •Parity Checker
- •Disabling the Receiver
- •Flushing the Receive Buffer
- •Asynchronous Data Recovery
- •Using MPCM
- •Two-wire Serial Interface
- •Features
- •TWI Terminology
- •Electrical Interconnection
- •Transferring Bits
- •START and STOP Conditions
- •Address Packet Format
- •Data Packet Format
- •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
- •Using the TWI
- •Transmission Modes
- •Master Transmitter Mode
- •Master Receiver Mode
- •Slave Receiver Mode
- •Slave Transmitter Mode
- •Miscellaneous States
- •Analog Comparator
- •Analog Comparator Multiplexed Input
- •Analog to Digital Converter
- •Features
- •Operation
- •Starting a Conversion
- •Differential Gain Channels
- •ADC Input Channels
- •ADC Voltage Reference
- •ADC Noise Canceler
- •Analog Input Circuitry
- •ADC Accuracy Definitions
- •ADC Conversion Result
- •ADLAR = 0:
- •ADLAR = 1:
- •Features
- •Overview
- •Test Access Port – TAP
- •TAP Controller
- •PRIVATE0; $8
- •PRIVATE1; $9
- •PRIVATE2; $A
- •PRIVATE3; $B
- •Bibliography
- •Features
- •System Overview
- •Data Registers
- •Bypass Register
- •Device Identification Register
- •Reset Register
- •Boundary-scan Chain
- •EXTEST; $0
- •IDCODE; $1
- •SAMPLE_PRELOAD; $2
- •AVR_RESET; $C
- •BYPASS; $F
- •Boundary-scan Chain
- •Scanning the Digital Port Pins
- •Scanning the RESET Pin
- •Scanning the Clock Pins
- •Scanning the ADC
- •Boot Loader Features
- •Application Section
- •Boot Loader Section – BLS
- •Boot Loader Lock Bits
- •Performing a Page Write
- •Using the SPM Interrupt
- •Setting the Boot Loader Lock Bits by SPM
- •Reading the Fuse and Lock Bits from Software
- •Preventing Flash Corruption
- •Simple Assembly Code Example for a Boot Loader
- •Fuse Bits
- •Latching of Fuses
- •Signature Bytes
- •Calibration Byte
- •Signal Names
- •Parallel Programming
- •Enter Programming Mode
- •Chip Erase
- •Programming the Flash
- •Programming the EEPROM
- •Reading the Flash
- •Reading the EEPROM
- •Programming the Lock Bits
- •Reading the Signature Bytes
- •Reading the Calibration Byte
- •Serial Downloading
- •Data Polling Flash
- •Data Polling EEPROM
- •AVR_RESET ($C)
- •PROG_ENABLE ($4)
- •PROG_COMMANDS ($5)
- •PROG_PAGELOAD ($6)
- •PROG_PAGEREAD ($7)
- •Data Registers
- •Reset Register
- •Programming Enable Register
- •Virtual Flash Page Read Register
- •Programming Algorithm
- •Entering Programming Mode
- •Leaving Programming Mode
- •Performing Chip Erase
- •Programming the Flash
- •Reading the Flash
- •Programming the EEPROM
- •Reading the EEPROM
- •Programming the Fuses
- •Programming the Lock Bits
- •Reading the Signature Bytes
- •Reading the Calibration Byte
- •Electrical Characteristics
- •Absolute Maximum Ratings*
- •DC Characteristics
- •External Clock Drive Waveforms
- •External Clock Drive
- •2-wire Serial Interface Characteristics
- •ADC Characteristics - Preliminary Data
- •External Data Memory Timing
- •Ordering Information
- •Packaging Information
Two-wire Serial Interface
Features
•Simple yet Powerful and Flexible Communication Interface, only Two Bus Lines Needed
•Both Master and Slave Operation Supported
•Device can Operate as Transmitter or Receiver
•7-bit Address Space allows up to 128 Different Slave Addresses
•Multi-master Arbitration Support
•Up to 400 kHz Data Transfer Speed
•Slew-rate Limited Output Drivers
•Noise Suppression Circuitry Rejects Spikes on Bus Lines
•Fully Programmable Slave Address with General Call Support
•Address Recognition Causes Wake-up when AVR is in Sleep Mode
Two-wire Serial Interface
Bus Definition
The Two-wire Serial Interface (TWI) is ideally suited for typical microcontroller applications. The TWI protocol allows the systems designer to interconnect up to 128 different devices using only two bidirectional bus lines, one for clock (SCL) and one for data (SDA). The only external hardware needed to implement the bus is a single pull-up resistor for each of the TWI bus lines. All devices connected to the bus have individual addresses, and mechanisms for resolving bus contention are inherent in the TWI protocol.
Figure 85. TWI Bus Interconnection
VCC
Device 1 |
|
|
|
Device 3 |
|
|
|
|
|
|
|
Device 2 |
|
........ |
Device n |
|
R1 |
|
R2 |
||
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
SDA
SCL
TWI Terminology |
The following definitions are frequently encountered in this chapter. |
|
|
Table 86. TWI Terminology |
|
|
|
|
|
Term |
Description |
|
|
|
|
Master |
The device that initiates and terminates a transmission. The master also |
|
|
generates the SCL clock |
|
|
|
|
Slave |
The device addressed by a master |
|
|
|
|
Transmitter |
The device placing data on the bus |
|
|
|
|
Receiver |
The device reading data from the bus |
|
|
|
Electrical Interconnection |
As depicted in Figure 85, both bus lines are connected to the positive supply voltage |
|
|
through pull-up resistors. The bus drivers of all TWI-compliant devices are open-drain or |
|
|
open-collector. This implements a wired-AND function which is essential to the opera- |
|
|
tion of the interface. A low level on a TWI bus line is generated when one or more TWI |
|
|
devices output a zero. A high level is output when all TWI devices tri-state their outputs, |
190 ATmega128(L)
2467B–09/01
ATmega128(L)
allowing the pull-up resistors to pull the line high. Note that all AVR devices connected to the TWI bus must be powered in order to allow any bus operation.
The number of devices that can be connected to the bus is only limited by the bus
capacitance limit of 400 pF and the 7-bit slave address space. A detailed specification of the electrical characteristics of the TWI is given in “2-wire Serial Interface Characteris-
tics” on page 313. Two different sets of specifications are presented there, one relevant for bus speeds below 100 kHz, and one valid for bus speeds up to 400 kHz.
Data Transfer and Frame
Format
Transferring Bits |
Each data bit transferred on the TWI bus is accompanied by a pulse on the clock line. |
|
The level of the data line must be stable when the clock line is high. The only exception |
|
to this rule is for generating start and stop conditions. |
|
Figure 86. Data Validity |
SDA |
|
SCL |
|
Data Stable |
Data Stable |
Data Change
START and STOP Conditions The master initiates and terminates a data transmission. The transmission is initiated when the master issues a START condition on the bus, and it is terminated when the master issues a STOP condition. Between a START and a STOP condition, the bus is considered busy, and no other master should try to seize control of the bus. A special case occurs when a new START condition is issued between a START and STOP condition. This is referred to as a REPEATED START condition, and is used when the master wishes to initiate a new transfer without relinquishing control of the bus. After a REPEATED START, the bus is considered busy until the next STOP. This is identical to the START behaviour, and therefore START is used to describe both START and REPEATED START for the remainder of this datasheet, unless otherwise noted. As depicted below, START and STOP conditions are signalled by changing the level of the SDA line when the SCL line is high.
Figure 87. START, REPEATED START and STOP Conditions
SDA
SCL
|
STOP START |
|
|
|
|
START |
|
REPEATED START |
STOP |
191
2467B–09/01
Address Packet Format |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
All address packets transmitted on the TWI bus are 9 bits long, consisting of 7 address |
||||||||||||
|
bits, one READ/WRITE control bit and an acknowledge bit. If the READ/WRITE bit is |
|||||||||||
|
set, a read operation is to be performed, otherwise a write operation should be per- |
|||||||||||
|
formed. When a slave recognizes that it is being addressed, it should acknowledge by |
|||||||||||
|
pulling SDA low in the ninth SCL (ACK) cycle. If the addressed slave is busy, or for |
|||||||||||
|
some other reason can not service the master’s request, the SDA line should be left |
|||||||||||
|
high in the ACK clock cycle. The master can then transmit a STOP condition, or a |
|||||||||||
|
REPEATED START condition to initiate a new transmission. An address packet consist- |
|||||||||||
|
ing of a slave address and a READ or a WRITE bit is called SLA+R or SLA+W, |
|||||||||||
|
respectively. |
|
|
|
|
|
|
|
|
|||
|
The MSB of the address byte is transmitted first. Slave addresses can freely be allo- |
|||||||||||
|
cated by the designer, but the address 0000 000 is reserved for a general call. |
|||||||||||
|
When a general call is issued, all slaves should respond by pulling the SDA line low in |
|||||||||||
|
the ACK cycle. A general call is used when a master wishes to transmit the same mes- |
|||||||||||
|
sage to several slaves in the system. When the general call address followed by a Write |
|||||||||||
|
bit is transmitted on the bus, all slaves set up to acknowledge the general call will pull |
|||||||||||
|
the SDA line low in the ack cycle. The following data packets will then be received by all |
|||||||||||
|
the slaves that acknowledged the general call. Note that transmitting the general call |
|||||||||||
|
address followed by a Read bit is meaningless, as this would cause contention if several |
|||||||||||
|
slaves started transmitting different data. |
|
|
|
|
|
||||||
|
All addresses of the format 1111 xxx should be reserved for future purposes. |
|||||||||||
|
Figure 88. Address Packet Format |
|
|
|
|
|
|
|
|
|||
|
Addr MSB |
|
|
|
|
|
Addr LSB |
R/W |
ACK |
|||
|
SDA |
|
|
|
|
|
|
|
|
|||
|
|
|
|
|
|
|
|
|
||||
|
SCL |
|
|
|
|
|
|
|
|
|||
|
|
|
|
|
|
|
|
|
||||
|
1 |
|
|
|
2 |
|
|
|
7 |
8 |
9 |
|
|
|
|
|
|
|
|||||||
|
START |
|
|
|
|
|
|
|
|
|||
Data Packet Format |
All data packets transmitted on the TWI bus are 9 bits long, consisting of one data byte |
|||||||||||
|
and an acknowledge bit. During a data transfer, the master generates the clock and the |
|||||||||||
|
START and STOP conditions, while the receiver is responsible for acknowledging the |
|||||||||||
|
reception. An Acknowledge (ACK) is signalled by the receiver pulling the SDA line low |
|||||||||||
|
during the ninth SCL cycle. If the receiver leaves the SDA line high, a NACK is signalled. |
|||||||||||
|
When the receiver has received the last byte, or for some reason cannot receive any |
|||||||||||
|
more bytes, it should inform the transmitter by sending a NACK after the final byte. The |
|||||||||||
|
MSB of the data byte is transmitted first. |
|
|
|
|
|
192 ATmega128(L)
2467B–09/01