LEDC unstable frequency
Posted: Sat Nov 17, 2018 2:13 pm
esp-idf v3.1.1
I'm tying to generate an 1 KHz frequency uning ledc (without fade) and the frequency is not stable. How can i fix this ? Code is as follows:
I'm tying to generate an 1 KHz frequency uning ledc (without fade) and the frequency is not stable. How can i fix this ? Code is as follows:
Code: Select all
#define J1772_LEDC_TIMER LEDC_TIMER_0
#define J1772_LEDC_CHANNEL LEDC_CHANNEL_0
#define J1772_LEDC_TIMER_RES LEDC_TIMER_10_BIT
#define J1772_DUTY_MAX 1023
void ledc_init() {
esp_err_t err;
/* Prepare and set configuration of timers
* that will be used by LED Controller */
ledc_timer_config_t ledc_timer;
memset((void *)&ledc_timer,0,sizeof(ledc_timer));
ledc_timer.speed_mode = LEDC_HIGH_SPEED_MODE; // timer mode
ledc_timer.duty_resolution = J1772_LEDC_TIMER_RES; // resolution of PWM duty
ledc_timer.timer_num = J1772_LEDC_TIMER; // timer index
ledc_timer.freq_hz = 1000; // frequency of PWM signal
//ledc_timer_resume(LEDC_HIGH_SPEED_MODE, J1772_LEDC_TIMER);
// Set configuration of timer0 for high speed channels
err = ledc_timer_config(&ledc_timer);
ledc_channel_config_t ledc_channel = {
.gpio_num = j1772_pilot_GPIO,
.speed_mode = LEDC_HIGH_SPEED_MODE,
.channel = J1772_LEDC_CHANNEL,
.intr_type = LEDC_INTR_DISABLE,
.timer_sel = J1772_LEDC_TIMER,
.duty = J1772_DUTY_MAX,
.hpoint = 0 // TODO: AD 10.11.2018: new, does 0 work (0xfffff does not work)
};
err = ledc_channel_config(&ledc_channel);
}
void ledcSetDuty (int pwmPercent) {
const char * ME = __func__;
if ((pwmPercent == 0) | (pwmPercent == 100)) {
// set the port to 0 or 1 without pwm, 100% is unstable, we get glitches
ledc_stop(LEDC_HIGH_SPEED_MODE, J1772_LEDC_CHANNEL, (pwmPercent == 100) );
} else {
uint32_t duty;
duty = (pwmPercent * J1772_DUTY_MAX) / 100; // duty for timer (0..1023)
esp_err_t err;
err = ledc_set_duty(LEDC_HIGH_SPEED_MODE, J1772_LEDC_CHANNEL, duty);
if (err != ESP_OK) ESP_LOGE(TAG, "ledc_set_duty returned %d", err);
ledc_update_duty(LEDC_HIGH_SPEED_MODE, J1772_LEDC_CHANNEL);
}
}
ledc_init();
ledcSetDuty (50);