LEDC unstable frequency

ESP_houwenxiang
Posts: 118
Joined: Tue Jun 26, 2018 3:09 am

Re: LEDC unstable frequency

Postby ESP_houwenxiang » Wed Dec 05, 2018 3:15 am

loboris wrote:
Tue Dec 04, 2018 10:24 pm
This value can be set by set by ledc_timer_set() function (source).
clk_src parameter can be LEDC_APB_CLK or LEDC_REF_TICK
Yes, when the expected output frequency is too low, we will choose ref_tick as the source clock. So, you can use ` ledc_timer_set` to configure the timer.
wookooho

ardiehl
Posts: 7
Joined: Fri Oct 27, 2017 10:44 pm

[solved] LEDC unstable frequency

Postby ardiehl » Sun Dec 09, 2018 12:19 pm

tried it and it works. With power management enabled, frequency is now stable as it should be. Due to the lower timer input frequency, i had to reduce the resolution from 10 to 9 bit.

Code: Select all

#include "esp_err.h"
#include "esp_log.h"
#include "driver/ledc.h"
#include "driver/periph_ctrl.h"
#include "soc/ledc_reg.h"

#define J1772_LEDC_TIMER       LEDC_TIMER_0
#define J1772_LEDC_CHANNEL     LEDC_CHANNEL_0
#define J1772_LEDC_TIMER_RES   LEDC_TIMER_9_BIT
#define J1772_DUTY_MAX         ((1 << LEDC_TIMER_9_BIT) -1 )
#define J1772_PWM_FREQUENCY_HZ 1000
#define J1772_LEDC_SPEEDMODE   LEDC_HIGH_SPEED_MODE

void ledc_init() {
	const char * ME = __func__;

	esp_err_t err;
	periph_module_enable(PERIPH_LEDC_MODULE);

	uint32_t precision = J1772_DUTY_MAX + 1;
	uint64_t div_param = ((uint64_t) LEDC_REF_CLK_HZ << 8) / J1772_PWM_FREQUENCY_HZ / precision;
	if (div_param < 256 || div_param > LEDC_DIV_NUM_HSTIMER0_V) {
		ESP_LOGE(ME, "requested frequency and duty resolution can not be achieved, try increasing freq_hz or duty_resolution. div_param=%d", (uint32_t ) div_param);
	}
	
	err = ledc_timer_set(LEDC_HIGH_SPEED_MODE, J1772_LEDC_TIMER, div_param, J1772_LEDC_TIMER_RES, LEDC_REF_TICK);
	if (err) ESP_LOGE(ME, "ledc_timer_set returned %d",err);
	ledc_timer_rst(J1772_LEDC_SPEEDMODE, J1772_LEDC_TIMER);
	ESP_LOGD(ME, "ledc_timer_set: divider: 0x%05x duty_resolution: %d\n", (uint32_t) div_param, J1772_LEDC_TIMER_RES);
	ledc_channel_config_t ledc_channel = {
			.gpio_num   = j1772_pilot_GPIO,
			.speed_mode = J1772_LEDC_SPEEDMODE,
			.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);
	ESP_LOGD(ME,"ledc_channel_config returned %d",err);
}

Who is online

Users browsing this forum: HighVoltage and 140 guests