Esp32 mcpwm issues

Hi all,

I am need to generate complementary PWM using MCPWM. The duty cycle needs to be of typically 5us and time period 10us. While search through forum, I found a code for PWM (not complementary). I am trying to change frequency and duty. However the code seems to work sometimes and some times it doesn't. I removed a syn() line form original code posted in ( ESP32 mcPWM) . As I am new to using MCPWM can somebody suggest what needs to be done.

#include "driver/mcpwm.h"
//MCPWM TRY 2

// REF:https://forum.arduino.cc/t/esp32-mcpwm/608899


                       
#define MOSFET 22          //pin to trigger the MOSFET (Output) pin22

void setup() {
  // put your setup code here, to run once:
  mcpwm_gpio_init(MCPWM_UNIT_0, MCPWM0A, MOSFET);


  mcpwm_pin_config_t pin_config; 
   // pin_config.mcpwm_sync0_in_num  = GPIO_SYNC0_IN;
      
  mcpwm_config_t pwm_config;
    pwm_config.frequency = 100000;    //frequency = 110Hz
    pwm_config.cmpr_a = 50;       //duty cycle of PWMxA = 60.0%
    pwm_config.counter_mode = MCPWM_UP_COUNTER;
    pwm_config.duty_mode = MCPWM_DUTY_MODE_0;
    mcpwm_set_pin(MCPWM_UNIT_0, &pin_config);



  mcpwm_init(MCPWM_UNIT_0, MCPWM_TIMER_0, &pwm_config);   //Configure PWM0A & PWM0B with above settings
  mcpwm_set_duty_type(MCPWM_UNIT_0, MCPWM_TIMER_0, MCPWM_OPR_A, MCPWM_DUTY_MODE_0);
  mcpwm_start(MCPWM_UNIT_0, MCPWM_TIMER_0);
  //mcpwm_sync_enable(MCPWM_UNIT_0, MCPWM_TIMER_0, MCPWM_SELECT_SYNC0, 0);
  mcpwm_set_duty(MCPWM_UNIT_0, MCPWM_TIMER_0, MCPWM_OPR_A,50);
}

void loop() {

}

Here you have more information about MCPWM.

"MCPWM - ESP32 - — ESP-IDF Programming Guide v4.3 documentation

"https://www.instructables.com/Electric-Turbine-With-ESP32/

1 Like

Thanks Ruilviana,

Based on the example reference. I was able to modify the code and get basic PWM.

The issue was due to
mcpwm_start(MCPWM_UNIT_0, MCPWM_TIMER_0);

and

additional lines
mcpwm_set_pin(MCPWM_UNIT_0, &pin_config);
mcpwm_pin_config_t pin_config;

When these lines were removed. The PWM is being generated.

WIll check now for comlementary generation.

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.