Postby HF CHENG » Thu Jul 14, 2022 2:15 pm
巧啊,我昨天也尝试相同的方法驱动舵机,可能你的占空比设置的不太正确,可以试一下我改的示例程序,看看有没有动起来
#define LEDC_CHANNEL_0 0// use first channel of 16 channels (started from zero)
#define LEDC_TIMER_13_BIT 13// use 13 bit precission for LEDC timer
#define LEDC_BASE_FREQ 50// use 50 Hz as a LEDC base frequency
#define LED_PIN 14// fade LED PIN (replace with LED_BUILTIN constant for built-in LED)
int brightness = 700; // how bright the LED is
int fadeAmount = 1; // how many points to fade the LED by
void ledcAnalogWrite(uint8_t channel, uint32_t value, uint32_t valueMax = 8191) {// Arduino like analogWrite// value has to be between 0 and valueMax
uint32_t duty = (8191 / valueMax) * min(value, valueMax);// calculate duty, 8191 from 2 ^ 13 - 1
ledcWrite(channel, duty);// write duty to LEDC
}
void setup() {
ledcSetup(LEDC_CHANNEL_0, LEDC_BASE_FREQ, LEDC_TIMER_13_BIT);// Setup timer and attach timer to a led pin
ledcAttachPin(LED_PIN, LEDC_CHANNEL_0);
}
void loop() {
ledcAnalogWrite(LEDC_CHANNEL_0, brightness);// set the brightness on LEDC channel 0
brightness = brightness + fadeAmount;// change the brightness for next time through the loop:
if (brightness <= 700 || brightness >= 899) {// reverse the direction of the fading at the ends of the fade:
fadeAmount = -fadeAmount;
}
delay(100);// wait for 30 milliseconds to see the dimming effect
}