Microcontroller implements RS232 serial port command control design for two-phase four-wire stepper motors

Preface

The purpose of this design is to use the STC12C5A60S2 microcontroller to realize the computer sending serial port instructions through RS232 to control the two-phase stepper motor
(This is the microcontroller used in this experiment. In fact, the STC80C51 series microcontroller also can achieve the same purpose)

1. Analysis of design ideas

Design block diagram of two-phase four-wire stepper motor controlled by RS232 serial port command
To implement RS232 to send serial port commands to control a two-phase stepper motor, it is necessary to design and develop the following functional modules: control system, drive system, stepper motor, and RS232 communication module.
(1) The control system here chooses the microcontroller STC12C5A60S2. With the help of the minimum system control IO port of the microcontroller, the pin control signal required by the stepper motor is output to achieve the control effect. This experiment is Reduce welding and circuit design steps, and directly use the smallest system module of the microcontroller for design. Pay attention to the selection of microcontroller chips and crystal oscillators;
(2) The TB6600 driver is selected as the driver module here. It is a commonly used stepper motor driver that can achieve 7-speed subdivision control and 8-speed current control. It can be used to drive all common stepper motors on the market.
(3) Stepper motor. The stepper motor used here is a two-phase four-wire stepper motor. It is also a very commonly used motor. It can control forward and reverse rotation after being controlled by a microcontroller. , control the rotation speed, control the rotation distance.
(4) The purpose of the RS232 communication module is to realize communication between the microcontroller and the PC. With the help of this module, the PC can send RS232 serial port instructions to the microcontroller. After processing by the microcontroller, the stepper motor can be controlled. control. Here you can directly use the USB to TTL CH340 module for experiments, replacing the design of the RS232 circuit module of the microcontroller.
The following is mainly to realize the design and development around the above functional modules, so as to achieve the purpose of sending serial port commands through RS232 to control the two-phase stepper motor.


1. Microcontroller control system and circuit diagram

Insert image description here
As mentioned above, the control system uses the single-chip microcomputer STC12C5A60S2. Based on this single-chip microcomputer, build the smallest single-chip computer system, and based on this, build the communication and stepper motor control circuit. In order to be lazy, the smallest single-chip computer system module and USB are directly used here. The CH340 module converted to TTl level simplifies the design of the minimum welding system and RS232 communication circuit from scratch (if you are interested and have time, you can design the complete circuit from scratch by yourself).

2. Stepper motor driver (TB6600)

1. Basic principles

Insert image description here
We all know that the current and voltage of the IO port of a microcontroller as a controller are very small, which is not enough to drive a stepper motor. Therefore, when we use a microcontroller to control a stepper motor, we need to use a driver (driver It can be an amplifier circuit or some commonly used drive modules. The drive module used here is the TB6600 driver). With the help of the TB6600 driver, the two-phase four-term drive motor can be controlled. Mainly pay attention to the following parameters
(1) Driver power supply (DC9-40V). Both 12V and 24V power adapters have been used here to drive the motor. According to the stepper motor required to be driven in this experiment, we chose to use 12V power adapter to power it.
(2) Subdivision (6400 subdivisions). The subdivision driver can change the angle of the synthesized magnetic field by changing the size of the A and B phase currents, thereby making a step angle finer. Divided into multiple steps, the TB6600 driver contains 7-level subdivision control (1, 2/A, 2/B, 4, 8, 16, 32). The experiment here uses 4 subdivisions, so S1, S2, and S3 are set The switch status is: ON, OFF, OFF. According to the formula: step angle = motor inherent step angle/subdivision number, the actual value of the motor step angle can be calculated, that is, the angle value of the motor rotation for each pulse transmitted. .
(3) Current (output current), 8-level current control (0.5A, 1A, 1.5A, 2A, 2.5A, 2.8A, 3.0A, 3.5A), because the stepper motor Different models support different driving voltage and current values. The 8-level current control provided by the TB6600 driver is well adapted to the driving needs of most motors. The currents supported by the two motors used later are different. There are clear differences. Generally, it is set to 0.5A first, and then adjusted according to the actual situation, that is, the switch status of S4, S5, and S6 is set to: ON, ON, ON
(4) Motor interface, as shown in the figure above , the TB6600 driver provides four motor interfaces: electrical phase B-, electrical phase B+, electrical phase A-, and electrical phase A+. These four motor interfaces are respectively connected to the electrical phase B-, electrical phase B+, and electrical phase of the stepper motor. A-, electrical phase A+. Generally, when purchasing a stepper motor, the store will provide the electrical phase represented by the corresponding line sequence of the four wires of the stepper motor. We can just connect them according to the specifications. If you are not sure, you can use the diode of a multimeter. Gear position test, the red pen is connected to the electrical phase B-/A- of the stepper motor and the black pen is connected to the electrical phase B+/A+ of the stepper motor. For the same phase, the multimeter will beep. This method can determine When wiring the two sets of phases, just make sure that the two sets of phases are connected correctly (there is no obvious difference between phase A and phase B. Reverse connection of phase A and phase B will only cause the motor to rotate differently and will not affect the rotation of the motor)
(5) Control signal interface, as shown in the figure above, the control signal of the TB6600 driver has a total of 6 interfaces: ENA-, ENA+, DIR-, DIR+, PUL-, PUL+, these six interfaces are connected To our minimum microcontroller system, the TB6600 driver can be controlled through the IO pins of the microcontroller, thereby realizing the control and use of stepper motors. There are two connection methods: common anode and common cathode. The common anode connection is used here. The three interfaces ENA+, DIR+, and PUL+ are connected to the +5V power supply provided by the microcontroller; ENA- (P1.2), DIR- (P1.0), and PUL- (P1.1) are connected to the corresponding microcontroller pins respectively. , the ENA- pin is an enable pin, which is valid when the level is low (that is, when the microcontroller control pin inputs a high level, the motor stops rotating, and when it is a low level, it can rotate); the DIR- pin is a forward and reverse control pin, Low level and high level control forward and reverse rotation respectively (since the motor rotation is also related to the wiring of the motor, it is best to determine the level of forward and reverse rotation based on actual debugging); PUL- (P1.1) is Pulse control pin, by sending a certain frequency of level pulses to this pin, the speed and rotation angle of the motor can be controlled. As mentioned above, whenever a pulse signal is transmitted, the motor will move by a step angle (the motor's inherent step angle/subdivision number). By controlling the value of the pulse, the motor's rotation angle can be controlled. For example, this time The number of subdivisions used in the implementation is 4 subdivisions. The inherent step angle of the stepper motor used is 18°. Therefore, when a pulse is sent, the angle of rotation of the motor is 18°/4=4.5°. The number of pulses required for the motor to rotate once is =360°/4.5°=80; the frequency of pulse transmission (how many pulses can be sent in 1s) can control the motor speed.

2.Wiring method

Insert image description here
After connecting the TB6600 driver and microcontroller according to the above circuit diagram and physical diagram, you can proceed to the next step.

The code is as follows: drive the TB6600 driver through the P1 port of the microcontroller to control the stepper motor

#include"reg52.h"
#include <intrins.h>
sbit dir=P1^0;	//DIR-引脚为正反转控制引脚
sbit pul=P1^1;	//PUL-(P1.1)为脉冲控制引脚
sbit ena=P1^2;	//ENA- 引脚为使能引脚,低电平时有效

3. Stepper motor

Insert image description here
In this experiment, the above stepper motor module was used. When the motor rotates, it will be forwarded and forwarded.

1. Basic parameters

(1) Motor type: two-phase four-wire system
(2) Drive voltage: 4-9V/100-500mA (the higher the voltage, the greater the force and the greater the heat. )
(3) Screw length: 90mm
(4) Slider stroke: 80mm
(5) Motor diameter: 15mm
(6) Screw diameter: 3mm
(7) Screw pitch: 0.5mm
(8) Step distance Angle: 18°
(9) Phase resistance: 15.5 ohms
(10) Overall size 158105mm
(11) Blue A+ , black A-, red B+, yellow B-
To drive this stepper motor, we need to understand some parameters related to the motor, and the most basic ones are the driving voltage and current, as shown in the above parameters. , the driving voltage is 4-9V, the current is 100-500mA, we use the TB6600 driver here, the driver voltage is 12V/24V, and then set the current gear to 0.5A to achieve the purpose of driving this stepper motor; The length of the screw rod is the total length of the screw rod; the slider stroke controls the distance traveled by the stepper motor; the screw pitch is the distance traveled by one revolution; the step angle is the distance the stepper motor rotates when it receives a pulse. Angle; the above parameters are the parameters needed to operate this stepper motor. For example, to realize the stepper motor moving forward by 50mm, the calculation and design method is as follows (TB6600 is 4 subdivisions, 0.5A):
(1) The pulse required for one revolution: 360°/(step angle/number of subdivisions) = 360°/(18°/4) = 80
(2) How many turns are needed to advance 50mm: 50mm/0.5=100 a>
(3) To achieve 50mm advancement, 80*100 = 8000 pulses need to be sent

2. Use of stepper motors and code analysis

As mentioned above, after using the TB6600 driver to connect the stepper motor, the pulse signal can be sent through the microcontroller to control the stepper motor.

The code is as follows: This function is the driving function of the stepper motor. Calling this function can control the moving distance and speed of the motor.

void distance_160(uint distance,uint velocity)
{
    
    
	uint x,y;
	//distance参数为步进电机转动多少圈的参数,通过圈数可以控制步进距离
	//velocity为脉冲的延时时间,改变这里的时间,可以改变电机移动的速度
	for( x = 0; x < distance; x++){
    
    
		for( y = 0; y < 80; y++) {
    
    	//这里的80指的是,80个脉冲转一圈(18/4=4.5 360/4.5=80,一圈为0.5mm);电机长度80mm,80/0.5=160(电机可移动距离)
		//下面为一个脉冲
			pul=1; 
			delay_1s(velocity);                //修改微秒值可以调速
			pul=0;
			delay_1s(velocity);             
		}
	}
}

3. Extension

Insert image description here

As shown in the picture, there is also a common two-phase four-wire stepper motor (42 stepper motor) on the market. Although the type is different, its driving principle is the same. We can change the current and subdivision of the TB6600 driver. value to control it.

4. RS232 communication

Insert image description here

1. USB to TTL CH340 module

In this experiment, in order to be lazy, we directly used the USB to TTL CH340 module to implement this part. This module can be directly connected to the TXD and RXD pins of the microcontroller through the RXD and TXD pins on the module, and this The module can also output the 5V voltage required by the microcontroller, which greatly simplifies the experiment.

The RS232 serial communication code analysis is as follows:

//RS232串口功能初始化函数
void init()
{
    
    
	TMOD=0x20;	//定时器1,工作方式2,8位自动重装
	TH1=0xfd;	//初值
	TL1=0xfd;	//初值
	TR1=1;	//开定时器
	REN=1;	//允许串口接收
	SM0=0;	//设定串口工作方式
	SM1=1;	//设定串口工作方式
	EA=1;	//开总中断
	ES=1;	//开串口中断
}
//主函数
void main()
{
    
    
	uchar i;
	//初始化函数
	init();
	while(1)
	{
    
    
		if(flag==1)
		{
    
    
			ES=0;	//关串口中断
			//根据串口接受到的数值进行判断进行不同的步进操作
			switch(a)
			{
    
    
				case '1':	break;
				case '2':	break;	
				...
				...
				default:	break;		
			}
			ES=1;	//开串口中断
			flag=0;	//标志位清零
		}
	}
				
void ser() interrupt 4
{
    
    
	//目前已经实现把数据存储到数组再判断,但必须要一次性发送固定的字符串,不然会错乱
		if(RI)										//如果是串口接收到一帧数据,就会产生中断,RI标志变为1
		{
    
    
			RI = 0;									//手动将标志置0,方便下次判断
			a=SBUF;			//出去接收到的数据	
		}
		flag=1;		//标志位用于,只有当4位数据接收完,主函数才进行数据比较
}

5. Practical application and code analysis

The function of the following code is: numbers 0-9 can be sent through the serial port to control the stepper motor in different directions and different moving distances
Video

The complete code is as follows:

#include"reg52.h"
#include <intrins.h>
sbit dir=P1^0;
sbit pul=P1^1;
sbit ena=P1^2;
#define uint  unsigned int
#define uchar unsigned char
uchar flag,a;
uchar code table1[]="A01B01";
uchar code table2[]="A05B05";
uchar code table3[]="A10B10";
uchar code table4[]="A30B30";
uchar code table5[]="A50B50";
uchar code table6[]="No OK";
void distance_160(uint distance,uint velocity);

//RS232串口功能初始化函数
void init()
{
    
    
	TMOD=0x20;	//定时器1,工作方式2,8位自动重装
	TH1=0xfd;	//初值
	TL1=0xfd;	//初值
	TR1=1;	//开定时器
	REN=1;	//允许串口接收
	SM0=0;	//设定串口工作方式
	SM1=1;	//设定串口工作方式
	EA=1;	//开总中断
	ES=1;	//开串口中断
}

//延时函数,但是实际并不准确
void delay_1s(uint xms)
{
    
    
	uint i,j;
	for(i=xms;i>0;i--)
		for(j=110;j>0;j--);
}

void distance_160(uint distance,uint velocity)
{
    
    
	uint x,y;
	//distance参数为步进电机转动多少圈的参数,通过圈数可以控制步进距离
	//velocity为脉冲的延时时间,改变这里的时间,可以改变电机移动的速度
	for( x = 0; x < distance; x++){
    
    
		for( y = 0; y < 80; y++) {
    
    	//这里的80指的是,80个脉冲转一圈(18/4=4.5 360/4.5=80,一圈为0.5mm);电机长度80mm,80/0.5=160(电机可移动距离)
		//下面为一个脉冲
			pul=1; 
			delay_1s(velocity);                //修改微秒值可以调速
			pul=0;
			delay_1s(velocity);             
		}
	}
}

void main()
{
    
    
	uchar i;
	//初始化
	init();
	while(1)
	{
    
    
		if(flag==1)
		{
    
    
			ES=0;	//关串口中断
			//根据串口接受到的a的数值进行判断进行不同的步进操作	
			switch(a)
			{
    
    
				//RS232发送指令为1时,正向前进1mm,返回A01
				case '1':
					dir=0;       //dir为1时正转,dir为0时反转
					ena=1;      //使能电机
					distance_160(2,4);
					for(i=0;i<3;i++)
					{
    
    
						SBUF=table1[i];
						while(!TI);
						TI=0;
					}
					break;
				//RS232发送指令为2时,反向前进1mm,返回B01
				case '2':
					dir=1;
					ena=1;				
					distance_160(2,4);
					for(i=3;i<6;i++)
					{
    
    
						SBUF=table1[i];
						while(!TI);
						TI=0;
					}
					break;
				//RS232发送指令为3时,正向前进2.5mm,返回A05
				case '3':
					dir=0;
					ena=1;				
					distance_160(5,1);	
					for(i=0;i<3;i++)
					{
    
    
						SBUF=table2[i];
						while(!TI);
						TI=0;
					}
					break;
				//RS232发送指令为4时,反向前进2.5mm,返回B05
				case '4':
					dir=1;
					ena=1;				
					distance_160(5,1);
					for(i=3;i<6;i++)
					{
    
    
						SBUF=table2[i];
						while(!TI);
						TI=0;
					}
					break;		
				//RS232发送指令为5时,正向前进5mm,返回A10	
				case '5':
					dir=0;
					ena=1;				
					distance_160(10,1);	
					for(i=0;i<3;i++)
					{
    
    
						SBUF=table3[i];
						while(!TI);
						TI=0;
					}
					break;
				//RS232发送指令为6时,反向前进5mm,返回B10
				case '6':
					dir=1;
					ena=1;				
					distance_160(10,1);	
					for(i=3;i<6;i++)
					{
    
    
						SBUF=table3[i];
						while(!TI);
						TI=0;
					}
					break;
				//RS232发送指令为7时,正向前进15mm,返回A30
				case '7':
					dir=0;
					ena=1;				
					distance_160(30,1);	
					for(i=0;i<3;i++)
					{
    
    
						SBUF=table4[i];
						while(!TI);
						TI=0;
					}
					break;
				//RS232发送指令为8时,反向前进15mm,返回B30
				case '8':
					dir=1;
					ena=1;				
					distance_160(30,1);	
					for(i=3;i<6;i++)
					{
    
    
						SBUF=table4[i];
						while(!TI);
						TI=0;
					}
					break;
				//RS232发送指令为9时,正向前进25mm,返回A50
				case '9':
					dir=0;
					ena=1;				
					distance_160(50,1);	
					for(i=0;i<3;i++)
					{
    
    
						SBUF=table5[i];
						while(!TI);
						TI=0;
					}
					break;
				//RS232发送指令为0时,反向前进25mm,返回B50
				case '0':
					dir=1;
					ena=1;				
					distance_160(50,1);	
					for(i=3;i<6;i++)
					{
    
    
						SBUF=table5[i];
						while(!TI);
						TI=0;
					}
					break;
				//RS232发送指令不为0-9的数值时,返回No OK
				default:
					for(i=0;i<5;i++)
					{
    
    
						SBUF=table6[i];
						while(!TI);
						TI=0;
					}
					break;
			}
			ES=1;
			flag=0;
		}
	}	
}

void ser() interrupt 4
{
    
    
	//目前已经实现把数据存储到数组再判断,但必须要一次性发送固定的字符串,不然会错乱
		if(RI)										//如果是串口接收到一帧数据,就会产生中断,RI标志变为1
		{
    
    
			RI = 0;									//手动将标志置0,方便下次判断
			a=SBUF;			//出去接收到的数据	
		}
		flag=1;		//标志位用于,只有当4位数据接收完,主函数才进行数据比较
}

Summarize

Stepper motor effect video


At this point, the content of this experiment is finished. The above is the actual effect of this experiment. This article briefly introduces the basic method of controlling stepper motors using serial port instructions. After completing this experiment, you can master the basic stepper motor control methods.

Guess you like

Origin blog.csdn.net/qq_46166916/article/details/126513669