STM32(HAL)通过UN2003芯片驱动步进电机正反转

目录

1、简介

2、Cubemax配置

2.1 RCC配置

 2.2 SYS配置

 2.3 GPIO配置

 2.4 生成文件

 3、硬件连接

4、keil端代码整合

4.1 宏定义

4.2 添加fun.c func.h


1、简介

STM32F103C8T6单片机(HAL库方式)通过UN2003芯片对步进电机进行正反转控制。

2、Cubemax配置

2.1 RCC配置

STM32(HAL)通过UN2003芯片驱动步进电机正反转_第1张图片

 2.2 SYS配置

STM32(HAL)通过UN2003芯片驱动步进电机正反转_第2张图片

 2.3 GPIO配置

STM32(HAL)通过UN2003芯片驱动步进电机正反转_第3张图片

 2.4 生成文件

STM32(HAL)通过UN2003芯片驱动步进电机正反转_第4张图片

STM32(HAL)通过UN2003芯片驱动步进电机正反转_第5张图片

 3、硬件连接

IN1---PB15

IN2---PB14

IN3---PB13

IN4----PB12

+5V---5V

GND---GND

4、keil端代码整合

4.1 宏定义

STM32(HAL)通过UN2003芯片驱动步进电机正反转_第6张图片

#define LD_ON HAL_GPIO_WritePin(LD_GPIO_Port, LD_Pin, GPIO_PIN_SET)
#define LD_OFF HAL_GPIO_WritePin(LD_GPIO_Port, LD_Pin, GPIO_PIN_RESET)
#define LC_ON HAL_GPIO_WritePin(LC_GPIO_Port, LC_Pin, GPIO_PIN_SET)
#define LC_OFF HAL_GPIO_WritePin(LC_GPIO_Port, LC_Pin, GPIO_PIN_RESET)
#define LB_ON HAL_GPIO_WritePin(LB_GPIO_Port, LB_Pin, GPIO_PIN_SET)
#define LB_OFF HAL_GPIO_WritePin(LB_GPIO_Port, LB_Pin, GPIO_PIN_RESET)
#define LA_ON HAL_GPIO_WritePin(LA_GPIO_Port, LA_Pin, GPIO_PIN_SET)
#define LA_OFF HAL_GPIO_WritePin(LA_GPIO_Port, LA_Pin, GPIO_PIN_RESET)

4.2 添加fun.c func.h

func.h

#ifndef FUNC_FUNC_H_
#define FUNC_FUNC_H_

#include "main.h"

#define time 24000
void anticlockwise(void);
void clockwise(void);

#endif /* FUNC_FUNC_H_ */

func.c

#include "func.h"
uint32_t free_time_delay;
void anticlockwise()
{
	 //D4-LD
		//D5-LC
		//D6-LB
		//D7-LA
		
	LD_ON;
  //1
  free_time_delay = time; while(--free_time_delay>0);
  LC_ON;
  //2
  free_time_delay = time; while(--free_time_delay>0);
  LD_OFF;
  //3
  free_time_delay = time; while(--free_time_delay>0);
  LB_ON;
  //4
  free_time_delay = time; while(--free_time_delay>0);
  LC_OFF;
  //5
  free_time_delay = time; while(--free_time_delay>0);
  LA_ON;;
  //6
  free_time_delay = time; while(--free_time_delay>0);
  LB_OFF;
  //7
  free_time_delay = time; while(--free_time_delay>0);
  LD_ON;
  //8
  free_time_delay = time; while(--free_time_delay>0);
  LA_OFF;

}

void clockwise()
{
	 LA_ON;
  //1
  free_time_delay = time; while(--free_time_delay>0);
  LB_ON;
  //2
  free_time_delay = time; while(--free_time_delay>0);
  LA_OFF;
  //3
  free_time_delay = time; while(--free_time_delay>0);
  LC_ON;
  //4
  free_time_delay = time; while(--free_time_delay>0);
  LB_OFF;
  //5
  free_time_delay = time; while(--free_time_delay>0);
  LD_ON;;
  //6
  free_time_delay = time; while(--free_time_delay>0);
  LC_OFF;
  //7
  free_time_delay = time; while(--free_time_delay>0);
  LA_ON;
  //8
  free_time_delay = time; while(--free_time_delay>0);
  LD_OFF;
}

main.c

while (1)
  {
		clockwise();//正
		HAL_Delay(5000);
		
		anticlockwise();//反
		HAL_Delay(5000);
    /* USER CODE END WHILE */

    /* USER CODE BEGIN 3 */
  }
  /* USER CODE END 3 */

你可能感兴趣的:(stm32,单片机,嵌入式硬件)