Skip to content

portOutputReg

gicking edited this page Feb 6, 2018 · 3 revisions

back to Command Reference / Digital-IO

Description

Direct write access to the port output state register. This allows fast write access e.g. for parallel interfaces.

For all port pins, which have been configured as an OUTPUT with pinMode(), writing a state drives the corresponding pin voltage: Vdd for HIGH, 0V (ground) for LOW.

Notes:

  • after reset all I/O port pins are configured as INPUT
  • for port pins configured as INPUT*, writing to portOutputReg() has no effect.

Inclusion

  • defined in gpio.h
  • auto-loaded in main_general.h
  • no #define required

Syntax

portOutputReg(pPort)

Parameters

  • input:

    • pPort: address of port you wish to set, e.g. &PORT_D
  • output:

    • none

Returns

  • register for setting port output states

Example Code

The below code makes pins PD0..3 to output, push-pull, fast, and pins PD4..7 to input, pull-up, no interrupt. Then toggle all output port pins HIGH and LOW with 1s delay.

#include "main_general.h"   // board-independent main

void setup()
{
  // set PD0..3 to output/push-pull/fast, and  
  portMode(&PORT_D, 0b00001111, 0b11111111, 0b00001111);
}

void loop()
{
  portOutputReg(&PORT_D) ^= 0b1111111;  // toggle PD0..3, others are inputs -> don't care
  delay(1000);                          // wait 1s
}

Relevant Tutorial

  • tbd

Notes and Warnings

To avoid damage

  • do not expose I/Os to voltages outside [-0.3V; Vdd+0.3V], else limit injection currents to the specificied limits
  • for OUTPUT pins assert that sink and source currents are below the specificied limits
  • do not directly connect two OUTPUT pins. If e.g. half-duplex is required, use a pull-up and OUTPUT_OPENDRAIN, instead

See also

Clone this wiki locally