cancel
Showing results for 
Search instead for 
Did you mean: 

Input Capture not consistent (STM32F103C6)

GSaw
Associate II

I'm using STM32F103C6T6 to capture input frequency configured on TIM2, Channel 1 (A0).

I'm generating 1KHz frequency from Arduino Uno (using tone(Pin, 1000) function) connected to STM32 Pin PA0.

Timer Configurations of STM32:

Prescaler set to : 10000-1

ARR set to : 7200-1

Code is as below

void HAL_TIM_IC_CaptureCallback(TIM_HandleTypeDef* htim)
{
    if(gu8_State == IDLE)
    {
        gu32_T1 =  HAL_TIM_ReadCaptureValue(htim, TIM_CHANNEL_1);
        gu8_State = DONE;
    }
    else if(gu8_State == DONE)
    {
        gu32_T2 = HAL_TIM_ReadCaptureValue(htim, TIM_CHANNEL_1);
        gu32_Ticks = gu32_T2 - gu32_T1;
        gu32_Freq = (HAL_RCC_GetPCLK2Freq()/gu32_Ticks * 10000);
        if(gu32_Freq != 0)
        {
          sprintf(gu8_MSG, "Frequency = %lu Hz\n\r", gu32_Freq);
          HAL_UART_Transmit(&huart1, gu8_MSG, sizeof(gu8_MSG), 100);
        }
        gu8_State = IDLE;
    }
}

When Frequency in Arduino is set as : 1000, Input frequency is calculated as 1028.

When Frequency in Arduino is set as : 500, Input frequency is calculated as 514.

When Frequency in Arduino is set as : 100, Input frequency is calculated as 100.

When Frequency in Arduino is set as : 200, Input frequency is calculated as 200.

and likewise.

I'm unable to understand what is incorrect in above code and why is this happening for frequencies (500 and 1000) and not others.

Please help. Thank you.

12 REPLIES 12

Can you explain it a little bit please? I'm unable to understand.

Thank you.

I have understood your point. The difference of gu32_Ticks is causing this issue. The prescalar value is important as I am using it in frequency calculation in program. However, when I use smaller pre-scalar value (65,100 or 1000...), then lower frequencies (500, 1000 Hz...) are inaccurate and for higher pre-scalar (10000) higher frequencies (4700, 5000 Hz...) are inaccurate.

Yes, this is the point.Change down prescaler or extend measure time.