我想找...

搜尋

分享

目錄

analogWriteResolution()

[Analog I/O]

描述

analogWriteResolution() sets the resolution of the analogWrite() function. It defaults to 8 bits (values between 0-255) for compatibility with AVR-based Arduino boards.

The 86Duino’s CPU includes twelve 32-bit PWM timers, each of which allows a PWM duty cycle of 10ns at the minimum.

In our implementation of the analogWrite() function with a 1000Hz PWM frequency, you can set the write resolution up to 13. And by setting the write resolution to 13, you can use analogWrite() with values between 0 and 8191 to set the PWM signal without rolling over.

語法

analogWriteResolution(bits)

參數

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 the allowed 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.

回傳

注意

If you set the analogWriteResolution() value to a value higher than the allowed capabilities, our implementation will discard the extra bits. For example: using the 86Duino with analogWriteResolution(16), only the first 13 bits of the values passed to analogWrite() will be used and the last 3 bits will be discarded.

If you set the analogWriteResolution() value to a value lower than the allowed capabilities, the missing bits will be padded with zeros to fill the required size. For example: using the 86Duino with analogWriteResolution(8), the 86Duino will add 5 zero bits to the 8-bit value used in analogWrite() to obtain the 13 bits required.

範例程式碼

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("Analog Read) : ");
  Serial.print(sensorVal);
 
  // the default PWM resolution
  analogWriteResolution(8);
  analogWrite(11, map(sensorVal, 0, 1023, 0 ,255));
  Serial.print(" , 8-bit PWM value : ");
  Serial.print(map(sensorVal, 0, 1023, 0 ,255));
 
  // change the PWM resolution to 12 bits
  // the full 12 bit resolution is only supported
  // on the Due
  analogWriteResolution(12);
  analogWrite(12, map(sensorVal, 0, 1023, 0, 4095));
  Serial.print(" , 12-bit PWM value : ");
  Serial.print(map(sensorVal, 0, 1023, 0, 4095));
 
  // change the PWM resolution to 4 bits
  analogWriteResolution(4);
  analogWrite(13, map(sensorVal, 0, 1023, 0, 127));
  Serial.print(", 4-bit PWM value : ");
  Serial.println(map(sensorVal, 0, 1023, 0, 127));
 
  delay(5);
}

參考


語法參考主頁面

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

發表評論

上部へスクロール