Generatig a clock frequency

OneTwo
Posts: 20
Joined: Mon Jan 15, 2018 9:24 am

Generatig a clock frequency

Postby OneTwo » Thu Jun 21, 2018 9:26 am

Hallo all,

i would like to know if it is possible to generate a constant frequency of 1.024 Mhz (~1Mhz) on a PIN and if so do you have example codes?

Many thanks in advance

WiFive
Posts: 3529
Joined: Tue Dec 01, 2015 7:35 am

Re: Generatig a clock frequency

Postby WiFive » Thu Jun 21, 2018 10:24 am


OneTwo
Posts: 20
Joined: Mon Jan 15, 2018 9:24 am

Re: Generatig a clock frequency

Postby OneTwo » Thu Jun 21, 2018 11:23 am

Because im not an expert at hardware level, could you try to explain the following attributes:

  • ledc_channel_config_t.duty
    ledc_timer_config_t.duty_resolution


Should i use LEDC_APB_CLK_HZ which gives 80MHz or LEDC_REF_CLK_HZ with 1MHz?

For example without tesing:

Code: Select all

    periph_module_enable(PERIPH_LEDC_MODULE);

    int bit_width = 8; // idk 
    int divider =  80;  // 80MHz / 80 ~ 1MHz
    int duty_cycle = 1 << (bit_width - 1);	// ?? 128

    float freq_hz = ((uint64_t) LEDC_APB_CLK_HZ << 8) / (float) divider / (1 << bit_width);
    printf("frequency: %f Hz\n", freq_hz);

    ledc_timer_set(LEDC_HIGH_SPEED_MODE, LEDC_TIMER_0, divider, bit_width, LEDC_APB_CLK_HZ );
    ledc_timer_rst(LEDC_HIGH_SPEED_MODE, LEDC_TIMER_0);
    ledc_timer_resume(LEDC_HIGH_SPEED_MODE, LEDC_TIMER_0);
    ledc_channel_config_t channel_config = {
        .channel    = LEDC_CHANNEL_0,
        .duty       = duty_cycle,
        .gpio_num   = GPIO_NUM_2,
        .speed_mode = LEDC_HIGH_SPEED_MODE,
        .timer_sel  = LEDC_TIMER_0
    };

    ledc_channel_config(&channel_config);

ESP_igrr
Posts: 2071
Joined: Tue Dec 01, 2015 8:37 am

Re: Generatig a clock frequency

Postby ESP_igrr » Thu Jun 21, 2018 1:26 pm

Divider has 8-bit fractional part, so you probably need to set it to 80MHz/1.024MHz*256=20000.
Bit_num=8 and duty=127 seems to be okay, should generate 50% duty cycle.

OneTwo
Posts: 20
Joined: Mon Jan 15, 2018 9:24 am

Re: Generatig a clock frequency

Postby OneTwo » Mon Jun 25, 2018 11:07 am

ESP_igrr wrote:Divider has 8-bit fractional part, so you probably need to set it to 80MHz/1.024MHz*256=20000.
Bit_num=8 and duty=127 seems to be okay, should generate 50% duty cycle.
Thank you, i solved that with the following code:

Code: Select all

	periph_module_enable(PERIPH_LEDC_MODULE);

	int bit_width = 1; // 1 - 20 bits
	int divider = 10240;  // Q10.8 fixed point number, 0x100 — 0x3FFFF
	int duty_cycle = 1 << (bit_width - 1);

	float freq_hz = ((uint64_t) LEDC_APB_CLK_HZ << 8) / (float) divider / (1 << bit_width);
	printf("frequency: %f Hz\n", freq_hz);

	ledc_timer_set(LEDC_HIGH_SPEED_MODE, LEDC_TIMER_0, divider, bit_width, LEDC_APB_CLK);
	ledc_timer_rst(LEDC_HIGH_SPEED_MODE, LEDC_TIMER_0);
	ledc_timer_resume(LEDC_HIGH_SPEED_MODE, LEDC_TIMER_0);

	ledc_channel_config_t channel_config = {0};
	channel_config.channel    = LEDC_CHANNEL_0;
	channel_config.duty       = duty_cycle;
	channel_config.gpio_num   = PIN_CLOCK;
	channel_config.speed_mode = LEDC_HIGH_SPEED_MODE;
	channel_config.timer_sel  = LEDC_TIMER_0;

	ledc_channel_config(&channel_config);

Who is online

Users browsing this forum: Baidu [Spider], Bing [Bot] and 78 guests