#include <stdio.h>
#include "freertos/FreeRTOS.h"
#include "freertos/task.h"
#include "driver/ledc.h"
#define pinServo_forChannel_0 2
#define pinServo_forChannel_1 4
uint32_t duty;
void Servo(){
// First configure the timer of the LEDC
ledc_timer_config_t Timer = {
.speed_mode = LEDC_LOW_SPEED_MODE,
.timer_num = LEDC_TIMER_0,
.duty_resolution = LEDC_TIMER_13_BIT,
.freq_hz = 50,
.clk_cfg = LEDC_AUTO_CLK
};
ledc_timer_config(&Timer);
// Second configure the channel of the LEDC
ledc_channel_config_t ledc_channel = {
.speed_mode = LEDC_LOW_SPEED_MODE,
.channel = LEDC_CHANNEL_0,
.timer_sel = LEDC_TIMER_0,
.intr_type = LEDC_INTR_DISABLE,
.gpio_num = pinServo_forChannel_0,
.duty = 0,
.hpoint = 0
};
ledc_channel_config(&ledc_channel);
// Finally, set and update the PWM
for (uint32_t i = 200; i < 1000; i += 50) {
duty = i;
ledc_set_duty(LEDC_LOW_SPEED_MODE, LEDC_CHANNEL_0, duty);
//printf("duty->%ld\n", ledc_get_duty(LEDC_LOW_SPEED_MODE, LEDC_CHANNEL_0));
printf("duty -> %ld\n", duty);
ledc_update_duty(LEDC_LOW_SPEED_MODE, LEDC_CHANNEL_0);
vTaskDelay(1000 / portTICK_PERIOD_MS);
ledc_stop(LEDC_LOW_SPEED_MODE, LEDC_CHANNEL_0 , 0);
}
}
void app_main() {
printf("Hello, Wokwi!\n");
while (true) {
Servo();
}
}