cancel
Showing results for 
Search instead for 
Did you mean: 

How ARR value impact on timer input capture result?

HB
Associate II

Hello guys,

I have just started working with stm32 controllers. I am trying to capture the frequency of signal using timer 2 input capture mode.

I am getting absolutely correct frequency if i use ARR = 65535 but when i use ARR = 0xffffffff (As timer 2 is 32bit) i am getting wrong result.

I am using this reference equation

Frequency = TIM_CLOCK / ((C2-C1)*(prescaler+1))

Where C2 = capture value 2 // Both Rising edge

C1 = capture value 1

TIM_CLOCK = 1Mhz

TIMER_PSC = 16-1

SYSTEM_CLOCK = 16Mhz

Please help if i am missing something.

1 ACCEPTED SOLUTION

Accepted Solutions

After you've set PSC and ARR, e.g. somewhere between steps 5 and 8, do

TIM2->EGR = TIM_EGR_UG;

JW

View solution in original post

26 REPLIES 26

Perhaps it relates to how you're doing the math and the types you are using?

Tips, Buy me a coffee, or three.. PayPal Venmo
Up vote any posts that you find helpful, it shows what's working..

Which STM32? If 'L0, TIM2 is not 32-bit.

> Frequency = TIM_CLOCK / ((C2-C1)*(prescaler+1))

Shouldn't SYSTEM_CLOCK be in this formula instead of TIM_CLOCK?

Give us an example with concrete numbers and - as Clive said above - types.

JW

HB
Associate II

I am using STM32F429ZI Nucleo board.

@Community member​ brother you are right the equation should be

Frequency = SYSTEM_CLOCK  / ((C2-C1)*(prescaler+1)).

I am using HSI clock 16Mhz as system clock

To get 1Mhz (1us for 1 clock pulse) timer clock i am using 16-1 as timer prescaler i.e 16000000/16 = 1000000

I am applying 20-1000hz signal on input capture & using Upcounter.

Testcase_1:- //ARR = 65535

79hz signal is input on TIM2_CH1

I got Capture 1 = 7885 & Capture 2 = 20430 therefore C2-C1 = 12545

so as per equation

Frequency = 16000000 / 12545 * 16 ----> 79.7130 Hz

Testcase_2:- //ARR = 0xffffffff

79hz signal is input on TIM2_CH1

I got Capture 1 = 79601 & Capture 2 = 280255 therefore C2-C1 = 200654

so as per equation

Frequency = 16000000 / 200654 * 16 ----> 4.9837 Hz

I have only changed ARR value of timer nothing else!!!

As per my understandings ARR is the value at which the timer will overflow. So if i use 65535 the timer will overflow at that value which means the count will go from 0-65535 and then reset. Now when i increase the value of ARR it should increase the amount of count. So there should be no change in both capture values of input capture.

So please tell me why this is happening or I have done anything wrong.

Thank you.

Note, that prescaler is preloaded.

JW​

HB
Associate II

@Community member​ Bro i dont get it

You haven't posted any code, but my guess is, that you set TIM2_PSC without generating an Update event. In that case, the timer runs with the original prescaler's value until CNT reaches ARR, which in case of ARR=0xFFFFFFFF and 16MHz clock takes 268 seconds or cca 4.5 minutes.

Have you read the article I've linked to above?

JW

Post the code, not your interpretation of what's happening.

Tips, Buy me a coffee, or three.. PayPal Venmo
Up vote any posts that you find helpful, it shows what's working..

This is a public forum, not your private beer party with "bros"! You still haven't shown what data types are you using at least for C1 and C2 variables. And Jan showed you the problem and the solution. If you don't understand it, then open the reference manual, AN4776 and Jan's link, and reread those until you do understand it.

HB
Associate II

@Piranha​ Thank you for correcting me.I will use formal words.

I am using uint32_t for both C1 & C2.