laitimes

51 MCU Study Note 17 4-wire bipolar motor and SG90 servos

author:Programming circles

1. Four-wire bipolar motor control

1. Introduction to the working mode of four-wire bipolar motor

51 MCU Study Note 17 4-wire bipolar motor and SG90 servos

Four-wire bipolar motors are typically used to drive applications that require high efficiency and reliability. Its stator usually contains a fixed coil, while the rotor is the part that can rotate, usually by the interaction of a magnetic field with the stator to generate rotational force.

(1) Working principle

The working principle of a four-wire bipolar motor is based on the interaction between the electric current and the magnetic field. When an electric current passes through the stator coil, the resulting magnetic field interacts with the magnetic field on the rotor, resulting in a rotational motion of the rotor.

51 MCU Study Note 17 4-wire bipolar motor and SG90 servos

(2) Drive mode

Four-wire bipolar motors are typically powered by a DC power supply. By applying currents in different directions in the stator coil, the steering and speed of the motor can be controlled;

The motor drive principle has 4 beats drive and 8 beats drive, 4 beats is 4 beats and one rotation circle, and 8 beats is 8 beats one circle.

In this paper, a TC1508S chip is used to drive a 4-wire bipolar motor.

(3) Polarity distinction

In contrast to a five-wire, four-phase stepper motor, a four-wire bipolar stepper motor has only four electrical lines, two of which are used to control two coils (phases) and the other two are common yang or common yin and ground.

  • The coils of the four-wire bipolar stepper motor are connected separately and independently, and each coil is connected to only one control line.
  • Four-wire bipolar stepper motors typically have a larger stepping angle because they only have two coils, and the stepping angle is limited by how the coils are arranged.

(4) Drive mode

The four-wire bipolar stepper motor only needs to use two control signals, and the activation of the coil is controlled by changing the polarity of the signal, so as to achieve stepper motion.

2. Introduction to TC1508S chips

TC1508S is a dual-channel DC motor drive integrated circuit, which is mainly used to drive small and medium-sized DC motors or four-wire bipolar stepper motors. This chip has a built-in power MOSFET full-bridge drive circuit, which can easily realize the forward rotation, reversal, stop and brake control of the motor.

The stepper motor I have at hand is rated at 6-24V, TC1508S it can't be driven directly. TMI8549 chips have been purchased online, and the driving circuit will be supplemented after the successful experiment.

(1) Main features

  1. Dual-channel design: Capable of independently driving two DC motors or two windings of a stepper motor.
  2. Built-in MOSFET: Integrated high-voltage and high-current MOSFETs, the maximum continuous output current per channel can reach 1.8A, and the peak current is 2.5A, which means that it can directly drive low- and medium-power DC motors without additional H-bridge drive circuits.
  3. Wide voltage operating range: suitable for applications under a wide range of supply voltages.
  4. High efficiency: With ultra-low standby and operating currents, it helps to save energy and improve overall system efficiency.
  5. Control Functions: Provides control interfaces for forward, reverse, and braking modes, making it easy to precisely control the motor in embedded systems.
  6. Protection mechanism: Although the specific protection function is not clearly specified, this kind of motor driver chip usually contains overcurrent, short circuit and other protection measures to ensure the safety of the chip and motor.

(2) Pins

51 MCU Study Note 17 4-wire bipolar motor and SG90 servos
  • INA, INB first channel input
  • OUTA, OUTB first channel output
  • INC, IND second channel input
  • OUTC, OUTD second channel output
  • Several GNDs are connected to the ground
  • VDD with 5V

(3) Input/output logical tables

IN WHICH THE INB OUTA OUTB Mode of exercise
L L Hi-Z Hi-Z Standby status
H L H L advance
L H L H Back off
H H L L brake

Waveform:

51 MCU Study Note 17 4-wire bipolar motor and SG90 servos

The second channel is similar.

Since the output can be reversed, the motor can be controlled to rotate forward or reverse.

3. Module circuit diagram

The example in this article is a module component purchased directly from the Internet, and the schematic diagram is as follows:

51 MCU Study Note 17 4-wire bipolar motor and SG90 servos

PCB Examples:

51 MCU Study Note 17 4-wire bipolar motor and SG90 servos

4. Code implementation

dc_motor_4_wire_utils.c

#include "dc_motor_4_wire_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_4_wire_send_pulse(void){
	u8 temp=step;
	
	if(dir==0)	//如果为逆时针旋转
		temp=7-step;//调换节拍信号

  // (A+)---(A+B+)---(B+)--(B+A-)---(A-)---(A-B-)---(B-)---(B-A+)
	// A-AC-C-CB-B-BD-D-DA-A
	switch(temp)
	{   //        黑A+  绿A-    黄B+   红B-
		case 0: IN_A=1;IN_B=0;IN_C=0;IN_D=0;break;
		case 1: IN_A=1;IN_B=0;IN_C=1;IN_D=0;break;
		case 2: IN_A=0;IN_B=0;IN_C=1;IN_D=0;break;
		case 3: IN_A=0;IN_B=1;IN_C=1;IN_D=0;break;
		case 4: IN_A=0;IN_B=1;IN_C=0;IN_D=0;break;
		case 5: IN_A=0;IN_B=1;IN_C=0;IN_D=1;break;
		case 6: IN_A=0;IN_B=0;IN_C=0;IN_D=1;break;
		case 7: IN_A=1;IN_B=0;IN_C=0;IN_D=1;break;
        //default: IN_A=0;IN_B=0;IN_C=0;IN_D=0;break;
	}			
}
/**
* @brief 设置速度
*/
void step_motor_4_wire_set_speed(u8 s){
    speed=s;
}
/**
* @brief 增加速度
*/
void step_motor_4_wire_increase_speed(void){
    if(speed>STEPMOTOR_MAXSPEED)speed--;
    else{
        speed = STEPMOTOR_MINSPEED;
    }
}
/**
* @brief 设置方向
*/
void step_motor_4_wire_set_dir(u8 d){
    dir=d;
}
/**
* @brief 反转方向
*/
void step_motor_4_wire_revert_dir(void){
	if(dir==0)
		dir=1;
	else
		dir=0;
}
/**
* @brief 启动
*/
void step_motor_4_wire_start(void){
    run_flag=1;
}
/**
* @brief 运行
*/
void step_motor_4_wire_run(void){

	if(run_flag==1){
		step_motor_4_wire_send_pulse();
		step++;
		if(step>7)step=0;
		delay_10us(speed*300);
	}
}
/**
* @brief 停止
*/
void step_motor_4_wire_stop(void){
    run_flag=0;
}
void step_motor_4_wire_init(void){
	IN_A=0;
	IN_B=0;
	IN_C=0;
	IN_D=0;
}

           

main.c

#include "dc_motor_4_wire_utils.h"
#include "key_utils.h"

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

/**
* @brief 主函数
*/
void main()
{
	step_motor_4_wire_init();
	step_motor_4_wire_set_speed(1);
	// 顺时针旋转
	step_motor_4_wire_set_dir(1);
	// 启动步进电机
	step_motor_4_wire_start();

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

           

2. SG90 servo control

1. Introduction to SG90

The SG90 servo is a common and affordable small analog servo motor that is widely used in all kinds of DIY electronics production, robotics and model making. A brief description of its main features and parameters:

  1. Structure & Size:
  2. With a small size of approximately 21.5mm x 11.8mm x 22.7mm and a weight of only 9 grams, it is suitable for use in applications where space is limited.
  3. How it works:
  4. As a position servo drive, the servo receives the PWM (pulse width modulation) signal from an external controller (such as microcontroller, remote control, etc.), and accurately controls the rotation angle according to the change of pulse width.
  5. Electrical Specifications:
  6. The operating voltage range is 4.8V to 6V, with a typical operating voltage of 4.8V.
  7. The maximum torque is around 1.2 to 1.4 kg/cm (at 4.8V), which means it provides enough force to hold a certain angle or overcome some resistance.
  8. The control signal cycle is typically 20 milliseconds, and the pulse width varies between 0.5 ms and 2.5 ms, corresponding to different rotation angles, and is centered at 1.5 ms (about half of the 180-degree mechanical structure).
  9. The pulse control accuracy is high, up to 2 microseconds.
  10. The position level is 1024, which theoretically allows for finer angular positioning.
  11. Performance indicators:
  12. At no load, the SG90 servos can rotate 60 degrees in about 0.12 seconds at 4.8V, i.e. about 0.002 seconds per degree.
  13. The operating temperature range is wide, from -30 degrees Celsius to +60 degrees Celsius.
  14. It is equipped with a dead zone setting to prevent the servo from shaking excessively when approaching the target angle, improving stability.
  15. Type:
  16. Depending on the internal structural material, the SG90 servo is available in two versions, plastic gears and metal gears, with the metal gear version usually having higher durability and load carrying capacity.

2. Hardware connection

51 The I/O port of the single-chip microcomputer can be directly connected to SG90,

color pins
Red VDC
brown GND
orange Signal lines

3. Control Signals

By adjusting the PWM of the signal line, the SG90 can be rotated at all angles.

180 degree servo control parameters:

Angle (°) Pulse Cycle (ms) Occupancy Ratio (%) Pulse High Time (ms)
20 2.5 0.5
45 20 5.0 1
90 20 7.5 1.5
135 20 10 2
180 20 12.5 2.5

4. Code Samples

dc_motor_sg90_utils.c

#include "dc_motor_sg90_utils.h"
 
// 信号引脚
sbit SG_PWM = P0^0;
// 计数用来切换高低电平
static u8 PWM_count=3;   
static u8 count=0;
 //1--0度,2--45度,3--90度,4--135度,5--180度
static u8 sg90_angle=0;
void sg90_timer_Init()
{
	TMOD = 0X01;      	// T0定时方式1
	TH0 = 0Xfe;
	TL0 = 0X33;       	// 计数初值设置为0.5ms
	ET0=1;          	// 打开定时器0的中断
	TR0=1;          	// 打开定时器0
	EA=1;           	// 开总中断
}
// 定时器中断0
void sg90_timer_irq() interrupt 1
{
	TR0=0;
	TH0 = 0XFE;
	TL0 = 0X33;       //重新赋计数初值为0.5ms
	if(count <= PWM_count){
		SG_PWM=1;
	}
	else{
		SG_PWM=0;
	}
	count++;
	if(count>=40){
		count=0;
		sg90_angle++;
	}
	TR0=1;
}
void sg90_run(void){
	if(sg90_angle==16){
		PWM_count=1;
	}
	if(sg90_angle==32){
		PWM_count=3;
		sg90_angle=0;
	}
}
           

main.c

#include "dc_motor_sg90_utils.h"

void main()
{
  	sg90_timer_Init();
  	while(1){
      	sg90_run();
  	}
}
           

The code of this article is open source:

https://gitee.com/xundh/learn51

Read on