laitimes

51MCU Study Notes16 Small DC motor and five-wire four-phase motor control

author:Programming circles

First, the classification of motors

Motors can be classified according to different standards, including their working principle, structure, use, etc., such as DC motors and AC motors according to their working principles; According to the structure, it is divided into brushed motors, brushless motors, etc.

This article will introduce the control of ordinary small DC motors and five-wire four-phase stepper motors.

2. Small DC motor control

1. Introduction

The small DC motor used in this article is often used in toys, two wires, change the speed when changing the current intensity, and change the steering when changing the direction of the current.

2. 驱动芯片ULN2003D

Generally, the IO port of a single-chip microcomputer cannot be used to directly drive a DC motor, and a ULN2003D can be used as a driver for the DC motor on the development board. The basic specifications of the chip have been introduced above:

https://xund.blog.csdn.net/article/details/128106835

Pin Diagram:

51MCU Study Notes16 Small DC motor and five-wire four-phase motor control

The motor is connected to the VCC and OUT1 pins.

51MCU Study Notes16 Small DC motor and five-wire four-phase motor control

3. Code implementation

The following code realizes that the DC motor rotates for 5 seconds, stops for 5 seconds, and repeats the cycle.

dc_motor_utils.c

#include "dc_motor_utils.h"
#include <reg52.h>

// DC Motor 引脚
sbit DC_motor = P1^0;
/**
* @brief DC Motor 启动
*/
void dc_motor_start(void){
    DC_motor = 1;
}
/**
* @brief DC Motor 停止
*/
void dc_motor_stop(void){
    DC_motor = 0;
}
           

main.c

#include "led_utils.h"
#include "dc_motor_utils.h"
#include "common_utils.h"

/**
* @brief 主函数
*/
void main()
{
	// 关闭所有led
	led_all_off();

	while(1)
	{
		dc_motor_start();
		delay_ms(1000*5);
		dc_motor_stop();
		delay_ms(1000*5);
	}
}
           

Three, five-wire four-phase stepper motor control

1. Working principle of stepper motor

The stepper motor achieves a precise rotational motion by gradually activating the coils inside. The five-wire, four-phase stepper motor is controlled by four independent coils (phases). As an electric current passes through each coil, they create a magnetic field that interacts with the permanent magnets inside the motor to drive the motor to rotate.

2. Construction

A five-wire, four-phase stepper motor typically contains two magnetic poles with two coils on each pole, for a total of four coils. These four coils can be activated in different sequences to create a rotational motion. In addition, stepper motors typically contain a center pin (common positive or common cathodo) to provide power.

51MCU Study Notes16 Small DC motor and five-wire four-phase motor control

3. Polarity Differentiation

51MCU Study Notes16 Small DC motor and five-wire four-phase motor control

A five-wire four-phase stepper motor has five electrical lines, four of which are used to control the four coils (phases) and the other is a common yang or common yin line that is used to provide power.

The coils of a five-wire four-phase stepper motor are usually connected in twos and twos to form four coils, each of which is connected to a corresponding control line.

4. Drive mode

Five-wire, four-phase stepper motors typically require the use of four independent control signals to activate each coil in order to achieve stepper motion.

5. 28BYJ-48 stepper motor

The 28BYJ-48 stepper motor usually consists of a fixed housing and a rotor. The housing contains four coils, while the rotor has a permanent magnet. The rotor rotates by activating the coil with an electric current.

51MCU Study Notes16 Small DC motor and five-wire four-phase motor control

28BYJ-48 Basic Numbers:

  • Voltage: 5V
  • Number of phases: 4
  • Step angle: 5.625/64
  • Reduction ratio: 1:64

    Reduction ratio calculation:

51MCU Study Notes16 Small DC motor and five-wire four-phase motor control

(1) Hardware circuits

The same hardware circuitry is used for DC motor drives.

51MCU Study Notes16 Small DC motor and five-wire four-phase motor control

(2) Drive control

To drive the 28BYJ-48 stepper motor, the coils need to be activated in the correct order and the direction of the current is controlled to achieve rotation.

The 8-beat signal and motion beat table are as follows:

Wire color Step 1 Step 2 Step 3 Step 4 Step 5 Step 6 Step 7 Step 8
VCC Red 5V 5V 5V 5V 5V 5V 5V 5V
D Orange GND GND GND
C yellow GND GND GND
B powder GND GND GND
A 蓝 GND GND GND

Since there may be a problem with the onboard chip, I have an external ULN2003APC drive stepper motor here.

(3) Code implementation

The following code realizes the driving operation of the stepper motor, press key3 to turn the direction, press key5 to increase the speed, and the speed is reduced to the lowest when it is increased to the maximum. (In fact, increasing the speed is reducing the delay)

dc_motor_5_wire_4_phase_utils.h

#include "dc_motor_5_wire_4_phase_utils.h"
#include "common_utils.h"

// 默认逆时针方向
static u8 dir=0;
// 默认最大速度旋转
static u8 speed=STEPMOTOR_MAXSPEED;
static u8 step=0;
// 1运行中 0停止运行
static u8 run_flag=0;

/**
* @brief 向步进电机发送一个脉冲
* @param step 指定步进序号,可选值0~7
* @param dir 方向选择,1:顺时针,0:逆时针
*/
void step_motor_28BYJ48_send_pulse(void){
	u8 temp=step;
	
	if(dir==0)	//如果为逆时针旋转
		temp=7-step;//调换节拍信号
	switch(temp)//8个节拍控制:A->AB->B->BC->C->CD->D->DA
	{
		case 0: IN1_A=1;IN2_B=0;IN3_C=0;IN4_D=0;break;
		case 1: IN1_A=1;IN2_B=1;IN3_C=0;IN4_D=0;break;
		case 2: IN1_A=0;IN2_B=1;IN3_C=0;IN4_D=0;break;
		case 3: IN1_A=0;IN2_B=1;IN3_C=1;IN4_D=0;break;
		case 4: IN1_A=0;IN2_B=0;IN3_C=1;IN4_D=0;break;
		case 5: IN1_A=0;IN2_B=0;IN3_C=1;IN4_D=1;break;
		case 6: IN1_A=0;IN2_B=0;IN3_C=0;IN4_D=1;break;
		case 7: IN1_A=1;IN2_B=0;IN3_C=0;IN4_D=1;break;
		default: IN1_A=0;IN2_B=0;IN3_C=0;IN4_D=0;break;//停止相序	
	}			
}
/**
* @brief 设置步进电机速度
* @param s 速度值,可选值1~STEPMOTOR_MAXSPEED
*/
void step_motor_28BYJ48_set_speed(u8 s){
    speed=s;
}
/**
* @brief 增加步进电机速度,加到最头0开始
*/
void step_motor_28BYJ48_increase_speed(void){
    if(speed>STEPMOTOR_MAXSPEED)speed--;
    else{
        speed = STEPMOTOR_MINSPEED;
    }
}
/**
* @brief 设置步进电机方向
* @param d 方向选择,1:顺时针,0:逆时针
*/
void step_motor_28BYJ48_set_dir(u8 d){
    dir=d;
}
/**
* @brief 反转步进电机方向
*/
void step_motor_28BYJ48_revert_dir(void){
    dir=!dir;
}

/**
* @brief 启动步进电机
*/
void step_motor_28BYJ48_start(void){
    run_flag=1;
}
/**
* @brief 运行步进电机
*/
void step_motor_28BYJ48_run(void){
    if(run_flag==0)return;
    step_motor_28BYJ48_send_pulse();
		step++;
    if(step>7)step=0;
	delay_ms(speed);
}
/**
* @brief 停止步进电机
*/
void step_motor_28BYJ48_stop(void){
    run_flag=0;
}
           

main.c

/**
* @brief 按键3_4回调函数
*/
void key3_4Callback(int keyNum){
	// key3 转方向 
	if(keyNum == 3){
		step_motor_28BYJ48_revert_dir();
	}else{
		// key4 增加速度
		step_motor_28BYJ48_increase_speed();
	}
}

/**
* @brief 主函数
*/
void main()
{
	step_motor_28BYJ48_set_speed(2);
	// 顺时针旋转
	step_motor_28BYJ48_set_dir(1);
	// 启动步进电机
	step_motor_28BYJ48_start();

	key3_init();
	key4_init();
	setCallback(key3_4Callback);
	while(1)
	{
		// 运行步进电机
		step_motor_28BYJ48_run();
	}		
}
           

The code of this article is open source:

https://gitee.com/xundh/learn51

Read on