portModeRegister()

Abstract

The portModeRegister() is a macro that returns a mode register that controlls the mode of the specified port.

Souce Code

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

1
#define portModeRegister(P) ( (volatile uint8_t *)( pgm_read_word( port_to_mode_PGM + (P))) )

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

The port_to_mode_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_mode_PGM[] = {
        NOT_A_PORT,
        NOT_A_PORT,
        (uint16_t) &DDRB,
        (uint16_t) &DDRC,
        (uint16_t) &DDRD,
};

As the port_to_mode_PGM is an array, adding (P) to the port_to_mode_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 DDRB, is returned.

DDRB, DDRC and DDRDare registers which determine if the deigital pin is output mode or input mode.

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

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

バージョン

Arduino AVR Boards 1.8.6

License

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
/*
  Copyright (c) 2016 garretlab.
  Added source code explanation by garretlab.
*/

/*
  Arduino.h - Main include file for the Arduino SDK
  Copyright (c) 2005-2013 Arduino Team.  All right reserved.

  This library is free software; you can redistribute it and/or
  modify it under the terms of the GNU Lesser General Public
  License as published by the Free Software Foundation; either
  version 2.1 of the License, or (at your option) any later version.

  This library is distributed in the hope that it will be useful,
  but WITHOUT ANY WARRANTY; without even the implied warranty of
  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
  Lesser General Public License for more details.

  You should have received a copy of the GNU Lesser General Public
  License along with this library; if not, write to the Free Software
  Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
*/

Last Update

July 31, 2025

inserted by FC2 system