2022-09-26 04:35 AM
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.
Solved! Go to Solution.
2022-09-29 02:45 AM
After you've set PSC and ARR, e.g. somewhere between steps 5 and 8, do
TIM2->EGR = TIM_EGR_UG;
JW
2022-09-26 07:11 AM
Perhaps it relates to how you're doing the math and the types you are using?
2022-09-26 10:08 AM
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
2022-09-26 10:37 PM
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.
2022-09-26 11:27 PM
2022-09-27 03:01 AM
@Community member Bro i dont get it
2022-09-27 02:00 PM
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
2022-09-27 02:12 PM
Post the code, not your interpretation of what's happening.
2022-09-27 02:33 PM
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.
2022-09-27 09:48 PM
@Piranha Thank you for correcting me.I will use formal words.
I am using uint32_t for both C1 & C2.