STM32 LL库踩坑系列二(TIM编码器)

STM32 LL库踩坑系列二(TIM编码器)

这个作者很懒,想到什么就写什么!

写在前面:这个是记录学习LL库的踩坑系列,这个资料很少,想要找例程可以在电脑用户文件下的STM32Cube\Repository里,前提是下载好那个单片机的pack,部分没有例程的可以借鉴同系列的文件。

打开cubemx,配置TIM为编码器模式。

STM32 LL库踩坑系列二(TIM编码器)_第1张图片
注意编码器模式需要对应好硬件,这算上一个小坑。

/* TIM4 init function */
void MX_TIM4_Init(void)
{
  LL_TIM_InitTypeDef TIM_InitStruct = {0};

  LL_GPIO_InitTypeDef GPIO_InitStruct = {0};

  /* Peripheral clock enable */
  LL_APB1_GRP1_EnableClock(LL_APB1_GRP1_PERIPH_TIM4);
  
  LL_AHB1_GRP1_EnableClock(LL_AHB1_GRP1_PERIPH_GPIOD);
  /**TIM4 GPIO Configuration  
  PD12   ------> TIM4_CH1
  PD13   ------> TIM4_CH2 
  */
  GPIO_InitStruct.Pin = ENC_A_Pin;
  GPIO_InitStruct.Mode = LL_GPIO_MODE_ALTERNATE;
  GPIO_InitStruct.Speed = LL_GPIO_SPEED_FREQ_VERY_HIGH;
  GPIO_InitStruct.OutputType = LL_GPIO_OUTPUT_PUSHPULL;
  GPIO_InitStruct.Pull = LL_GPIO_PULL_NO;
  GPIO_InitStruct.Alternate = LL_GPIO_AF_2;
  LL_GPIO_Init(ENC_A_GPIO_Port, &GPIO_InitStruct);

  GPIO_InitStruct.Pin = ENC_B_Pin;
  GPIO_InitStruct.Mode = LL_GPIO_MODE_ALTERNATE;
  GPIO_InitStruct.Speed = LL_GPIO_SPEED_FREQ_VERY_HIGH;
  GPIO_InitStruct.OutputType = LL_GPIO_OUTPUT_PUSHPULL;
  GPIO_InitStruct.Pull = LL_GPIO_PULL_NO;
  GPIO_InitStruct.Alternate = LL_GPIO_AF_2;
  LL_GPIO_Init(ENC_B_GPIO_Port, &GPIO_InitStruct);

  LL_TIM_SetEncoderMode(TIM4, LL_TIM_ENCODERMODE_X2_TI2);
  LL_TIM_IC_SetActiveInput(TIM4, LL_TIM_CHANNEL_CH1, LL_TIM_ACTIVEINPUT_DIRECTTI);
  LL_TIM_IC_SetPrescaler(TIM4, LL_TIM_CHANNEL_CH1, LL_TIM_ICPSC_DIV1);
  LL_TIM_IC_SetFilter(TIM4, LL_TIM_CHANNEL_CH1, LL_TIM_IC_FILTER_FDIV2_N6);
  LL_TIM_IC_SetPolarity(TIM4, LL_TIM_CHANNEL_CH1, LL_TIM_IC_POLARITY_RISING);
  LL_TIM_IC_SetActiveInput(TIM4, LL_TIM_CHANNEL_CH2, LL_TIM_ACTIVEINPUT_DIRECTTI);
  LL_TIM_IC_SetPrescaler(TIM4, LL_TIM_CHANNEL_CH2, LL_TIM_ICPSC_DIV1);
  LL_TIM_IC_SetFilter(TIM4, LL_TIM_CHANNEL_CH2, LL_TIM_IC_FILTER_FDIV2_N6);
  LL_TIM_IC_SetPolarity(TIM4, LL_TIM_CHANNEL_CH2, LL_TIM_IC_POLARITY_RISING);
  TIM_InitStruct.Prescaler = 0;
  TIM_InitStruct.CounterMode = LL_TIM_COUNTERMODE_UP;
  TIM_InitStruct.Autoreload = 2000;
  TIM_InitStruct.ClockDivision = LL_TIM_CLOCKDIVISION_DIV1;
  LL_TIM_Init(TIM4, &TIM_InitStruct);
  LL_TIM_DisableARRPreload(TIM4);
  LL_TIM_SetTriggerOutput(TIM4, LL_TIM_TRGO_RESET);
  LL_TIM_DisableMasterSlaveMode(TIM4);

}

另外还需要在添加TIM启动

  /* Enable counter */
  LL_TIM_CC_EnableChannel(TIM4,LL_TIM_CHANNEL_CH1);
  LL_TIM_CC_EnableChannel(TIM4,LL_TIM_CHANNEL_CH2);
  LL_TIM_EnableCounter(TIM4);

测试使用LL_TIM_GetCounter()读取对应的TIM即可。
关于编码器的可以参考下面代码,其中Tim_ms是使用另一个TIM毫秒计数的,来判断旋钮旋转的速度。MSG_ENC_RIGHT和MSG_ENC_LEFT为自己设置的标志值。

/************************************************************
*
*			encoder key detect is norm or fast
*
*************************************************************/
#define MSG_ENC_RIGHT    Set_Bit0
#define MSG_ENC_LEFT     Set_Bit1

#define CMP_VALUE    (500)
#define FAST_RANK_2  (200)
#define FAST_RANK_1  (30)

unsigned char enc_speed_grade;

unsigned char Enc_GetSpeed(void)
{
	return enc_speed_grade;
}

unsigned short int Encoder_Process(void)
{
	static unsigned char flag = 0; 
	unsigned short int enc_value;		
	
	if(LL_TIM_GetCounter(TIM4) == CMP_VALUE) //获取定时器的值
	{
		flag = 0;
		enc_speed_grade = ENC_SPEED_0;

		return 0;
	}

	if((LL_TIM_GetCounter(TIM4)) > CMP_VALUE)
	{
		enc_value = MSG_ENC_RIGHT;
		LL_TIM_SetCounter(TIM4, CMP_VALUE);

	}else if(LL_TIM_GetCounter(TIM4) < CMP_VALUE){

		enc_value = MSG_ENC_LEFT;
		LL_TIM_SetCounter(TIM4, CMP_VALUE);		
	}

	if(!flag)
	{
		flag = 1;
		Tim_ms = 0;

		return enc_value;
	}

	if(Tim_ms < FAST_RANK_2)
	{
		enc_speed_grade = ENC_SPEED_2;

		if(Tim_ms <= FAST_RANK_1)
		{
			enc_speed_grade = ENC_SPEED_1;
		}

		Tim_ms = 0;

	}else{
	
		enc_speed_grade = ENC_SPEED_0;		
	}

	return enc_value;
}

你可能感兴趣的:(STM32,LL库,单片机,CubeMX)