cancel
Showing results for 
Search instead for 
Did you mean: 

frequency capture

c10390377
Associate II
Posted on June 06, 2013 at 00:56

im trying to capture frequency from 0 to 100khz optical encoder . and here is my code doesn't seem to work. any help please im new to micro controller.

#include ''stm32f10x.h''

#include ''stm32f10x_tim.h''

#include ''stm32f10x_rcc.h''

#include ''stm32f10x_gpio.h''

#include ''misc.h''

unsigned int IC2Value, DutyCycle ,Frequency ;

   void RCC_Configuration(void)

   {

  RCC_APB1PeriphClockCmd (RCC_APB1Periph_TIM3, ENABLE); // clock configuration

//RCC_APB2PeriphClockCmd (RCC_APB2Periph_GPIOA, ENABLE);

   RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOC | RCC_APB2Periph_AFIO, ENABLE);

   }

   void GPIO_Configuration(void)

   {

// GPIO configuration

GPIO_InitTypeDef   GPIO_InitStructure;

GPIO_InitStructure.GPIO_Pin = GPIO_Pin_7;

GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING;

GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;

GPIO_Init(GPIOC, &GPIO_InitStructure);

GPIO_PinRemapConfig( GPIO_FullRemap_TIM3, ENABLE );        // Map TIM3 to GPIOC

   }

   void NVIC_Configuration(void)

   {

// NVIC configuration

NVIC_InitTypeDef NVIC_InitStructure;

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);

   }

   void TIM3_Configuration(void)

   {

// TIMER configuration

//TTIM_ICInitStructure (& TIM_ICInitStructure);

TIM_ICInitTypeDef  TIM_ICInitStructure;

TIM_ICInitStructure.TIM_Channel = TIM_Channel_2; // channel selection

TIM_ICInitStructure.TIM_ICPolarity = TIM_ICPolarity_Rising; // rising edge of the trigger

TIM_ICInitStructure.TIM_ICSelection = TIM_ICSelection_DirectTI; // correspondence between the pin and register

TIM_ICInitStructure.TIM_ICPrescaler = TIM_ICPSC_DIV1; // input prescaler. Control in the number of input cycle time capture, if

//TIM_ICInit(TIM3 , &TIM_ICInitStructure);

/* Input signal frequency has not changed, will not change the measured period. Such as divided by 4,

* each of the four input period before doing a capture,

*so that the input signal does not change frequently,

*Can reduce the frequency and software are constantly interrupted. */

TIM_ICInitStructure.TIM_ICFilter = 0x0; // filter settings, go through several cycles transition finds the stable waveform 0x0 to 0xF

TIM_PWMIConfig (TIM3, & TIM_ICInitStructure); // the parameter configuration TIM peripheral information

TIM_SelectInputTrigger (TIM3, TIM_TS_TI2FP2); // select IC2 always trigger source

TIM_SelectSlaveMode (TIM3, TIM_SlaveMode_Reset) ;// TIM mode: update event triggers the rising edge of the signal to re-initialize the counter and trigger register

TIM_SelectMasterSlaveMode (TIM3, TIM_MasterSlaveMode_Enable); // start the timer passive trigger

TIM_Cmd (TIM3, ENABLE); // start TIM3

TIM_ITConfig (TIM3, TIM_IT_CC2, ENABLE); // Open interrupt

   }

int main(void)

{

 RCC_Configuration();

 GPIO_Configuration();

 TIM3_Configuration();

 NVIC_Configuration();

void TIM3_IRQHandler (void)

{

IC2Value = TIM_GetCapture2 (TIM3); // read IC2 capture register, the count value is the PWM cycle

if (IC2Value != 0)

{

DutyCycle = (TIM_GetCapture1 (TIM3) * 100) / IC2Value; // read IC1 to capture the value of the register, and calculate the duty cycle

Frequency = 24000000 / IC2Value; // calculate the PWM frequency.

}

else

{

DutyCycle = 0;

Frequency = 0;

}

TIM_ClearITPendingBit (TIM3, TIM_IT_CC2); // clear interrupt pending bit TIM

}

while(1) {}

}

0 REPLIES 0