Intelligent Dual-Control System for Motors Based on 51 Microcontroller: Integrated Design of Stepper Closed-Loop PID and DC PWM Speed Control

In modern automation control systems, motors serve as the core actuators, and their control precision and response speed directly determine the performance of the system. The 51 microcontroller, due to its low cost, high stability, and abundant development resources, has become the preferred controller in many embedded projects. This article introduces a dual-motor control system design that integrates closed-loop PID speed control for stepper motors and PWM speed control for DC motors, combined with Hall speed detection, achieving high precision and high response intelligent control.

1. System Architecture Overview
This system is based on the STC89C52 microcontroller, constructing a dual-channel motor control platform:
• Channel One: Stepper Motor Closed-Loop PID Control
• Driver Chip: ULN2003
• Feedback Method: Encoder Feedback (Analog Closed-Loop)
• Control Algorithm: Incremental PID
• Channel Two: DC Motor PWM Speed Control
• Driving Method: H-Bridge (L298N)
• Speed Detection Method: Hall Sensor (Speed Wheel + Magnetic Piece)
• Speed Control Method: PWM Pulse Width Modulation
The system displays the current speed, target value, and PID parameters on an LCD1602, supporting button settings for target speed and mode switching.

2. Implementation of Stepper Motor Closed-Loop PID Control
Traditional stepper motors operate under open-loop control, which can easily lead to loss of steps. This design introduces encoder feedback to form a quasi-closed-loop system, dynamically adjusting the pulse output frequency using the PID algorithm to enhance control precision.
1. PID Algorithm Selection: Incremental PID is suitable for resource-constrained 51 microcontrollers, avoiding integral saturation and requiring minimal computation:

typedef struct<br/>{<br/>    float Kp, Ki, Kd;<br/>    float error, error_last, error_sum;<br/>    float output;<br/>} PID_TypeDef;<br/>float PID_Calculate(PID_TypeDef *pid, float setpoint, float feedback)<br/>{<br/>    pid->error = setpoint - feedback;<br/>    pid->error_sum += pid->error;<br/>    // Incremental output<br/>    float output_inc = pid->Kp * (pid->error - pid->error_last)<br/>                     + pid->Ki * pid->error<br/>                     + pid->Kd * (pid->error - 2*pid->error_last + pid->error_last2);<br/>    pid->output += output_inc;<br/>    pid->error_last2 = pid->error_last;<br/>    pid->error_last = pid->error;<br/>    return pid->output;<br/>}

2. Closed-Loop Process
• The encoder outputs N pulses per revolution, and timer T1 counts to calculate the actual speed.
• In the main loop, the PID calculation is called, and the output value is mapped to the delay time (controlling the step pulse interval).
• Supports integral clamping and integral separation optimization to prevent overshoot at startup.

#define INTEGRAL_MAX 100.0<br/>if (fabs(pid.error) > 50)<br/>{<br/>    pid.error_sum = 0;  // Integral separation<br/>}<br/>else {<br/>    if (pid.error_sum > INTEGRAL_MAX) pid.error_sum = INTEGRAL_MAX;<br/>}

3. DC Motor PWM Speed Control and Hall Speed Detection
DC motors achieve stepless speed control by adjusting the average voltage through PWM. A Hall sensor (e.g., A3144) is used to detect speed, forming a closed-loop feedback.
1. Hardware Connections
• P2.0 → L298N IN1 (Direction)
• P2.1 → L298N IN2 (Direction)
• P2.5 → L298N ENA (PWM Input)
• P3.2 (INT0) → Hall Sensor Output (External Interrupt)
2. PWM Generation
Using timer T0 to generate PWM waves with adjustable duty cycle:

unsigned char pwm_duty = 50;  // Duty cycle 0~100<br/>void Timer0_Init()<br/>{<br/>    TMOD |= 0x01;<br/>    TH0 = (65536 - 100)/256;<br/>    TL0 = (65536 - 100)%256;<br/>    ET0 = 1; TR0 = 1;<br/>}<br/>void Timer0_ISR() interrupt 1<br/>{<br/>    static unsigned char cnt = 0;<br/>    cnt++;<br/>    if (cnt < pwm_duty) MOTOR_PWM = 1;<br/>    else MOTOR_PWM = 0;<br/>    if (cnt >= 100) cnt = 0;<br/>}

3. Hall Speed Detection
Using external interrupt INT0 to capture pulses, timer T1 counts the number of pulses within one second to calculate RPM:

volatile unsigned int pulse_count = 0;<br/>void INT0_ISR() interrupt 0<br/>{<br/>    pulse_count++;<br/>}<br/>// In the main loop, read once per second<br/>speed_rpm = pulse_count * 60 / PULSES_PER_REV;  // Convert to RPM<br/>pulse_count = 0;

4. System Integration and Optimization
• Mode Switching: K1 switches between stepper/DC control, K2/K3 sets target speed.
• LCD Display: Real-time display of target/actual speed and PID parameters.
• Safety Protection: Introduce a delay before motor direction change to prevent H-bridge short circuit.
• Anti-Interference Design: Button debouncing, opto-isolation, power filtering.

5. Application Prospects
This system can be applied in scenarios such as intelligent vehicles, 3D printer Z-axis, and automated assembly lines, balancing precision and cost. Future expansions may include Bluetooth/Wi-Fi remote control to achieve IoT capabilities.

Leave a Comment