一、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