cancel
Showing results for 
Search instead for 
Did you mean: 

quadrature encoder

waaizkhan
Associate II
Posted on August 10, 2017 at 19:48

hi everyone i have configured the timer for quadrature encoder which is working but as my quadrature encoder resolution is 1920 counts in 360 degree but in debugging mode it is give me 180 counts per 360 degree my code is given .in main function

e my code is given .in main function //for encoder initializing HAL_TIM_Encoder_Start(&htim5,TIM_CHANNEL_1|TIM_CHANNEL_2); 

while()

{

count=Encoder();

}

float Encoder(void)

{

Position=__HAL_TIM_GET_COUNTER(&htim5);

return(

Position

);

}

static void MX_TIM5_Init(void)

{

TIM_Encoder_InitTypeDef sConfig;

TIM_MasterConfigTypeDef sMasterConfig;

htim5.Instance = TIM5;

htim5.Init.Prescaler = 0;

htim5.Init.CounterMode = TIM_COUNTERMODE_UP;

htim5.Init.Period = 0xffff;

htim5.Init.ClockDivision = TIM_CLOCKDIVISION_DIV1;

sConfig.EncoderMode = TIM_ENCODERMODE_TI2;

sConfig.IC1Polarity = TIM_ICPOLARITY_RISING;

sConfig.IC1Selection = TIM_ICSELECTION_DIRECTTI;

sConfig.IC1Prescaler = TIM_ICPSC_DIV1;

sConfig.IC1Filter = 6;

sConfig.IC2Polarity = TIM_ICPOLARITY_RISING;

sConfig.IC2Selection = TIM_ICSELECTION_DIRECTTI;

sConfig.IC2Prescaler = TIM_ICPSC_DIV1;

sConfig.IC2Filter = 6;

if (HAL_TIM_Encoder_Init(&htim5, &sConfig) != HAL_OK)

{

_Error_Handler(__FILE__, __LINE__);

}

sMasterConfig.MasterOutputTrigger = TIM_TRGO_RESET;

sMasterConfig.MasterSlaveMode = TIM_MASTERSLAVEMODE_DISABLE;

if (HAL_TIMEx_MasterConfigSynchronization(&htim5, &sMasterConfig) != HAL_OK)

{

_Error_Handler(__FILE__, __LINE__);

}

}

3 REPLIES 3
Posted on August 10, 2017 at 20:48

I'd recommend some cleaner initialization.

TIM_Encoder_InitTypeDef sConfig = { 0 };

TIM_MasterConfigTypeDef sMasterConfig = { 0 };
Tips, Buy me a coffee, or three.. PayPal Venmo
Up vote any posts that you find helpful, it shows what's working..
waaizkhan
Associate II
Posted on August 11, 2017 at 18:39

thanks a lot clive i always found u helpfull.

Posted on August 11, 2017 at 19:51

STM32Cube_FW_F4_V1.16.0\Projects\STM32469I_EVAL\Examples\TIM\TIM_Encoder\Src\main.c

/* -1- Initialize TIM1 to handle the encoder sensor */

/* Initialize TIM1 peripheral as follows:

+ Period = 65535

+ Prescaler = 0

+ ClockDivision = 0

+ Counter direction = Up

*/

Encoder_Handle.Instance = TIM1;

Encoder_Handle.Init.Period = 65535;

Encoder_Handle.Init.Prescaler = 0;

Encoder_Handle.Init.ClockDivision = 0;

Encoder_Handle.Init.CounterMode = TIM_COUNTERMODE_UP;

Encoder_Handle.Init.RepetitionCounter = 0;

sEncoderConfig.EncoderMode = TIM_ENCODERMODE_TI12;

sEncoderConfig.IC1Polarity = TIM_ICPOLARITY_RISING;

sEncoderConfig.IC1Selection = TIM_ICSELECTION_DIRECTTI;

sEncoderConfig.IC1Prescaler = TIM_ICPSC_DIV1;

sEncoderConfig.IC1Filter = 0;

sEncoderConfig.IC2Polarity = TIM_ICPOLARITY_RISING;

sEncoderConfig.IC2Selection = TIM_ICSELECTION_DIRECTTI;

sEncoderConfig.IC2Prescaler = TIM_ICPSC_DIV1;

sEncoderConfig.IC2Filter = 0;

if(HAL_TIM_Encoder_Init(&Encoder_Handle, &sEncoderConfig) != HAL_OK)

{

/* Initialization Error */

Error_Handler();

}

/* Start the encoder interface */

HAL_TIM_Encoder_Start(&Encoder_Handle, TIM_CHANNEL_ALL);
Tips, Buy me a coffee, or three.. PayPal Venmo
Up vote any posts that you find helpful, it shows what's working..