STM32CubeMX ULN2003步进电机驱动

一、28BYJ-48 步进电机

28BYJ-48是一款5线单极步进电机,运行电压为5V。

根据数据表,当28BYJ-48电机在全步模式下运行时,每步对应于11.25°的旋转。这意味着每转有32步 (360°/11.25° = 32)。

如上图所示,步距角=5.625°/64

意思就是每64个脉冲步进电机就会转5.625度,因此我们很容易得出以下计算公式:

电机转一圈有360°,那么转一圈的脉冲数 = 360 / 5.625 * 64 = 4096 个脉冲。

进而很容易得到以下角度与脉冲的转换:

/*
	Rotation_Angle:旋转角度
	返回:Motor_Pulse 根据公式计算得出的脉冲个数
*/
int Motor_Angle_Cal(int Rotation_Angle)
{
    Motor_Pulse = (int)((double)(Rotation_Angle / 5.625) * 64) ;
    return Motor_Pulse ;
}

二、CubeMX配置

sys

rcc

时钟树

gpio

生成工程

三、代码

Motor.h

#ifndef __MOTOR_H
#define __MOTOR_H
#include "main.h"

//4相控制定义
#define MOTOR_A_ON 	HAL_GPIO_WritePin(GPIOB, GPIO_PIN_3, GPIO_PIN_SET);
#define MOTOR_A_OFF HAL_GPIO_WritePin(GPIOB, GPIO_PIN_3, GPIO_PIN_RESET);

#define MOTOR_B_ON 	HAL_GPIO_WritePin(GPIOB, GPIO_PIN_4, GPIO_PIN_SET);
#define MOTOR_B_OFF	HAL_GPIO_WritePin(GPIOB, GPIO_PIN_4, GPIO_PIN_RESET);

#define MOTOR_C_ON 	HAL_GPIO_WritePin(GPIOB, GPIO_PIN_5, GPIO_PIN_SET);
#define MOTOR_C_OFF	HAL_GPIO_WritePin(GPIOB, GPIO_PIN_5, GPIO_PIN_RESET);

#define MOTOR_D_ON 	HAL_GPIO_WritePin(GPIOB, GPIO_PIN_6, GPIO_PIN_SET);
#define MOTOR_D_OFF	HAL_GPIO_WritePin(GPIOB, GPIO_PIN_6, GPIO_PIN_RESET);


extern int direction  ;
extern uint16_t Motor_Pulse ;

//电机脉冲计算
int Motor_Angle_Cal(int Rotation_Angle);
//电机8节拍控制
void MOTOR_CONTROLD(uint8_t step, uint8_t direction);
//关闭电机
void CLOSE_MOTOR(void);

#endif //__MOTOR_H

Motor.c

#include "Motor.h"
//电机旋转的方向
int direction = 0 ;
//电机旋转的脉冲个数
uint16_t Motor_Pulse = 0 ;


//电机控制,采用8节拍来做
//A->AB->B->BC->C->CD->D->DA
void MOTOR_CONTROLD(uint8_t step, uint8_t direction)
{
    uint8_t __step = step ;

    //判断电机的旋转方向,如果为1,则逆向旋转
    if(1 == direction)
        __step = 8 - step ;

    switch(__step)
    {
        //A
        case 0:
            MOTOR_A_ON;
            MOTOR_B_OFF;
            MOTOR_C_OFF;
            MOTOR_D_OFF;
            break ;

        //AB
        case 1:
            MOTOR_A_ON;
            MOTOR_B_ON;
            MOTOR_C_OFF;
            MOTOR_D_OFF;
            break ;

        //B
        case 2:
            MOTOR_A_OFF;
            MOTOR_B_ON;
            MOTOR_C_OFF;
            MOTOR_D_OFF;
            break ;

        //BC
        case 3:
            MOTOR_A_OFF;
            MOTOR_B_ON;
            MOTOR_C_ON;
            MOTOR_D_OFF;
            break ;

        //C
        case 4:
            MOTOR_A_OFF;
            MOTOR_B_OFF;
            MOTOR_C_ON;
            MOTOR_D_OFF;
            break ;

        //CD
        case 5:
            MOTOR_A_OFF;
            MOTOR_B_OFF;
            MOTOR_C_ON;
            MOTOR_D_ON;
            break ;

        //D
        case 6:
            MOTOR_A_OFF;
            MOTOR_B_OFF;
            MOTOR_C_OFF;
            MOTOR_D_ON;

        //DA
        case 7:
            MOTOR_A_ON;
            MOTOR_B_OFF;
            MOTOR_C_OFF;
            MOTOR_D_ON;
            break ;
    }
}

//关闭电机
void CLOSE_MOTOR(void)
{
	HAL_GPIO_WritePin(GPIOB, GPIO_PIN_3 | GPIO_PIN_4 | GPIO_PIN_5 | GPIO_PIN_6, GPIO_PIN_RESET);
}

/*
	Rotation_Angle:旋转角度
	返回:Motor_Pulse 根据公式计算得出的脉冲个数
*/
int Motor_Angle_Cal(int Rotation_Angle)
{
    Motor_Pulse = (int)((double)(Rotation_Angle / 5.625) * 64) ;
    return Motor_Pulse ;
}

main.c

/* Private includes ----------------------------------------------------------*/
/* USER CODE BEGIN Includes */
#include "Motor.h"
/* USER CODE END Includes */

while

步进电机简单正反转

 /* USER CODE BEGIN WHILE */
  while (1)
  {
		//1反向 0正向
		direction = 1;	
	    for(uint8_t step=0;step<=8;step++){
	    //控制步进电机旋转
		MOTOR_CONTROLD(step, direction);
		HAL_Delay(1);
		}
    /* USER CODE END WHILE */

效果

step_motor

链接: https://pan.baidu.com/s/1iucYeZGygwHi3DYeds4gqA?pwd=qabt 提取码: qabt

相关推荐
苏慕TRYACE2 小时前
RT-Thread+STM32L475VET6——USB鼠标模拟
stm32·单片机·嵌入式硬件·计算机外设·rt_thread
楼台的春风9 小时前
【MCU驱动开发概述】
c语言·驱动开发·单片机·嵌入式硬件·mcu·自动驾驶·嵌入式
Moonnnn.9 小时前
51单片机学习——动态数码管显示
笔记·嵌入式硬件·学习·51单片机
LS_learner10 小时前
小智机器人CMakeLists编译文件解析
嵌入式硬件·机器人
小鸡岛保安11 小时前
学习笔记-8MQTT-韦东山
stm32·物联网
暗碳11 小时前
stm32 74hc238流水灯
stm32·单片机·嵌入式硬件
1101 110111 小时前
STM32-温湿度上传OneNET项目
stm32·单片机·嵌入式硬件
余衫马12 小时前
ESP32-S3 实战指南:BOOT-KEY 按键驱动开发全解析
驱动开发·单片机·嵌入式硬件
文军的烹饪实验室15 小时前
处理器架构、单片机、芯片、光刻机之间的关系
单片机·嵌入式硬件·架构
Leiditech__15 小时前
人工智能时代电子机器人静电问题及电路设计防范措施
人工智能·嵌入式硬件·机器人·硬件工程