cancel
Showing results for 
Search instead for 
Did you mean: 

Encoder Counter be decreased in sometimes when is the motor running in one direction?

HPham.1590
Senior

When I used Encoder Mode on TIM2, I have set the motor to runs in one direction, so data in the CNT register must be increase. but it seems to be decreased in sometimes.

So anyone can explain to me about it?

I'm using encoder mode TI1&TI2, Counter Mode Up.

Many thanks.

Below is my function:

#define PWM_STEP 500
int test_pwm = 20000;
void MeasureData()
{
	const int N = 26; // Encoder resolution
	const float pi = 3.14159;
	const float R = 0.025;
	HAL_TIM_Encoder_Start(&htim2,TIM_CHANNEL_1);
	HAL_TIM_Encoder_Start(&htim4,TIM_CHANNEL_1);
	HAL_TIM_Encoder_Start(&htim2,TIM_CHANNEL_2);
	HAL_TIM_Encoder_Start(&htim4,TIM_CHANNEL_2);
	int i;
	while(test_pwm < 65500)
	{
		HAL_Delay(200);
		__HAL_TIM_SET_COUNTER(&htim4,0);
		__HAL_TIM_SET_COUNTER(&htim2,0);
		for(i = 0; i < 50000; i++)
		{
			DodoGo(1,test_pwm,test_pwm);
			DodoGo(2,0,0);
		}
		HAL_Delay(956);
		count_1 = __HAL_TIM_GET_COUNTER(&htim2);
		count_0 = __HAL_TIM_GET_COUNTER(&htim4);
			//HAL_USART_Transmit(&husart2,(uint8_t*)test_pwm,32,100);
			//HAL_USART_Transmit(&husart2,(uint8_t*)count,32,100);
		delta_count_0 = (count_0 - pre_count_0);
		delta_count_1 = (count_1 - pre_count_1);
		// Update postion 
		pre_count_0 = count_0;
		pre_count_1 = count_1;
		test_pwm = test_pwm + PWM_STEP;
		HAL_Delay(500);
		}
}
void DodoGo(uint8_t direct,int pwm_0,int pwm_1)
{
		if (direct == 0)
		{
			TIM1->CCR2 = pwm_0;
			TIM1->CCR1 = 0;
			TIM1->CCR3 = 0;
			TIM1->CCR4 = pwm_1;
		}
		if (direct == 1)
		{
			TIM1->CCR1 = pwm_0;
			TIM1->CCR2 = 0;
			TIM1->CCR4 = 0;
			TIM1->CCR3 = pwm_1;
		}
		if (direct == 2)
		{
			TIM1->CCR2 = 0;
			TIM1->CCR1 = 0;
			TIM1->CCR3 = 0;
			TIM1->CCR4 = 0;
		}
		
}

1 REPLY 1
Dvorak.Peter
Senior II

Switch bounce or noise ?