cancel
Showing results for 
Search instead for 
Did you mean: 

frequency capture

c10390377
Associate II
Posted on June 06, 2013 at 15:59

frequency capture

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. im using stm32f100rb

&sharpinclude ''stm32f10x.h''

&sharpinclude ''stm32f10x_tim.h''

&sharpinclude ''stm32f10x_rcc.h''

&sharpinclude ''stm32f10x_gpio.h''

&sharpinclude ''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) {}

}

0690X0000060Mm3QAE.gif

#help-me-!=-do-it-for-me
6 REPLIES 6
c10390377
Associate II
Posted on June 06, 2013 at 20:31

what im trying to do is count the rpm (Speed) of the motor to give out pwm to control the motor. working on this for a month and half without any thing .. if any have a code or can do sthing hlp plz  

Posted on June 06, 2013 at 22:18

Yeah, 45 days does seem a tad excessive, I'd probably use Input Capture mode and do a delta measurement between two edges against a fast time base. This would allow you to measure the period of one cycle, and thus the instantaneous frequency. 200 KHz a bit on the high side, for interrupting a CPU running at 24 MHz.

You'd probably also want a 32-bit timer, depends on how close to DC you want to measure, or how large a prescaler you can tolerate on a 16-bit counter. The F100 might be a problem in this regard.

The other alternative is to count external pulses, and integrate over time, or gate/sample with a secondary timer. Would need to check quicker than 333ms to stay withing 16-bit and have 200KHz to DC range.

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

hi clive1

thanx for replaying i really dont understand alot of things take easy with me plz 🙂 i have been reading alot specially in this side i almost red all your posts . delta measurement ?? but how can i implement this things how do i do it this my problem and can;t find any one to help me. i have another code for you for encoder interface (can i use this code to measure period or frequency?? here is the code

// STM32 TIM3 Encoder Decoder (PC.06:A PC.07:B) VLDiscovery - sourcer32@gmail.com
#include ''stm32F10x.h''
#include ''STM32vldiscovery.h''
volatile int32_t Capture;
/**************************************************************************************/
void RCC_Configuration(void)
{
// clock for GPIO and AFIO (for ReMap)
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOC | RCC_APB2Periph_AFIO, ENABLE);
// clock for TIM3
RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM3, ENABLE);
}
/**************************************************************************************/
void GPIO_Configuration(void)
{
GPIO_InitTypeDef GPIO_InitStructure;
// PC.06 TIM3_CH1, PC.07 TIM3_CH2
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_6 | GPIO_Pin_7;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING;
GPIO_Init(GPIOC, &GPIO_InitStructure);
GPIO_PinRemapConfig( GPIO_FullRemap_TIM3, ENABLE ); // Map TIM3 to GPIOC
}
/**************************************************************************************/
void TIM3_Configuration(void)
{
TIM_TimeBaseInitTypeDef TIM_TimeBaseStructure;
TIM_TimeBaseStructure.TIM_Prescaler = 0;
TIM_TimeBaseStructure.TIM_Period = 65535; // Maximal
TIM_TimeBaseStructure.TIM_ClockDivision = TIM_CKD_DIV1;
TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_Up;
TIM_TimeBaseInit(TIM3, &TIM_TimeBaseStructure);
// TIM_EncoderMode_TI1: Counter counts on TI1FP1 edge depending on TI2FP2 level.
TIM_EncoderInterfaceConfig(TIM3, TIM_EncoderMode_TI1, TIM_ICPolarity_Rising, TIM_ICPolarity_Rising);
TIM_Cmd(TIM3, ENABLE);
}
/**************************************************************************************/
int main(void)
{
RCC_Configuration();
GPIO_Configuration();
TIM3_Configuration();
// TIM3->CNT contains current position
 
 capture = TIM_GetCounter (TIM3) ;
while(1); // Do not exit
}

Posted on June 07, 2013 at 01:20

A delta measurement, ie a difference

So if you have an encoder setup up you could sample the value periodically, say every 1 ms, if the shaft is generating a 200 KHz signal this would appear as a delta measurement of 200 ticks up or down of the counter since the last measurement, 2000 for a 10 ms sampling. You could store that measurement in an array, and average 100 or 1000 samples.

You should perhaps be seeking help from your colleagues, teachers, or whatever.
Tips, Buy me a coffee, or three.. PayPal Venmo
Up vote any posts that you find helpful, it shows what's working..
c10390377
Associate II
Posted on June 07, 2013 at 02:07

i did he always buzy.  this my final project have no collegee.  can you show the part for measuring ?

Posted on June 07, 2013 at 17:14

You'll want to look at the SysTick examples in the firmware library.

Set up a 1 ms SysTick, and then in the SysTick_Handler() read the TIM3->CNT and compare the current value with the prior value.

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