說明
analogReadResolution() 是用於 Zero、Due、MKR 係列、Nano 33(BLE 和 IoT)和 Portenta 的模擬 API 的擴展。
設置 analogRead()
返回的值的大小(以位為單位)。它默認為 10 位(返回 0-1023 之間的值)以向後兼容基於 AVR 的板。
Zero、Due、MKR 係列和 Nano 33(BLE 和 IoT)板具有 12 位 ADC 函數,可以通過將分辨率更改為 12 來訪問。這將返回值analogRead()
介於 0 和 4095 之間。
波特塔H7有一個 16 位 ADC,它允許 0 到 65535 之間的值。
用法
analogReadResolution(bits)
參數
bits
:確定analogRead()
函數返回的值的分辨率(以位為單位)。您可以將其設置在 1 到 32 之間。您可以將分辨率設置為高於支持的 12 位或 16 位,但 analogRead()
返回的值將受到近似值。有關詳細信息,請參閱下麵的注釋。
返回
無
示例代碼
該代碼顯示了如何使用具有不同分辨率的 ADC。
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()
值設置為高於您的板能力的值,Arduino 將僅以最高分辨率報告,用零填充額外的位。
例如:使用帶有analogReadResolution(16)
的 Due 將為您提供一個近似的 16 位數字,其中前 12 位包含實際 ADC 讀數,最後 4 位用零填充。
如果您將 analogReadResolution()
值設置為低於您的板能力的值,則從 ADC 讀取的額外最低有效位將被丟棄。
使用 16 位分辨率(或任何高於實際硬件函數的分辨率)允許您編寫草圖,當這些設備在未來的板上可用時自動處理具有更高分辨率 ADC 的設備,而無需更改代碼行。
相關用法
- Arduino analogRead()用法及代碼示例
- Arduino analogWriteResolution()用法及代碼示例
- Arduino analogWrite()用法及代碼示例
- Arduino array用法及代碼示例
- Arduino abs()用法及代碼示例
- Arduino attachInterrupt()用法及代碼示例
- Arduino long用法及代碼示例
- Arduino Arduino_EMBRYO_2 - setLengthXY()用法及代碼示例
- Arduino ~用法及代碼示例
- Arduino ArduinoBLE - bleDevice.advertisedServiceUuidCount()用法及代碼示例
- Arduino const用法及代碼示例
- Arduino Ethernet - server.begin()用法及代碼示例
- Arduino ArduinoBLE - BLEService()用法及代碼示例
- Arduino digitalWrite()用法及代碼示例
- Arduino ArduinoBLE - bleCharacteristic.subscribe()用法及代碼示例
- Arduino Servo - attach()用法及代碼示例
- Arduino write()用法及代碼示例
- Arduino Arduino_LSM9DS1 - readGyroscope()用法及代碼示例
- Arduino ArduinoSound - FFTAnalyzer.input()用法及代碼示例
- Arduino MKRGSM - gprs.attachGPRS()用法及代碼示例
- Arduino WiFiNINA - WiFi.config()用法及代碼示例
- Arduino MKRGSM - sms.read()用法及代碼示例
- Arduino MKRNB - getCurrentCarrier()用法及代碼示例
- Arduino Scheduler - Scheduler.startLoop()用法及代碼示例
- Arduino Arduino_LSM9DS1 - magneticFieldAvailable()用法及代碼示例
注:本文由純淨天空篩選整理自arduino.cc大神的英文原創作品 analogReadResolution()。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。