Input Capture not consistent (STM32F103C6)
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.
