2017-04-26 04:41 AM
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
2017-04-26 04:56 AM
PMW Input mode would work better, one input pin, uses channel 1 and 2 to measure period and duty.
2017-04-26 05:43 AM
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.
2017-05-30 09:44 PM
how do you determine the the PWM has stopped, as the Update Event is triggered for an overflow and a reset of the counter ?