Showing results for 
Search instead for 
Did you mean: 

STM32F4 Input capture TIM

Associate III

Good evening everyone, I'm trying to evaluate the duration of a square wave using input capture of the timer, by measuring the time between the rising and falling edges. I wanted to understand what is the maximum frequency at which I can perform this operation with the timers in the STM32F407 microcontroller. I'm having trouble understanding it clearly from the datasheet. Do you have any advice?


> I wanted to understand what is the maximum frequency at which I can perform this operation with the timers in the STM32F407 microcontroller.

This is not as simple to tell as it may appear, and also after finding "maximum frequency" you may be disappointed by the resolution. Let me explain.

First, what's the maximum clock of timers? They are on two buses, APB1 and APB2. APB1 is the slower bus, max. 42MHz,  APB2 is max.84MHz. If the APB clock is generated by dividing from higher frequency AHB clock (i.e. AHB/system clock is 168MHz), timers' clock frequency is twice the APB clock. In other words, only timers which are on APB2 can run at 168MHz (TIM1 and TIM8; also TIM9-TIM11 but they have limited features e.g. less channels and no DMA capability, which may or may not matter).

That means, that basic resolution of period measurement is 1/168MHz=cca 6ns. The timer channel inputs are synchronous, i.e. they can't distinguish edges which are not at least one period apart, there are two edges per period (one rising and one falling), so the minimum input signal period is 2 timer clocks, i.e. cca 12ns, and thus the maximum theoretical input frequency is 168MHz/2=84MHz.

But 6ns resolution also means, that the next lower frequency which can be distinguished from the highest one corresponds to 3 timer cycles, i.e. if you gradually decrease the input signal's frequency, the timer will indicate steady 84MHz, and then jumps to 168MHz/3=56MHz. Decreasing input frequency further, you'd see jumps to 42MHz-33.6MHz-28MHz-24MHz-21MHz-18.7MHz-16.8MHz... So the resolution in terms of hertzs is not that great.

However, this all it theory. Now how would you capture the period at all? The simplest method is to use what RM calls "PWM input mode", where the input signal is used both to capture and reset the timer (and its other edge is captured in second channel, but that's not important for period). This works well, however, there is a lag between the input capture and subsequent reset. This is not mentioned nor specified by ST; @Tesla DeLorean measured it once (the respective thread has been lost in forum migrations) to be 2 cycles. In other words, the measured period is 2 timer cycles less than the actual period, and that of course also limits the available minimum measurable period (thus maximum frequency) to some 3 or 4 cycles.

Other solution would be to avoid the problematic reset i.e. leave the timer run freely, and to use DMA to move two (or more) consecutively captured values to RAM, and subtract them in software afterwards. This is limited by the total time it takes the DMA to move the data around (and possible jitter in this time), and it would be unwise to consider anything less than 6 system cycles (I personally wouldn't even think less than 10) - for some of the painful details see AN4031. This may appear to be vastly inferior to the "PWM input" method, but it still may be very useful in other regards, e.g. if the input signal is not stable and we want to quantify that.

So the practical "maximum frequencies" measurable using these methods are somewhere around 10-20MHz and below; with the resolution caveat as explained.

However, if the input signal is relatively stable and you want to know its frequency rather than other characteristics (duty, stability), you can do much better by simply not trying to measure one period, but several of them.

One easy way to do that is to use the capture prescaler, see TIMx_CCMRx.ICxPSC. If you set it to its maximum 8 and using DMA to transfer captured value to memory, and then subtract stored consecutive values and divide by 8, you can go with the input up to almost the maximum attainable frequency i.e. around 80MHz; and even the resolution will improve due to the period being divided (i.e. the resolution in period will be 6/8ns).

A slightly different method is not measuring duration of one period (or N periods) of the input signal, but to measure number of input signal periods in a fixed time, e.g. 1 second. In that case, you'd use the timer as counter (in external clock mode), the maximum frequency is the maximum frequency it can use to count (i.e. 84MHz), and the resolution is the inverse of the period during which you take the measurement, i.e. in case of 1 second window resolution is 1Hz.