cancel
Showing results for 
Search instead for 
Did you mean: 

measure pulse width using both edge input capture mode

Raider E
Associate III
Posted on April 26, 2017 at 13:41

Hi Everyone,

I want to measure the pulse width of a square wave signal and thought about using input capture mode and enable interrupt on both edges (rising and falling) and then in the interrupt routine i will read the signal state if its high or low to determine if its the on cycle or the off cycle. would that save me the hassle of using two pins and configure one on rising and one on falling?

one more thing, does the macro 

__HAL_TIM_GetCompare ( when placed inside the ISR routine 

HAL_TIM_IC_CaptureCallback) give me the counter value difference between last two interrupts (rising and falling)?

many thanks

3 REPLIES 3
Posted on April 26, 2017 at 13:56

PMW Input mode would work better, one input pin, uses channel 1 and 2 to measure period and duty.

Tips, Buy me a coffee, or three.. PayPal Venmo
Up vote any posts that you find helpful, it shows what's working..
RomainR.
ST Employee
Posted on April 26, 2017 at 14:43

You can take inspiration from the examples in your local CubeMx repository. Example \ TIM \ TIM_PWMInput.

The use of a GP Timer in PWM IN mode on a single GPIO but internally connected to 2 channel Input Capture greatly simplifies the measurement of low / high pulses, period or frequency.

First input capture channel should be configured for falling edge, second one for rising edge (and perform a reset of Timer counter)

In HAL_TIM_IC_CaptureCallback() you can get 2 variables to store sequentially

  • Each falling edge,  get Timer value with __HAL_TIM_GetCompare() macro to obtain duration of high pulse

  • Each rising edge, get another Timer value (same macro) to obtain total period low + high duration of signal.

Then it's easy to compute low pulse and frequency.  

You can also use function 

HAL_TIM_ReadCapturedValue(TIM_HandleTypeDef *htim, uint32_t Channel)

To give better visibility on the answered topics, please click on Accept as Solution on the reply which solved your issue or answered your question.

Ray Abram
Associate
Posted on May 31, 2017 at 06:44

how do you determine the the PWM has stopped, as the Update Event is triggered for an overflow and a reset of the counter ?