While I have been able to use the PWM feature of the EPS32’s MCPWM module, I have been struggling to find a good reference to take advantage of the Capture feature of the MCPWM module that would allow me to measure how long a PWM signal is low as well as how long a PWM signal is high via captures of the 32-bit timer. it is also my understanding that an interrupt routine can be used to capture the timer capture events, so if it is possible to include how to setup the interrupt handling for this Capture Event interrupt that would also be VERY helpful! 🙂
Hi Manuel.
To be honest, I’ve never experimented with that module.
What do you have so far? Have you experimented any code?
Are you following this reference: https://docs.espressif.com/projects/esp-idf/en/latest/esp32/api-reference/peripherals/mcpwm.html#capture ?
Regards,
Sara
Hi Sara,
  Yes, I was already familiar with that link, but that is not really a complete answer as I want to be able to access those functions with the Arduino IDE environment, which is where I am currently developing the code. However, to use the information on the link you provide, I will have to switch over to developing my code in the ESPressif development environment. While that is certainly an option, I would prefer to stay in the Arduino IDE environment. Maybe someone at RNTLabs can put together some example code? I am mainly a hardware person and too often tracking down the “*.h” files can be a challenge for me and too often when moving between one IDE and the other the API’s change, both the function names and the API specific defines tend to change. Thanks in advance for any help that you can provide!!! 😀
Hi.
You can use those functions in Arduino IDE:
You just need to include the following:
#include "driver/mcpwm.h"
Then, you can use those functions.
Accordingly to the documentation you have to do something like this:
Init MCPWM in your desired GPIO:
mcpwm_gpio_init(MCPWM_UNIT_0, MCPWM0A, GPIO_OUT);
Enable capture:
mcpwm_capture_enable(MCPWM_UNIT_0,MCPWM_SELECT_CAP0, MCPWM_NEG_EDGE, 0);
Get the value:
unsigned int number = mcpwm_capture_signal_get_value(MCPWM_UNIT_0,MCPWM_SELECT_CAP0);
Please see the meaning of each parameter on the documentation.
Regards,
Sara
@ Sara
Could you please show how, within Arduino, you define MCPWM_SELECT_CAP0 gpio pin?
I tried #define MCPWM_SELECT_CAP0 25
I tried int MCPWM_SELECT_CAP0 = 25;
none are ok.Â
Thank you very much in advance.
Henri
Hi.
Can you provide more details about what you want to do and about your issue?
When you say it doesn’t work, what happens?
Regards,
Sara
Hi Sara.
Would like to measure High an low value of a pulse ( 2 to 15 milliseconds).
Using mcpwm_capture_signal_get_valueÂ
Question:Â
The following code compile ok ** IF ** MCPWM_SELECT_CAP0 is not defineÂ
How do you define the ESP32 GPIO pin number to be capture?
(I surely wrongly assume that MCPWM_SELECT_CAP0 was the GPIO pin number).
Thank you in advance for your help.
// ————————————————————————————————————-
// This compile with no error. Which GPIO pin is captured?
#include “driver/mcpwm.h”
//#include “mcpwm_periph.h”
#include “esp_attr.h”
#define GPIO_OUT 26
//#define MCPWM_SELECT_CAP0 25 ***** DOES NOT ACCEPT A VALUE ****
//=========================================================================
void setup(void)
{
Serial.begin(115200);
mcpwm_gpio_init(MCPWM_UNIT_0, MCPWM0A, GPIO_OUT);
mcpwm_capture_enable(MCPWM_UNIT_0, MCPWM_SELECT_CAP0, MCPWM_NEG_EDGE, 0);
}
//=========================================================================
void loop()
{
mcpwm_capture_enable(MCPWM_UNIT_0, MCPWM_SELECT_CAP0, MCPWM_NEG_EDGE, 0);
uint32_t LOW_VALUE = mcpwm_capture_signal_get_value(MCPWM_UNIT_0,MCPWM_SELECT_CAP0);
mcpwm_capture_enable(MCPWM_UNIT_0, MCPWM_SELECT_CAP0, MCPWM_POS_EDGE, 0);
uint32_t HIGH_VALUE = mcpwm_capture_signal_get_value(MCPWM_UNIT_0,MCPWM_SELECT_CAP0);
Serial.println(LOW_VALUE);
Serial.println(HIGH_VALUE);
}
// This compile ok with Arduino AND GPIO #define of the input pin works:
#include “driver/mcpwm.h”
//#include “mcpwm_periph.h”
#include “esp_attr.h”
#define GPIO_CAP0_IN 25 //Set GPIO 25 as input CAP0
//=========================================================================
void setup(void)
{
Serial.begin(115200);
//mcpwm_gpio_init(MCPWM_UNIT_0, MCPWM0A, GPIO_OUT);
mcpwm_gpio_init(MCPWM_UNIT_0, MCPWM_CAP_0, GPIO_CAP0_IN); // MAGIC LINE to define WHICH GPIO
// gpio_pulldown_en(GPIO_CAP0_IN); //Enable pull down on CAP0 signal
mcpwm_capture_enable(MCPWM_UNIT_0, MCPWM_SELECT_CAP0, MCPWM_NEG_EDGE, 0);
}
//=========================================================================
void loop()
{
mcpwm_capture_enable(MCPWM_UNIT_0, MCPWM_SELECT_CAP0, MCPWM_NEG_EDGE, 0);
uint32_t LOW_VALUE = mcpwm_capture_signal_get_value(MCPWM_UNIT_0,MCPWM_SELECT_CAP0);
mcpwm_capture_enable(MCPWM_UNIT_0, MCPWM_SELECT_CAP0, MCPWM_POS_EDGE, 0);
uint32_t HIGH_VALUE = mcpwm_capture_signal_get_value(MCPWM_UNIT_0,MCPWM_SELECT_CAP0);
Serial.println(LOW_VALUE);
Serial.println(HIGH_VALUE);
}
Hi.
According to the documentation:
You set the GPIO on the mcpwm_gpio_init() function. The GPIO number is the last argument (gpio_num):
mcpwm_gpio_init(mcpwm_unit_tmcpwm_num, mcpwm_io_signals_tio_signal, int gpio_num)
The GPIO number must be an integer.
Don’t define the MCPWM_SELECT_CAP0 as you’ve shown in the first snippet you shared. Pass the GPIO number on the mcpwm_gpio_init() function.
I hope this helps.
Regards,
Sara