Cannot make PWM work

soci75
Posts: 1
Joined: Sun May 05, 2024 2:00 pm

Cannot make PWM work

Postby soci75 » Sun May 05, 2024 2:19 pm

I can generate a PWM signal by the following code:

Code: Select all

    mcpwm_unit_t unit = MCPWM_UNIT_1;
    mcpwm_io_signals_t pin = MCPWM0A; 
    int gpio = 27;
    mcpwm_timer_t timer = MCPWM_TIMER_0; 
    mcpwm_generator_t generator = MCPWM_GEN_A;
    float defaultDuty = 50;
    uint32_t frequency = 1;
    unsigned long int resolution = 32;
    
    int res = mcpwm_gpio_init(unit, pin, gpio);
    Serial.println(res);
    if (res != 0) return;
    
    res = mcpwm_timer_set_resolution(unit, timer, resolution);
    Serial.println(res);
    if (res != 0) return;

    mcpwm_config_t pwm_config = {
        frequency,          // frequency    /*!<Set frequency of MCPWM in Hz*/
        0.0F,               // cmpr_a       /*!<Set % duty cycle for operator a(MCPWMXA)
        0.0F,               // cmpr_b       /*!<Set % duty cycle for operator b(MCPWMXB)
        MCPWM_DUTY_MODE_0,  // duty_mode    /*!<Set type of duty cycle*/
        MCPWM_UP_COUNTER    // counter_mode /*!<Set  type of MCPWM counter*/
    };

    res = mcpwm_init(unit, timer, &pwm_config);
    Serial.println(res);
    if (res != 0)  return;

    res = mcpwm_set_duty(unit, timer, generator, defaultDuty);
    Serial.println(res);
    if (res != 0)  return;

    res = mcpwm_start(unit, timer);
    Serial.println(res);
    if (res != 0)  return;
   
So, this works well. However, I cannot make it work using other settings.
E.g., I change this line:
mcpwm_io_signals_t pin = MCPWM0A;
to
mcpwm_io_signals_t pin = MCPWM0B;
then it won't produce output.
I cannot understand why.
Similarly, setting the timer to MCPWM_TIMER_1 won't produce an output signal.

And one more question. Using this setting, I can generate a (barely) 1Hz PWM. You see, the resolution is set to 32. I assume the timer clock has a frequency of 40MHz. The frequency is set to 1Hz. What is the math behind this?

Who is online

Users browsing this forum: No registered users and 17 guests