1. Preface
This project is based on the STC89C52 microcontroller and realizes the function of forward and reverse rotation according to the angle by controlling the 28BYJ-48 stepper motor. 28BYJ-48 stepper motor is a commonly used motor with precise positioning and high torque output, suitable for many small automation systems and mechanical devices.
In this project, the STC89C52 microcontroller is used as the controller, which is a powerful and commonly used 8-bit microcontroller chip with rich peripherals and powerful computing capabilities. By writing appropriate programs, the movement of the stepper motor can be controlled through the IO port of the microcontroller.
The 28BYJ-48 stepper motor is a low-cost, low-power stepper motor with precise positioning capabilities and high torque output. The interface signal between the microcontroller and the stepper motor will be used to drive the motor to rotate, and the motor forward or backward and the angle of rotation will be controlled by controlling the frequency and sequence of the current pulses.
The goal of this project is to control the 28BYJ-48 stepper motor to rotate forward and reverse at a specified angle based on the angle value input by the user. By flexibly adjusting the control signal of the stepper motor, precise rotation within different angle ranges can be achieved.
The following content will introduce the required hardware and software resources, including the basic characteristics of the STC89C52 microcontroller, the working principle of the 28BYJ-48 stepper motor, and the key steps in writing control programs.
2. Design process
【1】Hardware preparation:
- 51 microcontroller development board: Choose STC89C52 microcontroller development board.
- 28BYJ-48 stepper motor: one 28BYJ-48 stepper motor + ULN2003 driver board.
- Drive circuit: Use ULN2003 chip to drive stepper motor.
- Cable and power supply: Prepare the cable and power supply.
【2】Connection circuit:
- Connect the 51 microcontroller with the drive circuit and stepper motor.
【3】Write a program:
- Use the keil integrated development environment (IDE) to write the control program for the 51 microcontroller.
- Initialize the pin and port settings to configure the pins required to control the stepper motor.
- Write a function to control the forward and reverse rotation of a stepper motor.
- Write a function to control the stepper motor to rotate according to a specified angle.
【4】Control stepper motor rotation:
- In the main program, call the appropriate function to control the rotation of the stepper motor.
- Use a key input device to trigger the rotation of a stepper motor.
- Control the angle, speed and direction of rotation.
【5】Debugging and testing:
- Compile the program and download the generated executable file to the 51 microcontroller development board.
3. Code implementation
3.1 Motor forward and reverse control
The following is the implementation code for controlling the 28BYJ-48 stepper motor to achieve forward and reverse rotation through the STC89C52 microcontroller:
#include <reg52.h> #include <intrins.h> #define motorPort P1 //The control pin of the stepper motor is connected to the P1 port #define clockwise 0 // clockwise #define counterclockwise 1 // Counterclockwise // function declaration void delay(unsigned int time); void motorRotate(unsigned char direction, unsigned int steps); void main() {<!-- --> while (1) {<!-- --> // Forward rotation, execute a certain number of steps (here is 512 steps, which can be modified as needed) motorRotate(clockwise, 512); delay(1000); //Delay 1 second //Reverse, execute a certain number of steps motorRotate(counterclockwise, 256); delay(1000); //Delay 1 second } } //delay function void delay(unsigned int time) {<!-- --> unsigned int i, j; for (i = time; i > 0; i--) {<!-- --> for (j = 110; j > 0; j--); // Instruction cycle delay } } //Control the stepper motor rotation void motorRotate(unsigned char direction, unsigned int steps) {<!-- --> unsigned int i; unsigned char motorSequence[8] = {<!-- -->0x01, 0x03, 0x02, 0x06, 0x04, 0x0C, 0x08, 0x09}; // Stepper motor control sequence for (i = 0; i < steps; i + + ) {<!-- --> if (direction == clockwise) {<!-- --> motorPort = motorSequence[i % 8]; } else if (direction == counterclockwise) {<!-- --> motorPort = motorSequence[7 - (i % 8)]; } delay(2); //The delay between each step can be adjusted as needed } motorPort = 0x00; // Stop the motor }
The code uses the P1 port of the STC89C52 microcontroller to connect to the control pin of the 28BYJ-48 stepper motor. In the main
function, the forward and reverse functions are implemented through loops. The motorRotate
function is used to control the rotation direction and number of steps of the stepper motor, where clockwise
and counterclockwise
represent clockwise and counterclockwise directions respectively.
3.2 Angle rotation
The following code uses the STC89C52 microcontroller to control the 28BYJ-48 stepper motor to rotate forward and reverse at a specified angle, and the encapsulated sub-function is called.
#include <reg52.h> // Define the phase sequence of 28BYJ-48 stepper motor unsigned char stepSequence[8] = {<!-- -->0x01, 0x03, 0x02, 0x06, 0x04, 0x0C, 0x08, 0x09}; //Define the current position and angle of the stepper motor unsigned char currentPosition = 0; unsigned int currentAngle = 0; //delay function void delay(unsigned int time) {<!-- --> unsigned int i, j; for (i = 0; i < time; i + + ) {<!-- --> for (j = 0; j < 120; j + + ); } } //Stepper motor forward rotation function void stepForward(unsigned int angle) {<!-- --> unsigned int steps = angle / 5; // The rotation angle of each step is 5 degrees unsigned int i; for (i = 0; i < steps; i + + ) {<!-- --> currentPosition + + ; if (currentPosition >= 8) {<!-- --> currentPosition = 0; } P1 = stepSequence[currentPosition]; delay(10); // Control the stepper motor speed and adjust the delay time } currentAngle + = angle; } // Stepper motor reversal function void stepBackward(unsigned int angle) {<!-- --> unsigned int steps = angle / 5; // The rotation angle of each step is 5 degrees unsigned int i; for (i = 0; i < steps; i + + ) {<!-- --> if (currentPosition == 0) {<!-- --> currentPosition = 8; } currentPosition--; P1 = stepSequence[currentPosition]; delay(10); // Control the stepper motor speed and adjust the delay time } currentAngle -= angle; } // main function void main() {<!-- --> while (1) {<!-- --> // Rotate forward 180 degrees stepForward(180); delay(1000); // Pause for 1 second // Flip 90 degrees stepBackward(90); delay(1000); // Pause for 1 second } }
The code uses the P1 port of the STC89C52 microcontroller as the output port, and controls the rotation of the stepper motor by controlling the level of the P1 port output. The phase sequence of the stepper motor is stored in the stepSequence
array, and each element corresponds to a phase. The stepForward
function is used to realize the forward rotation of the stepper motor, and the stepBackward
function is used to realize the reverse rotation of the stepper motor. The delay
function is used to control the speed of the stepper motor, and the delay time can be adjusted as needed.
In the main function, the stepper motor’s forward rotation of 180 degrees and reverse rotation of 90 degrees are demonstrated.
3.3 Button control motor
There are 2 buttons, the one connected to P2 port 3 is low level when pressed. The following code adds 2 buttons to realize the functions of 2 buttons.
#include <reg52.h> #define motorPort P1 //The control pin of the stepper motor is connected to the P1 port #define clockwise 0 // clockwise #define counterclockwise 1 // Counterclockwise sbit startBtn = P2^0; // The start button is connected to the P2.0 port sbit stopBtn = P2^1; // The stop button is connected to the P2.1 port sbit cwBtn = P2^2; // The clockwise button is connected to the P2.2 port sbit ccwBtn = P2^3; // Counterclockwise button is connected to port P2.3 unsigned char motorSequence[8] = {<!-- -->0x01, 0x03, 0x02, 0x06, 0x04, 0x0C, 0x08, 0x09}; // Stepper motor control sequence bit motorRunning = 0; // Is the stepper motor running? unsigned int targetAngle = 0; // Target rotation angle, initially 0 bit clockwiseDirection = 1; //The default starting direction of the motor is clockwise // function declaration void delay(unsigned int time); void motorRotate(unsigned char direction, unsigned int steps); void main() {<!-- --> while (1) {<!-- --> if (startBtn == 0) // If the start button is pressed {<!-- --> while (startBtn == 0); // Wait for the button to be released if (!motorRunning) {<!-- --> motorRunning = 1; motorRotate(clockwiseDirection, targetAngle); // Start the motor } } if (stopBtn == 0) // If the stop button is pressed {<!-- --> while (stopBtn == 0); // Wait for the button to be released if(motorRunning) {<!-- --> motorRunning = 0; motorPort = 0x00; // Stop the motor } } if (cwBtn == 0) // If the clockwise button is pressed {<!-- --> while (cwBtn == 0); // Wait for the button to be released clockwiseDirection = 1; // Set the motor starting direction to clockwise } if (ccwBtn == 0) // If the counterclockwise button is pressed {<!-- --> while (ccwBtn == 0); // Wait for button release clockwiseDirection = 0; //Set the motor starting direction to counterclockwise } } } //delay function void delay(unsigned int time) {<!-- --> unsigned int i, j; for (i = time; i > 0; i--) {<!-- --> for (j = 110; j > 0; j--); // Instruction cycle delay } } //Control the stepper motor rotation void motorRotate(unsigned char direction, unsigned int steps) {<!-- --> unsigned int i; for (i = 0; i < steps; i + + ) {<!-- --> if (!motorRunning) break; if (direction == clockwise) {<!-- --> motorPort = motorSequence[i % 8]; } else if (direction == counterclockwise) {<!-- --> motorPort = motorSequence[7 - (i % 8)]; } delay(2); //The delay between each step can be adjusted as needed } motorPort = 0x00; // Stop the motor }
In the above code, two button pins cwBtn
and ccwBtn
are added and defined as P2^2
and P2^3
. When the clockwise button is pressed, set clockwiseDirection
to 1, indicating that the startup direction is clockwise; when the counterclockwise button is pressed, set clockwiseDirection
to 0, indicating the startup direction. is counterclockwise.