Arduino Multi-Processor

Hardware Design

Bus Organisation

Library Software

  1. Port Registers

The Bus Controller

Node Identifiers

Miscellaneous Functions

Programming Techniques




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

Port C
bit 0bit 1bit 2bit 3bit 4bit 5bit 6bit 7

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

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;