Atmega128: I/O Ports
Atmega128: I/O Ports
Atmega128: I/O Ports
I/O Ports
Introduction All Atmel® AVR® ports have true Read-Modify-Write functionality when used as general digital
I/O ports. This means that the direction of one port pin can be changed without unintentionally
changing the direction of any other pin with the SBI and CBI instructions. The same applies
when changing drive value (if configured as output) or enabling/disabling of pull-up resistors (if
configured as input). Each output buffer has symmetrical drive characteristics with both high sink
and source capability. The pin driver is strong enough to drive LED displays directly. All port pins
have individually selectable pull-up resistors with a supply-voltage invariant resistance. All I/O
pins have protection diodes to both VCC and Ground as indicated in Figure 29. Refer to “Electri-
cal Characteristics” on page 318 for a complete list of parameters.
RPU
Pxn Logic
CPIN
See Figure
"General Digital I/O" for
Details
All registers and bit references in this section are written in general form. A lower case “x” repre-
sents the numbering letter for the port, and a lower case “n” represents the bit number. However,
when using the register or bit defines in a program, the precise form must be used. For example,
PORTB3 for bit no. 3 in Port B, here documented generally as PORTxn. The physical I/O regis-
ters and bit locations are listed in “Register Description for I/O Ports” on page 86.
Three I/O memory address locations are allocated for each port, one each for the Data Register
– PORTx, Data Direction Register – DDRx, and the Port Input Pins – PINx. The Port Input Pins
I/O location is read only, while the Data Register and the Data Direction Register are read/write.
In addition, the Pull-up Disable – PUD bit in SFIOR disables the pull-up function for all pins in all
ports when set.
Using the I/O port as General Digital I/O is described in “Ports as General Digital I/O” on page
66. Most port pins are multiplexed with alternate functions for the peripheral features on the
device. How each alternate function interferes with the port pin is described in “Alternate Port
Functions” on page 70. Refer to the individual module sections for a full description of the alter-
nate functions.
Note that enabling the alternate function of some of the port pins does not affect the use of the
other pins in the port as General Digital I/O.
65
2467X–AVR–06/11
ATmega128
Ports as General The ports are bi-directional I/O ports with optional internal pull-ups. Figure 30 shows a functional
Digital I/O description of one I/O port pin, here generically called Pxn.
PUD
Q D
DDxn
Q CLR
WDx
RESET
RDx
DATA BUS
Pxn Q D
PORTxn
Q CLR
WPx
RESET
SLEEP RRx
SYNCHRONIZER
RPx
D Q D Q
PINxn
L Q Q
clk I/O
Note: 1. WPx, WDx, RRx, RPx, and RDx are common to all pins within the same port. clkI/O, SLEEP,
and PUD are common to all ports.
Configuring the Pin Each port pin consists of three Register bits: DDxn, PORTxn, and PINxn. As shown in “Register
Description for I/O Ports” on page 86, the DDxn bits are accessed at the DDRx I/O address, the
PORTxn bits at the PORTx I/O address, and the PINxn bits at the PINx I/O address.
The DDxn bit in the DDRx Register selects the direction of this pin. If DDxn is written logic one,
Pxn is configured as an output pin. If DDxn is written logic zero, Pxn is configured as an input
pin.
If PORTxn is written logic one when the pin is configured as an input pin, the pull-up resistor is
activated. To switch the pull-up resistor off, PORTxn has to be written logic zero or the pin has to
be configured as an output pin. The port pins are tri-stated when a Reset condition becomes
active, even if no clocks are running.
If PORTxn is written logic one when the pin is configured as an output pin, the port pin is driven
high (one). If PORTxn is written logic zero when the pin is configured as an output pin, the port
pin is driven low (zero).
When switching between tri-state ({DDxn, PORTxn} = 0b00) and output high ({DDxn, PORTxn}
= 0b11), an intermediate state with either pull-up enabled ({DDxn, PORTxn} = 0b01) or output
66
2467X–AVR–06/11
ATmega128
low ({DDxn, PORTxn} = 0b10) must occur. Normally, the pull-up enabled state is fully accept-
able, as a high-impedant environment will not notice the difference between a strong high driver
and a pull-up. If this is not the case, the PUD bit in the SFIOR Register can be written to one to
disable all pull-ups in all ports.
Switching between input with pull-up and output low generates the same problem. The user
must use either the tri-state ({DDxn, PORTxn} = 0b00) or the output high state ({DDxn, PORTxn}
= 0b11) as an intermediate step.
Table 25 summarizes the control signals for the pin value.
Reading the Pin Value Independent of the setting of Data Direction bit DDxn, the port pin can be read through the
PINxn Register bit. As shown in Figure 30, the PINxn Register bit and the preceding latch consti-
tute a synchronizer. This is needed to avoid metastability if the physical pin changes value near
the edge of the internal clock, but it also introduces a delay. Figure 31 shows a timing diagram of
the synchronization when reading an externally applied pin value. The maximum and minimum
propagation delays are denoted tpd,max and tpd,min respectively.
SYSTEM CLK
SYNC LATCH
PINxn
tpd, max
tpd, min
Consider the clock period starting shortly after the first falling edge of the system clock. The latch
is closed when the clock is low, and goes transparent when the clock is high, as indicated by the
67
2467X–AVR–06/11
ATmega128
shaded region of the “SYNC LATCH” signal. The signal value is latched when the system clock
goes low. It is clocked into the PINxn Register at the succeeding positive clock edge. As indi-
cated by the two arrows tpd,max and tpd,min, a single signal transition on the pin will be delayed
between ½ and 1½ system clock period depending upon the time of assertion.
When reading back a software assigned pin value, a nop instruction must be inserted as indi-
cated in Figure 32. The out instruction sets the “SYNC LATCH” signal at the positive edge of the
clock. In this case, the delay tpd through the synchronizer is one system clock period.
SYSTEM CLK
r16 0xFF
SYNC LATCH
PINxn
tpd
68
2467X–AVR–06/11
ATmega128
The following code example shows how to set port B pins 0 and 1 high, 2 and 3 low, and define
the port pins from 4 to 7 as input with pull-ups assigned to port pins 6 and 7. The resulting pin
values are read back again, but as previously discussed, a nop instruction is included to be able
to read back the value recently assigned to some of the pins.
Assembly Code Example(1)
...
; Define pull-ups and set outputs high
; Define directions for port pins
ldi r16,(1<<PB7)|(1<<PB6)|(1<<PB1)|(1<<PB0)
ldi r17,(1<<DDB3)|(1<<DDB2)|(1<<DDB1)|(1<<DDB0)
out PORTB,r16
out DDRB,r17
; Insert nop for synchronization
nop
; Read port pins
in r16,PINB
...
C Code Example(1)
unsigned char i;
...
/* Define pull-ups and set outputs high */
/* Define directions for port pins */
PORTB = (1<<PB7)|(1<<PB6)|(1<<PB1)|(1<<PB0);
DDRB = (1<<DDB3)|(1<<DDB2)|(1<<DDB1)|(1<<DDB0);
/* Insert nop for synchronization*/
__no_operation();
/* Read port pins */
i = PINB;
...
Note: 1. For the assembly program, two temporary registers are used to minimize the time from pull-
ups are set on pins 0, 1, 6, and 7, until the direction bits are correctly set, defining bit 2 and 3
as low and redefining bits 0 and 1 as strong high drivers.
Digital Input Enable As shown in Figure 30, the digital input signal can be clamped to ground at the input of the
and Sleep Modes schmitt-trigger. The signal denoted SLEEP in the figure, is set by the MCU Sleep Controller in
Power-down mode, Power-save mode, Standby mode, and Extended Standby mode to avoid
high power consumption if some input signals are left floating, or have an analog signal level
close to VCC/2.
SLEEP is overridden for port pins enabled as External Interrupt pins. If the External Interrupt
Request is not enabled, SLEEP is active also for these pins. SLEEP is also overridden by vari-
ous other alternate functions as described in “Alternate Port Functions” on page 70.
If a logic high level (“one”) is present on an Asynchronous External Interrupt pin configured as
“Interrupt on Rising Edge, Falling Edge, or Any Logic Change on Pin” while the external interrupt
is not enabled, the corresponding External Interrupt Flag will be set when resuming from the
above mentioned sleep modes, as the clamping in these sleep modes produces the requested
logic change.
69
2467X–AVR–06/11
ATmega128
Unconnected pins If some pins are unused, it is recommended to ensure that these pins have a defined level. Even
though most of the digital inputs are disabled in the deep sleep modes as described above, float-
ing inputs should be avoided to reduce current consumption in all other modes where the digital
inputs are enabled (Reset, Active mode and Idle mode).
The simplest method to ensure a defined level of an unused pin, is to enable the internal pull-up.
In this case, the pull-up will be disabled during reset. If low power consumption during reset is
important, it is recommended to use an external pull-up or pull-down. Connecting unused pins
directly to VCC or GND is not recommended, since this may cause excessive currents if the pin is
accidentally configured as an output.
Alternate Port Most port pins have alternate functions in addition to being general digital I/Os. Figure 33 shows
Functions how the port pin control signals from the simplified Figure 30 can be overridden by alternate
functions. The overriding signals may not be present in all port pins, but the figure serves as a
generic description applicable to all port pins in the AVR microcontroller family.
PUOVxn
1
0
PUD
DDOExn
DDOVxn
1
0 Q D
DDxn
Q CLR
WDx
PVOExn
RESET
PVOVxn RDx
DATA BUS
1
Pxn
0 Q D
PORTxn
Q CLR
DIEOExn
WPx
DIEOVxn RESET
1
RRx
0 SLEEP
SYNCHRONIZER
RPx
SET
D Q D Q
PINxn
L CLR Q CLR Q
clk I/O
DIxn
AIOxn
Note: 1. WPx, WDx, RLx, RPx, and RDx are common to all pins within the same port. clkI/O, SLEEP,
and PUD are common to all ports. All other signals are unique for each pin.
70
2467X–AVR–06/11
ATmega128
Table 26 summarizes the function of the overriding signals. The pin and port indexes from Fig-
ure 33 are not shown in the succeeding tables. The overriding signals are generated internally in
the modules having the alternate function.
The following subsections shortly describes the alternate functions for each port, and relates the
overriding signals to the alternate function. Refer to the alternate function description for further
details.
71
2467X–AVR–06/11
ATmega128
Special Function IO
Register – SFIOR Bit 7 6 5 4 3 2 1 0
TSM – – – ACME PUD PSR0 PSR321 SFIOR
Read/Write R/W R R R R/W R/W R/W R/W
Initial Value 0 0 0 0 0 0 0 0
Alternate Functions of The Port A has an alternate function as the address low byte and data lines for the External
Port A Memory Interface.
Table 28 and Table 29 relates the alternate functions of Port A to the overriding signals shown in
Figure 33 on page 70.
72
2467X–AVR–06/11
ATmega128
Alternate Functions of The Port B pins with alternate functions are shown in Table 30.
Port B
Table 30. Port B Pins Alternate Functions
Port Pin Alternate Functions
OC2/OC1C(1) (Output Compare and PWM Output for Timer/Counter2 or Output
PB7
Compare and PWM Output C for Timer/Counter1)
PB6 OC1B (Output Compare and PWM Output B for Timer/Counter1)
PB5 OC1A (Output Compare and PWM Output A for Timer/Counter1)
PB4 OC0 (Output Compare and PWM Output for Timer/Counter0)
PB3 MISO (SPI Bus Master Input/Slave Output)
PB2 MOSI (SPI Bus Master Output/Slave Input)
PB1 SCK (SPI Bus Serial Clock)
PB0 SS (SPI Slave Select input)
Note: 1. OC1C not applicable in ATmega103 compatibility mode.
The alternate pin configuration is as follows:
• OC2/OC1C, Bit 7
OC2, Output Compare Match output: The PB7 pin can serve as an external output for the
Timer/Counter2 Output Compare. The pin has to be configured as an output (DDB7 set “one”) to
serve this function. The OC2 pin is also the output pin for the PWM mode timer function.
OC1C, Output Compare Match C output: The PB7 pin can serve as an external output for the
Timer/Counter1 Output Compare C. The pin has to be configured as an output (DDB7 set (one))
to serve this function. The OC1C pin is also the output pin for the PWM mode timer function.
73
2467X–AVR–06/11
ATmega128
• OC1B, Bit 6
OC1B, Output Compare Match B output: The PB6 pin can serve as an external output for the
Timer/Counter1 Output Compare B. The pin has to be configured as an output (DDB6 set (one))
to serve this function. The OC1B pin is also the output pin for the PWM mode timer function.
• OC1A, Bit 5
OC1A, Output Compare Match A output: The PB5 pin can serve as an external output for the
Timer/Counter1 Output Compare A. The pin has to be configured as an output (DDB5 set (one))
to serve this function. The OC1A pin is also the output pin for the PWM mode timer function.
• OC0, Bit 4
OC0, Output Compare Match output: The PB4 pin can serve as an external output for the
Timer/Counter0 Output Compare. The pin has to be configured as an output (DDB4 set (one)) to
serve this function. The OC0 pin is also the output pin for the PWM mode timer function.
• MISO – Port B, Bit 3
MISO: Master Data input, Slave Data output pin for SPI channel. When the SPI is enabled as a
master, this pin is configured as an input regardless of the setting of DDB3. When the SPI is
enabled as a slave, the data direction of this pin is controlled by DDB3. When the pin is forced to
be an input, the pull-up can still be controlled by the PORTB3 bit.
• MOSI – Port B, Bit 2
MOSI: SPI Master Data output, Slave Data input for SPI channel. When the SPI is enabled as a
slave, this pin is configured as an input regardless of the setting of DDB2. When the SPI is
enabled as a master, the data direction of this pin is controlled by DDB2. When the pin is forced
to be an input, the pull-up can still be controlled by the PORTB2 bit.
• SCK – Port B, Bit 1
SCK: Master Clock output, Slave Clock input pin for SPI channel. When the SPI is enabled as a
slave, this pin is configured as an input regardless of the setting of DDB1. When the SPI is
enabled as a master, the data direction of this pin is controlled by DDB1. When the pin is forced
to be an input, the pull-up can still be controlled by the PORTB1 bit.
• SS – Port B, Bit 0
SS: Slave Port Select input. When the SPI is enabled as a slave, this pin is configured as an
input regardless of the setting of DDB0. As a slave, the SPI is activated when this pin is driven
low. When the SPI is enabled as a master, the data direction of this pin is controlled by DDB0.
When the pin is forced to be an input, the pull-up can still be controlled by the PORTB0 bit.
Table 31 and Table 32 relate the alternate functions of Port B to the overriding signals shown in
Figure 33 on page 70. SPI MSTR INPUT and SPI SLAVE OUTPUT constitute the MISO signal,
while MOSI is divided into SPI MSTR OUTPUT and SPI SLAVE INPUT.
74
2467X–AVR–06/11
ATmega128
75
2467X–AVR–06/11
ATmega128
Alternate Functions of In ATmega103 compatibility mode, Port C is output only. The ATmega128 is by default shipped
Port C in compatibility mode. Thus, if the parts are not programmed before they are put on the PCB,
PORTC will be output during first power up, and until the ATmega103 compatibility mode is dis-
abled. The Port C has an alternate function as the address high byte for the External Memory
Interface.
Table 34 and Table 35 relate the alternate functions of Port C to the overriding signals shown in
Figure 33 on page 70.
76
2467X–AVR–06/11
ATmega128
Alternate Functions of The Port D pins with alternate functions are shown in Table 36.
Port D
Table 36. Port D Pins Alternate Functions
Port Pin Alternate Function
PD7 T2 (Timer/Counter2 Clock Input)
PD6 T1 (Timer/Counter1 Clock Input)
PD5 XCK1(1) (USART1 External Clock Input/Output)
PD4 ICP1 (Timer/Counter1 Input Capture Pin)
PD3 INT3/TXD1(1) (External Interrupt3 Input or UART1 Transmit Pin)
PD2 INT2/RXD1(1) (External Interrupt2 Input or UART1 Receive Pin)
PD1 INT1/SDA(1) (External Interrupt1 Input or TWI Serial DAta)
PD0 INT0/SCL(1) (External Interrupt0 Input or TWI Serial CLock)
Note: 1. XCK1, TXD1, RXD1, SDA, and SCL not applicable in ATmega103 compatibility mode.
The alternate pin configuration is as follows:
• T2 – Port D, Bit 7
T2, Timer/Counter2 counter source.
• T1 – Port D, Bit 6
T1, Timer/Counter1 counter source.
• XCK1 – Port D, Bit 5
XCK1, USART1 External clock. The Data Direction Register (DDD4) controls whether the clock
is output (DDD4 set) or input (DDD4 cleared). The XCK1 pin is active only when the USART1
operates in Synchronous mode.
77
2467X–AVR–06/11
ATmega128
78
2467X–AVR–06/11
ATmega128
79
2467X–AVR–06/11
ATmega128
Alternate Functions of The Port E pins with alternate functions are shown in Table 39.
Port E
Table 39. Port E Pins Alternate Functions
Port Pin Alternate Function
PE7 INT7/ICP3(1) (External Interrupt 7 Input or Timer/Counter3 Input Capture Pin)
PE6 INT6/ T3(1) (External Interrupt 6 Input or Timer/Counter3 Clock Input)
INT5/OC3C(1) (External Interrupt 5 Input or Output Compare and PWM Output C
PE5
for Timer/Counter3)
INT4/OC3B(1) (External Interrupt4 Input or Output Compare and PWM Output B for
PE4
Timer/Counter3)
AIN1/OC3A (1) (Analog Comparator Negative Input or Output Compare and PWM
PE3
Output A for Timer/Counter3)
AIN0/XCK0(1) (Analog Comparator Positive Input or USART0 external clock
PE2
input/output)
PE1 PDO/TXD0 (Programming Data Output or UART0 Transmit Pin)
PE0 PDI/RXD0 (Programming Data Input or UART0 Receive Pin)
Note: 1. ICP3, T3, OC3C, OC3B, OC3B, OC3A, and XCK0 not applicable in ATmega103 compatibility
mode.
• INT7/ICP3 – Port E, Bit 7
INT7, External Interrupt source 7: The PE7 pin can serve as an external interrupt source.
ICP3 – Input Capture Pin3: The PE7 pin can act as an Input Capture Pin for Timer/Counter3.
• INT6/T3 – Port E, Bit 6
INT6, External Interrupt source 6: The PE6 pin can serve as an external interrupt source.
T3, Timer/Counter3 counter source.
• INT5/OC3C – Port E, Bit 5
INT5, External Interrupt source 5: The PE5 pin can serve as an External Interrupt source.
OC3C, Output Compare Match C output: The PE5 pin can serve as an External output for the
Timer/Counter3 Output Compare C. The pin has to be configured as an output (DDE5 set “one”)
to serve this function. The OC3C pin is also the output pin for the PWM mode timer function.
• INT4/OC3B – Port E, Bit 4
INT4, External Interrupt source 4: The PE4 pin can serve as an External Interrupt source.
OC3B, Output Compare Match B output: The PE4 pin can serve as an External output for the
Timer/Counter3 Output Compare B. The pin has to be configured as an output (DDE4 set (one))
to serve this function. The OC3B pin is also the output pin for the PWM mode timer function.
• AIN1/OC3A – Port E, Bit 3
AIN1 – Analog Comparator Negative input. This pin is directly connected to the negative input of
the Analog Comparator.
OC3A, Output Compare Match A output: The PE3 pin can serve as an External output for the
Timer/Counter3 Output Compare A. The pin has to be configured as an output (DDE3 set “one”)
to serve this function. The OC3A pin is also the output pin for the PWM mode timer function.
• AIN0/XCK0 – Port E, Bit 2
AIN0 – Analog Comparator Positive input. This pin is directly connected to the positive input of
the Analog Comparator.
80
2467X–AVR–06/11
ATmega128
XCK0, USART0 External clock. The Data Direction Register (DDE2) controls whether the clock
is output (DDE2 set) or input (DDE2 cleared). The XCK0 pin is active only when the USART0
operates in Synchronous mode.
• PDO/TXD0 – Port E, Bit 1
PDO, SPI Serial Programming Data Output. During Serial Program Downloading, this pin is
used as data output line for the ATmega128.
TXD0, UART0 Transmit pin.
• PDI/RXD0 – Port E, Bit 0
PDI, SPI Serial Programming Data Input. During Serial Program Downloading, this pin is used
as data input line for the ATmega128.
RXD0, USART0 Receive Pin. Receive Data (Data input pin for the USART0). When the
USART0 receiver is enabled this pin is configured as an input regardless of the value of DDRE0.
When the USART0 forces this pin to be an input, a logical one in PORTE0 will turn on the inter-
nal pull-up.
Table 40 and Table 41 relates the alternate functions of Port E to the overriding signals shown in
Figure 33 on page 70.
81
2467X–AVR–06/11
ATmega128
Alternate Functions of The Port F has an alternate function as analog input for the ADC as shown in Table 42. If some
Port F Port F pins are configured as outputs, it is essential that these do not switch when a conversion
is in progress. This might corrupt the result of the conversion. In ATmega103 compatibility mode
Port F is input only. If the JTAG interface is enabled, the pull-up resistors on pins PF7(TDI),
PF5(TMS), and PF4(TCK) will be activated even if a Reset occurs.
82
2467X–AVR–06/11
ATmega128
TMS, JTAG Test Mode Select: This pin is used for navigating through the TAP-controller state
machine. When the JTAG interface is enabled, this pin can not be used as an I/O pin.
• TCK, ADC4 – Port F, Bit 4
ADC4, Analog to Digital Converter, Channel 4.
TCK, JTAG Test Clock: JTAG operation is synchronous to TCK. When the JTAG interface is
enabled, this pin can not be used as an I/O pin.
• ADC3 – ADC0 – Port F, Bit 3..0
Analog to Digital Converter, Channel 3..0.
83
2467X–AVR–06/11
ATmega128
Alternate Functions of In Atmel® AVR®ATmega103 compatibility mode, only the alternate functions are the defaults for
Port G Port G, and Port G cannot be used as General Digital Port Pins. The alternate pin configuration
is as follows:
84
2467X–AVR–06/11
ATmega128
85
2467X–AVR–06/11
ATmega128
Register
Description for I/O
Ports
86
2467X–AVR–06/11
ATmega128
In Atmel® AVR®ATmega103 compatibility mode, DDRC and PINC Registers are initialized to
being Push-Pull Zero Output. The port pins assumes their initial value, even if the clock is not
running. Note that the DDRC and PINC Registers are available in ATmega103 compatibility
mode, and should not be used for 100% back-ward compatibility.
87
2467X–AVR–06/11
ATmega128
Note that PORTF and DDRF Registers are not available in Atmel® AVR®ATmega103 compatibil-
ity mode where Port F serves as digital input only.
Note that PORTG, DDRG, and PING are not available in ATmega103 compatibility mode. In the
ATmega103 compatibility mode Port G serves its alternate functions only (TOSC1, TOSC2, WR,
RD and ALE).
88
2467X–AVR–06/11