|
| 1 | +:source-highlighter: pygments |
| 2 | +:pygments-style: arduino |
| 3 | +:ext-relative: adoc |
| 4 | + |
| 5 | += analogWriteResolution() |
| 6 | + |
| 7 | + |
| 8 | +// OVERVIEW SECTION STARTS |
| 9 | +[#overview] |
| 10 | +-- |
| 11 | + |
| 12 | +[float] |
| 13 | +=== Description |
| 14 | +`analogWriteResolution()` is an extension of the Analog API for the Arduino Due. |
| 15 | + |
| 16 | +`analogWriteResolution()` sets the resolution of the `analogWrite()` function. It defaults to 8 bits (values between 0-255) for backward compatibility with AVR based boards. |
| 17 | + |
| 18 | +The Due has the following hardare capabilities |
| 19 | +* 12 pins which default to 8-bit PWM, like the AVR-based boards. These can be changed to 12-bit resolution. |
| 20 | +* Pns with 12-bit DAC (Digital-to-Analog Converter). |
| 21 | + |
| 22 | +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. |
| 23 | +[%hardbreaks] |
| 24 | + |
| 25 | + |
| 26 | +[float] |
| 27 | +=== Syntax |
| 28 | +`analogWriteResolution(bits)` |
| 29 | + |
| 30 | + |
| 31 | +[float] |
| 32 | +=== Parameters |
| 33 | +`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's hardware capabilities, the value used in `analogWrite()` will be either truncated if it's too high or padded with zeros if it's too low. See the note below for details. |
| 34 | + |
| 35 | +[float] |
| 36 | +=== Returns |
| 37 | +Nothing |
| 38 | + |
| 39 | +-- |
| 40 | +// OVERVIEW SECTION ENDS |
| 41 | + |
| 42 | + |
| 43 | + |
| 44 | + |
| 45 | +// HOW TO USE SECTION STARTS |
| 46 | +[#howtouse] |
| 47 | +-- |
| 48 | + |
| 49 | +[float] |
| 50 | +=== Example Code |
| 51 | +// Describe what the example code is all about and add relevant code ►►►►► THIS SECTION IS MANDATORY ◄◄◄◄◄ |
| 52 | +Explain Code |
| 53 | + |
| 54 | +[source,arduino] |
| 55 | +---- |
| 56 | +void setup(){ |
| 57 | + // open a serial connection |
| 58 | + Serial.begin(9600); |
| 59 | + // make our digital pin an output |
| 60 | + pinMode(11, OUTPUT); |
| 61 | + pinMode(12, OUTPUT); |
| 62 | + pinMode(13, OUTPUT); |
| 63 | +} |
| 64 | +
|
| 65 | +void loop(){ |
| 66 | + // read the input on A0 and map it to a PWM pin |
| 67 | + // with an attached LED |
| 68 | + int sensorVal = analogRead(A0); |
| 69 | + Serial.print("Analog Read) : "); |
| 70 | + Serial.print(sensorVal); |
| 71 | +
|
| 72 | + // the default PWM resolution |
| 73 | + analogWriteResolution(8); |
| 74 | + analogWrite(11, map(sensorVal, 0, 1023, 0 ,255)); |
| 75 | + Serial.print(" , 8-bit PWM value : "); |
| 76 | + Serial.print(map(sensorVal, 0, 1023, 0 ,255)); |
| 77 | +
|
| 78 | + // change the PWM resolution to 12 bits |
| 79 | + // the full 12 bit resolution is only supported |
| 80 | + // on the Due |
| 81 | + analogWriteResolution(12); |
| 82 | + analogWrite(12, map(sensorVal, 0, 1023, 0, 4095)); |
| 83 | + Serial.print(" , 12-bit PWM value : "); |
| 84 | + Serial.print(map(sensorVal, 0, 1023, 0, 4095)); |
| 85 | +
|
| 86 | + // change the PWM resolution to 4 bits |
| 87 | + analogWriteResolution(4); |
| 88 | + analogWrite(13, map(sensorVal, 0, 1023, 0, 127)); |
| 89 | + Serial.print(", 4-bit PWM value : "); |
| 90 | + Serial.println(map(sensorVal, 0, 1023, 0, 127)); |
| 91 | +
|
| 92 | + delay(5); |
| 93 | +} |
| 94 | +---- |
| 95 | +[%hardbreaks] |
| 96 | + |
| 97 | +[float] |
| 98 | +=== Notes and Warnings |
| 99 | +If you set the `analogWriteResolution()` value to a value higher than your board's capabilities, the Arduino will discard the extra bits. For example: using the Due with `analogWriteResolution(16)` on a 12-bit DAC pin, only the first 12 bits of the values passed to `analogWrite()` will be used and the last 4 bits will be discarded. |
| 100 | + |
| 101 | +If you set the `analogWriteResolution()` value to a value lower than your board's capabilities, the missing bits will be *padded with zeros* to fill the hardware required size. For example: using the Due with analogWriteResolution(8) on a 12-bit DAC pin, the Arduino will add 4 zero bits to the 8-bit value used in `analogWrite()` to obtain the 12 bits required. |
| 102 | +[%hardbreaks] |
| 103 | + |
| 104 | +[float] |
| 105 | +=== See also |
| 106 | +// Link relevant content by category, such as other Reference terms (please add the tag #LANGUAGE#), |
| 107 | +// definitions (please add the tag #DEFINITION#), and examples of Projects and Tutorials |
| 108 | +// (please add the tag #EXAMPLE#) ►►►►► THIS SECTION IS MANDATORY ◄◄◄◄◄ |
| 109 | + |
| 110 | +[role="example"] |
| 111 | +* #EXAMPLE# http://arduino.cc/en/Tutorial/AnalogInputPins[Description of the analog input pins] |
| 112 | + |
| 113 | +[role="language"] |
| 114 | +* #LANGUAGE# link:analogWrite{ext-relative}[analogWrite()] + |
| 115 | +* #LANGUAGE# link:analogRead{ext-relative}[analogRead()] + |
| 116 | +* #LANGUAGE# link:map{ext-relative}[map()] |
| 117 | + |
| 118 | + |
| 119 | +-- |
| 120 | +// HOW TO USE SECTION ENDS |
0 commit comments