I am trying to configuring one of the timer inputs on a STM32 to behave as a frequency counter for an external signal. The STM32 datasheet describes a "input capture mode" in section 17.3.6 (page 342)

My understanding of this mode is that whenever an input comes in on the selected TIMx_CHx input, the processor captures the value of a free-running internal counter. Frequency can then be calculated using the counter value and the frequency of the internal counter.

I am trying to enable this in CubeMX and am having difficulties correlating the datasheet explanation to the settings in CubeMX. This is what I currently have configured. Is is this correct?

Also, can I run multiple independent inputs into separate channels on the same timer to capture timing simultaneously?

enter image description here

  • \$\begingroup\$ You've found the detailed description of what to do, what value goes to which register bit. There are even code examples there. Why are you struggling with CubeMX then? \$\endgroup\$ Jun 4, 2019 at 15:08

1 Answer 1


Frequency counters can get complicated quick depending on the bandwidth you want and the clock speed of the internal clock as well as the size of the registers holding the clock ticks and the input ticks.

I mean it looks like it'll work but you should make sure that (1) the clock counter and the frequency counter are refreshed at the end of the sample (2) that neither will overflow during the sample (3) that the setup will give you the resolution you want and (4) that the MCU will make adjustments if the bandwidth is large enough where it'll overflow some of the time and/or dip very low. The first of which would cause incorrect results and the second of which would drastically reduce the accuracy at low Hz.

  • \$\begingroup\$ Thanks for the insight. I'm measuring an engine RPM in the ranges of 1k to 6k Hz \$\endgroup\$ Jun 5, 2019 at 14:56
  • \$\begingroup\$ @user8908459 How are you measuring the rpm? Reluctance sensor? \$\endgroup\$
    – Jaywalk
    Jun 5, 2019 at 14:59
  • \$\begingroup\$ Yes using a MAX9926 \$\endgroup\$ Jun 5, 2019 at 18:04
  • \$\begingroup\$ In that case keep in mind that you're reading the frequency of tooth passes on the gear, not the frequency of the gear. You'll need to divide by the number of teeth, that means your ticks on the input capture will be significantly higher than 6k. If you have a 16 bit timer register you should be good though unless theres a crazy number of teeth on the gear. \$\endgroup\$
    – Jaywalk
    Jun 5, 2019 at 18:15

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.