这里会显示出您选择的修订版和当前版本之间的差别。
后一修订版 | 前一修订版 | ||
reference:language:analogwriteresolution [2016/01/06 07:41] – created nzhu | en:reference:language:analogwriteresolution [2023/06/07 04:23] (当前版本) – 外部编辑 127.0.0.1 | ||
---|---|---|---|
行 1: | 行 1: | ||
+ | |||
+ | ====== Description ====== | ||
+ | |||
+ | |||
+ | analogWriteResolution() is an extension of the Analog API for the Arduino Due and Zero. | ||
+ | |||
+ | analogWriteResolution() sets the resolution of the analogWrite() function. It defaults to 8 bits (values between 0-255) for backward compatibility with AVR based boards. | ||
+ | |||
+ | The Due has the following hardare capabilities: | ||
+ | |||
+ | 12 pins which default to 8-bit PWM, like the AVR-based boards. These can be changed to 12-bit resolution. | ||
+ | 2 pins with 12-bit DAC (Digital-to-Analog Converter) | ||
+ | By setting the write resolution to 12, you can use analogWrite() with values between 0 and 4095 to exploit the full DAC resolution or to set the PWM signal without rolling over. | ||
+ | |||
+ | The Zero has the following hardare capabilities: | ||
+ | |||
+ | 10 pins which default to 8-bit PWM, like the AVR-based boards. These can be changed to 12-bit resolution. | ||
+ | 1 pin with 10-bit DAC (Digital-to-Analog Converter). | ||
+ | By setting the write resolution to 10, you can use analogWrite() with values between 0 and 1023 to exploit the full DAC resolution | ||
+ | |||
+ | ===== Syntax ===== | ||
+ | |||
+ | |||
+ | analogWriteResolution(bits) | ||
+ | |||
+ | ===== Parameters ===== | ||
+ | |||
+ | |||
+ | bits: determines the resolution (in bits) of the values used in the analogWrite() function. The value can range from 1 to 32. If you choose a resolution higher or lower than your board' | ||
+ | |||
+ | ===== Returns ===== | ||
+ | |||
+ | |||
+ | None. | ||
+ | |||
+ | Note | ||
+ | |||
+ | If you set the analogWriteResolution() value to a value higher than your board' | ||
+ | |||
+ | If you set the analogWriteResolution() value to a value lower than your board' | ||
+ | |||
+ | ===== Example ===== | ||
+ | |||
+ | <code cpp> | ||
+ | void setup(){ | ||
+ | // open a serial connection | ||
+ | Serial.begin(9600); | ||
+ | // make our digital pin an output | ||
+ | pinMode(11, OUTPUT); | ||
+ | pinMode(12, OUTPUT); | ||
+ | pinMode(13, OUTPUT); | ||
+ | } | ||
+ | |||
+ | void loop(){ | ||
+ | // read the input on A0 and map it to a PWM pin | ||
+ | // with an attached LED | ||
+ | int sensorVal = analogRead(A0); | ||
+ | Serial.print(" | ||
+ | Serial.print(sensorVal); | ||
+ | |||
+ | // the default PWM resolution | ||
+ | analogWriteResolution(8); | ||
+ | analogWrite(11, | ||
+ | Serial.print(" | ||
+ | Serial.print(map(sensorVal, | ||
+ | |||
+ | // change the PWM resolution to 12 bits | ||
+ | // the full 12 bit resolution is only supported | ||
+ | // on the Due | ||
+ | analogWriteResolution(12); | ||
+ | analogWrite(12, | ||
+ | Serial.print(" | ||
+ | Serial.print(map(sensorVal, | ||
+ | |||
+ | // change the PWM resolution to 4 bits | ||
+ | analogWriteResolution(4); | ||
+ | analogWrite(13, | ||
+ | Serial.print(", | ||
+ | Serial.println(map(sensorVal, | ||
+ | |||
+ | delay(5); | ||
+ | } | ||
+ | </ |