Skip to main content
HB_it
Associate II
September 26, 2022
Solved

How ARR value impact on timer input capture result?

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.

This topic has been closed for replies.
waclawek.jan
waclawek.janBest answer
Super User
September 29, 2022

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

TIM2->EGR = TIM_EGR_UG;

JW

26 replies

Tesla DeLorean
Guru
September 26, 2022

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 (See Profile) Up vote any posts that you find helpful, it shows what's working..
waclawek.jan
Super User
September 26, 2022

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_it
HB_itAuthor
Associate II
September 27, 2022

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.

Tesla DeLorean
Guru
September 27, 2022

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

Tips, Buy me a coffee, or three.. PayPal Venmo (See Profile) Up vote any posts that you find helpful, it shows what's working..
waclawek.jan
Super User
September 27, 2022

Note, that prescaler is preloaded.

​

JW​

HB_it
HB_itAuthor
Associate II
September 27, 2022

@Community member​ Bro i dont get it

Piranha
Principal III
September 27, 2022

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_it
HB_itAuthor
Associate II
September 28, 2022

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

I am using uint32_t for both C1 & C2.

waclawek.jan
Super User
September 27, 2022

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

HB_it
HB_itAuthor
Associate II
September 28, 2022

@Community member​ Yes i have read that.

Please note that i am not changing ARR after timer started.

#include "stm32f401xc.h"

void Gpio_Init(void);

void Timer_Init(void);

//-----------------------Main start----------------------------

int main()

{

volatile uint32_t C1, C2;

Gpio_Init();

Timer_Init();

while(1)

{

TIM1->CNT = 0;

//Polling method

while((TIM1->SR & (1 << 1)) == 0);

C1 = TIM1->CCR1; //Capture 1

while((TIM1->SR & (1 << 1)) == 0);

C2 = TIM1->CCR1; //Capture 2

}

}//----------------------Main end------------------------------

//F01-GPIO Init

void Gpio_Init(void)

{

//1 Enable clock to gpio

RCC->AHB1ENR |= (1 << 0); //Port A

//2 Gpio mode

GPIOA->MODER &= ~(1U << 16);

GPIOA->MODER |= (1 << 17); //PA8 alternate func mode

//3 Alter function define

GPIOA->AFR[1] |= (1 << 0); //AF == TIM1_CH1

}

//F02-Timer Init

void Timer_Init(void)

{

//1 Timer clock enable

RCC->APB2ENR |= (1 << 0);

//2 Stop timer

TIM1->CR1 &= ~(1U << 0);

//3 Set timer prescaler 

TIM1->PSC = 16-1;

//4 Set autoreload value

TIM1->ARR = 65535;

//5 Select timer configurations

TIM1->CR1 &= ~(1U << 7); //Autoreoad disable 

TIM1->CR1 &= ~(1U << 4); //Upcount

//6 Input channel select 

TIM1->CR2 &= ~(1U << 7); //TIM1_CH1 as TI1

TIM1->CCMR1 &= ~(3U << 0); //Clear CC1S bits

TIM1->CCMR1 |= (1 << 0); //IC1 is mapped on TI1

//7 Select filter value

TIM1->CR1 &= ~(3U << 8); //tDTS == tCK_CNT(prescaler)

TIM1->CCMR1 &= ~(0xfU << 4); //No filter

//8 Select edge

TIM1->CCER &= ~((1U << 1)|(1U << 3));

//9 Select prescalers for capture

TIM1->CCMR1 &= ~(3U << 2);

//10 Capture enable

TIM1->CCER |= (1 << 0);

//11 Timer run

TIM1->CR1 |= (1U << 0);

}

Thank you

waclawek.jan
Super User
September 28, 2022

After you've set TIM1->PSC, do

TIM1->EGR = TIM_EGR_UG;

JW

Piranha
Principal III
September 28, 2022

Better set TIM_EGR_UG after both the PSC and ARR registers are set.

> TIM1->ARR = 65535;

No need to explicitly set the maximum value as that is the default value after reset.

HB_it
HB_itAuthor
Associate II
September 29, 2022

@Community member​  @Piranha​ Tried both the things but still result is same.

waclawek.jan
Super User
September 29, 2022

Post code and concrete numbers.

​

JW​