Skip to content
yz88 edited this page Oct 7, 2018 · 9 revisions

Port registers

Port registers allow for lower-level and faster manipulation of the i/o pins of the microcontroller on an Arduino board. The chips used on the Arduino board have three ports:

  • B (digital pin 8 to 13)
  • C (analog input pins)
  • D (digital pins 0 to 7)

Each port is controlled by three registers, which are also defined variables in the arduino language.

  • The DDR register, determines whether the pin is an INPUT or OUTPUT.
  • The PORT register controls whether the pin is HIGH or LOW
  • The PIN register reads the state of INPUT pins set to input 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.

PORTD

maps to Arduino digital pins 0 to 7

  • DDRD - The Port D Data Direction Register - read/write
  • PORTD - The Port D Data Register - read/write
  • PIND - The Port D Input Pins Register - read only

PORTB

maps to Arduino digital pins 8 to 13. The two high bits (6 & 7) map to the crystal pins and are not usable

  • DDRB - The Port B Data Direction Register - read/write
  • PORTB - The Port B Data Register - read/write
  • PINB - The Port B Input Pins Register - read only

PORTC

maps to Arduino analog pins 0 to 5. Pins 6 & 7 are only accessible on the Arduino Mini

  • DDRC - The Port C Data Direction Register - read/write
  • PORTC - The Port C Data Register - read/write
  • PINC - The Port C Input Pins Register - read only

PORTC data register

bit 7 6 5 4 3 2 1 0
(high) (low)
- PORTC6 PORTC5 PORTC4 PORTC3 PORTC2 PORTC1 PORTC0
Read/Write R R/W R/W R/W R/W R/W R/W R/W
initial value 0 0 0 0 0 0 0 0

Port mapping

pin Ax PORTCx PCx ADCx
analog input 0 A0 PORTC0 PC0 ADC0
analog input 1 A1 PORTC1 PC1 ADC1
analog input 2 A2 PORTC2 PC2 ADC2
analog input 3 A3 PORTC3 PC3 ADC3
analog input 4 A4 PORTC4 PC4 ADC4
analog input 5 A5 PORTC5 PC5 ADC5
reset PORTC6 PC6 RESE

Each bit of these registers corresponds to a single pin; e.g. the low bit of DDRC, PORTC, and PINC refers to pin PC0 (analog pin 0, A0). For a complete mapping of Arduino pin numbers to ports and bits, see the diagram for your chip: ATmega8, ATmega168. (Note that some bits of a port may be used for things other than i/o; be careful not to change the values of the register bits corresponding to them.)

Examples

Referring to the pin map above, the PortC registers control Arduino analog pins 0 to 5.

DDRC is the direction register for Port C (Arduino analog pins 0-5). The bits in this register control whether the pins in PORTC are configured as inputs or outputs so, for example:

DDRC = B00000000;  // sets Arduino analog pins 0 to 5 as inputs (0)
DDRC = B00111111;  // sets Arduino analog pins 0 to 5 as outputs (1)

PORTC is the register for the state of the outputs. For example; PORTC = B00101000; // sets analog pins 5,3 HIGH

You will only see 5 volts on these pins however if the pins have been set as outputs using the DDRC register or with pinMode().

PINC is the input register variable It will read all of the analog input pins at the same time.

Clone this wiki locally