cancel
Showing results for 
Search instead for 
Did you mean: 

Detect PWM Frequency using STM32F207ZG

Nimit Vachhani
Associate III

Greetings Everybody,

I have a hall effect sensor and i want to detect pulse generated by the sensor using STM32F207ZG.

My approach is to generate pulse using hall effect switch and feed that pulse to Timer 5 Channel 1 which runs in Input Capture Direct Mode to detect the frequency generated by the hall effect sensor. Can my approach work ? I have never used pwm in stm32 and this is my first time.

Please guide me if my approach is wrong.

Thanks.

4 REPLIES 4

Read the TIM chapter in RM, in particular the PWM input mode subchapter.

JW

Ok. Now i am using pwm generator and feeding it to PA0 pin of STM32F207ZG nucleo board. I want to measure 1 HZ to 80KHz frequency...

The timer clock settings are as below

0693W00000BatwVQAR.jpgBelow is my timer5 setting..

0693W00000BauC9QAJ.jpgBelow is my interrupt code to measure the difference between two rising edge...

void HAL_TIM_IC_CaptureCallback(TIM_HandleTypeDef *htim)
{
	if (htim->Channel == HAL_TIM_ACTIVE_CHANNEL_1)
	{
		if (Is_First_Captured==0)
		{
			IC_Value1 = HAL_TIM_ReadCapturedValue(htim, TIM_CHANNEL_1);
			Is_First_Captured =1;
		}
 
		else if (Is_First_Captured)
		{
			IC_Value2 = HAL_TIM_ReadCapturedValue(htim, TIM_CHANNEL_1);
 
			if (IC_Value2 > IC_Value1)
			{
				Difference = IC_Value2-IC_Value1;
			}
 
			else if (IC_Value2 < IC_Value1)
			{
				Difference = ((0xffffffff-IC_Value1)+IC_Value2) +1;
			}
 
			else
			{
				Error_Handler();
			}
 
			Frequency = HAL_RCC_GetPCLK1Freq();
			Frequency = Frequency / Difference;
			Is_First_Captured = 0;
		}
	}
}

The problem is that it is showing almost half the frequency and the accuracy is also low. Suppose for 100Hz input pulse it shows me 48 Hz. Any idea where i am going wrong and how to improve the measuring accuracy ?

NOTE : I am using nucleo board and i am using internal crystal. Do i need to use external crystal for better accuracy and timing performance ?

What is "internal crystal"? HSI? If yes, it's not very precise, see datasheet.

If you use a non-1 APB divider, the timer's clock is twice as much as APB clock, see RCC chapter of RM.

JW

I will try it and let you know.