Method for accurately outputting pulse number by MCU (Microprogrammed Control Unit)
Through the MCU combined with the timer and EtherCAT bus protocol, the accurate number of pulses is calculated and output, which solves the problems of low pulse frequency and poor synchronization of the MCU chip output in the prior art, and achieves high-precision, stable and unstoppable pulse output, reducing production costs and ensuring the stable operation of the motor.
Patent Information
- Application Number
- CN202510018944.X
- Authority / Receiving Office
- CN · China
- Patent Type
- Applications(China)
- Current Assignee / Owner
- Filing Date
- 2025-01-07
- Publication Date
- 2025-04-18
AI Technical Summary
In the prior art, the pulse frequency output by the MCU chip is low and the output pulse synchronization is poor, resulting in discontinuous output pulses during speed change, causing jitter; the output pulse number is inaccurate when stopped, resulting in overshoot of the motor. At the same time, the field programmable gate array (FPGA) chip development threshold is high and the cost is high.
Through the MCU combined with the timer and synchronous clock bus protocol, the position increment value and motion direction of each cycle are calculated, the pulse frequency to be output and the period value and duty cycle value of the timer are calculated, and the accurate output number of pulses is achieved. Real-time synchronization is performed using the EtherCAT bus protocol to ensure the accuracy and stability of pulse output.
It achieves high accuracy, stability and unstoppable pulse output, high frequency change responsiveness, reduces production costs, and ensures stable operation of the motor.
Smart Images

Figure CN119853646A_ABST
Abstract
Description
Technical Field
[0001] The invention relates to the technical field of industrial control, in particular to a method for an MCU to accurately output the number of pulses. Background Art
[0002] There are two main pulse output methods used in motor control on the market: One method is to use a field programmable gate array (FPGA) chip to output pulses, but the development method of this chip has the disadvantages of high threshold, high difficulty, large chip area, complex peripheral circuits and high cost.
[0003] The other is developed using MCU chip, but its output pulse frequency is low and the output pulse synchronization is poor. Therefore, the discontinuous output pulse will cause motor jitter when changing speed, and the inaccurate output pulse number will cause motor overshoot when stopping.
[0004] In order to solve the above problems, the present invention designs a method for MCU to accurately output the number of pulses. By combining MCU with a timer and a synchronous clock bus protocol, the current position, the position increment value and the movement direction of each cycle are calculated, thereby achieving a pulse effect with high output accuracy and stable without pause. Summary of the invention
[0005] The purpose of the present invention is to overcome the shortcomings of the prior art and provide a method for an MCU to accurately output the number of pulses. By combining the MCU with a timer and a synchronous clock bus protocol, the current position, the position increment value and the movement direction of each cycle are calculated, thereby achieving a pulse effect with high output accuracy and stable without pause.
[0006] In order to achieve the above object, the present invention provides a method for MCU to accurately output the number of pulses, comprising the following steps: S1, configure the timer to work in PWM mode for output pulses; S2, configure DMA to transfer cache array data to the timer TIMER_CAR register for cyclic transfer function; S3, configure timer channel 1 to trigger DMA to transfer data, and configure IO pin output to switch the movement direction; S4, EtherCAT bus protocol using real-time synchronous clock technology, used to synchronize the master station cycle with the slave station cycle; S5, the slave station receives the target position sent by the master station, and the MCU calculates the position increment value and movement direction of each cycle based on the current position; S6, calculate the frequency of the required output pulse according to the increment value calculated by the MCU; S7, calculating the count period value Arr and the duty cycle value Duty required by the timer according to the frequency of the output pulse required; S8, by comparing and adopting the smallest value of the duty cycle value Duty in the latest 4 cycles, is used to prevent the cycle value from being less than the frequency value when the frequency increases, thereby avoiding output pulse waveform errors; S9, stop the timer, clear the counter value, and configure the duty cycle; put the count cycle value Arr value into the DMA cache array in order, and execute a timer event update separately to trigger a DMA data transfer to the timer count cycle value Arr; output the direction signal according to the calculated movement direction, start the timer for pulse output, and the pre-configured DMA function will transfer the data of the cache array to the register of the timer count cycle value Arr one by one; S10, when a new master station cycle arrives, the current target position minus the target value of the previous cycle is used to obtain the incremental value; S11, when the increment value in S10 is not zero and the direction of movement is unchanged; then steps S6 to S8 are executed cyclically and then the duty cycle is updated to the timer; S12, when the increment value in S10 is zero, first set the period value to X, where 0<X<minimum duty cycle value, and fill it into the DMA buffer array to stop the timer from outputting pulses; then set the period value to 0, and fill it into the DMA buffer array to stop DMA transmission; wait for the timer to finish outputting pulses, and clear the DMA transmission count value; S13, when the incremental value in S10 is not zero and the movement direction changes, first execute S2 to stop the pulse output, and then execute S6 to S9 to complete the pulse output of the steering direction.
[0007] Compared with the prior art, the present invention has the following beneficial effects: The present invention sends the target position to the slave station in real time through the EtherCAT communication protocol. The slave station calculates the increment according to the target position and the actual position, calculates the corresponding pulse frequency according to the increment value, and then calculates the period value and duty cycle value of the timer, assigns the value to the timer, and when the next synchronization signal arrives, the timer outputs a specified number of pulses. The present invention is applicable to bus protocols using real-time synchronous clock technology, and has a simple system structure, low design cost, non-stop output pulses, high precision, and high frequency change responsiveness. BRIEF DESCRIPTION OF THE DRAWINGS
[0008] Figure 1 Schematic diagram of the method of the present invention. DETAILED DESCRIPTION
[0009] The present invention will now be further described with reference to the accompanying drawings.
[0010] See also Figure 1The present invention provides a method for accurately outputting the number of pulses of an MCU, comprising the following steps: S1, configure the timer to work in PWM mode for output pulses; S2, configure DMA to transfer cache array data to the timer TIMER_CAR register for cyclic transfer function; S3, configure timer channel 1 to trigger DMA to transfer data, and configure IO pin output to switch the movement direction; S4, EtherCAT bus protocol using real-time synchronous clock technology, used to synchronize the master station cycle with the slave station cycle; S5, the slave station receives the target position sent by the master station, and the MCU calculates the position increment value and movement direction of each cycle based on the current position; S6, calculate the frequency of the required output pulse according to the increment value calculated by the MCU; S7, calculating the count period value Arr and the duty cycle value Duty required by the timer according to the frequency of the output pulse required; S8, by comparing and adopting the smallest value of the duty cycle value Duty in the latest 4 cycles, is used to prevent the cycle value from being smaller than the frequency value when the frequency increases, thereby avoiding output pulse waveform errors; S9, stop the timer, clear the counter value, and configure the duty cycle; put the count cycle value Arr value into the DMA cache array in order, and execute a timer event update separately to trigger a DMA data transfer to the timer count cycle value Arr; output the direction signal according to the calculated movement direction, start the timer for pulse output, and the pre-configured DMA function will transfer the data of the cache array to the register of the timer count cycle value Arr one by one; S10, when a new master station cycle arrives, the current target position minus the target value of the previous cycle is used to obtain the incremental value; S11, when the increment value in S10 is not zero and the direction of movement is unchanged; then steps S6 to S8 are executed cyclically and then the duty cycle is updated to the timer; S12, when the increment value in S10 is zero, first set the period value to X, where 0<X<minimum duty cycle value, and fill it into the DMA buffer array to stop the timer from outputting pulses; then set the period value to 0, and fill it into the DMA buffer array to stop DMA transmission; wait for the timer to finish outputting pulses, and clear the DMA transmission count value; S13, when the incremental value in S10 is not zero and the movement direction changes, first execute S2 to stop the pulse output, and then execute S6 to S9 to complete the pulse output of the steering direction.
[0011] The above are only preferred embodiments of the present invention, which are only used to help understand the method and core ideas of the present application. The protection scope of the present invention is not limited to the above embodiments. All technical solutions under the concept of the present invention belong to the protection scope of the present invention. It should be pointed out that for ordinary technicians in this technical field, some improvements and modifications without departing from the principle of the present invention should also be regarded as the protection scope of the present invention.
[0012] The present invention solves the shortcomings of the prior art as a whole, such as low pulse frequency and poor synchronization of output pulses due to chip output, discontinuous output pulses during speed change, which causes motor jitter, inaccurate output pulse number during stop, which causes motor overshoot, large chip footprint, complex peripheral circuits, and high cost. After the target position is sent in real time through the EtherCAT communication protocol, the target and actual position increments and the corresponding frequency as well as the period value and duty cycle value of the timer are calculated. When the next synchronization signal arrives, the timer outputs a specified number of pulses. The system structure of the present invention is simple, the design cost is low, the output pulses are continuous, the precision is high, and the frequency change responsiveness is high, which reduces the production cost while ensuring the stable operation of the motor.
Claims
1. A method for accurately outputting the number of pulses of an MCU, characterized in that: The following steps are involved: S1, configure the timer to work in PWM mode for output pulses; S2, configure DMA to transfer cache array data to the timer TIMER_CAR register for cyclic transfer function; S3, configure timer channel 1 to trigger DMA to transfer data, and configure IO pin output to switch the movement direction; S4, EtherCAT bus protocol using real-time synchronous clock technology, used to synchronize the master station cycle with the slave station cycle; S5, the slave station receives the target position sent by the master station, and the MCU calculates the position increment value and movement direction of each cycle based on the current position; S6, calculate the frequency of the required output pulse according to the increment value calculated by the MCU; S7, calculating the count period value Arr and the duty cycle value Duty required by the timer according to the frequency of the output pulse required; S8, by comparing and adopting the smallest value of the duty cycle value Duty in the latest 4 cycles, is used to prevent the cycle value from being smaller than the frequency value when the frequency increases, thereby avoiding output pulse waveform errors; S9, stop the timer, clear the counter value, and configure the duty cycle; put the count cycle value Arr into the DMA cache array in sequence, and execute a timer event update separately to trigger a DMA data transfer to the timer count cycle value Arr; Output direction signal according to the calculated movement direction, start timer for pulse output, and the pre-configured DMA function will transfer the data of the cache array to the register of the count cycle value Arr of the timer one by one; S10, when a new master station cycle arrives, the current target position minus the target value of the previous cycle is used to obtain the incremental value; S11, when the increment value in S10 is not zero and the movement direction is unchanged; then loop through steps S6 to S8 and then update the duty cycle Duty to the timer; S12, when the increment value in S10 is zero, first set the period value Arr to X, where 0<X<minimum duty cycle value Duty, and fill it into the DMA buffer array to stop the timer output pulse; then set the period value Arr to 0, and fill it into the DMA buffer array to stop DMA transmission; Wait for the timer to output the completion pulse and clear the DMA transfer count value; S13, when the incremental value in S10 is not zero and the movement direction changes, first execute S2 to stop the pulse output, and then execute S6 to S9 to complete the pulse output of the turning direction.