portModeRegister()

概要

portModeRegister()は、指定したポートのモードを制御するレジスタを返すマクロです。

ソースコード

portModeRegister()は、hardware/arduino/avr/cores/arduino/Arduino.h に定義されています。以下に全ソースコードを示します。

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

入力はPで、port_to_mode_PGMにPを足した値をpgm_read_word()に渡しています。pgm_read_word()は、PROGMEM指定した変数を読み出すためのマクロで、hardware/tools/avr/avr/include/avr/pgmspace.hに定義されています。

port_to_mode_PGMは、Arduino Unoの場合、hardware/arduino/avr/variants/standard/pins_arduino.h に定義されています。port_to_mode_PGMは、以下に示す配列です。この配列はPROGMEMが指定されているため、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,
};

port_to_mode_PGMは配列なので、port_to_mode_PGM + (P)というのは、足し算は足し算ですが、配列port_to_mode_PGMのP+1番目の要素が入っているアドレスを表すことになります。C言語では、配列x[i]は、*(x + i)という形でアクセスすることができます。例えば、PがPB(2)のときは、3番目(配列は0番から始まります)の要素であるDDRBが入っているアドレスということになります。

DDRBとDDRC、DDRDは、デジタルピンが入力か出力かを示すレジスタです。

pgm_read_word()は、PROGMEM指定した変数を読み出すためのマクロです。

結果としてportModeRegister()は、フラッシュメモリに配置したport_to_mode_PGMのP+1番目の要素を返却することになります。

バージョン

Arduino AVR Boards 1.8.6

ライセンス

 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
*/

最終更新日

July 31, 2025

inserted by FC2 system