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

HB_it
HB_itAuthor
Associate II
September 29, 2022

Output 1: ARR 65535

0693W00000UFPV9QAP.pngOutput 2: ARR 0xFFFFFFFF

0693W00000UFPVOQA5.png

HB_it
HB_itAuthor
Associate II
September 29, 2022

Please note that I have not changed ARR once the timer start.

waclawek.jan
Super User
September 29, 2022

And the code?

JW

HB_it
HB_itAuthor
Associate II
September 29, 2022

#include "stm32f429xx.h"

//Function Prototypes

void Gpio_Init(void);

void Timer_Init(void);

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

int main()

{

volatile uint32_t time1, time2, time3;

volatile float fre, per, Toff, Duty;

Gpio_Init();

Timer_Init();

while(1)

{

TIM2->CNT = 0;

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

time1 = TIM2->CCR1;

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

time2 = TIM2->CCR1;

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

time3 = TIM2->CCR1;

//Frequency calculations 1Mhz Timer clock so 1us per clock pulse

per = (float)(time3 - time1); //To convert in ms divide it by 1000

fre = (float)1 / per;

fre *= 1000000;

//Duty cycle calculations

Toff = (float)(time3 - time2);

Duty = (float)(((per-Toff)*100)/per);

time1 = 0; time2 = 0; time3 = 0;

}

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

//F01-GPIO config

void Gpio_Init(void)

{

//1 Enable clock 

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

//2 Pin mode

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

GPIOA->MODER |= (1 << 11); //PA5 as alterenate function

//3 Alternate function decide

GPIOA->AFR[0] |= (1 << 20);

}

//F02-Timer config

void Timer_Init(void)

{

//1 Enable clock

RCC->APB1ENR |= (1 << 0); //Timer 2

//2 Stop timer

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

//3 Prescaler for timer

TIM2->PSC = 16 - 1;

//4 Select autoreload value

TIM2->ARR = 65535; //Not working for 0xffffffff? Ask 

//5 Timer config

TIM2->CR1 &= ~(1U << 7); //Autoreload disable

TIM2->CR1 &= ~(1U << 4); //Upcounter

//6 Timer channel config

TIM2->CCMR1 |= (1 << 0); //Tim1_CH1 as TI1

//7 Edge select

TIM2->CCER |= (1U << 1)|(1U << 3); //Both edges for PWM input

//TIM2->CCER &= ~((1U << 1)|(1U << 3)); //Rising edge

//8 Input capture enable 

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

//9 Timer run

TIM2->CNT = 0;

TIM2->CR1 |= (1 << 0);

}

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

HB_it
HB_itAuthor
Associate II
September 29, 2022

Yes i did but still not working.

waclawek.jan
Super User
September 29, 2022

> I think update event generation is only required if i change ARR/PSC after the timer has started

No.

JW

HB_it
HB_itAuthor
Associate II
September 29, 2022

Okay. I have tried it still not working.

waclawek.jan
Super User
September 29, 2022

Post code you have tried (exactly that code, copy/paste) and numbers.

JW

HB_it
HB_itAuthor
Associate II
September 29, 2022

It is working. Bymistake i wrote TIM1->EGR instead of TIM2->EGR.

Thank you very much for your time and efforts.