cancel
Showing results for 
Search instead for 
Did you mean: 

stm32f100x discovery board input capture problem

adhiyanrs
Associate
Posted on December 13, 2014 at 16:34

Hello every one ,

I am using stm32vl discovery board for PWM input capture.I am using TIM3 channel 2 and counter clock frequency is 24MHz  with this i can measure the frequency and duty cycle of input PWM only till 367Hz.But i am facing problems in configuring prescalar value  to measure even lesser input frequency . please help me out in  setting prescalar value  . code which i am using is as below

main code

int main(void)

{

   

       

  /* System Clocks Configuration */

  RCC_Configuration();

  /* NVIC configuration */

  NVIC_Configuration();

  /* Configure the GPIO ports */

  GPIO_Configuration();

  

  TIM_ICInitStructure.TIM_Channel = TIM_Channel_2;

  TIM_ICInitStructure.TIM_ICPolarity = TIM_ICPolarity_Rising;

  TIM_ICInitStructure.TIM_ICSelection = TIM_ICSelection_DirectTI;

  TIM_ICInitStructure.TIM_ICPrescaler = TIM_ICPSC_DIV1;

  TIM_ICInitStructure.TIM_ICFilter = 0x0;

  TIM_PWMIConfig(TIM3, &TIM_ICInitStructure);

  /* Select the TIM3 Input Trigger: TI2FP2 */

  TIM_SelectInputTrigger(TIM3, TIM_TS_TI2FP2);

  /* Select the slave Mode: Reset Mode */

  TIM_SelectSlaveMode(TIM3, TIM_SlaveMode_Reset);

  /* Enable the Master/Slave Mode */

  TIM_SelectMasterSlaveMode(TIM3, TIM_MasterSlaveMode_Enable);

  /* TIM enable counter */

  TIM_Cmd(TIM3, ENABLE);

  /* Enable the CC2 Interrupt Request */

  TIM_ITConfig(TIM3, TIM_IT_CC2, ENABLE);

  while (1);

}

void RCC_Configuration(void)

{

  /* TIM3 clock enable */

  RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM3, ENABLE);

  /* GPIOA clock enable */

  RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA, ENABLE);

}

void GPIO_Configuration(void)

{

  GPIO_InitTypeDef GPIO_InitStructure;

  /* TIM3 channel 2 pin (PA.07) configuration */

  GPIO_InitStructure.GPIO_Pin = GPIO_Pin_7;

  GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING;

  GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;

  GPIO_Init(GPIOA, &GPIO_InitStructure);

}

void NVIC_Configuration(void)

{

  NVIC_InitTypeDef NVIC_InitStructure;

  /* Enable the TIM3 global Interrupt */

  NVIC_InitStructure.NVIC_IRQChannel = TIM3_IRQn;

  NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 0;

  NVIC_InitStructure.NVIC_IRQChannelSubPriority = 1;

  NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;

  NVIC_Init(&NVIC_InitStructure);

}

 

Timer interrupt handler code 

void TIM3_IRQHandler(void)

{

  /* Clear TIM3 Capture compare interrupt pending bit */

  TIM_ClearITPendingBit(TIM3, TIM_IT_CC2);

  /* Get the Input Capture value */

  IC2Value = TIM_GetCapture2(TIM3);

  if (IC2Value != 0)

  {

    /* Duty cycle computation */

    DutyCycle = (TIM_GetCapture1(TIM3) * 100) / IC2Value;

    /* Frequency computation */

    Frequency = SystemCoreClock / IC2Value;

  }

  else

  {

    DutyCycle = 0;

    Frequency = 0;

  }

}

 

1 REPLY 1
Posted on December 13, 2014 at 21:29

You'd need to configure the time base to a suitable frequency, and then adjust your frequency computation to use the time base frequency, and not the system one.

TIM_TimeBaseInitTypeDef TIM_TimeBaseStructure;
uint32_t PrescalerValue;
/* Compute the prescaler value */
PrescalerValue = (SystemCoreClock / 1000000); // 1MHz time base
/* Time base configuration */
TIM_TimeBaseStructure.TIM_Period = 65535; // Maximal
TIM_TimeBaseStructure.TIM_Prescaler = (uint16_t)PrescalerValue - 1;
TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_Up;
TIM_TimeBaseStructure.TIM_ClockDivision = 0; // Input sychronizer
TIM_TimeBaseStructure.TIM_RepetitionCounter = 0; // Unused
TIM_TimeBaseInit(TIM3, &TIM_TimeBaseStructure);
...
/* Frequency computation */
Frequency = (SystemCoreClock / IC2Value) / PrescalerValue;

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