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:)
So, you just need to enable encoder through function HAL_TIM_Encoder_Start.
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.
Very nice explanation, thanks for the contribution.
ReplyDeleteThanks man, saved me some time!
ReplyDeleteThanks..
ReplyDeletethank you very much, just wondering this for a long time
ReplyDeleteThank you.
ReplyDeleteCan thuis be used to count motor quadrature pulses?
ReplyDeleteThis approach can be used for any incremental encoder interface.
Deletethanks for your fine explanation, I was reading HAL manual, but have not figured how to start the timer with it
ReplyDeleteThank you for saving me a bunch of time !
ReplyDeletebut you arent using i in the code?
ReplyDelete