How PWM speed esp32 can running?

Shaco.Ma
Posts: 3
Joined: Sat May 05, 2018 3:10 am

How PWM speed esp32 can running?

Postby Shaco.Ma » Sat May 05, 2018 10:48 am

Hi:
I use pwm and change duty_resolution = 8,freq_hz to 200*1000,duty to 20, but pwm freq just running 100*1000 Hz, Is this chip limit?

Shaco.Ma
Posts: 3
Joined: Sat May 05, 2018 3:10 am

Re: How PWM speed esp32 can running?

Postby Shaco.Ma » Wed May 09, 2018 11:17 am

Hi:
Now I can make pwm freq to 2MHz,just let reduce .duty_resolution value.
BUT, I want use :LEDC_HSCHn_CONF1_REG this register to let PWM out 138 preiod , and then, pwm gpio goto level 0, I use ledc_set_fade_with_step(LEDC_HS_MODE, LEDC_HS_CH0_CHANNEL, 0, 21, 138), the pwm out is not square wave, code next:

Code: Select all

#include <stdio.h>
#include "freertos/FreeRTOS.h"
#include "freertos/task.h"
#include "driver/ledc.h"
#include "crypto/common.h"
#include "esp_err.h"
#include "crypto/aes.h"
#include "crypto/includes.h"
#include "soc/gpio_sig_map.h"

#define LEDC_HS_TIMER          LEDC_TIMER_0
#define LEDC_HS_MODE           LEDC_HIGH_SPEED_MODE
#define LEDC_HS_CH0_GPIO       (18)
#define LEDC_HS_CH0_CHANNEL    LEDC_CHANNEL_0
#define LEDC_HS_CH1_GPIO       (19)
#define LEDC_HS_CH1_CHANNEL    LEDC_CHANNEL_1

#define LEDC_LS_TIMER          LEDC_TIMER_1
#define LEDC_LS_MODE           LEDC_LOW_SPEED_MODE
#define LEDC_LS_CH2_GPIO       (4)
#define LEDC_LS_CH2_CHANNEL    LEDC_CHANNEL_2
#define LEDC_LS_CH3_GPIO       (5)
#define LEDC_LS_CH3_CHANNEL    LEDC_CHANNEL_3
#define MAX_DUTY               ((1 << (LEDC_TIMER_13_BIT)) - 1)

//#define LEDC_TEST_CH_NUM       (4)
#define LEDC_TEST_CH_NUM       (1)
//#define LEDC_TEST_DUTY         (4000)
#define LEDC_TEST_DUTY         (21)
#define LEDC_TEST_FADE_TIME    (5000)

void app_main()
{
    int ch;

    /*
     * Prepare and set configuration of timers
     * that will be used by LED Controller
     */
    ledc_timer_config_t ledc_timer = {
        .duty_resolution = 5, // resolution of PWM duty
        .freq_hz = 2*1000*1000,                      // frequency of PWM signal
        //.duty_resolution = 10, // resolution of PWM duty
        //.freq_hz = 5000,                      // frequency of PWM signal
        .speed_mode = LEDC_HS_MODE,           // timer mode
        .timer_num = LEDC_HS_TIMER            // timer index
    };
    // Set configuration of timer0 for high speed channels
    ledc_timer_config(&ledc_timer);

    // Prepare and set configuration of timer1 for low speed channels
    //ledc_timer.speed_mode = LEDC_LS_MODE;
    //ledc_timer.timer_num = LEDC_LS_TIMER;
    //ledc_timer_config(&ledc_timer);

    /*
     * Prepare individual configuration
     * for each channel of LED Controller
     * by selecting:
     * - controller's channel number
     * - output duty cycle, set initially to 0
     * - GPIO number where LED is connected to
     * - speed mode, either high or low
     * - timer servicing selected channel
     *   Note: if different channels use one timer,
     *         then frequency and bit_num of these channels
     *         will be the same
     */
    ledc_channel_config_t ledc_channel[LEDC_TEST_CH_NUM] = {
        {
            .channel    = LEDC_CHANNEL_0,
            .duty       = LEDC_TEST_DUTY,
            .gpio_num   = 0,//LEDC_HS_CH0_GPIO,
            .speed_mode = LEDC_HS_MODE,
            .timer_sel  = LEDC_TIMER_0
        },
	};

    while (1) {
		printf("ledc_get_duty = %d\n", ledc_get_duty(LEDC_HS_MODE, LEDC_HS_CH0_CHANNEL));
		for(ch = 0; ch < LEDC_TEST_CH_NUM; ch++) {
			ledc_set_fade_with_step(ledc_channel[ch].speed_mode,
					ledc_channel[ch].channel, 0, 21, 1023);
            ledc_fade_start(ledc_channel[ch].speed_mode,
                    ledc_channel[ch].channel, LEDC_FADE_NO_WAIT);
		}
        vTaskDelay(LEDC_TEST_FADE_TIME / portTICK_PERIOD_MS);
return;
    }
}
Image


WHAT WRONG WITH ME?
and how to do I can let PWM just out 138 preiod and then pwm out goto level 0?

Who is online

Users browsing this forum: pacucha42 and 149 guests