Skip to main content
jugal
Associate II
June 18, 2021
Solved

Input capture prescaler does not work as expected no matter what i try.

  • June 18, 2021
  • 1 reply
  • 1527 views

I am trying to measure the frequency of a square wave using TIM1 in direct input capture mode (STM32F3-DISCOVERY) . I can measure the frequency successfully , but i want to add an input prescaler, no matter what the value of prescalar i set i get the same value on my capture/compare register (CCR1).

My code for TIM1:

void TIM1_IC_INIT(void) {
	RCC->AHBENR |= RCC_AHBENR_GPIOAEN;		 //ENABLE GPIOA CLOCK
	RCC->APB2ENR |= RCC_APB2ENR_TIM1EN; 	 //ENABLE TIMER CLOCK 
	RCC->CFGR3 |= RCC_CFGR3_TIM1SW; //SELECT CLOCK SOURCE PLLCLK*2
	GPIOA->MODER |= GPIO_MODER_MODER8_1; //SET GPIO TO ALETERNATE FUNCTION
	GPIOA->AFR[1] |= 6 << GPIO_AFRH_AFRH0_Pos; //AF6 FOR PA8 , TIM1_CH1
 
	TIM1->SMCR |= 5 << TIM_SMCR_TS_Pos | TIM_SMCR_SMS_2; //RESET MODE
 
	TIM1->CCMR1 |= 1 << TIM_CCMR1_CC1S_Pos; //CONFIGURE CH1 AS INPUT
	TIM1->CCER &= ~(TIM_CCER_CC1P | TIM_CCER_CC1NP);	//RISING EDGE POLARITY
	TIM1->CCMR1 |= 3 << TIM_CCMR1_IC1PSC_Pos; //SET A PRESCALER OF 8
	TIM1->CCER |= TIM_CCER_CC1E; //ENABLE CAPTURE
	TIM1->CR1 |= TIM_CR1_CEN; // START TIMER
}

While debugging I checked the value of CCMR1 to check if the prescaler bits have been set properly and yes they were, Value of IC1PSC[3:2] is set to 0b11. I dont know where the problem is, any ideas?

This topic has been closed for replies.
Best answer by waclawek.jan

The input to slave-mode controller is not prescaled, i.e. each rising edge will reset the counter, so the captured value is from the previous reset i.e. previous edge, no matter what is the prescaler.

Don't use the slave-mode controller reset, rather, calculate the frequency from difference of two consecutive captured values.

JW

1 reply

waclawek.jan
waclawek.janBest answer
Super User
June 18, 2021

The input to slave-mode controller is not prescaled, i.e. each rising edge will reset the counter, so the captured value is from the previous reset i.e. previous edge, no matter what is the prescaler.

Don't use the slave-mode controller reset, rather, calculate the frequency from difference of two consecutive captured values.

JW

jugal
jugalAuthor
Associate II
June 18, 2021

thank you so much, that solves it.