cancel
Showing results for 
Search instead for 
Did you mean: 

Setting Up Input Capture To Measure Period and Duty Cycle of PWM

chadposner
Associate II
Posted on March 21, 2016 at 19:27

I'm using an STM32F405/STM32F4xx family Mcu and attempting to setup Input Capture to measure Period and Duty Cycle of a PWM. After some trial and error after following section 13.3.6 PWM Input Mode I was able to get some values in CCR1 and CCR2. First, the period seems to be CCR1+CCR2 where as the example says CCR1 is the period. Also, I seem to get incorrect results about 20% of the time, which makes me question the setup.

Specifically, I am using Timer 9, Input Channel 2 via PA3 to accomplish this.

Can someone please explain, step-by-step, with a little more detail how to setup the registers to get this to work correctly. Including any precursory register setup/clearing that may affect the result that may be a little outside of the Timer 9 registers. Bonus, if you can explain what's happening when the registers are set that would be helpful! Thanks!

#stm32f4-inputcapture #!stm32f4-!stm32f401 #recurring-topic #coded-by-the-janitor
4 REPLIES 4
Posted on March 21, 2016 at 19:56

Is that a 16 or 32-bit timer?

Are you doing the math correctly using suitably sized variables.

All the ST examples have broken math instead of using Delta = B - A for ALL values of A and B

What frequencies are we talking about, and does the period fit into the timebase of the timer. Consider the rate at which you are clocking the counter via the prescaler, and the width of the counter (16-bit?)

PWM Input works better than Input Capture with respect to interrupt loading.

Tips, Buy me a coffee, or three.. PayPal Venmo
Up vote any posts that you find helpful, it shows what's working..
chadposner
Associate II
Posted on March 21, 2016 at 20:03

Hey @clive1,

It's a 16 bit counter. I've setup the prescaler for the timer correctly and I'm measuring frequencies from 5Hz to 1000hz. I've setup the prescaler so it should not overflow. For my purpose, there's enough precision in my measurment with the 16 bit timer.

Is the example in RM0090 incorrect? I don't follow your statement about the math.

Posted on March 21, 2016 at 20:20

All ST's examples, for years, have had this broken non-sense in them

/* Capture computation */
if (uhIC3ReadValue2 > uhIC3ReadValue1)
{
uwCapture = (uhIC3ReadValue2 - uhIC3ReadValue1);
}
else if (uhIC3ReadValue2 < uhIC3ReadValue1)
{
uwCapture = ((0xFFFF - uhIC3ReadValue1) + uhIC3ReadValue2);
}
else
{
uwCapture = 0;
}
/* Frequency computation */
uwTIM1Freq = (uint32_t) SystemCoreClock / uwCapture;

The math is broken, and as a double-bonus it can also fault the processor with a division by zero.
Tips, Buy me a coffee, or three.. PayPal Venmo
Up vote any posts that you find helpful, it shows what's working..
chadposner
Associate II
Posted on March 21, 2016 at 20:53

@clive1

What registers do 

uhIC3ReadValue2 and 

uhIC3ReadValue1 correspond to?

Also, isn't CCR1 is supposed to contain the period?.. based on the PWM input description from RM0090?