AnsweredAssumed Answered

stm32f10x - Input capture

Question asked by theirs.fernando on Aug 5, 2016
Latest reply on Aug 8, 2016 by Clive One
Hi everyone,
I'm using the stm32f10x to measure a pulse width and I followed the Input Capture example but I still have some errors. The measurement is erratic, sometimes the value is ok and most of the times it's wrong.

What I need to do, is measure the time high from a pulse. I don't know if this pulse is comming periodically or not, so the period measurement doesn't work for me. As you can see I changed TIM_ICPolarity_Rising for TIM_ICPolarity_BothEdge. Do I need to change something else to get the pulse width?

Sorry if it's a newbie question but I started on Monday with this MCU and I'm a little bit confuse.

Many thanks,
Fernando

    //Remmaping TIM3_CH2 PA0 --> PC7
    GPIO_PinRemapConfig(GPIO_FullRemap_TIM3, ENABLE);
 
    /* TIM3 clock enable */
    RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM3, ENABLE);
 
    /* GPIOC clock enable */
    RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOC, ENABLE);
 
    /* TIM3 channel 2 pin (PC.07) configuration */
    GPIO_InitStructure.GPIO_Pin =  GPIO_Pin_7;
    GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IPD;
    GPIO_InitStructure.GPIO_Speed = GPIO_Speed_2MHz;
     
    GPIO_Init(GPIOC, &GPIO_InitStructure);
     
    TIM_DeInit(TIM3);
     
    TIM_ICInitStructure.TIM_Channel = TIM_Channel_2;
    TIM_ICInitStructure.TIM_ICPolarity = TIM_ICPolarity_BothEdge;
    TIM_ICInitStructure.TIM_ICSelection = TIM_ICSelection_DirectTI;
    TIM_ICInitStructure.TIM_ICPrescaler = TIM_ICPSC_DIV1;
    TIM_ICInitStructure.TIM_ICFilter = 0x0;
 
    TIM_ICInit(TIM3, &TIM_ICInitStructure);
   
    /* TIM enable counter */
    TIM_Cmd(TIM3, ENABLE);
 
    /* Enable the CC2 Interrupt Request */
    TIM_ITConfig(TIM3, TIM_IT_CC2, ENABLE);
    TIM_ITConfig(TIM3, TIM_IT_Update, DISABLE);
   
    /* Enable the TIM3 global Interrupt */
    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_IRQHandler(void)
{
    if(TIM_GetITStatus(TIM3, TIM_IT_CC2) == SET)
    {
        /* Clear TIM3 Capture compare interrupt pending bit */
        TIM_ClearITPendingBit(TIM3, TIM_IT_CC2);
        if(CaptureNumber == 0)
        {
        /* Get the Input Capture value */
        IC3ReadValue1 = TIM_GetCapture2(TIM3);
        CaptureNumber = 1;
        }
        else if(CaptureNumber == 1)
        {
            /* Get the Input Capture value */
            //IC3ReadValue2 = TIM_GetCapture2(TIM3);
               
            /* Capture computation */
            if (IC3ReadValue2 > IC3ReadValue1)
            {
                Capture = (IC3ReadValue2 - IC3ReadValue1);
            }
            else
            {
                Capture = ((0xFFFF - IC3ReadValue1) + IC3ReadValue2);
            }
       
            /* Frequency computation */
            TIM3Freq = (uint32_t) SystemCoreClock / Capture;
            CaptureNumber = 0;
        }
    }
}

Outcomes