cancel
Showing results for 
Search instead for 
Did you mean: 

Problem Encoder on STM32F071

matteo239955_st
Associate II
Posted on January 09, 2017 at 08:37

Hi,

 I'm using XCube for init a SMT32F071CBT6 with Keil V5, I need to use the encoder periphery. (ALL to the last version)

In attachment three file. The project and the 2 file modify after generation.

I've use 4 alternative pins, because the other are use by an other periphery.

When start the encoder the 2 pins become output and go Low. Why?

After this command:

  HAL_TIM_Encoder_Start(&htim3, TIM_CHANNEL_ALL);

I' ve try with tim1 with exactly the same code with no problem.

Please help me.

#encoder-stm32f0
5 REPLIES 5
Imen.D
ST Employee
Posted on January 09, 2017 at 17:55

Dear

Bandini.Matteo

‌,

I've use 4 alternative pins ..

Which alternative pins are you using ?

Could you please provide more details about your

G

PIO configuration ?

Best Regards

Imen

When your question is answered, please close this topic by clicking "Accept as Solution".
Thanks
Imen
matteo239955_st
Associate II
Posted on January 10, 2017 at 08:56

/* TIM2 init function */

void MX_TIM2_Init(void)

{

TIM_Encoder_InitTypeDef sConfig;

TIM_MasterConfigTypeDef sMasterConfig;

htim2.Instance = TIM2;

htim2.Init.Prescaler = 0;

htim2.Init.CounterMode = TIM_COUNTERMODE_UP;

htim2.Init.Period = 65535;

htim2.Init.ClockDivision = TIM_CLOCKDIVISION_DIV1;

htim2.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(&htim2, &sConfig) != HAL_OK)

{

Error_Handler();

}

sMasterConfig.MasterOutputTrigger = TIM_TRGO_RESET;

sMasterConfig.MasterSlaveMode = TIM_MASTERSLAVEMODE_DISABLE;

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

{

Error_Handler();

}

}

/* TIM3 init function */

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 = 65535;

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();

}

}

Posted on January 10, 2017 at 08:29

/* USER CODE END TIM2_MspInit 0 */

/* Peripheral clock enable */

__HAL_RCC_TIM2_CLK_ENABLE();

/**TIM2 GPIO Configuration

PA15 ------> TIM2_CH1

PB3 ------> TIM2_CH2

*/

GPIO_InitStruct.Pin = GPIO_PIN_15;

GPIO_InitStruct.Mode = GPIO_MODE_AF_PP;

GPIO_InitStruct.Pull = GPIO_PULLUP;

GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_LOW;

GPIO_InitStruct.Alternate = GPIO_AF2_TIM2;

HAL_GPIO_Init(GPIOA, &GPIO_InitStruct);

GPIO_InitStruct.Pin = GPIO_PIN_3;

GPIO_InitStruct.Mode = GPIO_MODE_AF_PP;

GPIO_InitStruct.Pull = GPIO_PULLUP;

GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_LOW;

GPIO_InitStruct.Alternate = GPIO_AF2_TIM2;

HAL_GPIO_Init(GPIOB, &GPIO_InitStruct);

/* USER CODE BEGIN TIM2_MspInit 1 */

HAL_TIM_Encoder_Start(&htim2, TIM_CHANNEL_ALL);

/* USER CODE END TIM2_MspInit 1 */

/* USER CODE BEGIN TIM3_MspInit 0 */

/* USER CODE END TIM3_MspInit 0 */

/* Peripheral clock enable */

__HAL_RCC_TIM3_CLK_ENABLE();

/**TIM3 GPIO Configuration

PB4 ------> TIM3_CH1

PB5 ------> TIM3_CH2

*/

GPIO_InitStruct.Pin = GPIO_PIN_4|GPIO_PIN_5;

GPIO_InitStruct.Mode = GPIO_MODE_AF_PP;

GPIO_InitStruct.Pull = GPIO_PULLUP;

GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_LOW;

GPIO_InitStruct.Alternate = GPIO_AF1_TIM3;

HAL_GPIO_Init(GPIOB, &GPIO_InitStruct);

/* USER CODE BEGIN TIM3_MspInit 1 */

HAL_TIM_Encoder_Start(&htim3, TIM_CHANNEL_ALL);

/* USER CODE END TIM3_MspInit 1 */
Posted on January 10, 2017 at 08:50

And TIM2/TIM3 configuration?

Rather than code, please provide the read-back content of the TIM2/TIM3 registers at the point when the problem occurs.

Mark Shoe
Senior
Posted on May 03, 2017 at 17:09

The structure TIM_Base_InitTypeDef does not have a member AutoReloadPreload. However CubeMX does generate this code. The old version of CubeMX doen not generate the autoreload line in tim.c So just comment it out every time you generate new code by CubeMX.