cancel
Showing results for 
Search instead for 
Did you mean: 

stm32f411 tim5 and tim9 can not use to be TIM_Encoder mode

yuming
Associate II

int main(void)

{

 HAL_Init();

 BSP_LED_Init(LED3);

 BSP_LED_Init(LED4);

BSP_LED_Init(LED5);

BSP_LED_Init(LED6);

 /* Initialize TIM3 to emulate a quadrature encoder outputs */

 Init_TIM_Emulator(&EmulatorHandle);  

 UartHandle.Instance     = USARTx;

  

 UartHandle.Init.BaudRate   = 9600;

 UartHandle.Init.WordLength  = UART_WORDLENGTH_8B;

 UartHandle.Init.StopBits   = UART_STOPBITS_1;

 UartHandle.Init.Parity    = UART_PARITY_NONE;

 UartHandle.Init.HwFlowCtl  = UART_HWCONTROL_NONE;

 UartHandle.Init.Mode     = UART_MODE_TX_RX;

 UartHandle.Init.OverSampling = UART_OVERSAMPLING_16;

   

 if(HAL_UART_Init(&UartHandle) != HAL_OK)

 {

  Error_Handler();

 }

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

 }

  

BSP_PB_Init(BUTTON_KEY, BUTTON_MODE_GPIO);

 /* Wait for USER Button press before starting the Communication */

 while (BSP_PB_GetState(BUTTON_KEY) == RESET)

 {

  /* Toggle LED3 waiting for user to press button */

  BSP_LED_Toggle(LED3);

  HAL_Delay(40);

 }

 /* Wait for USER Button to be release before starting the Communication */

 while (BSP_PB_GetState(BUTTON_KEY) == SET)

 {

 }

  

 /* Turn LED3 off */

 BSP_LED_Off(LED3);

 /* Start the encoder interface */

 HAL_TIM_Encoder_Start(&Encoder_Handle, TIM_CHANNEL_ALL);

 /* Infinite loop */

 while (1)

 {

  /* Step 1: */

  Emulate_Forward_Direction(&EmulatorHandle);

  HAL_Delay(10);

  /* Get the current direction */

  uwDirection = __HAL_TIM_IS_TIM_COUNTING_DOWN(&Encoder_Handle);

 uwcount = __HAL_TIM_GET_COUNTER(&Encoder_Handle);

TxBuffer[0] =(unsigned int)uwcount>>8 & 0xff;

 TxBuffer[1] =(unsigned int)uwcount & 0xff;

TxBuffer[2]=uwDirection;

TxBuffer[3] =3;

 TxBuffer[4] =4;

  if(HAL_UART_Transmit(&UartHandle, (uint8_t*)TxBuffer, 5, 50)!= HAL_OK)

 {

  Error_Handler();  

 }

 }

}

static void Init_TIM_Emulator(TIM_HandleTypeDef* htim)

{

 /* Initialize TIM3 peripheral as follow:

    + Prescaler     = 0

    + Period      = 65535

    + ClockDivision   = 0

    + Counter direction = Up

 */

 htim->Instance = TIM3;

  

 htim->Init.Period    = EMU_PERIOD;

 htim->Init.Prescaler   = 0;

 htim->Init.ClockDivision = 0;

 htim->Init.CounterMode  = TIM_COUNTERMODE_UP;

 if(HAL_TIM_OC_Init(htim) != HAL_OK)

 {

  /* Initialization Error */

  Error_Handler();

 }

  

 /*## Configure the Output Compare channels #########################################*/ 

 /* Output Compare Toggle Mode configuration: Channel1 */

 sConfig.OCMode = TIM_OCMODE_TOGGLE;

 sConfig.Pulse = (EMU_PERIOD * 1 )/4;

 sConfig.OCPolarity = TIM_OCPOLARITY_LOW;

 if(HAL_TIM_OC_ConfigChannel(htim, &sConfig, TIM_CHANNEL_1) != HAL_OK)

 {

  /* Configuration Error */

  Error_Handler();

 }

  

 /* Output Compare Toggle Mode configuration: Channel2 */

 sConfig.Pulse = (EMU_PERIOD * 3 )/4;

 if(HAL_TIM_OC_ConfigChannel(htim, &sConfig, TIM_CHANNEL_2) != HAL_OK)

 {

  /* Configuration Error */

  Error_Handler();

 }

}

static void SystemClock_Config(void)

{

 RCC_ClkInitTypeDef RCC_ClkInitStruct;

 RCC_OscInitTypeDef RCC_OscInitStruct;

 /* Enable Power Control clock */

 __HAL_RCC_PWR_CLK_ENABLE();

  

 __HAL_PWR_VOLTAGESCALING_CONFIG(PWR_REGULATOR_VOLTAGE_SCALE2);

  

 /* Enable HSI Oscillator and activate PLL with HSI as source */

 RCC_OscInitStruct.OscillatorType = RCC_OSCILLATORTYPE_HSI;

 RCC_OscInitStruct.HSIState = RCC_HSI_ON;

 RCC_OscInitStruct.HSICalibrationValue = 0x10;

 RCC_OscInitStruct.PLL.PLLState = RCC_PLL_ON;

 RCC_OscInitStruct.PLL.PLLSource = RCC_PLLSOURCE_HSI;

 RCC_OscInitStruct.PLL.PLLM = 16;

 RCC_OscInitStruct.PLL.PLLN = 400;

 RCC_OscInitStruct.PLL.PLLP = RCC_PLLP_DIV4;

 RCC_OscInitStruct.PLL.PLLQ = 7;

 if(HAL_RCC_OscConfig(&RCC_OscInitStruct) != HAL_OK)

 {

  Error_Handler();

 }

 RCC_ClkInitStruct.ClockType = (RCC_CLOCKTYPE_SYSCLK | RCC_CLOCKTYPE_HCLK | RCC_CLOCKTYPE_PCLK1 | RCC_CLOCKTYPE_PCLK2);

 RCC_ClkInitStruct.SYSCLKSource = RCC_SYSCLKSOURCE_PLLCLK;

 RCC_ClkInitStruct.AHBCLKDivider = RCC_SYSCLK_DIV1;

 RCC_ClkInitStruct.APB1CLKDivider = RCC_HCLK_DIV2;  

 RCC_ClkInitStruct.APB2CLKDivider = RCC_HCLK_DIV1;  

 if(HAL_RCC_ClockConfig(&RCC_ClkInitStruct, FLASH_LATENCY_3) != HAL_OK)

 {

  Error_Handler();

 }

}

this is msp.c

void HAL_TIM_Encoder_MspInit(TIM_HandleTypeDef *htim)

{

 GPIO_InitTypeDef  GPIO_InitStruct;

  

 /*##-1- Enable peripherals and GPIO Clocks #################################*/

 /* TIM1 Peripheral clock enable */

  __HAL_RCC_TIM1_CLK_ENABLE();

   

 /* Enable GPIO Channels Clock */

 __HAL_RCC_GPIOA_CLK_ENABLE();

 __HAL_RCC_GPIOE_CLK_ENABLE();

  

 /*##-2- Configure I/Os #####################################################*/  

 /* Common configuration for all channels */

 GPIO_InitStruct.Mode = GPIO_MODE_AF_PP;

 GPIO_InitStruct.Pull = GPIO_PULLUP;

 GPIO_InitStruct.Speed = GPIO_SPEED_HIGH;

 GPIO_InitStruct.Alternate = GPIO_AF1_TIM1;

  

 /* Channel 1 configuration */

 GPIO_InitStruct.Pin = GPIO_PIN_8;

 HAL_GPIO_Init(GPIOA, &GPIO_InitStruct);

  

 /* Channel 2 configuration */

 GPIO_InitStruct.Pin = GPIO_PIN_11;

 HAL_GPIO_Init(GPIOE, &GPIO_InitStruct);

}

when i set TIM_Encoder_MspInit, tim9(PE5 and PE6) to be encoder timer then i just read 0, and tim5 also, by the way now i use tim1(PA8 and PE11), and when change to (PA8 and PA9) then can not work, change to (PE11 and PE9) can also work, i dont know why.

3 REPLIES 3

Read out and check the content of relevant GPIO and TIM registers.

JW

PS. Change your username to a normal nick.

yuming
Associate II

hi, well~~ i am new to use stm32, which registers should i read out, what is that register call. i have only read Instance->CNT.

The easiest is to read out the registers in a debugger.

But first, read the GPIO and TIM chapters of the Reference Manual (RM) for your STM32.

Read out the GPIO registers, concentrate on GPIO_MODER - for given pins, their respective GPIO_MODER bits have to be set to 0b10 for AF (Alternative Function). Then check the setting of AFR bits for the given pins - check if they match the chosen TIM setting in the Alternative Functions table in the Datasheet.

Then read out the TIM registers. For encoder mode, concentrate on TIM_SMCR, and also check if the timer's counter is enabled by having set TIM_CR1.CEN.

JW