当前位置:网站首页>[STM32 learning] (22) STM32 realizes 360 degree rotary encoder

[STM32 learning] (22) STM32 realizes 360 degree rotary encoder

2022-07-24 09:50:00 Use small materials

360 degree The object of rotary encoder is as follows :

KY-040 Rotary encoder module

  Working voltage :5V

Number of pulses per cycle :20

The rotary encoder can count the number of output pulses in the process of positive and negative rotation by rotation , Rotation counting is not like potentiometer , This rotation count is unlimited . Cooperate with the keys on the rotary encoder , It can be reset to the initial state , From 0 Start counting .

  working principle : Incremental encoder is a kind of rotary sensor that converts rotary displacement into a series of digital pulse signals . These pulses are used to control the angular displacement . stay Eltra The conversion of angular displacement in the encoder adopts the photoelectric scanning principle . The reading system consists of a radial dividing plate composed of alternating light transmission windows and light tight windows ( Code disk ) Based on the rotation of , At the same time, it is vertically illuminated by an infrared light source , Light projects the image of the code disk onto the surface of the receiver . The receiver is covered with a diffraction grating , It has the same window width as the code disk . The work of the receiver is to feel the changes caused by the rotation of the disc , Then convert the light change into the corresponding electrical change . Then raise the low-level signal to a higher level , There is no square pulse without any interference , This must be handled by electronic circuits . The reading system usually adopts differential mode , That is, the two waveforms are the same, but the phase difference is 180° Compare different signals , In order to improve the quality and stability of the output signal . The reading is based on the difference between the two signals , Thus, the interference is eliminated .

The incremental encoder gives two-phase square wave , Their phase difference 90°, Often referred to as A Channels and B passageway . One of the channels gives information related to speed , meanwhile , Compare the signals of two channels in sequence , Get information about the direction of rotation . There is also a special signal called Z Or zero channel , This channel gives the absolute zero position of the encoder , This signal is a square wave and A The center lines of the channel square waves coincide .

 

Now realize the detection of this incremental encoder Positive rotation still reverse

Here we use read IO To read data , To determine whether it is a forward turn or a reverse turn .

Single chip model :STM32L052K8*

connection :CLK  Pick up   PA0

            DT    Pick up   PA1

            SW  Pick up   PA2

int main(void)
{
  /* USER CODE BEGIN 1 */
	unsigned char dt;
	unsigned char clk,key;
  /* USER CODE END 1 */

  /* MCU Configuration--------------------------------------------------------*/

  /* Reset of all peripherals, Initializes the Flash interface and the Systick. */
  HAL_Init();

  /* USER CODE BEGIN Init */

  /* USER CODE END Init */

  /* Configure the system clock */
  SystemClock_Config();

  /* USER CODE BEGIN SysInit */

  /* USER CODE END SysInit */

  /* Initialize all configured peripherals */
  MX_GPIO_Init();
  MX_USART1_UART_Init();
  /* USER CODE BEGIN 2 */
	printf("\n\r****wantin****\n\r");
  /* USER CODE END 2 */

  /* Infinite loop */
  /* USER CODE BEGIN WHILE */
  while (1)
  {
    /* USER CODE END WHILE */

    /* USER CODE BEGIN 3 */
	  printf("\n\**************\n\r");
	  clk = HAL_GPIO_ReadPin(DT_GPIO_Port,DT_Pin);
	  dt = HAL_GPIO_ReadPin(DT_GPIO_Port,DT_Pin);
	  key = HAL_GPIO_ReadPin(SW_GPIO_Port,SW_Pin);
	  if(1 == clk)
	  {
		  printf("\n\clk == 1\n\r");
	  }
	  else
	  {
		  printf("\n\clk == 0\n\r");
	  }
	  if(1 == dt)
	  {
		  printf("\n\dt == 1\n\r");
	  }
	  else
	  {
		  printf("\n\dt == 0\n\r");
	  }
	  if(1 == key)
	  {
		  printf("\n\key == 1\n\r");
	  }
	  else
	  {
		  printf("\n\key == 0\n\r");
	  }
	  HAL_Delay(1000);
  }
  /* USER CODE END 3 */
}

 

In terms of implementation effect , Not ideal :

Whether you turn forward or backward ,CLK and DT The level of these two pins , All are successful 0、1 alternate , And the two levels are the same , It is impossible to distinguish between forward rotation and reverse rotation .

And then , Plug it in , Use an oscilloscope to test the waveform generated by these two pins . The effect is as follows :

Acquisition interrupted 0 mouth , And then clk The signal

Waveforms of forward rotation : The yellow line is clk, The blue line is dt

Falling edge trigger , Collected level   All for Low level   clk = 0   dt = 0;

Inverted waveform : The yellow line is clk, The blue line is dt

Falling edge trigger , Collected level   clk = 0   dt = 1;

It can be seen from the waveform that , There is waveform difference between positive and negative , How to realize forward and reverse detection , Interrupt can be used to realize .

Adopt the falling edge trigger method to collect the changes of waveform .

int main(void)
{
  /* USER CODE BEGIN 1 */
	unsigned char dt;
	unsigned char clk,key;
  /* USER CODE END 1 */

  /* MCU Configuration--------------------------------------------------------*/

  /* Reset of all peripherals, Initializes the Flash interface and the Systick. */
  HAL_Init();

  /* USER CODE BEGIN Init */

  /* USER CODE END Init */

  /* Configure the system clock */
  SystemClock_Config();

  /* USER CODE BEGIN SysInit */

  /* USER CODE END SysInit */

  /* Initialize all configured peripherals */
  MX_GPIO_Init();
  MX_USART1_UART_Init();
  /* USER CODE BEGIN 2 */
	printf("\n\r****wantin****\n\r");
  /* USER CODE END 2 */

  /* Infinite loop */
  /* USER CODE BEGIN WHILE */
  while (1)
  {
    /* USER CODE END WHILE */

    /* USER CODE BEGIN 3 */
	  printf("\n\**************\n\r");
	  if(10 == spped_counter)
	  {
		  printf("\n\*** Positive rotation ***\n\r");
	  }
	  else
	  {
		  printf("\n\*** reverse ***\n\r");
	  }
//	  clk = HAL_GPIO_ReadPin(DT_GPIO_Port,DT_Pin);
//	  dt = HAL_GPIO_ReadPin(DT_GPIO_Port,DT_Pin);
//	  key = HAL_GPIO_ReadPin(SW_GPIO_Port,SW_Pin);
//	  if(1 == clk)
//	  {
//		  printf("\n\clk == 1\n\r");
//	  }
//	  else
//	  {
//		  printf("\n\clk == 0\n\r");
//	  }
//	  if(1 == dt)
//	  {
//		  printf("\n\dt == 1\n\r");
//	  }
//	  else
//	  {
//		  printf("\n\dt == 0\n\r");
//	  }
//	  if(1 == key)
//	  {
//		  printf("\n\key == 1\n\r");
//	  }
//	  else
//	  {
//		  printf("\n\key == 0\n\r");
//	  }
	  HAL_Delay(1000);
  }
  /* USER CODE END 3 */
}
void HAL_GPIO_EXTI_Callback(uint16_t GPIO_Pin)  // External interrupt   Callback function 
{
	if(GPIO_Pin == CLK_Pin) // If any change is detected, come in and deal with it 
	{
		if(HAL_GPIO_ReadPin(CLK_GPIO_Port,CLK_Pin) == HAL_GPIO_ReadPin(DT_GPIO_Port,DT_Pin))  //clk pb0 == dt pb1
		spped_counter=10;		//	 Express   Positive rotation 
		else
		spped_counter=100;		//	 Express   reverse 
		
	}
}

 

 

The effect is as follows :

 

 

 

原网站

版权声明
本文为[Use small materials]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/204/202207221455246226.html