Index

Arduino Multi-Processor

Hardware Design

Bus Organisation

Library Software

  1. Port Registers

The Bus Controller

Node Identifiers

Miscellaneous Functions

Programming Techniques

Schematics

Firmware

images/5-1.png

Port Registers



The ATmega 328 has 3 sets of registers of controlling the I/O pins. These are called port B, port C and port D. Each port is associated with 3 registers called DDRx, PORTx and PINx, where x is either B, C, or D. The bits in the DDRx registers are used to set individual pins controlled by the corresponding port to be either inputs (bit = 0) or outputs (bit = 1). If a pin is set to be an output, writing to the corresponding bit in the PORTx register will set its value. reading from a PORTx register will return the value last written to it independently of which bits have been set to outputs or inputs, i.e. if a pin is set to be an input, reading PORTx will not be affected by the actual state of the pin. To read the state of any pins set to be inputs, the PINx register must be used.

Bits in port registers map onto pins as follows.

Port B
bit 0bit 1bit 2bit 3bit 4bit 5bit 6bit 7
D8D9D10D11D12D13N/AN/A


Port C
bit 0bit 1bit 2bit 3bit 4bit 5bit 6bit 7
A0A1A2A3A4A5N/AN/A


Port D
bit 0bit 1bit 2bit 3bit 4bit 5bit 6bit 7
RxTxD2D3D4D5D6D7


Examples
void set_port_c(byte value)
{
  DDRC  = DDRC  | 0b00111111;            // set all pins to be outputs;
  PORTC = PORTC | (value & 0b00111111);  // ouput lower 6 bits of value on all 6 pins;
}

byte read_port_c()
{
  DDRC = DDRC & ~0b00111111;  // set all pins to be inputs;
  return PINC & 0b00111111;   // return lower 6 bits of port data;
}