analogReadResolution()

[Analog I/O]

描述

Sets the size (in bits) of the value returned by analogRead(). It defaults to 10 bits (returns values between 0-1023) for compatibility with AVR-based Arduino boards.

The 86Duino boards have 11-bit ADC capabilities that can be accessed by changing the resolution to 11. This will return values from analogRead() between 0 and 2047.

語法

analogReadResolution(bits)

參數

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 11 but values returned by analogRead() will suffer approximation. See the note below for details.

回傳

Note

If you set the analogReadResolution() value to a value higher than your board’s capabilities, the 86Duino will only report back at its highest resolution padding the extra bits with zeros.

For example: using the 86Duino with analogReadResolution(16) will give you an approximated 16-bit number with the first 11 bits containing the real ADC reading and the last 5 bits padded with zeros.

If you set the analogReadResolution() value to a value lower than your board’s capabilities, the extra least significant bits read from the ADC will be discarded.

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

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);
}

參考


語法參考主頁面

86Duino 參考的文本是根據 Creative Commons Attribution-ShareAlike 3.0 License,部分文本是從 the Arduino reference 修改的。 參考中的代碼示例已發佈到公共領域。

發表評論

上部へスクロール