AnsweredAssumed Answered

Timer 8 of STM32F4 Discovery don't work !

Question asked by Robert on Mar 11, 2013
Latest reply on Jul 10, 2015 by Clive One
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_CH3N. 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.

Outcomes