Implementation of a DC motor closed-loop speed monitoring system
2026-04-06 04:29:34··#1
1. Introduction DC motor monitoring systems are a crucial component of electromechanical products, and their control performance reflects the control quality of the equipment. Flexible, convenient, accurate, and real-time monitoring requires measuring and processing the motor's speed signal to achieve precise speed control. 2. System Overall Design While ARM/DSP/FPGA systems offer high precision and speed, their design is complex, and their price remains high. This system utilizes Salvo, a powerful RTOS suitable for microcontrollers with small memory capacities (such as the PIC series). It eliminates the need for extensive RAM and ROM expansion and offers excellent real-time performance, significantly reducing costs. The system uses a PC as the host computer, employing API functions and the MSCOMM control for computer communication. The PIC16F877A microcontroller and peripheral circuits form a microcontroller system, serving as the slave computer. The circuit design includes PWM drive, CCP capture, A/D conversion, LCD display, RS232 and SPI serial communication, and a dual-machine communication module for the slave computer. The overall system block diagram is shown in Figure 1. 3. Introduction to the Salvo System The Salvo embedded real-time operating system is characterized by its low system resource consumption and powerful functionality. 3.1 System Resource Consumption of Salvo Salvo's biggest advantage is its low system resource consumption, especially its minimal memory usage. It is suitable not only for 51 series microcontrollers but also for PIC series microcontrollers with even less memory. Salvo's ROM resource consumption depends on the system functions called by the user, while RAM consumption depends on the number of user-defined variables, tasks, and events. Taking the PIC16 series microcontroller as an example, each global variable occupies 10 bytes, a task occupies 5 bytes, and an event occupies 3 bytes. 3.2 Salvo Functionality and Performance Salvo is a priority-based, event-driven, multi-tasking embedded real-time operating system. Salvo supports 16 task priorities, and multiple tasks can share a single priority. Tasks are switched according to priority, and multiple tasks with the same priority are switched in a round-robin fashion. The number of tasks and events supported by Salvo depends on the RAM size (Salvo supports 255 tasks, 255 events, and 255 message queues by default). The Salvo kernel primarily provides the following types of functions for user applications: Task Management: provides task creation and cancellation functions; Time Management: provides task delay functions; Signal Management: provides semaphore creation, deletion, waiting, and sending functions; Message Management: provides message creation, deletion, waiting, and sending functions. 4. System Hardware Overview The communication section mainly consists of two parts: communication between the microcontroller and the PC, and communication between the PIC microcontroller and the PC. Data transfer between the PC and the lower-level microcontroller uses RS232 serial asynchronous communication. Communication between the PIC microcontrollers uses the SPI bus, a 4-wire interface mainly used for synchronous serial communication between the processor and peripherals. A synchronous clock is used to shift serial data into and out of the microcontroller in 8-bit code form. The SPI bus is a master/slave interface, with the master interface driving the serial clock. When using SPI, data is sent and received simultaneously, making it a full-duplex protocol with high communication efficiency and simple circuit wiring and software programming structure. The hardware circuit is shown in Figures 2 and 3. Its unit circuits include a CCP capture circuit, a PWM drive circuit, an RS232/SPI communication module, an A/D conversion algorithm, and an LCD display. 4.1 The CCP capture circuit uses photoelectric acquisition modules with high conversion accuracy, a good static operating point, a complete analog front-end, and on-chip calibration, making it particularly suitable for measuring low-frequency small signals. This circuit converts the optical signal into a voltage signal, which, after shaping and filtering, can be directly sent to the microcontroller. The rotational speed signal sent to the microcontroller can be measured by the CCP capture module. In this mode, to use the capture function, the corresponding TRIS should be set to 1, and the microcontroller's CCPx pin should be set as an input. For example, when the input frequency is stable, if the microcontroller's prescaler value is set to 1:16, the total error for these 16 cycles is 1 TCY. Its effective resolution is TCY/16, that is, an effective resolution of 6.25 ns at 40 MHz. This method is only effective when the input frequency is stable within 16 sampling periods. Without using a prescaler (1:1), each sample resolution is TCY. Furthermore, a capture interrupt is generated when the capture mode is changed. Users should keep the CCPxIE bit cleared to disable this interrupt and should also clear the CCPxIF bit after changing the operating mode. The entire capture process is quick and simple. 4.2 PWM Drive Module: The PWM signal generated by the PIC16F877A microcontroller is connected to the enable control terminal of the L298N via an optocoupler. Considering that the TLP521-4 circuit acts as an inverter, the high-level duration in the PWM signal corresponds to the power-off time of the DC motor. Signals output from other pins of the microcontroller are sent to the direction control terminal of the L298N to control the rotation direction and braking state of the DC motor. The maximum power supply voltage for the L298N driving the load is 46 V, the single-bridge drive power supply is close to 2A, and the maximum switching frequency is not less than 40 kHz. Compared with bridge drive circuits composed of discrete components, this circuit has a simple structure and reliable performance. By rewriting the program, multiple speed variations of a small DC motor can be controlled. A photoelectric encoder is used to detect motor speed, forming a complete closed-loop control system. 4.3 RS232 Serial Communication Module: The PIC16F877A serial port only occupies the RC6 and RC7 pins of the microcontroller, used for the receiver (RXD) and transmitter (TXD). Therefore, when the host computer communicates with the lower-level microcontroller, only the three wires (TX, RX) and ground (GND) need to be connected. The microcontroller's serial interface level is TTL, which is inconsistent with the computer's serial interface level, thus requiring level conversion. The system uses the Maxim MAX232 universal serial receiver/transmitter driver, whose peripheral circuitry is simple, requiring only four external 0.1μF capacitors. 4.4 LCD Display Module: The SMC1602B module consists of a dot-matrix LCD screen, a controller HD44780, and its auxiliary circuitry. This system design uses the OCMJ Chinese module system LCD as the lower-level display module. This module contains GB2312 16×16 dot matrix simplified Chinese characters (Level 1) and ASCII 8×8 (half-height) and 8×16 (full-height) dot matrix English character libraries. Users can input zone codes or ASCII codes to display text. The OCMJ Chinese LCD display module uses the ASK/ANSWER handshake method. 5 System Software Overview 5.1 Lower-Level Software Design The lower-level software design of the intelligent motor monitoring system mainly completes multiple functions such as CCP capture, A/D acquisition, PWM control, SPI communication, LCD display, and RS232 communication. The overall flowchart is shown in Figure 4. The control algorithm uses the PIC16F877A to directly control the PID parameters to achieve precise control. 5.2 Host Computer System Design The host computer system software was developed on the Windows platform using Visual Basic 6.0. The design includes an operation prompt section (automatic sending, manual sending, serial port selection, parity check, baud rate, data bits, etc.), a receiving and display section (received data bits, motor speed, motor forward/reverse rotation, etc.), and a sending content section (motor speed setting, overvoltage, overcurrent protection values, etc.). 6 Monitoring When the motor is working, the host and slave computers communicate in real time, displaying parameter values with good real-time performance. At this time, the motor is in reverse rotation (forward rotation "1", reverse rotation "0"). This can be seen from the forward/reverse rotation setting indicator on the host computer interface, and also from the parameters fed back by the slave computer, which also displays the motor's forward/reverse rotation. The slave computer's LCD panel also displays speed, voltage, current, and forward/reverse rotation (forward rotation "Right", reverse rotation "Left"), and the data display between the host and slave computers is synchronized. Meanwhile, overcurrent and overvoltage protection values can be set on the lower-level machine via the host computer. Once the circuit detects any overcurrent or overvoltage (exceeding the set value), the lower-level control unit will issue an alarm signal (red light illuminates, buzzer sounds, error flag appears on the host computer, and error message appears on the lower-level machine's LCD, etc.) and automatically shut down for protection, awaiting maintenance. 7. Conclusion This system successfully realizes real-time monitoring of the DC motor closed-loop speed system, simplifies the control logic system, and is lower in cost, more functional, and has better anti-interference performance compared to similar products. It has been successfully applied to DC motor speed regulation, monitoring, and protection applications, and the host computer interface is user-friendly and easy to use. The PC can perform real-time monitoring of the DC motor, which not only greatly improves the stability during high-speed operation but also realizes the protection function.