Stm32cubemx Encoder (Using Timer)

Description: Stm32cubemx Encoder (Using Timer). This post show how to get data from rotary encoder by using timer and send data to PC by usb-serial converter . Nucleo 144 board used. You can do the same with other board .

This post show how to get data from rotary encoder by using timer and send data to PC by usb-serial converter . Nucleo 144 board used. You can do the same with other board .
Encoder -----------------------------  MCU
5V          ----------------------------- 5V
Gnd        ----------------------------- Gnd
A Phase  ----------------------------- PE_9
B Phase  ------------------------------PE_11
1. Set pin out
1.1RCC->High speed clock->Crystal Ceramic Resonator
Up system clock to 216 Mhz
1.2 USART1->Mode->Asynchronous
1.3 Timer1 Combined channel -> Encoder Mode


2. Timer configuration 
Counter period->65535
Encoder Mode ->Encoder Mode TI1 and TI2
3. USART1 configuration
Baudrate->115200
Word length-> 8bits
4. Source generation

int main(void)
{
  HAL_Init();
  SystemClock_Config();
  MX_GPIO_Init();
  MX_USART1_UART_Init();
  MX_TIM1_Init();
   char buf[15];
   HAL_TIM_Encoder_Start(&htim1,  
TIM_CHANNEL_1|TIM_CHANNEL_2);      
   uint16_t count=0;

  while (1)
  {
    memset(buf,'\0',sizeof(buf));   
    count=TIM1->CNT;
    sprintf(buf,"count:[%d]\n",count);
    HAL_UART_Transmit(&huart1,buf,sizeof(buf),0x1000);
    HAL_Delay(10); 

  }
}