CubeMX - STM32 encoder interface

Couple a days ago, I've got struck with connection of encoder to my stm32 board. But,    the answer to the puzzle was quite simple:) So, just watch on screenshots:
Pin configuration:





Timer configuration:



Wiring is quite simple - encoder has 3 outputs. Central connect to GND, others - connect to timer's inputs.

Code is simple too:)
 int main(void)  
 {  
  /* USER CODE BEGIN 1 */  
  /* USER CODE END 1 */  
  /* MCU Configuration----------------------------------------------------------*/  
  /* Reset of all peripherals, Initializes the Flash interface and the Systick. */  
  HAL_Init();  
  /* Configure the system clock */  
  SystemClock_Config();  
  /* Initialize all configured peripherals */  
  MX_GPIO_Init();  
  MX_TIM1_Init();  
  /* USER CODE BEGIN 2 */  
  HAL_TIM_Encoder_Start(&htim1,TIM_CHANNEL_ALL);  
  /* USER CODE END 2 */  
  /* Infinite loop */  
  /* USER CODE BEGIN WHILE */  
  int i = 0;  
  while (1)  
  {  
  /* USER CODE END WHILE */  
  /* USER CODE BEGIN 3 */  
   i = TIM1->CNT;  
  }  
  /* USER CODE END 3 */  
 }  

So, you just need to enable encoder through function HAL_TIM_Encoder_Start.

Comments

  1. Very nice explanation, thanks for the contribution.

    ReplyDelete
  2. thank you very much, just wondering this for a long time

    ReplyDelete
  3. Can thuis be used to count motor quadrature pulses?

    ReplyDelete
    Replies
    1. This approach can be used for any incremental encoder interface.

      Delete
  4. thanks for your fine explanation, I was reading HAL manual, but have not figured how to start the timer with it

    ReplyDelete
  5. Thank you for saving me a bunch of time !

    ReplyDelete
  6. but you arent using i in the code?

    ReplyDelete

Post a Comment

Popular posts from this blog

u8g2 library usage with STM32 MCU

Use PCM5102 with STM32

RFFT in CMSIS DSP. Part 1.