Share this

A new method for directly controlling a stepper motor using a computer serial port

2026-04-06 05:08:48 · · #1
Abstract: This paper introduces a new method for using a computer serial port as a stepper motor controller through secondary development. The computer generates control pulses by sending data to the serial port to control the stepper motor. [font=SimSun] Keywords: [/font]Serial port Secondary development Control pulse Stepper motor controller Stepper motors are widely used in automatic or semi-automatic equipment such as CNC machine tools, medical devices, and instruments. The common practice of using a computer to control a stepper motor is to use a stepper control card. The system structure is shown in Figure 1 [1][2]. Among them, Pulse and Dir are signals that control the number of steps and the direction of rotation of the motor, respectively; CWL (Clock Wise Limit) and CCWL (Counter Clock Wise Limit) are limit signals for clockwise and counterclockwise rotation of the motor, respectively; ORG is the positioning signal. [img=300,200]http://www.mcu99.com/Article/UploadFile/200612/20061203095849156.gif[/img] This method is not only costly, but also inconvenient to operate. Installing a control card in a computer expansion slot requires opening the chassis, and small tablet PCs and embedded computers simply lack the space and expansion slots for such devices. Developing a secondary computer serial port for controlling stepper motors, replacing the function of a control card, offers advantages such as low cost, simple operation, and good compatibility. 1. RS232 Serial Port and Programming The computer serial interface uses the RS232 standard: logic 1 is defined as -3 to -15V, and logic 0 as +3 to +15V. There are eight commonly used signals (pin numbers are shown in Table 1 when the interface is a DB9M socket). RXD and TXD are for transmitting and receiving data, allowing direct communication with RS232 serial port devices. RTS, DTR, CD, DSR, CTS, and BELL are signals for controlling and detecting the modem, playing a role in communication and control. Data formats include 5, 6, 7, and 8 bits, with a 1-bit start bit (logic 0) and 1, 1.5, or 2 stop bits (logic 1). Odd parity, even parity, and no parity can be selected. Common baud rates are 2400, 4800, 7200, and 9600 bps. There are three main serial port programming methods: hardware programming, file operation, and serial port control. Table 1. Pin Definitions of RS232 Interface [table=98%][tr][td=1,1,33%]Pin Number[/td][td=1,1,33%]Function[/td][td=1,1,34%]Direction[/td][/tr][tr][td=1,1,33%]1 2 3 4 5 6 7 8 9[/td][td=1,1,33%]CD Data Carrier Detection RXD Receive Data TXD Transmit Data DTR Data Terminal Ready GND Signal Ground DSR Data Device Ready RTS Request to Transmit CTS Clear Transmit RI Ring Indicator[/td][td=1,1,34%]Input Input Output Output Input Output Input Input[/td][/tr][/table] 1.1 Hardware Programming Method: Directly use the input and output (I/O) functions of the port to program the control circuit of the serial port. It is applicable to DOS and Windows platforms (versions prior to Windows Me), but not to operating systems built on NT technology (Windows NT, Windows 2000, Windows XP, etc.) because it does not allow user programs to directly manipulate hardware. Commonly used port input and output functions (instructions) include: IN and OUT instructions and software interrupt calls in assembly language, inport(), outport(), inportb(), and outportb() in C language, and _inp() and _outp() in C++. [img=351,307]http://www.mcu99.com/Article/UploadFile/200612/20061203095849698.gif[/img] The computer serial port is composed of Intel 8250 asynchronous serial communication components. The base addresses of COM1, COM2, COM3 and COM4 are 16#3F8 (hexadecimal number 3F8, the same representation below), 16#2F8, 16#3E8 and 16E2E8 respectively. The baud rate factor (DR) is calculated as follows: DR=1.8432×1000000/16B. For 8250 programming, please refer to reference [3]. 1.2 File operation method [4] The file operation method treats the serial port as a file of the system. The serial port is triggered to receive and send data by reading and writing the file. This method works correctly on DOS, Windows, Windows NT, Windows 2000, and Windows XP platforms. For example, in BASIC, the statement `OPEN "COM1, 1200, n, 8, 1, rs, cs, ds, cd" AS #1` can be used to read and write to serial port COM1 as file #1. In VC++, functions such as `CreateFile()`, `BuildCommDCB()`, `Read()`, `Write()`, and `EscapeCOMMFunction()` can be used to operate on the serial port as a file. 1.3 Serial Port Control Method When programming in VB and VC++, a control can be used to program the serial port. This method has good versatility and works correctly on Windows, Windows NT, Windows 2000, and Windows XP platforms. In VB, the MSCOMM control is used; in VC++, the Microsoft Communication Control ActiveX control is used. Please refer to MSDN for serial port control usage methods. 2. Working Principle of Serial Port Stepper Motor Controller 2.1 Study on Serial Port Data Transmission Process [img=294,259]http://www.mcu99.com/Article/UploadFile/200612/20061203095849892.gif[/img] During the serial port data transmission process, the serial port first sends a start bit (logic 0) for synchronization, then sends each binary bit of the communication data sequentially from low to high according to the specified baud rate (B), and finally sends a stop bit (logic 1). This indicates that the logic level of each binary bit is held at the TXD terminal for 1/B seconds. If data is transmitted in a manner with 8 data bits, 1 stop bit, and no parity check, the binary bits of the transmitted data alternate between 0 and 1. For example, if the data is 01010101 (i.e., 16#55 in hexadecimal), the transmission process is shown in Figure 2, point 5. For each byte sent, 5 pulses are emitted at the TXD terminal, with a period T = 2/B, i.e., a frequency f = B/2. By changing the binary bits of the transmitted data, different waveforms can be obtained at the TXD terminal. Figure 2 shows the data to be emitted and the corresponding waveforms when generating 1 to 5 pulses. 2.2 Working Principle of Serial Port Stepper Motor Controller Analysis of the serial port data transmission process shows that the pulses emitted from the TXD terminal fully meet the needs of controlling the stepper motor: ① Changing the number of bytes sent and the content of the sent bytes allows for the generation of any number of pulses at the TXD terminal; ② Changing the baud rate dynamically changes the frequency of the transmitted pulses. Therefore, TXD can be used as the pulse signal for controlling the stepper motor. Although the serial port's DTR, RTS, CTS, DSR, CD, and RI are defined as handshake signals with different functions in the serial communication interface, through the analysis of the 8520, it can be seen that they can all be used as general I/O quantities, and regardless of the programming method used, these signals can be easily read and written. If DTR (or RTS) is used as the direction control signal (Dir), and CD, DSR, CTS, and RI are used as status detection signals (CWL, CCWL, ORG, etc.) respectively, then only one serial port is needed to provide all the signals required by the stepper motor controller. Based on this principle, we developed a serial port stepper motor controller and successfully applied it to the control system of a multi-point forming equipment for sheet metal. Since the serial port has directly provided all the signals required to control the stepper motor, it is only necessary to convert each signal from RS232 level to TTL level. Commonly used level conversion devices include DS1488, DS1489, MAX232, etc. [5]. The working principle of the stepper motor controller developed using the computer serial port is shown in Figure 3. [img=392,144]http://www.mcu99.com/Article/UploadFile/200612/20061203095849573.gif[/img] 3 Software Design and Calculation To avoid motor step loss and improve motor running speed, the stepper motor running process is divided into three stages: low-speed start and acceleration, high-speed operation, and deceleration and stop; correspondingly, the control pulses are also divided into three segments: frequency increase, high frequency, and frequency decrease [6], as shown in Figure 4. When using the serial port to send data to generate control pulses, although any number of pulses between 1 and 5 can be generated by changing the content of the sent byte, if the number of pulses of a byte sent is less than 5, the pulse frequency and duty cycle generated when the two bytes are connected will fluctuate. To ensure smooth filtering of the three stages of motor operation, the number of pulses in each stage needs to be adjusted so that the number of steps in stages I and II are both multiples of 5 (n1×5 and n2×5 respectively). Steps that are not multiples of 5 are scheduled for stage III (deceleration and stopping), with the number of pulses output being n3×5 + Δp, where Δp = (1 to 4). This can be achieved by changing the content of the last byte sent in stage III to generate any number of pulses. The data to be sent to generate 1 to 4 pulses are 16#FF, 16#FB, 16#F5, and 16#D5 respectively. [img=365,733]http://www.mcu99.com/Article/UploadFile/200612/20061203095850143.gif[/img] As shown in Figure 4, during motor operation, the frequency f of the control pulse should change constantly to meet the needs of low-speed start-stop and high-speed operation. The pulse frequency is determined by the baud rate (B) of the transmitted data. Each pulse requires two binary bits, 1 and 0, to form its high and low levels, so f=B/2. The frequency of the transmitted control pulse can be changed by adjusting the baud rate of the transmitted data. Conventionally, the frequency of the control pulse generated when transmitting data with a series of baud rates varies greatly, which cannot meet the requirements of normal motor start-stop and speed regulation. Therefore, the computer needs to transmit data at a non-standard baud rate to generate control pulses of arbitrary frequency. Generally, during the motor start-up and stop stages (I, III), the baud rate is adjusted once for each byte transmitted to make the motor start-stop as smooth as possible. The frequency adjustment amounts Δf1 and Δf2 for stages I and III are respectively: Δf1 = (FH - FL) / n1 (1) Δf2 = (FH - FL) / n3 + 1 (2) The corresponding baud rate adjustment amounts ΔB1 and ΔB2 are respectively: ΔB1 = (BH - BL) / n1 = (2FH - 2FL) / n1 (3) ΔB2 = (BH - BL) / (n3 + 1) = (2FH - 2FL) / (n3 + 1) (4) The baud rate Bi for sending the i-th byte is: [img=369,165]http://www.mcu99.com/Article/UploadFile/200612/20061203095850376.gif[/img] The flowchart of the control software for directly controlling a stepper motor via a serial port is shown in Figure 5. 4. Advanced Control Functions Advanced control of the stepper motor mainly refers to automatic zeroing and multi-motor linkage differential compensation. Zeroing refers to controlling the stepper motor to drive the operating mechanism and complete the positioning process. During zeroing, the motor generally runs at a low speed, stopping when the ORG signal is detected. Due to the low operating speed, to simplify the program design, only one pulse can be output at the TXD terminal for each byte sent, stopping transmission upon detecting the ORG signal. To ensure a 50% duty cycle for the output pulse, the sent byte is set to 16#F0. The design of the zeroing program can be seen in Figure 5. Differential compensation refers to controlling two or more motors to run at a specified speed ratio, thereby controlling the actuator to run along a specified trajectory. Since most computers only have two serial ports, this method is only suitable for two-dimensional differential control. Typically, both serial ports transmit at the same baud rate, and the speed and position of the two motors are adjusted by varying the number of bytes sent and the number of pulses generated for each byte. Practical experience has shown that stepper motor controllers using serial port devices offer advantages such as ease of use, stable performance, low cost, good portability, and no need to install hardware drivers. Stepper motor controllers designed directly using computer serial ports are suitable for controlling fully digital servo motors. This paper provides some reference for the secondary development and application of computer serial and parallel port resources.
Read next

CATDOLL 136CM Tami (Customer Photos)

Height: 136cm Weight: 23.3kg Shoulder Width: 31cm Bust/Waist/Hip: 60/54/68cm Oral Depth: 3-5cm Vaginal Depth: 3-15cm An...

Articles 2026-02-22