cancel
Showing results for 
Search instead for 
Did you mean: 

input capture mode

arunl4g
Associate II
Posted on June 05, 2015 at 11:29

hej,

i am getting a wrong values when i measuring the signal in input capture mode.can anyone help me what i am doing wrong and also i am new to this .i am using stm32f407vg

the below oen is intialization.

/new one

        TIM_TimeBaseInitTypeDef TIM_TimeBaseStructure;

          TIM_TimeBaseStructure.TIM_Prescaler =168 - 1;

          TIM_TimeBaseStructure.TIM_Period = 65535;

          TIM_TimeBaseStructure.TIM_ClockDivision = 0x0;

          TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_Up;

          TIM_TimeBaseInit(TIM1, &TIM_TimeBaseStructure);

         GPIO_InitTypeDef GPIO_IniStructure;

          NVIC_InitTypeDef NVIC_IniStructure;

          RCC_APB2PeriphClockCmd(RCC_APB2Periph_TIM1, ENABLE);

          RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOE, ENABLE);

          GPIO_IniStructure.GPIO_Pin   = GPIO_Pin_11;

          GPIO_IniStructure.GPIO_Mode  = GPIO_Mode_AF;

          GPIO_IniStructure.GPIO_Speed = GPIO_Speed_100MHz;

          GPIO_IniStructure.GPIO_OType = GPIO_OType_PP;

          GPIO_IniStructure.GPIO_PuPd  = GPIO_PuPd_NOPULL;

          GPIO_Init(GPIOE, &GPIO_IniStructure);

          GPIO_PinAFConfig(GPIOE, GPIO_PinSource11, GPIO_AF_TIM1); //timer alternative function mapping

          NVIC_IniStructure.NVIC_IRQChannel                   = TIM1_CC_IRQn;  //TIM1 Capture Compare Interrupt

          NVIC_IniStructure.NVIC_IRQChannelPreemptionPriority = 0;

          NVIC_IniStructure.NVIC_IRQChannelSubPriority        = 1;

          NVIC_IniStructure.NVIC_IRQChannelCmd                = ENABLE;

          NVIC_Init(&NVIC_IniStructure);

          TIM_ICInitTypeDef  TIM_ICInitStructure;

          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_ICInit(TIM1, &TIM_ICInitStructure);

          TIM_Cmd(TIM1, ENABLE);

          TIM_ITConfig(TIM1, TIM_IT_CC2, ENABLE);

the below one has to give me proper value but i am not measuring the proper signal .

void TIM1_CC_IRQHandler(void)

{

  if(TIM_GetITStatus(TIM1, TIM_IT_CC2) == SET)

  {

    TIM_ClearITPendingBit(TIM1, TIM_IT_CC2);

    if(CaptureNumber == 0)

    {

        //sprintf(processcommand, ''%d'', IC3ReadValue2);

         //printf( ''IC3ReadValue2 %d \n'', IC3ReadValue1 );

         //usart_SEND (USART6,processcommand);

        IC3ReadValue1 = TIM_GetCapture2(TIM2);

      CaptureNumber = 1;

    }

    else if(CaptureNumber == 1)

    {

        IC3ReadValue2 = TIM_GetCapture2(TIM1);

      //printf( ''IC3ReadValue2 %d \n'', IC3ReadValue2 );

      if (IC3ReadValue2 > IC3ReadValue1)

      {

          Capture = 0;

        Capture = (IC3ReadValue2 - IC3ReadValue1);

      }

      else if (IC3ReadValue2 < IC3ReadValue1)

      {

          Capture = 0;

        Capture = ((0xFFFF - IC3ReadValue1) + IC3ReadValue2);

      }

      else

      {

        Capture = 0;

      }

      TIM1Freq = (uint32_t) SystemCoreClock / Capture;

      sprintf(processcommand, ''%d'', TIM1Freq);

      usart_SEND (USART6,processcommand);

      usart_SEND (USART6,''\n\r'');

      //TIM1Freq = (uint32_t) SystemCoreClock / Capture1;

      CaptureNumber = 0;

    }

  }

}

i am using timer 1 and the clock frequency of that is 168MHZ.

 thank you ..
5 REPLIES 5
Posted on June 05, 2015 at 15:12

What is the nature of the signal you're trying to measure? Frequency, etc

What are the wrong/incorrect value you are getting vs expected?

The 16-bit math in your routine is broken.

Tips, Buy me a coffee, or three.. PayPal Venmo
Up vote any posts that you find helpful, it shows what's working..
es131245
Associate II
Posted on June 05, 2015 at 17:09

I've got it working on 429 disco with both SPL and HAL libraries.

Measured pulse length with 6.24nS step up to 14 sec.

PS frequency is 168MHz

I can post you codes (later today)

Posted on June 05, 2015 at 17:19

Probably not with 16-bit measurement/numbers.

Tips, Buy me a coffee, or three.. PayPal Venmo
Up vote any posts that you find helpful, it shows what's working..
es131245
Associate II
Posted on June 05, 2015 at 22:02

#include ''stm32f4xx_hal.h''

#include ''usb_device.h''

#include ''usbd_cdc_if.h''

 

TIM_HandleTypeDef timM;

TIM_HandleTypeDef timS;

TIM_MasterConfigTypeDef m;

TIM_SlaveConfigTypeDef s;

TIM_IC_InitTypeDef ic;

 

void Error_Handler(void)

{HAL_GPIO_WritePin(GPIOG,GPIO_PIN_14,GPIO_PIN_SET);

 while(1){}}

 

void EXTI0_IRQHandler(void)

{if(HAL_GPIO_ReadPin(GPIOG,GPIO_PIN_14)==GPIO_PIN_RESET)

 {if(HAL_TIM_IC_Stop_IT(&timM,TIM_CHANNEL_1)!=HAL_OK)Error_Handler();

  if(HAL_TIM_IC_Start_IT(&timM,TIM_CHANNEL_2)!=HAL_OK)Error_Handler();}

 else

 {if(HAL_TIM_IC_Stop_IT(&timM,TIM_CHANNEL_2)!=HAL_OK)Error_Handler();

  if(HAL_TIM_IC_Start_IT(&timM,TIM_CHANNEL_1)!=HAL_OK)Error_Handler();}

 HAL_GPIO_TogglePin(GPIOG,GPIO_PIN_14);

 HAL_NVIC_ClearPendingIRQ(EXTI0_IRQn);

 HAL_GPIO_EXTI_IRQHandler(GPIO_PIN_0);}

 

void HAL_TIM_IC_CaptureCallback(TIM_HandleTypeDef *htim)

{extern USBD_HandleTypeDef hUsbDeviceHS;

 extern TIM_HandleTypeDef timS;

 uint8_t buff_TX[128];

 buff_TX[0]='0';

 if(htim->Channel==HAL_TIM_ACTIVE_CHANNEL_1)

 {buff_TX[1]=1;

  buff_TX[2]=HAL_TIM_ReadCapturedValue(htim,TIM_CHANNEL_1)>>8;

  buff_TX[3]=HAL_TIM_ReadCapturedValue(htim,TIM_CHANNEL_1);}

 else

 {buff_TX[1]=2;

  buff_TX[2]=HAL_TIM_ReadCapturedValue(htim,TIM_CHANNEL_2)>>8;

  buff_TX[3]=HAL_TIM_ReadCapturedValue(htim,TIM_CHANNEL_2);}

  buff_TX[4]=TIM3->CNT>>8;

  buff_TX[5]=TIM3->CNT;

  buff_TX[6]=TIM9->CNT>>8;

  buff_TX[7]=TIM9->CNT;

 USBD_CDC_SetTxBuffer(&hUsbDeviceHS,&buff_TX[0],8);

 USBD_CDC_TransmitPacket(&hUsbDeviceHS);

 HAL_GPIO_TogglePin(GPIOG,GPIO_PIN_13);}

 

int main(void)

{HAL_Init();

 RCC_OscInitTypeDef RCC_OscInitStruct;

 RCC_ClkInitTypeDef RCC_ClkInitStruct;

 __PWR_CLK_ENABLE();

 __HAL_PWR_VOLTAGESCALING_CONFIG(PWR_REGULATOR_VOLTAGE_SCALE1);

 RCC_OscInitStruct.OscillatorType=RCC_OSCILLATORTYPE_HSE;

 RCC_OscInitStruct.HSEState=RCC_HSE_ON;

 RCC_OscInitStruct.PLL.PLLState=RCC_PLL_ON;

 RCC_OscInitStruct.PLL.PLLSource=RCC_PLLSOURCE_HSE;

 RCC_OscInitStruct.PLL.PLLM=8;

 RCC_OscInitStruct.PLL.PLLN=336;

 RCC_OscInitStruct.PLL.PLLP=RCC_PLLP_DIV2;

 RCC_OscInitStruct.PLL.PLLQ=7;

 HAL_RCC_OscConfig(&RCC_OscInitStruct);

 RCC_ClkInitStruct.ClockType=RCC_CLOCKTYPE_SYSCLK|RCC_CLOCKTYPE_PCLK1|RCC_CLOCKTYPE_PCLK2;

 RCC_ClkInitStruct.SYSCLKSource=RCC_SYSCLKSOURCE_PLLCLK;

 RCC_ClkInitStruct.AHBCLKDivider=RCC_SYSCLK_DIV1;

 RCC_ClkInitStruct.APB1CLKDivider=RCC_HCLK_DIV4;

 RCC_ClkInitStruct.APB2CLKDivider=RCC_HCLK_DIV2;

 HAL_RCC_ClockConfig(&RCC_ClkInitStruct,FLASH_LATENCY_5);

 __GPIOH_CLK_ENABLE();

 

 __GPIOG_CLK_ENABLE();

 GPIO_InitTypeDef initGPIO;

 initGPIO.Pin=GPIO_PIN_13|GPIO_PIN_14;

 initGPIO.Mode=GPIO_MODE_OUTPUT_PP;

 initGPIO.Pull=GPIO_PULLDOWN;

 initGPIO.Speed=GPIO_SPEED_LOW;

 HAL_GPIO_Init(GPIOG,&initGPIO);

 HAL_GPIO_WritePin(GPIOG,GPIO_PIN_13|GPIO_PIN_14,GPIO_PIN_SET);

 __GPIOA_CLK_ENABLE();

 initGPIO.Pin=GPIO_PIN_0;

 initGPIO.Mode=GPIO_MODE_IT_FALLING;

 initGPIO.Pull=GPIO_NOPULL;

 HAL_GPIO_Init(GPIOA,&initGPIO);

 HAL_NVIC_SetPriorityGrouping(NVIC_PRIORITYGROUP_0);

 HAL_NVIC_SetPriority(EXTI0_IRQn,4,1);

 HAL_NVIC_EnableIRQ(EXTI0_IRQn);

 

 __GPIOB_CLK_ENABLE();

 MX_USB_DEVICE_Init();

 HAL_GPIO_WritePin(GPIOG,GPIO_PIN_13|GPIO_PIN_14,GPIO_PIN_RESET);

 

 __GPIOE_CLK_ENABLE();

 GPIO_InitTypeDef GPIO_InitStruct;

 GPIO_InitStruct.Pin=GPIO_PIN_5|GPIO_PIN_6;

 GPIO_InitStruct.Mode=GPIO_MODE_AF_PP;

 GPIO_InitStruct.Pull=GPIO_PULLUP;

 GPIO_InitStruct.Speed=GPIO_SPEED_HIGH;

 GPIO_InitStruct.Alternate=GPIO_AF3_TIM9;

 HAL_GPIO_Init(GPIOE,&GPIO_InitStruct);

 HAL_NVIC_SetPriority(TIM1_BRK_TIM9_IRQn,5,5);

 HAL_NVIC_EnableIRQ(TIM1_BRK_TIM9_IRQn);

 

 __TIM9_CLK_ENABLE();

 timM.Instance=TIM9;

 timM.Init.Period=0xffff;

 timM.Init.Prescaler=0;

 timM.Init.ClockDivision=TIM_CLOCKDIVISION_DIV1;

 timM.Init.CounterMode=TIM_COUNTERMODE_UP;

 if(HAL_TIM_IC_Init(&timM)!=HAL_OK)Error_Handler();

 

 __TIM3_CLK_ENABLE();

 timS.Instance=TIM3;

 timS.Init.Period=0xffff;

 timS.Init.Prescaler=0;

 timS.Init.ClockDivision=TIM_CLOCKDIVISION_DIV1;

 timS.Init.CounterMode=TIM_COUNTERMODE_UP;

 if(HAL_TIM_IC_Init(&timS)!=HAL_OK)Error_Handler();

 

 ic.ICPrescaler=TIM_ICPSC_DIV1;

 ic.ICFilter=0;

 ic.ICPolarity=TIM_ICPOLARITY_FALLING;

 ic.ICSelection=TIM_ICSELECTION_INDIRECTTI;

 if(HAL_TIM_IC_ConfigChannel(&timM,&ic,TIM_CHANNEL_1)!=HAL_OK)Error_Handler();

 ic.ICPolarity=TIM_ICPOLARITY_RISING;

 ic.ICSelection=TIM_ICSELECTION_DIRECTTI;

 if(HAL_TIM_IC_ConfigChannel(&timM,&ic,TIM_CHANNEL_2)!=HAL_OK)Error_Handler();

 s.InputTrigger=TIM_TS_TI2FP2;

 s.SlaveMode=TIM_SLAVEMODE_RESET;

 if(HAL_TIM_SlaveConfigSynchronization(&timM,&s)!=HAL_OK)Error_Handler();

 if(HAL_TIM_IC_Start_IT(&timM,TIM_CHANNEL_1)!=HAL_OK)Error_Handler();

 if(HAL_TIM_IC_Start_IT(&timM,TIM_CHANNEL_2)!=HAL_OK)Error_Handler();

 

 while(1){}}

 

#ifdef USE_FULL_ASSERT

 void assert_failed(uint8_t* file,uint32_t line){}

#endif

arunl4g
Associate II
Posted on June 07, 2015 at 11:07

clive, i am measuring 2khz sqaure wave signal...

i am getting random values ..

whats meantby ''The 16-bit math in your routine is broken''....

if possible can you post a sample programme ....

thank you in advance