This paper, authored by Wei Bin, Zhao Jing, and Ma Hui of the School of Electronic Information Engineering, Xi'an University of Technology, introduces an example of a stepper motor control system based on a microcontroller. It details the hardware and software design principles of the system and provides a schematic diagram of the control circuit and part of the control program. 1. Stepper Motor and Control Requirements A stepper motor, also known as a pulse motor, is an actuator in a digital control system. Its function is to convert pulse electrical signals into corresponding angular or linear displacement; that is, given an electrical pulse signal, the motor rotates by an angle or moves forward one step. Therefore, by controlling the frequency of the input pulses and the energizing phase sequence of the motor windings, the required angle, speed, and direction of rotation can be obtained, making digital control easily implemented using a microcomputer. Because stepper motors have advantages such as step pitch values that do not change due to fluctuations in power supply voltage, load size, and environmental conditions within their load capacity range, no long-term error accumulation, and good control performance, they are widely used in industries such as textiles, instrumentation, and metallurgy. 2 System Hardware Design Stepper motors are classified into three types: PM (permanent magnet), VR (reactive), and HB (hybrid). They come in various phase configurations, including two-phase, three-phase, four-phase, five-phase, and six-phase. Currently, two-phase and four-phase hybrid stepper motors are commonly used. This article introduces a two-phase stepper motor controller composed of a 51 microcontroller and an SLA7026 two-phase power driver. Its control block diagram is shown in Figure 1. [IMG=Figure 1 Control System Block Diagram]/uploadpic/THESIS/2007/11/2007111616292667004S.jpg[/IMG] Figure 1 Control System Block Diagram In Figure 1, the microcontroller receives pulse, enable, and direction signals from the host computer. Internally, the microcontroller's program implements the pulse distributor function, generating the necessary pulse signals for each phase of the stepper motor. The power drive section amplifies the weak signals output by the microcontroller to generate the current required by the motor. This control system uses an STC microcontroller as the processor. The stepper motor selected in this system is a two-phase hybrid stepper motor with an operating voltage of +24V and a step angle of 1.8°. The system uses an SLA7026 as the power amplifier. The SLA7026 is a two-phase stepper motor integrated control/drive module. It uses a SIP18 package. The power supply is 10-44V, and the maximum output current is 3A. Its package and pin definitions are shown in Figure 2. [IMG=Figure 2 SLA7026 Package Diagram]/uploadpic/THESIS/2007/11/2007111616295276921B.jpg[/IMG] Figure 2 SLA7026 Package Diagram INA, IN/A, INB, IN/B (A and /A are the two ends of the stepper motor winding) are the stepper motor pulse control signal input terminals. Since the input is compatible with 5V logic levels, it can be directly connected to the microcontroller pins. OUTA, OUT/A, OUTB, and OUT/B are the stepper motor output control terminals. Their output current is 3A, so they can be directly connected to the motor without optocoupler drive or isolation. CONTROL SUPPLY and GROUND are the positive power supply and ground, respectively. The stepper motor has a self-locking capability, allowing it to stop at the end position of the angular displacement controlled by the last pulse. This enables rotor positioning when the motor stops. However, at this point, the stepper motor overheats significantly. The motor temperature rises with the self-locking time until a certain limit is reached. If this temperature exceeds a certain value, it will damage the internal windings of the motor, preventing it from operating normally. Therefore, we can activate the REFERENCE input of the SLA7026 at an appropriate time (set to 1 second in this system). When this pin is activated, its output current is reduced by half, effectively solving the motor overheating problem. The specific circuit for the half-current lockout is shown in Figure 3. [IMG=Figure 3 Half-current lockout circuit diagram]/uploadpic/THESIS/2007/11/2007111616302385512V.jpg[/IMG] Figure 3 Half-current lockout circuit diagram [IMG=Figure 4 DIP switch circuit diagram]/uploadpic/THESIS/2007/11/2007111616303081474B.jpg[/IMG] Figure 4 DIP switch circuit diagram During operation, the SLA-7026 has three operating modes, two of which are full-step operation and one is half-step operation. The three operating modes can be selected by the DIP switch connected to the microcontroller. The DIP switch circuit diagram is shown in Figure 4. When a certain segment of the DIP switch is closed and active, the P port connected to it is pulled low. The circuit diagram of the SLA7026 as a pulse distributor to drive the stepper motor is shown in Figure 5. Here, the pulses, enable signals, and direction signals sent by the host computer are input to the microcontroller after optocoupler isolation. The pulse input terminal of the SLA7026 is directly connected to the microcontroller. J2 is the stepper motor interface, where pin 1 is the common segment connected to +24V, and the other four are for the stepper motor windings. [IMG=Figure 5 Stepper Motor Drive Circuit Diagram]/uploadpic/THESIS/2007/11/2007111616304144841R.jpg[/IMG] Figure 5 Stepper Motor Drive Circuit Diagram 3 System Software Design In this system, the main function of the microcontroller is to implement the function of the ring distributor. It receives the pulses, enable signals, and direction signals sent by the host computer, counts the input pulse sequence, and then transmits the control pulses, i.e., the corresponding control model, in sequence. Upon power-up, the system first checks the enable signal. Other signals are only meaningful when the enable signal is valid. Then, it determines the rotation direction and operating mode. This system has three operating modes selected via external DIP switches. The first mode's power-on sequence is A→B→/A→/B→A; the second mode's sequence is AB→/AB→/A/B→A/B→AB; and the third mode's sequence is A→AB→B→/AB→/A→/A/B→/B→A/B→A. If powered in the above sequence, the stepper motor rotates in the forward direction; conversely, if the power-on sequence is reversed, the stepper motor rotates in the reverse direction. The program listing for this part is shown below. if (!P2_2) // Determine the selected mode by checking the state of port P { if (!P2_3) m1=1; else m1=2; } else m1=3; switch (i%4) // i is the pulse counter, this code controls mode 1 { case 0: // Model {ina1=1; //ina, Nina, inb, Ninb correspond to A, /A, B, /B respectively Nina1=0; inb1=0; Ninb1=0; }; break; case 1: {ina1=0; Nina1=0; inb1=1; Ninb1=0; }; break; case 2: {ina1=0; Nina1=1; inb1=0; Ninb1=0; }; break; default: {ina1=0; Nina1=0; inb1=0; Ninb1=1; The main function of the microcontroller is to count the input pulse sequence and then transmit the control pulses, i.e., the corresponding control model, in sequence. First, a dedicated counter is set up to count the pulse sequence input from the microcontroller's P1.2 port. Then, the count value is used to look up a table to select the desired output control model, and the counter is cleared to "zero" at the end of the model's state. The program listing for this part is shown below. `if (dir1) // If the direction signal is 1 (forward rotation), increment the counter; otherwise, decrement it. contr1++; else if (dir1==0) contr1--; if (i>=12||i<=4) // Clear the counter to "zero" contr1=8;` As mentioned earlier, the stepper motor heats up significantly in the locked state. Therefore, if the time between two pulse inputs exceeds a certain value (1 second in this system), a half-current state is initiated. This time flag is generated by a timer interrupt, without using software delay, to minimize CPU usage. The subroutine listing for this part is shown below. void timer1(void) interrupt 1 { counter1++; if (counter1>=20) { counter1=0; evtimer1=1; } } 4 Conclusion Stepper motors are widely used in the textile industry. Our microcontroller-based stepper motor control system features small size, low power consumption, high reliability, and strong maintainability. It has also achieved good results in the application of computerized flat knitting machine control systems. (Proceedings of the 2nd Servo and Motion Control Forum, Proceedings of the 3rd Servo and Motion Control Forum)