cancel
Showing results for 
Search instead for 
Did you mean: 

Timer 8 of STM32F4 Discovery don't work !

naceur2311
Associate II
Posted on March 11, 2013 at 10:54

Hi,

I have problems with Timer 8 this time. I want to measure the frequency of an input signal. I have to use PB1 that's connected to TIM8_CH3

N

. The code seem correct but it doesn't give any frequency. The code is the following :

The main function :

int main(void)

{

    /* TIM8 Configuration */

  TIM_Config();

  TIM_ICInitStructure.TIM_Channel     = TIM_Channel_3; 

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

  TIM_Cmd(TIM8, ENABLE);

  TIM_ITConfig(TIM8, TIM_IT_CC3, ENABLE);

  while (1);

}

void TIM_Config(void)

{

  GPIO_InitTypeDef GPIO_InitStructure;

  NVIC_InitTypeDef NVIC_InitStructure;

  RCC_APB2PeriphClockCmd(RCC_APB2Periph_TIM8, ENABLE);

  RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOB, ENABLE);

  GPIO_InitStructure.GPIO_Pin   = GPIO_Pin_1;

  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(GPIOB, &GPIO_InitStructure);

  GPIO_PinAFConfig(GPIOB, GPIO_PinSource1, GPIO_AF_TIM8);

  NVIC_InitStructure.NVIC_IRQChannel                   = TIM8_CC_IRQn;

  NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 0;

  NVIC_InitStructure.NVIC_IRQChannelSubPriority        = 1;

  NVIC_InitStructure.NVIC_IRQChannelCmd                = ENABLE;

  NVIC_Init(&NVIC_InitStructure);

}

The code of the interrupt routine is :

 

void TIM8_CC_IRQHandler(void)

{

  if(TIM_GetITStatus(TIM8, TIM_IT_CC3) == SET)

  {

    TIM_ClearITPendingBit(TIM8, TIM_IT_CC3);

    if(CaptureNumber == 0)

    {

       IC3ReadValue1 = TIM_GetCapture3(TIM8);

      CaptureNumber = 1;

    }

    else if(CaptureNumber == 1)

    {

      IC3ReadValue2 = TIM_GetCapture3(TIM8);

      if (IC3ReadValue2 > IC3ReadValue1)

      {

        Capture = (IC3ReadValue2 - IC3ReadValue1);

      }

      else if (IC3ReadValue2 < IC3ReadValue1)

      {

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

      }

      else

      {

        Capture = 0;

      }

      

      TIM8Freq = (uint32_t) SystemCoreClock / Capture;

      CaptureNumber = 0;

    }

  }

}

Any help to resolve this problem.

Thanks.
12 REPLIES 12
ppatel
Associate
Posted on July 10, 2015 at 21:38

I am not interrupting at 4MHz.

how can i use external count mod?? I need to read CNT register??

Posted on July 10, 2015 at 21:59

...when i set 4 MHz frequency, it is showing 8MHz...

 

I am not interrupting at 4MHz.

Really, and HAL_TIM_IC_CaptureCallback() is called by unicorns?

You're reading consecutive values of a CCRx register, and measuring the delta ticks between input clock edges.

If you were counting pulses, in External Clock mode, you'd sample TIMx->CNT with some periodicity, brief enough that the 16-bit count doesn't overflow. You could latch the value of count into some other TIMx->CCRx register, or via DMA.

Tips, Buy me a coffee, or three.. PayPal Venmo
Up vote any posts that you find helpful, it shows what's working..
Posted on July 10, 2015 at 22:01

As I've repeatedly pointed out in this thread, and others, this math is broken

 time = ((0xffff - counter)+time);

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