MCPWM Capture Mode

Posts: 7
Joined: Mon Jul 01, 2019 1:44 pm

MCPWM Capture Mode

Postby jw--rt » Sun Oct 27, 2019 11:14 am

Hello everybody

I caught interest in the "Capture" part of the MCPWM module after reading the docs.
Particularly, because of the "elapsed time measurement / period measurement" (see docs below) and/or counting time between 2 pulses as the API doc of function "mcpwm_capture_enable()" suggests (see below).

Yet, I noticed that the sample code provided in the examples [EX1, EX2] does not at all do an ad hoc measurement of a time difference between two pulses.
It rather uses interrupts, a queue, saving the previous values in variables and finally calculating the timer values between the interrupts.
Trying out these examples was not difficult, thus, thank you for providing them.

But I have the feeling, that using this indirect method will cost a lot of processing power.
My goal is to measure the RPM of three different sprockets that can variably spin slowly and rather fast in an experiment of mine.
The time between the rising and falling edge varies between 320ms and 420µs in the signal. Triggering interrupts at this fast rate seems to me not very efficient, especially with three different signals.
I was thinking about using the PCNT counter module but, I would like to handle timings rather than counts for accuracy reasons.

I thought after reading the documentation to find code in "mcpwm.c" that measures the time differences between edges using less code and not a queue. I'm very new to such hardware and I very likely do not yet fully know how to read the properly (eg. "mcpwm_struct.h").

So what am I missing? Is there some way, to program it such that:
- The capture starts for example with rising edge
- The timer counts until the next rising edge (or falling edge) while saving the time difference value to a register
- Reading out the register is done when needed, not particular with an interrupt

I'd greatly appreciate any kind of help/explanation! Thank you very much in advance.

Best regards

Let me recite what I've read:

Technical Documentation (Sept. 2019)
  • Page 404: 16.2 Features - Capture Module
    - [...]
    - Measurement of elapsed time between position sensor pulses
    - Period and duty-cycle measurement of pulse train signals
  • Pages 432/433: Introduction - Capture Submodule
    [...] At a sync event the counter is loaded with phase stored in register PWM_CAP_TIMER_PHASE_REG
Espressif Homepage ... ml#capture
  • Capture:
    […] The hardware is able to detect the input signal’s edge and measure time between signals. As result the control software is simpler and the CPU power may be used for other tasks.
  • On each capture event the capture timer’s value is stored in time-stamp register that may be then checked by calling mcpwm_capture_signal_get_value().
  • Code: Select all

    esp_err_t mcpwm_capture_enable(..., uint32_t num_of_pulse)
    - num_of_pulse: count time between rising/falling edge between 2*(pulses mentioned), counter uses APB_CLK
Code Examples
Last edited by jw--rt on Mon Oct 28, 2019 8:58 am, edited 1 time in total.

Posts: 118
Joined: Tue Jun 26, 2018 3:09 am

Re: MCPWM Capture Mode

Postby ESP_houwenxiang » Mon Oct 28, 2019 7:45 am

HI, jw--rt
How many channels do you need to measure? and why do you think that the precision of the PCNT module is not enough?

I would like to suggest you use the PCNT module. You can get the RPM by counting the number of pulses in 100ms.
thanks !!

Posts: 7
Joined: Mon Jul 01, 2019 1:44 pm

Re: MCPWM Capture Mode

Postby jw--rt » Mon Oct 28, 2019 8:58 am


As stated above, I would like to use three channels and do not want to use PCNT for the following reason:

It is a sampling time problem: imagine you have 1 Count in PCNT and your sampling time is 50ms using a n=45 tooth sprocket.
Then the speed you measure is

Code: Select all

v(n,dt,cnt) = cnt/n * 1/dt ,
where n is the number of teeth on the sprocket, cnt the counts and t the sampling time delta. This results in a low resolution:

Code: Select all

dv = v(cnt=2) - v(cnt=1) = 1/45 * 1/50ms = 0.44/sec .
As an example, take the speedometer on your bicycle. With a 26" wheel, it gives you a speed resolution of 3.6km/h with the values above. This is, I'm afraid too coarse.

This is why a hardware time difference measurement module is so interesting in that case.

Best regards,


Posts: 7
Joined: Mon Jul 01, 2019 1:44 pm

Re: MCPWM Capture Mode

Postby jw--rt » Thu Oct 31, 2019 6:07 pm


is there maybe a more detailed description of the capture module around than what one can find on pages 403/406 in the technical reference?

Best regards and thank you in advance


Who is online

Users browsing this forum: No registered users and 30 guests