这里会显示出您选择的修订版和当前版本之间的差别。
后一修订版 | 前一修订版 | ||
reference:language:analogreadresolution [2016/01/06 07:42] – created nzhu | en:reference:language:analogreadresolution [2023/06/07 04:23] (当前版本) – 外部编辑 127.0.0.1 | ||
---|---|---|---|
行 1: | 行 1: | ||
+ | ====== analogReadResolution() ====== | ||
+ | |||
+ | ===== Description ===== | ||
+ | |||
+ | |||
+ | analogReadResolution() is an extension of the Analog API for the Arduino Due and Zero. | ||
+ | |||
+ | Sets the size (in bits) of the value returned by analogRead(). It defaults to 10 bits (returns values between 0-1023) for backward compatibility with AVR based boards. | ||
+ | |||
+ | The Due and the Zero have 12-bit ADC capabilities that can be accessed by changing the resolution to 12. This will return values from analogRead() between 0 and 4095. | ||
+ | |||
+ | ===== Syntax ===== | ||
+ | |||
+ | |||
+ | analogReadResolution(bits) | ||
+ | |||
+ | ===== Parameters ===== | ||
+ | |||
+ | |||
+ | bits: determines the resolution (in bits) of the value returned by analogRead() function. You can set this 1 and 32. You can set resolutions higher than 12 but values returned by analogRead() will suffer approximation. See the note below for details. | ||
+ | |||
+ | ===== Returns ===== | ||
+ | |||
+ | |||
+ | None. | ||
+ | |||
+ | ===== Note ===== | ||
+ | |||
+ | |||
+ | If you set the analogReadResolution() value to a value higher than your board' | ||
+ | |||
+ | For example: using the Due or the Zero with analogReadResolution(16) will give you an approximated 16-bit number with the first 12 bits containing the real ADC reading and the last 4 bits padded with zeros. | ||
+ | |||
+ | If you set the analogReadResolution() value to a value lower than your board' | ||
+ | |||
+ | Using a 16 bit resolution (or any resolution higher than actual hardware capabilities) allows you to write sketches that automatically handle devices with a higher resolution ADC when these become available on future boards without changing a line of code. | ||
+ | |||
+ | ===== Example ===== | ||
+ | |||
+ | <code cpp> | ||
+ | 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(" | ||
+ | Serial.print(analogRead(A0)); | ||
+ | |||
+ | // change the resolution to 12 bits and read A0 | ||
+ | analogReadResolution(12); | ||
+ | Serial.print(", | ||
+ | Serial.print(analogRead(A0)); | ||
+ | |||
+ | // change the resolution to 16 bits and read A0 | ||
+ | analogReadResolution(16); | ||
+ | Serial.print(", | ||
+ | Serial.print(analogRead(A0)); | ||
+ | |||
+ | // change the resolution to 8 bits and read A0 | ||
+ | analogReadResolution(8); | ||
+ | Serial.print(", | ||
+ | Serial.println(analogRead(A0)); | ||
+ | |||
+ | // a little delay to not hog serial monitor | ||
+ | delay(100); | ||
+ | }</ |