【STM32学习】(21)STM32实现步进电机

实物图:

【STM32学习】(21)STM32实现步进电机_第1张图片

 

实现示意图:其实看着示意图就可以实现步进电机转动了。

【STM32学习】(21)STM32实现步进电机_第2张图片

 

【STM32学习】(21)STM32实现步进电机_第3张图片

四相五线的步进电机。

4线为:A B C D

该步进电机分为4拍和8拍

4拍:分为单四拍和双四拍

单四拍:A-B-C-D-A-

双四拍:AB-BC-CD-DA-AB-

8拍:

A-AB-B-BC-C-CD-D-DA-A-

想让步进电机反转,只需要将顺序反转就行。

主要代码为:

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();

  /* 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();
  /* USER CODE BEGIN 2 */

  /* USER CODE END 2 */

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

    /* USER CODE BEGIN 3 */
		/* 正转  8拍 */
//	  _74HC595_Send595OneByte(0X8F);
//	  _74HC595_OUT();
//	  HAL_Delay(1);
//	  _74HC595_Send595OneByte(0XCF);
//	  _74HC595_OUT();
//	  HAL_Delay(1);
//	  _74HC595_Send595OneByte(0X4F);
//	  _74HC595_OUT();
//	  HAL_Delay(1);
//	  _74HC595_Send595OneByte(0X6F);
//	  _74HC595_OUT();
//	  HAL_Delay(1);
//	  _74HC595_Send595OneByte(0X2F);
//	  _74HC595_OUT();
//	  HAL_Delay(1);
//	  _74HC595_Send595OneByte(0X3F);
//	  _74HC595_OUT();
//	  HAL_Delay(1);
//	  _74HC595_Send595OneByte(0X1F);
//	  _74HC595_OUT();
//	  HAL_Delay(1);
//	  _74HC595_Send595OneByte(0X9F);
//	  _74HC595_OUT();
//	  HAL_Delay(1);

		/* 反转 8拍 */
	  _74HC595_Send595OneByte(0X1F);
	  _74HC595_OUT();
	  HAL_Delay(1);
	  _74HC595_Send595OneByte(0X3F);
	  _74HC595_OUT();
	  HAL_Delay(1);
	  _74HC595_Send595OneByte(0X2F);
	  _74HC595_OUT();
	  HAL_Delay(1);
	  _74HC595_Send595OneByte(0X6F);
	  _74HC595_OUT();
	  HAL_Delay(1);
	  _74HC595_Send595OneByte(0X4F);
	  _74HC595_OUT();
	  HAL_Delay(1);
	  _74HC595_Send595OneByte(0XCF);
	  _74HC595_OUT();
	  HAL_Delay(1);
	  _74HC595_Send595OneByte(0X8F);
	  _74HC595_OUT();
	  HAL_Delay(1);
	  _74HC595_Send595OneByte(0X9F);
	  _74HC595_OUT();
	  HAL_Delay(1);


		/* 正转 4拍 单四拍 */
//	  _74HC595_Send595OneByte(0X8F);
//	  _74HC595_OUT();
//	  HAL_Delay(1);
//	  _74HC595_Send595OneByte(0X4F);
//	  _74HC595_OUT();
//	  HAL_Delay(1);
//	  _74HC595_Send595OneByte(0X2F);
//	  _74HC595_OUT();
//	  HAL_Delay(1);
//	  _74HC595_Send595OneByte(0X1F);
//	  _74HC595_OUT();
//	  HAL_Delay(1);
  
		/* 正转 4拍 双四拍 */
//	  _74HC595_Send595OneByte(0XCF);
//	  _74HC595_OUT();
//	  HAL_Delay(1);
//	  _74HC595_Send595OneByte(0X6F);
//	  _74HC595_OUT();
//	  HAL_Delay(1);
//	  _74HC595_Send595OneByte(0X3F);
//	  _74HC595_OUT();
//	  HAL_Delay(1);
//	  _74HC595_Send595OneByte(0X9F);
//	  _74HC595_OUT();
//	  HAL_Delay(1);
	  
	  /* 反转 4拍 单四拍 */
//	  _74HC595_Send595OneByte(0X8F);
//	  _74HC595_OUT();
//	  HAL_Delay(1);
//	  _74HC595_Send595OneByte(0X4F);
//	  _74HC595_OUT();
//	  HAL_Delay(1);
//	  _74HC595_Send595OneByte(0X2F);
//	  _74HC595_OUT();
//	  HAL_Delay(1);
//	  _74HC595_Send595OneByte(0X1F);
//	  _74HC595_OUT();
//	  HAL_Delay(1);

	  
	  
	  
  }
  /* USER CODE END 3 */
}

效果如下:

代码链接为:

 


 

你可能感兴趣的:(单片机c语言,STM32,步进电机)