analogReadResolution()

名称

analogReadResolution()

説明

analogReadResolution()は、ZeroとDue、MKR family、Nano33(BLEとIoT)、Portenta向けのアナログAPIの拡張である。

analogRead()で返却する値の大きさ(分解能)をビット単位で設定する。デフォルト値は10ビット(0から1023)である。これは、AVRベースのボードとの後方互換性のためである。

分解能を12に設定することにより、ZeroとDue、MKR family、Nano33(BLEとIoT)、Portentaの12ビットのADC(アナログ・デジタル変換)を利用することができる。この場合、analogRead()は0から4095までの値を返す。

Portenta H7は、16ビットのADCを持っているので、0から65535までの値が許容される。

書式

void analogReadResolution(int res);

引数

resanalogRead()が返す値の分解能(ビット)を設定する。1から32の値を設定することができる。12や16よりも大きい値に設定できるが、analogRead()が返す値には近似の問題が発生する。詳細は、注意を参照。

戻り値

なし。

使用例

このコードは、異なる分解能でADCを利用する方法を示す。

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
void setup() {
  // open a serial connection
  Serial.begin(9600);
}
 
void loop() {
  // read the input on A0 at default resolution (10 bits)
  // and send it out the serial connection
  analogReadResolution(10);
  Serial.print("ADC 10-bit (default) : ");
  Serial.print(analogRead(A0));
 
  // change the resolution to 12 bits and read A0
  analogReadResolution(12);
  Serial.print(", 12-bit : ");
  Serial.print(analogRead(A0));
 
  // change the resolution to 16 bits and read A0
  analogReadResolution(16);
  Serial.print(", 16-bit : ");
  Serial.print(analogRead(A0));
 
  // change the resolution to 8 bits and read A0
  analogReadResolution(8);
  Serial.print(", 8-bit : ");
  Serial.println(analogRead(A0));
 
  // a little delay to not hog serial monitor
  delay(100);
}

注意

analogReadResolution()で、ボードの能力よりも大きい値を設定した場合、分解能を超えた部分を0パディングした値を返却する。

例えば、DueもしくはZeroでanalogReadResolution(16)とすると、最初の12ビットに実際のADCの読み取り値が入り、後の4ビットは0パディングされた16ビットの値が返却される。

analogReadResolution()でボードの能力よりも小さい値を設定した場合、ADCで読み取った値の下位のビットが捨てられる。

16ビットの分解能(もしくは実際のハードウェア能力より大きい値)を使えば、将来ボードのADCの分解能が大きくなったときに、自動的にそのボードを使うことができるスケッチを書くことができる。

参照

利用例 Description of the analog input pins

言語 analogRead()

訳者註

ソースコードを読んだ範囲では、以下の処理により、値の変換が行われているようです。

1
2
3
4
5
6
7
8
static inline uint32_t mapResolution(uint32_t value, uint32_t from, uint32_t to) {
        if (from == to)
                return value;
        if (from > to)
                return value >> (from-to);
        else
                return value << (to-from);
}

上記で、valueはADCから読み取った値です。fromにはADCが提供する分解能が入ります。12ビットのADCだと12が入ります。toは、analogReadResolution()で設定した値です。

オリジナルのページ

https://www.arduino.cc/reference/en/language/functions/zero-due-mkr-family/analogreadresolution/

Last Revision: 2020/10/21

実装の解析

まだ解析していません。

最終更新日

January 4, 2024

inserted by FC2 system