Arduino內建的函式myservo.write與myservo.writeMicroseconds都有最小正導通週期的限制,且限制最小值在550us,因此像SG90這種操作在500us~2000us之間並不適用。
而Arduino編輯程式不僅內建許多函式,也可以直接採用AVR的C Code。
從IC腳位來看,共有6組PWM Output可用,Timer 0(D5,D6)、Timer 1(D9,D10)、Timer 3(D3,D11)。
const int analogOutPin = 9; //宣告輸出PWM Pin=9 int outputValue = 0; int degValue = 0; void setup() { pinMode(analogOutPin, OUTPUT);//設定Pin9為OUTPUT TCCR1A = ((1 << COM1A1) | (1 << WGM11)); // clear OC1A on match + WGM mode 14 TCCR1B = ((1 << WGM12) | (1 << WGM13) | (1 << CS11)); // WGM mode 14 (Fast PWM) ICR1 = 40000; //set ICR1 to produce 50Hz frequency (16000000 / 8 / 40000 = 50hz) OCR1A = 2000; // 40000 * 0.05 most left } // the loop routine runs over and over again forever: void loop() { degValue=900; outputValue = map(degValue, 0, 1800,2000, 4000); OCR1A = outputValue; delay(2000); }
http://www.avrbeginners.net/architecture/timers/timers.html
全站熱搜
留言列表