2016-03-21 11:27 AM
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-janitor2016-03-21 11:56 AM
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 BWhat 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.2016-03-21 12:03 PM
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.2016-03-21 12:20 PM
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.
2016-03-21 12:53 PM
@clive1
What registers douhIC3ReadValue2 and
uhIC3ReadValue1 correspond to?
Also, isn't CCR1 is supposed to contain the period?.. based on the PWM input description from RM0090?