PWM drifting with increasing dutycycle
Posted: Mon Oct 19, 2020 10:35 am
Hello everyone,
I just started programming an ESP32 and already ran into some problems. I use the Arduino framework of the 'Visual studio code' and the MCPWM library. I adapted the code from https://hackaday.io/project/169905/logs?sort=oldest
I have two issues:
My setup: ESP32-Devkit pcb connected to a notebook. The signal is measured using an oscilloscope against GND.
I just started programming an ESP32 and already ran into some problems. I use the Arduino framework of the 'Visual studio code' and the MCPWM library. I adapted the code from https://hackaday.io/project/169905/logs?sort=oldest
Code: Select all
#include <Arduino.h>
#include "driver/mcpwm.h"
#include "soc/mcpwm_reg.h"
#include "soc/mcpwm_struct.h"
#define GPIO_PWM0A_OUT 12 //Declara GPIO 12 como PWM0A
#define GPIO_PWM0B_OUT 14 //Declara GPIO 14 como PWM0B
void setup() {
Serial.begin(9600);
mcpwm_gpio_init(MCPWM_UNIT_0, MCPWM0A, GPIO_PWM0A_OUT);
mcpwm_gpio_init(MCPWM_UNIT_0, MCPWM0B, GPIO_PWM0B_OUT);
mcpwm_config_t pwm_config;
pwm_config.frequency = 3200; //frequência = 500Hz,
pwm_config.cmpr_a = 90; //Ciclo de trabalho (duty cycle) do PWMxA = 0
pwm_config.cmpr_b = 90; //Ciclo de trabalho (duty cycle) do PWMxb = 0
pwm_config.counter_mode = MCPWM_UP_COUNTER; //Para MCPWM assimetrico
pwm_config.duty_mode = MCPWM_DUTY_MODE_0; //Define ciclo de trabalho em nível alto
//Inicia(Unidade 0, Timer 0, Config PWM)
mcpwm_init(MCPWM_UNIT_0, MCPWM_TIMER_0, &pwm_config); //Define PWM0A & PWM0B com as configurações acim
//mcpwm_sync_enable(MCPWM_UNIT_0, MCPWM_TIMER_0, MCPWM_SELECT_SYNC_OUT0, 0);
//MCPWM0.timer[0].sync.out_sel = 1;
//delay(100);
//MCPWM0.timer[0].sync.out_sel = 0;
//mcpwm_set_duty(MCPWM_UNIT_0, MCPWM_TIMER_0, MCPWM_OPR_A, 70);
//mcpwm_set_duty(MCPWM_UNIT_0, MCPWM_TIMER_0, MCPWM_OPR_B, 60);
Serial.println("Setup");
delay(50);
}
void loop() {
Serial.println("Loop");
for(int i=0;i<100;i++) {
mcpwm_set_signal_low(MCPWM_UNIT_0, MCPWM_TIMER_0, MCPWM_OPR_B);
mcpwm_set_duty(MCPWM_UNIT_0, MCPWM_TIMER_0, MCPWM_OPR_A, i);
mcpwm_set_duty_type(MCPWM_UNIT_0, MCPWM_TIMER_0, MCPWM_OPR_A, MCPWM_DUTY_MODE_0);
Serial.println(i);
delay(250);
}
delay(1000);
}
- The output signal is not a true rectangle, but clearly distorted on the edges
- The signal drifts with increasing duty cycle
My setup: ESP32-Devkit pcb connected to a notebook. The signal is measured using an oscilloscope against GND.