Port Manipulation

Intro

Port registers allow lower-level and faster manipulation of the I/O pins of the microcontroller. Each port is controlled by three registers:

  • DDR register – determines whether the pin is an INPUT or OUTPUT
  • PORT register – controls whether the pin is HIGH or LOW
  • PIN register – reads the state of INPUT pins set with pinMode()

DDR and PORT registers may be both written to and read. PIN registers correspond to the state of inputs and may only be read.

See also: Arduino Port Manipulation reference

Output Parallelization

It is possible to parallelize some digital outputs to drive loads requiring more current, under these conditions:

  • Outputs are controlled from the same processor port (set via one instruction)
  • There is no delay between control signals for parallelized outputs

MINI: D0–D3 | D4–D5 | D6–D7

MAXI: D0,D1,D3 | D2 | D4–D7 | D8–D11

MEGA: D0,D1,D3 | D2 | D4–D7 | D8–D11 | D12–D19 | D20–D22 | D23

Hardware Required

  • NEXTuino RISE/MAXI/MEGA
  • 12/24V DC Power supply

Note: Pin header is working on 5V TTL levels. Voltage levels over 5.5V can damage the NEXTuino permanently.

Code

The OVL LED is connected to the 7th bit of PORT E on the ATmega2560. There is no Arduino number for this pin, so it must be controlled via port manipulation.

Blinking the OVL LED

void setup() {
  // Define PE7 as OUTPUT
  DDRE = DDRE | B10000000;
}

void loop() {
  // Set PE7 HIGH (OR logic — does not affect other bits)
  PORTE = PORTE | B10000000;
  delay(1000);
  // Set PE7 LOW (AND logic — does not affect other bits)
  PORTE = PORTE & B01111111;
  delay(1000);
}

Reading the OVL (Overload) Signal

void setup() {
  Serial.begin(9600);
  DDRE = DDRE & B01111111; // Set PE7 (OVL pin) as INPUT
  Serial.println("NEXTuino Overload read:");
}

void loop() {
  Serial.print("Overload signal: ");
  int Overload_signal = PINE >> 7;
  if (Overload_signal == 1) Serial.println("OFF");
  else Serial.println("ON");
  delay(1000);
}

To find which NEXTuino pin corresponds to which microprocessor PORT, refer to the NEXTuino PINOUT tables.