Section 5.1 Data Direction
Each I/O pin on the ATmega328P is bidirectional. Through associated registers, each pin can be configured as an input (which allows the microcontroller to read its corresponding logic level) or an output (which allows the microcontroller to send data to the pin). Figure 5.1.1 shows a schematic of how this works.
DDRxn controls the data direction, PORTxn controls the data sent to output pins, and PINxn contains the data state of input pins.The directionality of each I/O pin can be configured with the data direction register associated with that pin’s port. The three data direction registers on the ATmega328P are
DDRB, DDRC, and DDRD. By default upon startup, each bit in each data direction register has a value of 0, which corresponds to each I/O pin being configured as an input pin. By writing a 1 to a pin’s bit in its corresponding data direction register, that pin will be configured as an output pin.
Writing data to an output pin can be accomplished by sending data to that pin’s port data register. Each port on the microcontroller has its own port data register:
PORTB, PORTC, and PORTD. Reading data from an input pin can be accomplished by reading data from the corresponding pin register. The three pin registers are PINB, PINC, and PIND.
