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

相关推荐
电气_空空几秒前
基于 LabVIEW 的单片机串口通信设计
单片机·嵌入式硬件·毕业设计·labview
逻极30 分钟前
Windows 平台 Ollama AMD GPU 一键编译指南:基于 ROCm 7.1 的自动化实战
人工智能·windows·stm32·自动化·gpu·amd·ollama
caimouse2 小时前
Reactos 第 9 章 设备驱动 — 9.10 磁盘的Miniport驱动模块
windows·嵌入式硬件
xiangw@GZ2 小时前
WiFi系统BCC与LDPC纠错编码技术性能对比
单片机·嵌入式硬件
AoDeLuo2 小时前
EthercCAT软件主站方案对比
stm32·单片机·嵌入式硬件
平凡灵感码头2 小时前
半导体三大主流制程详解:Bipolar、CMOS 与 BCD
单片机·嵌入式硬件
国科安芯4 小时前
基于AS32S601ZIT2型抗辐照MCU的商业航天卫星姿态确定与控制系统研究
单片机·嵌入式硬件·安全·fpga开发·架构·risc-v
三品吉他手会点灯4 小时前
STM32F103 学习笔记-24-I2C-读写EEPROM(第2节)-I2C协议层介绍
笔记·stm32·学习
项目題供诗4 小时前
STM32-DMA数据转运+AD多通道(二十一)
stm32·单片机·嵌入式硬件
FPC_小西4 小时前
LDO 低压差线性稳压器 拆解电源稳压核心原理
人工智能·单片机·嵌入式硬件·集成学习·pcb工艺·hdi高密度互联