Playing with Arduino
A page to record my playing with Arduino
portInputRegister()

Abstract

The portInputRegister() is a macro that returns an input port register of the specified port.

Source Code

The portInputRegister() is defined in hardware/arduino/avr/cores/arduino/Arduino.h as below.

1
#define portInputRegister(P) ( (volatile uint8_t *)( pgm_read_word( port_to_input_PGM + (P))) )

The input is P. The macro calls pgm_read_word() with an argument port_to_input_PGM + (P).

The port_to_input_PGM is defined in hardware/arduino/variants/standard/pins_arduino.h(in case of Arduino Uno). It is an array shown below. As this array is declared as PROGMEM, it is placed on flash memory rather than SRAM.

1
2
3
4
5
6
7
const uint16_t PROGMEM port_to_input_PGM[] = {
        NOT_A_PORT,
        NOT_A_PORT,
        (uint16_t) &PINB,
        (uint16_t) &PINC,
        (uint16_t) &PIND,
};

As the port_to_input_PGM is an array, adding (P) to the port_to_input_PGM means the (P+1)th element of the array. In C++ language, the element of array x[i] can be accessed by *(x + i). For example, if the P is PB(2), the third element(note that the elements begins with 0) of the array, that is the address of PORTB, is returned.

PORTB, PORTC and PORTD are registers to execute input or output of digital pins.

The pgm_read_word() is a macro to read a variable defined as PROGMEM.

As a result of portOutputRegister(), it returns the (P+1)th element of port_to_input_PGM that is placed on flash memory.

Version

Arduino 1.8.13

Last Update

June 19, 2020

inserted by FC2 system