cancel
Showing results for 
Search instead for 
Did you mean: 

STM32F042 Encoder Problem

eugene239955
Associate II
Posted on April 28, 2017 at 20:24

I'm using the stm32f042 nucleo board, Atollic truestudio and CubeMX launched from the Atollic GUI (eclipse) to develop code. I am trying to get the encoder interface on timer 3 to function as a step/dir counter. I have used CubeMX to select the encoder function on TIM3 which produces this initialization code - 

static void MX_TIM3_Init(void)

{

TIM_Encoder_InitTypeDef sConfig;

TIM_MasterConfigTypeDef sMasterConfig;

htim3.Instance = TIM3;

htim3.Init.Prescaler = 0;

htim3.Init.CounterMode = TIM_COUNTERMODE_UP;

htim3.Init.Period = 1800;

htim3.Init.ClockDivision = TIM_CLOCKDIVISION_DIV1;

htim3.Init.AutoReloadPreload = TIM_AUTORELOAD_PRELOAD_DISABLE;

sConfig.EncoderMode = TIM_ENCODERMODE_TI1;

sConfig.IC1Polarity = TIM_ICPOLARITY_RISING;

sConfig.IC1Selection = TIM_ICSELECTION_DIRECTTI;

sConfig.IC1Prescaler = TIM_ICPSC_DIV1;

sConfig.IC1Filter = 0;

sConfig.IC2Polarity = TIM_ICPOLARITY_RISING;

sConfig.IC2Selection = TIM_ICSELECTION_DIRECTTI;

sConfig.IC2Prescaler = TIM_ICPSC_DIV1;

sConfig.IC2Filter = 0;

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

{

Error_Handler();

}

sMasterConfig.MasterOutputTrigger = TIM_TRGO_RESET;

sMasterConfig.MasterSlaveMode = TIM_MASTERSLAVEMODE_DISABLE;

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

{

Error_Handler();

}

}

I have used the following start before the while loop in main - 

TIM3->CNT = 900;

HAL_TIM_Encoder_Start(&htim3,TIM_CHANNEL_ALL);

I've also tried TIM_CHANNEL_1 and 2

I set up a global variable 'counter' to monitor the value of the TIM3 cnt register.

 counter = TIM3->CNT; 

I cannot get the counter variable to move. It returns 900 no matter 

how I treat the PA6 and 7 lines. This problem is identical to the question 

https://community.st.com/0D50X00009XkexPSAR

 with the exception that this is the F042. There are no responses to the referenced question.

Any help here is greatly appreciated. 

null
2 REPLIES 2
eugene239955
Associate II
Posted on April 28, 2017 at 22:23

Pilot error here. Apparently the encoder counter cannot be used as a 'step and direction' counter where the direction is held constant and the step is toggled. Even though the timer is only counting on one channel, it increments on the rising edge and decrements on the falling edge for a net result of zero if the alternate dir line is held constant. It need the quadrature signal to function. Back to the drawing board for this project.

Posted on April 28, 2017 at 22:36

I'm not providing support for HAL based code, perhaps why it was ignored in 2015.

I do seem to recall that there are a number of encoder modes, double check the Reference Manual.

There is only a single counting element in each timer, but the behaviour/gating of the two associated channels is configurable.

Tips, buy me a coffee, or three.. PayPal Venmo Up vote any posts that you find helpful, it shows what's working..