Skip to main content
imanpakii
Associate III
February 13, 2013
Question

capturing frequency

  • February 13, 2013
  • 4 replies
  • 893 views
Posted on February 13, 2013 at 18:40

Hi guys 

I'm working on a project, I need to fine filing edge of a stream with 4.000 Mhz  frequency.This is my code and IRQ sub program:

  GPIO_InitTypeDef GPIO_InitStructure;

  NVIC_InitTypeDef NVIC_InitStructure;

  /* TIM1 clock enable */

  RCC_APB2PeriphClockCmd(RCC_APB2Periph_TIM1, ENABLE);

  /* GPIOA clock enable */

  RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOE, ENABLE);

  /* TIM1 channel 2 pin (PE.11) configuration */

  GPIO_InitStructure.GPIO_Pin =  GPIO_Pin_11 ;

  GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF;

  GPIO_InitStructure.GPIO_Speed = GPIO_Speed_100MHz;

  GPIO_InitStructure.GPIO_OType = GPIO_OType_PP;

  GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_NOPULL;

  GPIO_Init(GPIOE, &GPIO_InitStructure);

  /* Connect TIM pins to AF2 */

  GPIO_PinAFConfig(GPIOE, GPIO_PinSource11, GPIO_AF_TIM1);

  /* Enable the TIM1 global Interrupt */

  NVIC_InitStructure.NVIC_IRQChannel = TIM1_CC_IRQn;

  NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 0;

  NVIC_InitStructure.NVIC_IRQChannelSubPriority = 1;

  NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;

  NVIC_Init(&NVIC_InitStructure); 

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 = 0;

Delay(0x3FFFFF);

TIM_ICInit(TIM1, &TIM_ICInitStructure);

/* TIM enable counter */

TIM_Cmd(TIM1, DISABLE);

//BLF=10144;

//BLF=24806;

/* Enable the CC2 Interrupt Request */

TIM_Cmd(TIM1, ENABLE);

TIM_ITConfig(TIM1, TIM_IT_CC2, ENABLE);

TIM_ICInit(TIM1, &TIM_ICInitStructure);

void TIM1_CC_IRQHandler(void)

{

Array1[index0] =TIM1->CCR2;    

if(++index0>=60) index0=0;

TIM_ClearITPendingBit(TIM1, TIM_IT_CC2);

}

but i couldn't measure 4 Mhz.

please help me

thank you

  

    This topic has been closed for replies.

    4 replies

    Tesla DeLorean
    Guru
    February 13, 2013
    Posted on February 13, 2013 at 19:13

    Yeah, 4 MHz, not going to be able to interrupt and count clock tick deltas with that rate.

    For high rates you want to slave a timer against the external clock, and integrate. Say over a period of 10us, sampling the counter, and observing 40000 ticks +/-

    Tips, Buy me a coffee, or three.. PayPal VenmoUp vote any posts that you find helpful, it shows what's working..
    imanpakii
    imanpakiiAuthor
    Associate III
    February 14, 2013
    Posted on February 14, 2013 at 02:01

    Thank you Clive1

    i want to change my algorithm and i want to count my event with 4 mhz external clock.

    I configure timer 4 as external clock and i connect my pulse to this clock but it doesn't work .

    this is my code for configure timer4 and connect it to pb.6

     GPIO_InitTypeDef GPIO_InitStructure;

     NVIC_InitTypeDef NVIC_InitStructure;

     /* GPIOA clock enable */

     RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM4, ENABLE);

     RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOB, ENABLE);

     /* TIM1 channel 2 pin (PE.11) configuration */

     GPIO_InitStructure.GPIO_Pin =  GPIO_Pin_6 ;

     GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF;

     GPIO_InitStructure.GPIO_Speed = GPIO_Speed_100MHz;

     GPIO_InitStructure.GPIO_OType = GPIO_OType_PP;

     GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_NOPULL;

     GPIO_Init(GPIOE, &GPIO_InitStructure);

     GPIO_PinAFConfig(GPIOB, GPIO_PinSource6, GPIO_AF_TIM4);

     /* Connect TIM pins to AF2 */

    GPIO_PinAFConfig(GPIOE, GPIO_PinSource11, GPIO_AF_TIM1);

    TIM_ETRClockMode1Config(TIM4, TIM_ExtTRGPSC_OFF,TIM_ExtTRGPolarity_Inverted,0);

    TIM_Cmd(TIM4, ENABLE);

      please help me as usual :)

    Thank you 

    Tesla DeLorean
    Guru
    February 14, 2013
    Posted on February 14, 2013 at 02:11

     GPIO_Init(GPIOE, &GPIO_InitStructure); // GPIO E ??

    Tips, Buy me a coffee, or three.. PayPal VenmoUp vote any posts that you find helpful, it shows what's working..
    imanpakii
    imanpakiiAuthor
    Associate III
    February 14, 2013
    Posted on February 14, 2013 at 20:45

    Hi Clive1 

    thank you for your reply 

    i changed that cod to portb but steel i have problem maybe i couldn't program my pins correctly. 

    i changed  my code like here :

    GPIO_InitTypeDef GPIO_InitStructure;

    NVIC_InitTypeDef NVIC_InitStructure;

    /* GPIOA clock enable */

    RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM4, ENABLE);

    // RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOB, ENABLE);

    /* TIM1 channel 2 pin (PE.11) configuration */

    GPIO_InitStructure.GPIO_Pin = GPIO_Pin_6 ;

    GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF;

    GPIO_InitStructure.GPIO_Speed = GPIO_Speed_100MHz;

    GPIO_InitStructure.GPIO_OType = GPIO_OType_PP;

    GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_DOWN;

    GPIO_Init(GPIOB, &GPIO_InitStructure);

    GPIO_PinAFConfig(GPIOB, GPIO_PinSource6, GPIO_AF_TIM4);

    /* Connect TIM pins to AF2 */

    //GPIO_PinAFConfig(GPIOE, GPIO_PinSource11, GPIO_AF_TIM1);

    TIM_TIxExternalClockConfig(TIM4,TIM_TIxExternalCLK1Source_TI1,TIM_ICPolarity_Rising, 0);

    // TI1_Config(TIM4,TIM_ICPolarity_Falling,TIM_ICSelection_DirectTI,0);

    TIM_ETRClockMode1Config(TIM4, TIM_ExtTRGPSC_OFF,TIM_ExtTRGPolarity_Inverted,0);

    TIM_CounterModeConfig(TIM4,TIM_CounterMode_Up);

    TIM_Cmd(TIM4, ENABLE);