stm32 cubemx 计外部脉冲 + 编码器模式

计数器模式:

 

1)cubemx配外设

 

 2)编程序

  MX_TIM2_Init();// cubemx 自动添加   外设初始化

/* USER CODE BEGIN WHILE */

HAL_TIM_Base_Start(&htim2);//启动定时器2进行外部脉冲计数

__HAL_TIM_SET_COUNTER(&htim4,0); // 计数器清零

while (1)
{
/* USER CODE END WHILE */

 

HAL_Delay(50);

Count_now = __HAL_TIM_GET_COUNTER(&htim2);//读取计数脉冲

  printf(" the Count of the period is %d\r\n",Count_now);

编码器模式:

 

posted @ 2022-10-27 19:36  辛河  阅读(998)  评论(0)    收藏  举报