Pulse output circuit, pulse output method and microcontroller unit

By using a pulse output circuit with a single timer and dual-channel logic gates, the problems of insufficient hardware resources and high cost for multi-channel pulse output in industrial automation control are solved. This achieves low-cost, highly versatile quantitative pulse output and improves the determinism of pulse cutoff and signal accuracy.

CN122870127BActive Publication Date: 2026-11-17SHENZHEN CITY SAMKOON TECH
View PDF 2 Cites 0 Cited by

Patent Information

Application Number
CN202611356806.3
Authority / Receiving Office
CN · China
Patent Type
Patents(China)
Current Assignee / Owner
Filing Date
2026-09-03
Publication Date
2026-11-17
Estimated Expiration
2046-09-03

AI Technical Summary

Technical Problem

In industrial automation control, existing technologies suffer from insufficient hardware resources, high costs, signal delays, and poor portability when outputting quantitative pulses. In particular, when multiple pulse outputs are required, existing solutions struggle to achieve a balance between low cost and high versatility.

Method used

A pulse output circuit using a single timer with dual channels and logic gates is adopted. Pulse output is achieved through a synchronous control unit and a logic operation module, eliminating the dependence on the hardware internal linkage channel. The logic operation module directly drives the pulse to cut off, reducing the timer resource consumption.

Benefits of technology

It reduces material costs and printed circuit board layout area, improves the portability and platform versatility of the solution, ensures the determinism and accuracy of pulse cutoff, and reduces signal delay.

✦ Generated by Eureka AI based on patent content.

Smart Images

  • Figure CN122870127B_ABST
    Figure CN122870127B_ABST
Patent Text Reader

Abstract

This invention discloses a pulse output circuit, a pulse output method, and a microcontroller unit. The pulse output circuit includes a timer and a logic operation module. The timer includes a first timer channel, a second timer channel, and a synchronization control unit. The first and second timer channels are driven by the system clock for counting, and the synchronization control unit controls them to start counting synchronously. The first timer channel counts cyclically according to a first channel target period and outputs a toggling level. The second timer channel outputs a second valid level after synchronous start, and after a second target duration interval, it toggles to a second invalid level and remains there. The second target duration interval is greater than the first channel target period. The logic operation module performs a logical AND operation on the outputs of the two channels and outputs the result to the target load device. This invention achieves hardware-level automatic cutoff through a single timer with dual channels and logic gates, reducing dependence on internal linkage channel resources and improving versatility and portability.
Need to check novelty before this filing date? Find Prior Art

Description

Technical Field

[0001] This invention relates to the field of industrial automation control technology, and in particular to pulse output circuits, pulse output methods, and microcontroller units. Background Technology

[0002] Currently, in the field of industrial automation control, pulse signals are widely used to drive actuators such as stepper motors, servo drivers, and proportional valves to achieve precise control of position, speed, and flow. Quantitative pulse transmission, that is, outputting a set number of pulse signals within a specified time window, is one of the most basic and frequent operational requirements in the above applications.

[0003] In related technologies, a common hardware implementation for quantitative pulse output involves utilizing two timer peripherals within the MCU (Microcontroller Unit) to work in concert: the first timer is configured in pulse width modulation mode, cyclically outputting a square wave signal at the target frequency as the pulse source; the second timer is configured in external clock counting mode, using the pulse edge output by the first timer as its clock source for hardware increment counting. When the count value of the second timer reaches a preset number, a stop control signal is sent to the first timer through the hardware interlocking trigger channel between the two timers to shut down the pulse output; alternatively, an interrupt is triggered by a comparison match event of the second timer, and the interrupt service routine shuts down the first timer via software.

[0004] However, the hardware interconnection channel between two timers is usually not fully interconnected within the MCU; only a few specific timers support this master-slave linkage function, resulting in a very limited number of available channels. When the required number of pulse output channels exceeds the number of available interconnection channels, existing technologies necessitate adding an external auxiliary control circuit on the printed circuit board to control the shutdown of the first timer via the second timer. This not only increases material costs and PCB layout area but also introduces additional signal transmission delays and signal integrity risks, while reducing the portability and platform versatility of the solution. Summary of the Invention

[0005] This invention aims to address at least one of the technical problems existing in the prior art. To this end, this invention proposes a pulse output circuit that achieves hardware-level automatic cutoff through a single timer with dual channels and logic gates. This reduces reliance on the scarce internal linkage channel resources within the chip, which helps reduce material costs and PCB layout area, and improves the versatility and portability of the solution across different microcontroller platforms.

[0006] This invention also proposes a pulse output method.

[0007] The present invention also proposes a microcontroller unit.

[0008] A first aspect of the present invention provides a pulse output circuit, which includes a timer and a logic operation module. The timer includes a first timer channel, a second timer channel, and a synchronization control unit. The synchronization control unit is coupled to both the first and second timer channels and is used to control them to start counting synchronously. The first and second timer channels are configured to count driven by the system clock. The first timer channel is configured to count cyclically at a first channel target period and output a first valid level and a first invalid level that toggle at a first target duration interval within each first channel target period. The second timer channel is configured to output a second valid level after starting counting synchronously with the first timer channel, and toggle to a second invalid level after a second target duration interval from the start of synchronous counting, and maintain the second invalid level after the toggle. A logic operation module is coupled to both the first and second timer channels and is used to output a pulse valid level when the first timer channel outputs the first valid level and the second timer channel outputs the second valid level, and to output a pulse invalid level when the first timer channel outputs the first invalid level or the second timer channel outputs the second invalid level. The logic output terminal of the logic operation module is used to electrically connect to a target load device. The second target duration interval is greater than the first channel target period.

[0009] Therefore, the pulse output circuit of this embodiment has the following technical effects: The pulse cutoff action is directly driven by the output toggle of the second timer channel through the logic operation module, without waiting for interrupt response and interrupt service routine execution. Therefore, the cutoff delay mainly depends on the propagation delay of the logic gate circuit, resulting in higher determinism in the cutoff response compared to schemes relying on software interrupts. The synchronization control unit controls the two channels to count synchronously, establishing a more accurate mapping between the output pulse count of the first timer channel and the count value of the second timer channel, reducing the likelihood of errors caused by asynchronous counting. The configuration of the second target duration interval being greater than the target period of the first channel ensures that the toggle time of the second timer channel is necessarily later than the time when the first timer channel completes a full pulse cycle. This guarantees at the hardware level that the pulse cutoff will not result in the effective level width of the last pulse being truncated due to insufficient timing. Compared to existing technologies that require an additional timer as a counter and to coordinate with the pulse output timer through an inline linkage channel, this invention only occupies one timer resource in each independent pulse output channel, reducing the number of timers required. Since it does not rely on hardware inter-channel linkage between timers, it is less restricted by the scarcity of inter-channel linkage in the microcontroller. In application scenarios that require multiple pulse outputs, there is no need to occupy a second timer for each pulse, nor is there a need to connect external gating logic chips on the printed circuit board to realize the linkage shutdown between timers due to insufficient available inter-channel linkage. This helps to reduce material costs and printed circuit board layout area, thereby improving portability and platform versatility.

[0010] A second aspect of the present invention provides a pulse output method, applied to the pulse output circuit of the first aspect of the present invention, comprising: Obtain the inherent count limit value of the second timer channel, as well as the load target pulse setting number, load target pulse period, load level detection threshold, and load inherent response delay of the target load device; The first channel target period of the first timer channel is determined based on the load target pulse period; Based on the load level detection threshold, the first target duration interval of the first timer channel is determined; The upper limit of the number of pulses sent in a single pulse is determined based on the inherent upper limit of the count and the inherent response delay of the load. Get the actual number of pulses sent; The actual set number of pulses to be sent in this instance is determined based on the upper limit of the number of pulses to be sent in a single instance, the actual number of pulses already sent, and the set number of target pulses for the load. Based on the actual number of pulses sent in this transmission, determine the second target duration interval of the second timer channel; Configure the first channel target period and the first target duration interval to the first timer channel; Configure the second target duration interval to the second timer channel; After configuring the first timer channel and the second timer channel, a start command is sent to the synchronization control unit so that the logic output terminal of the logic operation module outputs a pulse valid level or a pulse invalid level to the target load device.

[0011] A third aspect of the present invention provides a microcontroller, which includes a processor, a pulse output circuit, and a bus. The pulse output circuit is the output circuit of the first aspect of the present invention. The bus is used to couple the processor to the pulse output circuit; a system clock is used to drive a first timer channel and a second timer channel of the pulse output circuit to count. The processor is configured to execute the method described in the second aspect of the present invention.

[0012] Additional aspects and advantages of the invention will be set forth in part in the description which follows, and in part will be obvious from the description, or may be learned by practice of the invention. Attached Figure Description

[0013] The present invention will be further described below with reference to the accompanying drawings and embodiments, wherein: Figure 1 This is a schematic diagram of the overall structure of the microcontroller according to an embodiment of the present invention; Figure 2 This is a timing diagram of the output of the first timer channel, the second timer channel, and the logic operation module in an embodiment of the present invention; Figure 3 This is a schematic flowchart of a pulse output method according to some embodiments of the present invention; Figure 4 for Figure 3 The flowchart shown illustrates the process of obtaining the actual number of transmitted pulses in the method described. Figure 5 for Figure 3 The flowchart shown illustrates the process of determining the actual number of pulses sent in this operation. Figure 6 for Figure 5 The flowchart shown illustrates the subsequent processing when the remaining quantity exceeds the upper limit. Figure 7 for Figure 3 The flowchart shown illustrates the process for determining the upper limit of the number of pulses sent in a single pulse. Figure 8 for Figure 3 The flowchart shown illustrates the process of determining the second target duration interval in the method described. Detailed Implementation

[0014] Embodiments of the present invention are described in detail below. Examples of these embodiments are shown in the accompanying drawings, wherein the same or similar reference numerals denote the same or similar elements or elements having the same or similar functions throughout. The embodiments described below with reference to the accompanying drawings are exemplary and are only used to explain the present invention, and should not be construed as limiting the present invention.

[0015] In the description of this invention, "several" means one or more, "multiple" means two or more, "greater than," "less than," and "exceeding" are understood to exclude the stated number, while "above," "below," and "within" are understood to include the stated number. The use of "first" and "second" in the description is merely for distinguishing technical features and should not be construed as indicating or implying relative importance, or implicitly indicating the number of indicated technical features, or implicitly indicating the order of the indicated technical features.

[0016] In the description of this invention, unless otherwise explicitly defined, terms such as "set up," "install," and "connect" should be interpreted broadly, and those skilled in the art can reasonably determine the specific meaning of the above terms in this invention in conjunction with the specific content of the technical solution.

[0017] In the description of this invention, the terms "one embodiment," "some embodiments," "illustrative embodiment," "example," "specific example," or "some examples," etc., refer to specific features, structures, materials, or characteristics described in connection with that embodiment or example, which are included in at least one embodiment or example of the invention. In this specification, the illustrative expressions of the above terms do not necessarily refer to the same embodiment or example. Furthermore, the specific features, structures, materials, or characteristics described may be combined in any suitable manner in one or more embodiments or examples.

[0018] Currently, the technical solutions for achieving quantitative pulse transmission can be mainly divided into the following three categories.

[0019] I. Solution based on MCU software interrupts In this scheme, the system utilizes a timer in the MCU to generate pulse width modulation (PWM) pulse outputs while simultaneously enabling timer interrupts. For each pulse sent, the MCU enters an interrupt service routine, incrementing a counter value and comparing it to a preset target value. Once the target value is reached, the timer output is disabled in the interrupt. This scheme is simple to implement and requires little code, but its drawback is that the interrupt frequency increases linearly with the pulse frequency and the number of pulse channels. When the pulse frequency reaches 1MHz, the MCU needs to process 1 million interrupts per second, while the interrupt response of mainstream MCUs requires at least tens of instruction cycles. Such a high interrupt frequency can cause the system to fall into an "interrupt storm," preventing the execution of other tasks. The pulse sending, counting, and cutoff functions themselves may also malfunction. Therefore, the frequency upper limit of this scheme is typically limited to tens of kHz, which cannot meet the requirements of modern high-speed multi-channel control.

[0020] II. Hardware Counter-Based Solution To reduce interrupt frequency, an improved solution employs two timers working in tandem: the first timer operates in PWM mode for pulse output, while the second timer is configured in gated counting mode to perform pulse counting at the hardware level. When the count value reaches a preset value, pulse output is automatically stopped via hardware gating. Alternatively, some solutions use a separate hardware counter chip or a dedicated pulse counting module built into the MCU (such as a PCNT module) to implement hardware counting. The drawbacks of these solutions are: in the two-timer linkage scheme, each independent pulse requires two timer peripherals. When the PLC product requires 6 to 12 independent pulse channels, the cost of the MCU chip that can provide the corresponding number of timers is high; the separate counter chip solution requires additional chip and PCB area, and the MCU still needs to read the count value via the I2C bus, resulting in communication latency and making it difficult to meet microsecond-level real-time control requirements; the MCU's built-in PCNT module offers good real-time performance, but this module is typically designed for counting external input signals, and using it for counting the transmission of the main pulse would reduce the number of external counting functions. In short, hardware counting solutions struggle to achieve a good balance between real-time performance and cost.

[0021] III. CPLD / FPGA-based solutions In applications with the highest requirements for real-time performance and multi-channel operation, engineers use CPLDs (Complex Programmable Logic Devices) or FPGAs (Field-Programmable Gate Arrays) to implement pulse generation and counting functions. The pulse generation and counter modules are designed using hardware description languages ​​and directly connected via internal signal lines. The counter monitors the number of pulses output in real time, and immediately clears the enable signal of the pulse generation module through combinational logic when a preset value is reached. Alternatively, some high-end CNC systems employ a heterogeneous architecture of DSP+FPGA, where the DSP handles trajectory planning and the FPGA handles pulse generation and counting. The drawbacks of this approach are: the need for additional CPLD / FPGA chips, significantly increasing hardware and development costs, extending the development cycle, requiring specialized hardware description language design skills, and increasing system power consumption and PCB area. While this solution offers the best performance, its high cost and development complexity make it unsuitable.

[0022] In summary, existing technologies exhibit the following drawbacks: the first type of solution (software interrupt) suffers from computational overload and system crashes at high frequencies, resulting in a low frequency limit and inability to support multiple outputs; the second type of solution (hardware counting) struggles to balance real-time performance with cost, low-cost solutions (dual timer linkage) lack timer resources for multi-output needs, and dedicated counting chip solutions suffer from communication delays; the third type of solution (CPLD / FPGA) is characterized by high cost and development difficulty. Therefore, achieving real-time counting and automatic stopping of high-frequency pulses while maintaining low cost and high versatility remains a pressing technical challenge in this field.

[0023] The following is combined Figures 1-8 A pulse output circuit, pulse output method, and microcontroller unit according to embodiments of the present invention are described.

[0024] like Figure 1 As shown, a first aspect embodiment of the present invention provides a pulse output circuit, which includes a timer and a logic operation module.

[0025] The timer includes a first timer channel, a second timer channel, and a synchronization control unit. The synchronization control unit is coupled to both the first and second timer channels and controls their synchronous start-up counting. The first and second timer channels are configured to count driven by the system clock. The first timer channel is configured to count cyclically according to a first channel target period, and within each first channel target period, output a first valid level and a first invalid level that toggle at a first target duration interval. The second timer channel is configured to output a second valid level after synchronously starting counting with the first timer channel, and toggle to a second invalid level after a second target duration interval from the start of synchronous counting, maintaining the second invalid level after the toggle. A logic operation module is coupled to both the first and second timer channels and is used to output a pulse valid level when the first timer channel outputs a first valid level and the second timer channel outputs a second valid level, and to output a pulse invalid level when the first timer channel outputs a first invalid level or the second timer channel outputs a second invalid level. The logic output terminal of the logic operation module is used for electrical connection to a target load device; wherein the second target duration interval is greater than the first channel target period.

[0026] In a specific application scenario, the system clock is generated by the microcontroller's clock source via an internal clock tree. This clock is simultaneously connected to the first timer channel and the second timer channel, serving as the driving clock for their counters. The counters of the first and second timer channels share the same system clock source, and are controlled by a synchronization control unit to start counting synchronously. Therefore, at any time after synchronous start, the count values ​​of the first and second timer channels remain consistent.

[0027] Before pulse transmission is initiated, the count value of the target cycle for the first channel is determined based on the pulse frequency required by the target load device. The count value of the target cycle for the first channel, in conjunction with the system clock cycle, jointly determines the period of the output pulse from the first timer channel. The relationship between the output pulse period and the count value of the target cycle for the first channel differs depending on the counting mode. In center-aligned counting mode, one complete counting cycle corresponds to one complete output pulse, and the period of the output pulse is equal to twice the product of the count value of the target cycle for the first channel and the system clock cycle. In edge-aligned counting mode, the period of the output pulse is equal to the product of the count value of the target cycle for the first channel and the system clock cycle. The reciprocal of the output pulse period is the pulse frequency, which must be consistent with the pulse frequency required by the target load device.

[0028] Based on the minimum detection width requirement of the target load device for the pulse effective level, the count value corresponding to the first target duration interval is determined. The product of the count value corresponding to the first target duration interval and the system clock period is the effective level duration of the output pulse of the first timer channel. To ensure that the target load device can reliably identify each pulse, the effective level duration of the output pulse of the first timer channel needs to be greater than the level detection threshold of the target load device. Within each output pulse cycle, the output level of the first timer channel flips once between the first effective level and the first ineffective level, thereby forming a pulse waveform with alternating effective and ineffective levels.

[0029] Further, based on the total number of pulses to be sent, the count value corresponding to the second target duration interval is determined. The physical duration corresponding to the count value of the second target duration interval is equal to the product of the planned number of pulses to be sent and the output pulse period. This physical duration is the total duration from the pulse transmission start time to the completion time of the last pulse transmission. After the second timer channel starts, it outputs a second valid level. When the count value reaches the count value corresponding to the second target duration interval, the output level flips to the second invalid level and remains there. Since the counters of the first timer channel and the second timer channel start synchronously and their count values ​​are always consistent, when the count value of the second timer channel reaches the count value corresponding to the second target duration interval, the first timer channel has just finished outputting the planned number of pulses. At this time, the output level of the second timer channel flips, and the logic operation module immediately stops the output of subsequent pulses.

[0030] See Figure 2 When the pulse transmission is initiated, the synchronous control unit simultaneously triggers the first timer channel and the second timer channel to start counting. The counters of both timers are synchronously incremented from their initial values ​​on the same system clock edge.

[0031] The timer's count is driven by the system clock, and each increment of the counter corresponds to a physical time equal to one system clock cycle. The first timer channel counts cyclically according to the first channel's target cycle, meaning that the count value of the first timer channel starts from the initial value and increments sequentially with each cycle of the system clock. When the count value reaches the value corresponding to the first channel's target cycle, it automatically resets and starts incrementing again, repeating this cycle continuously. The counter of the second timer channel is driven by the same system clock as the counter of the first timer channel, and their timing is completely synchronized.

[0032] During the counting process, the first timer channel outputs a complete pulse within each target period of the first channel. Specifically, it outputs a first valid level during the pulse's valid period and a first invalid level during the pulse's invalid period. Within each counting cycle, the output level of the first timer channel flips once. Specifically, from the moment the count value of the first timer channel reaches the value corresponding to the first target duration interval, the output level of the first timer channel flips from the first valid level to the first invalid level, or vice versa. The physical duration corresponding to the target period of the first channel is determined by the product of the count value of the target period and the system clock period. This physical duration determines one complete cycle of the output pulse of the first timer channel. The physical duration corresponding to the first target duration interval is determined by the product of the count value corresponding to the first target duration interval and the system clock period. This determines the duration of the valid level of the output pulse of the first timer channel, and thus determines the duty cycle of the output pulse.

[0033] After the second timer channel is started, its output initially presents the second valid level. The second timer channel continues to output the second valid level until the count value reaches the second target duration interval.

[0034] At this point, of the two inputs to the logic operation module, the output of the second timer channel is at the second valid level. Under this condition, the output of the logic operation module depends entirely on the output state of the first timer channel: when the first timer channel outputs the first valid level, the logic operation module outputs a pulse valid level; when the first timer channel outputs the first invalid level, the logic operation module outputs a pulse invalid level. Therefore, before the second timer channel flips, the output waveform of the logic operation module is consistent with the output waveform of the first timer channel, and the target load device receives the pulse signal normally.

[0035] Because the two channels share the same system clock source and start synchronously, their count values ​​are equal at any given time. This synchronization characteristic allows the count value of the second timer channel to be directly mapped to the number of counting cycles already completed by the first timer channel: when both channels have a count value of C, the number of pulses completely output by the first timer channel is equal to C divided by the integer part of the first channel's target period. Therefore, by setting the value of the second target duration interval, the total number of pulses output by the first timer channel can be controlled.

[0036] When the counter reaches the value corresponding to the second target duration interval, since the two channels count synchronously, the first timer channel has already completed the number of pulses equal to the planned number of pulses. The output level of the second timer channel flips to the second invalid level and remains unchanged after the flip. The second target duration interval is greater than the target period of the first channel, which means that the flipping time of the second timer channel must be later than the completion of at least one complete pulse cycle by the first timer channel; that is, the cutoff action occurs only after at least one complete pulse output is completed.

[0037] The output of the logic operation module is then forced to an invalid pulse level. Thereafter, regardless of how the first timer channel continues to cycle, its signal is blocked by the logic operation module and cannot be transmitted to the logic output.

[0038] The logic operation module can be implemented using external independent logic gate chips or using the programmable logic unit inside the microcontroller. It does not require complex programmable logic devices such as CPLD or FPGA, which helps to reduce hardware costs and development difficulty.

[0039] The logic output terminal of the logic operation module serves as the signal output pin of the pulse output circuit in this embodiment, and is used for electrical connection to the target load device. The target load device can be the pulse input terminal of a programmable logic controller, a stepper motor driver, a servo driver, or other industrial equipment that needs to receive pulse signals as control commands.

[0040] It is important to emphasize that the conventional approach to achieving hardware-level automatic stop is to use a second independent timer / counter to count the number of pulses emitted by the first timer. This approach requires a signal transmission channel between the two independent counting units (internal triggering or external control). This invention eliminates the conventional approach of using another timer to count pulses. Instead, it leverages the characteristic of two channels within the same timer sharing a unified clock source. The first timer channel is configured for periodic toggling, and the second timer channel is configured for single-delay toggling. The outputs of the two are combined in real time through a logic operation module, achieving hardware-level automatic truncation of the pulse train. Each pulse requires only two channels of one timer module. Compared to the existing linkage method (which requires two complete timers per channel), the number of timer peripherals is reduced by 50%, which helps reduce material costs and allows low- to mid-range MCUs to meet the industrial application requirements of multi-channel pulse output.

[0041] Therefore, the pulse output circuit of this embodiment has the following technical effects: The pulse cutoff action is directly driven by the logic operation module through the output flipping of the second timer channel, without waiting for interrupt response and interrupt service routine execution. Therefore, the cutoff delay mainly depends on the propagation delay of the logic gate circuit, resulting in higher determinism in the cutoff response compared to schemes relying on software interrupts. The synchronization control unit controls the two channels to count synchronously, establishing a more accurate mapping between the number of output pulses from the first timer channel and the count value of the second timer channel, reducing the likelihood of errors caused by asynchronous counting. The configuration of the second target duration interval being greater than the target period of the first channel ensures that the cutoff time is later than the completion of at least one full pulse cycle by the first timer channel, helping to guarantee that the cutoff action occurs after the planned number of pulses have been output, rather than prematurely. Compared to existing technologies that require an additional timer as a counter and to work in conjunction with the pulse output timer through an inline linkage channel, this invention only occupies one timer resource in each independent pulse output channel, reducing the need for a larger number of timers. Since it does not rely on hardware inter-channel linkage between timers, it is less restricted by the scarcity of inter-channel linkage in the microcontroller. In application scenarios that require multiple pulse outputs, there is no need to occupy a second timer for each pulse, nor is there a need to connect external gating logic chips on the printed circuit board to realize the linkage shutdown between timers due to insufficient available inter-channel linkage. This helps to reduce material costs and printed circuit board layout area, thereby improving portability and platform versatility.

[0042] like Figure 2 As shown, for example, the first timer channel is configured to count cyclically according to a first channel target period, and output a first valid level and a first invalid level that toggle at a first target duration interval within each first channel target period. In this embodiment, the first target duration interval is configured to be half of the first channel target period, i.e., the duty cycle of the output pulse is 50%, so that the pulse signal has symmetrical waveform characteristics, which facilitates reliable edge detection by the receiving device. The first target duration interval and the first channel target period can be specifically determined according to the duty cycle of the output pulse, as long as the first target duration interval is less than the first channel target period.

[0043] like Figure 1As shown, in some embodiments, the first timer channel includes a first counter, a first comparator, a first comparison register, and a first cycle register; the first counter is coupled to the first comparator and the first cycle register respectively, the first comparator is coupled to the first comparison register, and the first comparator is also coupled to a logic operation module; wherein, the first cycle register is used to store the first channel target cycle; the first comparison register is used to store a first comparison setting value; the first counter is configured in a cyclic counting mode to increment from a first initial value to the first channel target cycle, and automatically reloads to the first initial value when the first channel target cycle is reached to enter the next cycle; the first comparator is used to compare the first current count value of the first counter with the first comparison setting value, and triggers the output level of the first timer channel to toggle between a first valid level and a first invalid level when the two match; the first timer channel periodically toggles its output between the first valid level and the first invalid level based on the cyclic counting of the first counter and the matching trigger of the first comparator.

[0044] Through the hardware coordination of the first counter, first comparator, first cycle register, and first comparison register, the first timer channel can autonomously generate continuous pulse waveforms without consuming processor computing resources. The frequency of the output pulse is determined by the target cycle value of the first channel combined with the system clock frequency, and the duty cycle is determined by the ratio of the first comparison setpoint to the target cycle value of the first channel. Both can be configured independently to adapt to the different requirements of various target load devices for pulse frequency and effective level width. The direct coupling between the first comparator and the logic operation module provides a signal input for subsequent pulse cutoff control.

[0045] Specifically, at the start of a counting cycle, the first counter increments from a first initial value, and the output of the first timer channel is at a first active level. The first comparator continuously monitors the current count value of the first counter. When the current count value of the first counter increases to equal the first comparison setpoint, the first comparator triggers a match signal, and the output level of the first timer channel undergoes its first toggle, switching from the first active level to the first inactive level. The first counter continues to increment until it reaches the target period value of the first channel. At this point, the first counter triggers a reload, the count value is reset to the first initial value, and the next cycle begins. In the next cycle, when the current count value of the first counter again increases to the first comparison setpoint, the first comparator triggers the match signal again, and the output level of the first timer channel undergoes its second toggle, switching from the first inactive level back to the first active level. This process repeats continuously, forming a continuous periodic pulse waveform at the output of the first timer channel.

[0046] The first counter is driven by the system clock and increments by 1 in each system clock cycle. When the current count value of the first counter increases to the target cycle value of the first channel stored in the first cycle register, the hardware triggers a reload, resets the current count value to the first initial value, and begins the next round of incrementing.

[0047] The value stored in the first cycle register determines the upper limit of the first counter, which is the end point of a complete counting cycle.

[0048] The first comparator compares the first current count value of the first counter with the first comparison setting value stored in the first comparison register. When the first current count value is equal to the first comparison setting value, i.e., they match, the first comparator outputs a match trigger signal. This match trigger signal is passed to the output control logic of the first timer channel, triggering a toggle of the output level of the first timer channel: if the current output is a first active level, it switches to a first inactive level; if the current output is a first inactive level, it switches to a first active level.

[0049] like Figure 1 As shown, in some embodiments, the second timer channel includes a second counter, a second comparator, a second comparison register, and a second cycle register; the second counter is coupled to the second comparator and the second cycle register respectively, the second comparator is coupled to the second comparison register, and the second comparator is also coupled to a logic operation module; wherein, the second cycle register is used to store a second cycle setting value, which is greater than the second comparison setting value stored in the second comparison register; the second comparison register is used to store a second comparison setting value corresponding to a second target duration interval; the second counter is configured in single-count mode to increment from a second initial value; the second comparator is used to compare the second current count value of the second counter with the second comparison setting value, and triggers the output level of the second timer channel to flip from a second valid level to a second invalid level when the two match; the reload function of the second cycle register is disabled so that the second counter stops counting after reaching the second cycle setting value, and the second invalid level is maintained.

[0050] Therefore, the second timer channel, through the combination of single-count mode and disabling the reload function, can lock the output level to the second invalid level after the second comparator triggers a match. This locked state is directly maintained by the hardware logic, without relying on continuous processor intervention or periodic software refresh. The second invalid level, as an input signal to the logic operation module, can continuously force the output of the logic operation module to a pulse invalid level after the match trigger, thereby blocking the transmission of the pulse signal from the first timer channel to the logic output. This continuous level locking mechanism ensures that the pulse cutoff state remains stable after the entire pulse transmission is completed, until the processor reconfigures and starts the next transmission task. The second period setting value is configured to be greater than the second comparison setting value, ensuring that the second counter still has remaining counting space at the time of the match trigger, preventing the counter from reaching its counting limit before or at the same time as the match trigger and causing unpredictable behavior. Disabling the reload function eliminates the possibility of the counter automatically resetting and recounting after reaching the second period setting value, thus avoiding the risk of the second invalid level being accidentally revoked due to the counter's cyclic restart. Together, these two features enable the second timer channel to provide a stable, reliable, and continuous gating signal to the logic operation module.

[0051] Specifically, upon pulse transmission initiation, the synchronization control unit triggers the second counter to increment from its second initial value. The output of the second timer channel initially displays a second valid level. The second comparator continuously monitors the second current count value of the second counter. When the second current count value increments to equal the second comparison set value, the second comparator triggers a match signal, and the output level of the second timer channel flips from the second valid level to the second invalid level. Thereafter, the second counter continues to increment until it reaches the second cycle set value. Because the reload function of the second cycle register is disabled, the second counter stops counting after reaching the second cycle set value, and no further reload occurs. The output level of the second timer channel remains at the second invalid level. One input of the logic operation module continuously receives this second invalid level, thereby continuously cutting off the final pulse output.

[0052] The second counter is driven by the system clock and increments by 1 in each system clock cycle.

[0053] The second cycle setting is configured to be greater than the second comparison setting stored in the second comparison register. The second cycle setting determines the upper limit of the second counter, that is, the maximum value that the second counter can increment to.

[0054] The second comparison setting corresponds to the second target time interval.

[0055] The second comparator compares the second current count value of the second counter with the second comparison setting value stored in the second comparison register. When the second current count value equals the second comparison setting value, i.e., they match, the second comparator outputs a match trigger signal. This match trigger signal is passed to the output control logic of the second timer channel, triggering the output level of the second timer channel to flip from the second active level to the second inactive level.

[0056] The reload function of the second cycle register is disabled. This disabling means that when the count value of the second counter increments to the second cycle set value, the counter will not trigger a reload event, will not reset the count value to the second initial value and restart counting, but will stop incrementing and maintain the current count value.

[0057] See Figure 3 and Figure 1 A second aspect of the present invention provides a pulse output method, applied to the pulse output circuit of the first aspect of the present invention, comprising: S100: Obtain the inherent count limit value of the second timer channel, as well as the load target pulse setting number, load target pulse period, load level detection threshold, and load inherent response delay of the target load device.

[0058] The inherent count upper limit refers to the upper limit of the count value determined by the hardware bit width of the second timer channel. The load target pulse setting quantity refers to the total number of pulses expected in this pulse transmission task. The load target pulse period refers to the duration of a single pulse expected by the target load device. The load level detection threshold refers to the minimum high-level duration required for the target load device to reliably identify the valid pulse level. The inherent load response delay refers to the time delay from receiving a pulse to generating a response from the target load device.

[0059] S200: Determine the first channel target period of the first timer channel based on the load target pulse period.

[0060] The count value of the target period for the first channel determines the upper limit of the count for the first timer channel. The product of this count value and the system clock period determines the physical duration of the output pulse. The relationship between the physical period of the output pulse and this count value differs in different counting modes: in edge-aligned mode, the period of the output pulse is equal to the product of the target period for the first channel and the system clock period; in center-aligned mode, the period of the output pulse is equal to twice the product of the target period for the first channel and the system clock period. The reciprocal of the output pulse period is the pulse frequency, which must be consistent with the pulse frequency required by the target load device.

[0061] In center-aligned mode, the count value of the first timer channel increments from its initial value to the count value of the target period of the first channel, and then decrements back to the initial value, completing one full counting cycle. This cycle corresponds to the output of one complete pulse. Therefore, the increment of the count value corresponding to one complete pulse is twice the count value of the target period of the first channel. For example, if the count value of the target period of the first channel is N, then one complete pulse corresponds to 2N system clock cycles.

[0062] S300: Determine the first target duration interval of the first timer channel based on the load level detection threshold.

[0063] The first target duration interval determines the effective duration of the output pulse. The target load device has a minimum recognition requirement for the effective pulse duration; if the effective duration is too short, the load device cannot reliably detect the pulse. Therefore, the first target duration interval needs to be determined based on the load level detection threshold to ensure that the effective duration meets the detection threshold of the target load device.

[0064] S400: Determine the upper limit of the number of pulses sent in a single pulse based on the inherent upper limit of the count and the inherent response delay of the load.

[0065] The maximum number of pulses that can be transmitted in a single operation without triggering an overflow of the second timer channel. The inherent count limit determines the maximum range within which the second timer channel can count. If the count value corresponding to the number of pulses transmitted exceeds the inherent count limit, the second timer channel will overflow and will fail to trigger the output toggle at the target time. Simultaneously, the inherent response delay of the load requires the second timer channel to retain a certain counting margin after the count value reaches the comparison value to ensure that the target load device has sufficient time to recognize the last pulse. Therefore, the maximum number of pulses that can be transmitted in a single operation needs to be determined jointly based on the inherent count limit and the inherent response delay of the load.

[0066] S500: Get the actual number of pulses sent.

[0067] The actual number of pulses sent refers to the total number of pulses that have been successfully sent to the target load device so far.

[0068] S600: Determine the actual set number of pulses to be sent this time based on the upper limit of the number of pulses to be sent in a single pulse, the actual number of pulses already sent, and the set number of target pulses for the load.

[0069] The total number of pulses required by the target load device may exceed the upper limit of the number of pulses sent in a single transmission. In this case, all the pulses required by the target load device need to be sent in multiple rounds. In each round of transmission, the transmission operation is completed from the start of the synchronization control unit until the output level of the second timer channel flips, causing the pulse to stop. If the number of remaining pulses to be sent is greater than the upper limit of the number of pulses to be sent in a single transmission, then the number of pulses sent in this round is equal to the upper limit; if the number of remaining pulses to be sent is not greater than the upper limit, then the number of pulses sent in this round is equal to the number of remaining pulses to be sent. After the transmission is completed, the entire pulse transmission task ends. The actual pulse transmission set number refers to the number of pulses planned to be sent in the current round of transmission operation.

[0070] The actual number of pulses sent in this operation refers to the number of pulses planned to be sent in this round of transmission.

[0071] S700: Determine the second target duration interval of the second timer channel based on the actual number of pulses sent in this transmission.

[0072] The physical duration corresponding to the second target duration interval is equal to the total time length occupied by the number of pulses planned to be sent this time. The second timer channel needs to trigger a toggle to stop the pulses after all the pulses planned to be sent this time have been output. Therefore, the count value corresponding to the second target duration interval needs to be determined based on the actual number of pulses sent this time, which serves as the trigger time for the output level toggle of the second timer channel.

[0073] For example, in this embodiment, the count value corresponding to the second target duration interval is calculated as follows: In center-aligned mode, the count value corresponding to the first channel target period is denoted as ARR1, and the actual number of pulses sent in this transmission is set to M. Then, the base count value is M × ARR1 × 2. The preset safety margin is ARR1 × 2 ÷ 8 (rounded down). This margin is less than the count value corresponding to a complete pulse to ensure that the cutoff time falls within the effective level range of the last pulse. Therefore, the count value corresponding to the second target duration interval = M × ARR1 × 2 - ARR1 × 2 ÷ 8. If edge-aligned mode is used, the formula is adjusted accordingly to the count value corresponding to the second target duration interval = M × ARR1 - ARR1 ÷ 8. Those skilled in the art can select the appropriate formula according to the timer's operating mode.

[0074] S800: Configure the first channel target period and the first target duration interval to the first timer channel.

[0075] This step writes the first target period value determined in S200 into the first period register of the first timer channel, and writes the count value corresponding to the first target duration interval determined in S300 into the first comparison register of the first timer channel, so that the first timer channel outputs a continuous pulse waveform according to the target frequency and duty cycle.

[0076] S900: Configure the second target duration interval to the second timer channel.

[0077] This step writes the count value corresponding to the second target duration interval determined in S700 into the second comparison register of the second timer channel, and configures the second timer channel into single-count mode so that the second timer channel triggers the output level to flip when the count value reaches the second target duration interval.

[0078] S1000: After configuring the first timer channel and the second timer channel, a start command is sent to the synchronization control unit so that the logic output terminal of the logic operation module outputs a pulse valid level or a pulse invalid level to the target load device.

[0079] In this step, the synchronization control unit simultaneously triggers the first and second timer channels to start counting. The counters on both channels begin to increment synchronously from the same moment. The first timer channel outputs a periodic pulse waveform, and the second timer channel outputs a second valid level before the count value reaches the second target duration interval. The output of the logic operation module follows the waveform of the first timer channel. When the count value of the second timer channel reaches the second target duration interval, its output flips to the second invalid level, the output of the logic operation module is forced to a pulse invalid level, and the pulse output automatically stops.

[0080] This implementation sets an upper limit on the number of pulses sent in a single burst, dividing the total number of pulses required by the target load device into segments. The number of pulses in each segment is limited to the counting capacity of the second timer channel, thus avoiding overflow due to excessive single-pulse transmission and failure to trigger output switching at the target time. During each pulse transmission segment, pulse generation and counting are independently completed by the timer hardware. Interrupts are triggered only once at the end of a single pulse transmission segment, rather than in every pulse cycle, thereby reducing the processor's interrupt response load. Simultaneously, the first and second timer channels are synchronously started and counted by a synchronization control unit, ensuring a precise mapping between the count values ​​of the two channels. This ensures that when the second timer channel reaches the second target duration interval, the first timer channel has just finished outputting the planned number of pulses, allowing the hardware logic to automatically complete pulse cutoff without processor intervention.

[0081] like Figure 4As shown, in some embodiments, step S500: obtaining the actual number of transmitted pulses includes: S1101: Read the cumulative reference value of sent pulses.

[0082] The cumulative base value of transmitted pulses refers to the total number of pulses transmitted in previous rounds. Each time a round of pulse transmission is completed, the actual number of pulses transmitted in that round is added to the cumulative base value. For example, if the target load device needs to transmit 150 pulses, and the maximum number of pulses transmitted at one time is 100, then in the first round, 100 pulses are transmitted, and after completion, 100 are added to the cumulative base value; in the second round, the remaining 50 pulses are transmitted, and after completion, 50 are added to the cumulative base value, ultimately resulting in a cumulative base value of 150.

[0083] S1102: When the second counter of the second timer channel reaches the count value corresponding to the second target duration interval, a target interrupt is triggered.

[0084] The target interrupt is automatically triggered by hardware when the second counter reaches the count value corresponding to the second target duration interval (the second comparison setting value).

[0085] S1103: In response to the target interrupt, read the first current count value of the first timer channel and the second current count value of the second timer channel.

[0086] In response to the triggering of the target interrupt, the first current count value of the first counter of the first timer channel and the second current count value of the second counter of the second timer channel are read.

[0087] S1104: Based on the comparison result between the first current count value and the count value corresponding to the first target duration interval, and the comparison result between the second current count value and the count value corresponding to the second target duration interval, the cumulative reference value of the transmitted pulses is corrected.

[0088] At the moment the interrupt is triggered, the second current count value has reached the count value corresponding to the second target duration interval. Since the two channels count synchronously, the first current count value is equal to the second current count value. At this point, it is necessary to determine whether the rising edge of the current pulse has been completed.

[0089] This embodiment uses a center-aligned pulse output mode with a low-to-high frequency response as an example. The count value of the first timer channel increases from 0 to the count value of the target period of the first channel, and then decreases back to 0, completing one complete counting cycle, which corresponds to one complete pulse. The stage where the count value increases from 0 to the count value of the target period of the first channel is the increment segment, and the stage where the count value decreases back to 0 is the decrement segment. The timer channel internally records the current counting direction, which determines whether it is currently in the increment or decrement segment.

[0090] If the current pulse is in the increment phase, compare the first current count value with the count value corresponding to the first target duration interval: if the first current count value is greater than or equal to the count value corresponding to the first target duration interval, it means that the rising edge of the current pulse has been completed, and the pulse is counted in the number of pulses sent; if the first current count value is less than the count value corresponding to the first target duration interval, it means that the current pulse has not yet reached the rising edge, and the pulse is not counted in the number of pulses sent.

[0091] If the current phase is in the decreasing phase, the rising edge must have already occurred, and this pulse is directly counted in the number of pulses sent.

[0092] S1105: Use the corrected cumulative reference value of transmitted pulses as the actual number of transmitted pulses.

[0093] The cumulative base value of transmitted pulses, corrected by S1104, is used as the actual number of transmitted pulses, i.e., the total number of pulses successfully transmitted to the target load device so far. This value is used in subsequent steps to calculate the number of remaining pulses to be transmitted.

[0094] By following the steps described above, reading the states of two counters at the target interrupt trigger moment and comparing the current count value with the first comparison set value, it is possible to accurately distinguish whether the rising edge of the current pulse has been completed, thereby correcting the cumulative reference value. This avoids counting ambiguity caused by the cutoff time falling exactly in the middle of the pulse period, enabling the acquisition of the actual number of transmitted pulses to be accurate to the single pulse level. The corrected actual number of transmitted pulses provides accurate input data for determining the actual number of pulses transmitted in subsequent segmented transmissions.

[0095] like Figure 5 As shown, in some embodiments, step S600: determining the actual set number of pulses to be transmitted this time based on the upper limit of the number of pulses transmitted in a single transmission, the actual number of pulses already transmitted, and the set number of target pulses for the load, includes: S1201: Based on the target number of pulses set for the load and the actual number of pulses sent, obtain the number of pulses remaining to be sent.

[0096] Under the segmented transmission mechanism, due to the limitation on the upper limit of the number of pulses transmitted in a single transmission, some pulses have not yet been transmitted, and it is necessary to determine the number of pulses still to be transmitted. This step subtracts the actual number of transmitted pulses from the target pulse set number to obtain the remaining number of pulses to be transmitted.

[0097] S1202: Compare the remaining number of pulses to be sent with the upper limit of the number of pulses to be sent in a single pulse.

[0098] The remaining number of pulses to be sent may be less than, equal to, or greater than the maximum number of pulses that can be sent in a single operation. This step compares the two to determine whether the remaining number of pulses to be sent can be completed in a single transmission operation. The comparison result will determine the actual number of pulses to be sent in this round.

[0099] S1203: When the number of remaining pulses to be sent is greater than the upper limit of the number of pulses to be sent in a single pulse, the upper limit of the number of pulses to be sent in a single pulse is determined as the actual number of pulses to be sent in this pulse.

[0100] When the number of remaining pulses to be sent exceeds the maximum number of pulses that can be sent in a single operation, it means that the remaining pulses cannot be sent all at once. In this case, the number of pulses corresponding to the maximum number of pulses that can be sent in a single operation is sent first in this round, and the remaining pulses are sent in subsequent rounds. This judgment ensures that the number of pulses sent in each round does not exceed the counting capacity of the second timer channel.

[0101] S1204: When the number of remaining pulses to be sent is not greater than the upper limit of the number of pulses to be sent in a single pulse, the number of remaining pulses to be sent is determined as the actual number of pulses to be sent in this pulse.

[0102] When the number of remaining pulses to be sent is not greater than the upper limit of the number of pulses to be sent in a single operation, it means that the remaining pulses can all be sent in one operation. At this time, the number of pulses sent in this round is equal to the number of remaining pulses to be sent, and the entire pulse sending task ends after this round of sending is completed.

[0103] Therefore, by comparing the remaining number of pulses to be sent with the upper limit of the number of pulses to be sent in a single round, the number of pulses to be sent in the current round is dynamically determined. When the remaining number exceeds the upper limit, each round sends pulses according to the upper limit to ensure that the counting capacity is not exceeded each time; when the remaining number does not exceed the upper limit, all pulses are sent at once to ensure that the task ends in a timely manner. This segmented decision-making mechanism enables pulse sending tasks of any length to be completed with the fewest interrupts within the counting capacity of the second timer channel.

[0104] like Figure 6 As shown, in some embodiments, step S1203: when the number of remaining pulses to be sent is greater than the upper limit of the number of pulses to be sent in a single pulse, further includes: S1301: After this pulse transmission is completed, the target interrupt is triggered by the second timer channel.

[0105] If the number of remaining pulses to be sent exceeds the upper limit for the number of pulses to be sent in a single transmission, the number of pulses sent in this round will equal the upper limit for the number of pulses to be sent in a single transmission. After this round of transmission is completed, the pulse transmission task must continue because the number of remaining pulses to be sent has not yet reached zero. At this time, the second timer channel triggers a target interrupt when the count value reaches the count value corresponding to the second target duration interval, so as to start the next round of configuration and transmission process.

[0106] S1302: In response to a target interruption, update the actual number of pulses sent based on the set number of pulses actually sent in this pulse.

[0107] In response to a target interruption, the actual number of pulses sent in this round is added to the cumulative base value of sent pulses. Since the number of pulses sent in this round is the same as the set number of pulses actually sent in this round, this value is added to the actual number of sent pulses to obtain the updated actual number of sent pulses.

[0108] S1303: Based on the updated actual number of pulses sent, redetermine the set number of pulses to be sent this time.

[0109] Based on the updated actual number of pulses sent, the remaining number of pulses to be sent is recalculated, and the recalculated number of remaining pulses to be sent is compared with the upper limit of the number of pulses to be sent in a single round to determine the set number of pulses to be sent in the next round.

[0110] S1304: Based on the redefined actual number of pulses sent this time, repeat the steps of determining the second target duration interval, configuring the first timer channel, configuring the second timer channel, and sending a start command to the synchronization control unit.

[0111] Based on the newly determined actual number of pulses transmitted, steps S700 to S1000 are executed again. That is, the second target duration interval is re-determined according to the new actual number of pulses transmitted, the first channel target period and the first target duration interval are configured to the first timer channel, the second target duration interval is configured to the second timer channel, and then a start command is sent to the synchronization control unit to start a new round of pulse transmission.

[0112] The above loop continues until the number of remaining pulses to be sent is no greater than the upper limit of the number of pulses to be sent in a single transmission. At this point, S1204 is executed, the number of remaining pulses to be sent is determined as the actual number of pulses to be sent in this transmission, and the last round of transmission is completed, ending the entire pulse transmission task. Thus, through the target interrupt-driven loop mechanism, long pulse trains exceeding the single transmission limit are automatically divided into multiple consecutive short segments and sent sequentially. After each segment is sent, a hardware interrupt is triggered. The system updates the cumulative number and configures the parameters for the next segment in the interrupt service routine, and then immediately starts sending the next segment. Switching between segments involves only one interrupt response and a few register write operations, without requiring processor participation in pulse-level counting and cutoff control. This loop mechanism allows pulse transmission tasks of any length to be completed automatically with fewer interrupts within the hardware counting capability.

[0113] like Figure 7 As shown, in some embodiments, step S400: determining the upper limit of the number of pulses transmitted in a single pulse based on the inherent upper limit of the count and the inherent response delay of the load includes: S1401: Determine the theoretical maximum number of pulses that can be sent based on the inherent upper limit of the count.

[0114] The inherent count limit is the maximum count value that the counter of the second timer channel can reach. The comparison setting value of the second timer channel must be less than this inherent count limit; otherwise, the counter will trigger a reload before or simultaneously with reaching the comparison setting value, and will be unable to generate a comparison match event at the target time. The theoretical maximum number of pulses that can be transmitted refers to the maximum number of pulses that can be transmitted in a single transmission under ideal conditions, without considering any margin. Its value is equal to the inherent count limit divided by the integer part of the count value of the target period of the first channel.

[0115] S1402: Determine the margin for the count value based on the inherent response delay of the load.

[0116] The inherent response delay of the load refers to the time required for the target load device to respond after receiving the last pulse. After the count value of the second timer channel reaches the comparison setpoint and triggers the output toggle, the last pulse has already been sent to the target load device, but the target load device needs a certain amount of time to complete the recognition and response to the pulse. If the comparison setpoint is too close to the inherent count limit, the counter will reach the count limit quickly after triggering the comparison event, which may affect the stability of the output level due to the change in the counter's state. Therefore, a certain count value margin needs to be reserved according to the inherent response delay of the load to ensure that the target load device has enough time to recognize the last pulse. The longer the inherent response delay of the load, the larger the required count value margin.

[0117] S1403: Determine the upper limit of the number of pulses to be transmitted in a single pulse based on the theoretical maximum number of pulses that can be transmitted and the margin reserved for the count value.

[0118] Subtracting the count value's margin from the theoretically maximum number of pulses that can be sent yields the upper limit for the number of pulses sent in a single burst. This upper limit is less than the theoretically maximum number of pulses that can be sent, ensuring sufficient counting space for the second timer channel after triggering a comparison event. The physical duration corresponding to this counting space is greater than the inherent response delay of the target load device.

[0119] Therefore, by reserving a count margin based on the inherent response delay of the load, the upper limit of the number of pulses sent in a single pulse is set within the inherent count upper limit but with a margin. This margin ensures that after the comparison event is triggered by the second timer channel, the counter still has remaining counting space, so that the second invalid level can remain stable during the period when the target load device completes the identification of the last pulse, avoiding the risk of the cutoff signal failing due to unpredictable state changes caused by the counter reaching the count upper limit.

[0120] like Figure 8 As shown, in some embodiments, step S700: determining the second target duration interval of the second timer channel based on the actual number of pulses sent in this transmission includes: S1501: Determine the reference duration interval based on the actual number of pulses sent and the target pulse period of the load.

[0121] The baseline duration interval refers to the total time required to send the planned number of pulses in this round. The actual number of pulses sent in this round is the planned number of pulses, and the target pulse period is the duration of each pulse. Multiplying the two gives the total time elapsed from the start of the first pulse to the end of the last pulse in this round, which is the baseline duration interval.

[0122] S1502: Obtain the preset safety margin.

[0123] The preset safety margin is a pre-configured time value that is less than the target pulse period of the load. The safety margin is used to adjust the output toggle time of the second timer channel backward from the end of the reference time interval, so that the toggle time falls within the effective level range of the last pulse, rather than after the end of the pulse period.

[0124] S1503: Determine the second target time interval based on the difference between the baseline time interval and the preset safety margin.

[0125] Subtracting the preset safety margin from the base duration interval yields the second target duration interval. The physical duration corresponding to this interval is slightly shorter than the total duration required to send the planned number of pulses in this round, ensuring that the output flip of the second timer channel occurs during the effective level duration of the last pulse in this round, rather than after the end of the pulse period.

[0126] For example, if the target load pulse period is 10 microseconds and the preset safety margin is 2 microseconds, then the second target duration interval is equal to 10 microseconds multiplied by the actual number of pulses sent in this instance, minus 2 microseconds. When the count value of the second timer channel reaches the value corresponding to this interval, the effective level of the last pulse has been maintained for 8 microseconds (greater than the level detection threshold of the target load device), but the pulse period has not yet ended (2 microseconds remaining). At this point, the trigger cutoff ensures that the last pulse has been recognized by the target load device without generating additional pulses.

[0127] The second target duration interval is determined by subtracting a preset safety margin from the baseline duration interval, ensuring that the count value corresponding to the cutoff time falls within the period range of the last pulse, rather than at the boundary between two pulses. The preset safety margin is less than the load target pulse period, guaranteeing that the cutoff time occurs after the effective level of the last pulse has been maintained for a sufficient time (greater than the level detection threshold) and before the start of the next pulse. This avoids the problem of insufficient effective level width of the last pulse or the generation of an extra half-pulse due to the cutoff time coinciding with the pulse edge, making the pulse cutoff position precisely controllable.

[0128] The following is combined Figures 1-8 Provide a specific example.

[0129] This example illustrates a quantitative pulse transmission task in an industrial control scenario. In this scenario, the target load device is a stepper motor driver. This driver requires an input pulse frequency of 100kHz, the ability to reliably identify pulse signals with a high-level duration of at least 3 microseconds, and an inherent load response delay of 2 microseconds. The task is to send 150 pulses to this driver.

[0130] In this example, the system clock frequency is 100MHz, meaning the system clock period is 10 nanoseconds. The timer includes a first timer channel and a second timer channel, both configured to count driven by the system clock. The synchronization control unit is used to control the first and second timer channels to start counting synchronously.

[0131] The first timer channel is configured to output a center-aligned pulse in a low-to-high pattern, and outputs a first active level and a first inactive level that toggle at a first target duration interval within each target period of the first channel. In this example, the first active level is high and the first inactive level is low.

[0132] The second timer channel is configured to output a center-aligned pulse in a high-then-low pattern. After starting the count, it outputs a second valid level, which then toggles to a second invalid level after a second target time interval from the start of synchronization, and remains at the second invalid level after the toggle. In this example, the second valid level is high, and the second invalid level is low.

[0133] The logic operation module is an AND gate. Its two inputs are connected to the GPIO (General Purpose Input / Output) output pins of the first timer channel and the second timer channel, respectively. The output is used to connect to the target load device. The logic operation module outputs a pulse-active level when the first timer channel outputs a first valid level and the second timer channel outputs a second valid level; it outputs a pulse-invalid level when the first timer channel outputs a first invalid level or the second timer channel outputs a second invalid level.

[0134] (a) Parameter acquisition and initial configuration First, obtain the inherent count limit value of the second timer channel. In this example, the second timer channel is a 32-bit timer, and the inherent count limit value (ARR2) is 0xFFFFFFFF.

[0135] The target load pulse acquisition quantity is set to 150 pulses. The target load pulse period is 10 microseconds (corresponding to a frequency of 100kHz). The load level detection threshold is 3 microseconds. The inherent load response delay is 2 microseconds.

[0136] Based on the target load pulse period (10 microseconds) and the system clock period (10 nanoseconds), in center-aligned mode, the count value of the first counter increments from 0 to the target period value of the first channel, and then decrements back to 0. One complete pulse period corresponds to the sum of the increment and decrement segments. Therefore, the counter value (ARR1) corresponding to the target period of the first channel = target load pulse period ÷ (2 × system clock period) = 10 microseconds ÷ (2 × 10 nanoseconds) = 500. The first cycle register stores the value 500. The first counter increments from 0 to 500 and then decrements back to 0. The physical duration of each complete counting cycle is 500 × 2 × 10 nanoseconds = 10 microseconds, and the output pulse frequency is 100kHz.

[0137] Based on the load level detection threshold (3 microseconds), the count value (CCR1) corresponding to the first target duration interval is determined. In the center-aligned mode (low-to-high), a low level is output during the period when the count value increases from 0 to the first comparison setpoint, a high level is output during the period when the count value increases from the first comparison setpoint to the first channel target period value of 500, a high level is maintained during the period when the count value decreases back from 500 to the first comparison setpoint, and a low level is output during the period when the count value decreases back to 0. The high level duration is (500-CCR1)×2×10 nanoseconds. To ensure that the high level duration is not less than 3 microseconds, (500-CCR1)×2×10 nanoseconds ≥ 3 microseconds, i.e., CCR1 ≤ 350. In this example, CCR1 = 350, and the high level duration is 3 microseconds.

[0138] (ii) Determine the upper limit of the number of pulses sent in a single pulse. The upper limit of the number of pulses sent in a single pulse is determined based on the inherent upper limit of the count (ARR2=0xFFFFFFFF) and the inherent response delay of the load (2 microseconds).

[0139] In center-aligned mode, a complete pulse corresponds to 1000 count values ​​for the first counter, which increments by 500 and decrements by 500. The theoretical maximum number of pulses that can be transmitted with ARR2=0xFFFFFFFF is approximately 0xFFFFFFFF÷1000≈4,294,967.

[0140] The count value is determined based on the inherent response delay of the load, with a margin of safety. A count value corresponding to an inherent response delay of 2 microseconds is 2 microseconds ÷ 10 nanoseconds = 200.

[0141] The upper limit of the number of pulses that can be sent in a single pulse = theoretical maximum number of pulses that can be sent - number of pulses corresponding to the reserved margin of the count value = 4,294,967 - 0 ≈ 4,294,967 (since 200 is much less than 1000, the margin is less than one pulse, and the actual impact is negligible). 150 is much less than this upper limit, so no segmentation is needed in this case.

[0142] (III) Determine the actual number of pulses sent in this transmission. Obtain the actual number of pulses sent. Before the first transmission, the cumulative base value of sent pulses is 0. The target interrupt has not yet been triggered, so no correction is needed, and the actual number of sent pulses is 0.

[0143] Based on the maximum number of pulses that can be sent in a single pulse, the actual number of pulses already sent, and the target pulse count (150), the actual number of pulses to be sent in this pulse is determined. The remaining number of pulses to be sent is 150 - 0 = 150. Since 150 is less than the maximum number of pulses that can be sent in a single pulse, the actual number of pulses to be sent in this pulse is 150.

[0144] (iv) Configure the first timer channel and the second timer channel Configuration of the first timer channel: Write the target period value ARR1=500 to the first period register, and write the count value CCR1=350 corresponding to the first target duration interval to the first comparison register. The first timer channel is configured to a center-aligned pulse output mode with low-to-high pulses.

[0145] Configuration of the second timer channel: ARR2 stored in the second cycle register is set to 0xFFFFFFFF (maximum value) to prevent the second counter from overloading during counting. The setting value of the second compare register (CCR2) is determined based on the actual number of pulses sent (150).

[0146] In center-aligned mode, the reference count value of the second comparator register corresponding to 150 pulses is 150 × 500 × 2 = 150000.

[0147] To ensure that the second timer channel does not immediately trigger a reload after the output level flips, a certain distance must be maintained between CCR2 and ARR2. CCR2 = number of pulses × ARR1 × 2 - ARR1 × 2 ÷ 8 = 150 × 500 × 2 - 500 × 2 ÷ 8 = 150000 - 125 = 149875.

[0148] The aforementioned CCR2 setting ensures an important condition: during the period from the start of the second counter until it reaches CCR2, the output of the second timer channel maintains the second valid level (high level), keeping the AND gate (logic operation module) in a conducting state. When the second counter reaches CCR2, the comparison event of the second timer channel is triggered, and the output toggles to the second invalid level (low level). At this time, the second counter still has a large count value space (0xFFFFFFFF-149875) away from ARR2 (0xFFFFFFFF), so the second timer channel can maintain the second invalid level for a sufficiently long time after the toggle without reloading. This distance ensures that even after any length of time has passed since the pulse transmission process ended, the low-level state of the second channel will not unexpectedly recover due to counter reload.

[0149] In this example, CCR2 = 149875, and CCR2 + ARR1 × 2 ÷ 8 = 150000. This means the comparison trigger time of the second timer channel is 125 count values ​​(corresponding to 1.25 microseconds) earlier than the end of the 150th pulse period, which is less than one pulse period (10 microseconds). Therefore, the cutoff action occurs during the high-level duration of the last pulse. Simultaneously, the current pulse state corresponding to CCR2 = 149875 is: 149 complete pulses have been completed, and the 150th pulse is currently being sent. The current count value of the 150th pulse is 149875 - 149 × 1000 = 875, which is in the decreasing phase, and the high-level phase has been completed. This cutoff position meets the requirements of the target load device.

[0150] (v) Pulse transmission process A start command is sent to the synchronization control unit. The synchronization control unit simultaneously triggers the first and second timer channels to start counting. The counters of the two channels begin to increment and decrement synchronously from the same moment.

[0151] The first timer channel outputs a complete pulse in each full counting cycle: a low level is output while the count value increases from 0 to 349, a high level is output while the count value increases from 350 to 500 and then decreases back to 350, and a low level is output while the count value decreases back to 0. The high level lasts for 3 microseconds.

[0152] After the second timer channel starts, it outputs a high level. Due to the AND gate's conduction condition, the pulse signal of the first timer channel is fully transmitted to the output terminal.

[0153] When the counter value reaches the second comparison setting CCR2 = 149875, the comparison event of the second timer channel is triggered, and its output level flips from high to low. Since the second cycle register ARR2 = 0xFFFFFFFF and the reload function is disabled, the second counter continues counting to 0xFFFFFFFF after reaching CCR2 and then stops. During this process, the second channel remains low and no reload occurs. The AND gate output is forcibly pulled low, and the output terminal presents a pulse invalid level. This cutoff action is directly completed by the hardware logic without requiring an MCU interrupt response. All 150 pulses have been sent.

[0154] (vi) Acquisition of real-time pulse accumulation count During pulse transmission, the system can read the current count values ​​of the first and second counters in real time to obtain the cumulative number of transmitted pulses. In this example, the value of the second counter, CNT2, is always synchronized with the value of the first counter, CNT1.

[0155] Case 1: CNT2 ≥ CCR2 (149875). At this time, the second timer channel has been triggered to flip, the output is controlled to a low level by GPIO2, and the number of pulses transmitted in this segment has reached the set value. Total pulse count = cumulative reference value of transmitted pulses + number of pulses transmitted in this segment.

[0156] Case 2: CNT2 < CCR2 (149875). At this time, the output is controlled by GPIO1, and the number of transmitted pulses is the integer part of CNT2 ÷ (ARR1 × 2) (each complete pulse corresponds to ARR1 × 2 count values, in this example ARR1=500, that is, 1000 count values). Further judge whether the rising edge of the current pulse has been completed: if CNT1 < CCR1 (350, that is, the count value at the position where the rising edge is located in this example), the current pulse has not reached the rising edge and is not counted in the transmitted quantity; if CNT1 ≥ 350, the current pulse has reached the rising edge, and 1 is added on the basis of the integer part.

[0157] For example, at a certain moment during the transmission process, it is read that CNT1=3200 and CNT2=3200. CNT2 < 149875, and the integer part of 3200 ÷ 1000 is 3 complete pulses. The CNT1 value of the current pulse is 200 (3200-3000), which is less than CCR1=350, indicating that the current pulse has not reached the rising edge. The actual number of transmitted pulses is 3.

[0158] If it is read that CNT1=3600 and CNT2=3600. The integer part of 3600 ÷ 1000 is 3 complete pulses. The CNT1 value of the current pulse is 600 (3600-3000), which is greater than CCR1=350, indicating that the current pulse has reached the rising edge. The actual number of transmitted pulses is 3+1=4.

[0159] (VII) Target Interrupt and Pulse Quantity Correction When the second timer channel reaches the second comparison set value CCR2=149875, the second timer channel triggers the target interrupt at the same time. In response to the target interrupt, the first current count value of the first timer channel is read as 875. 875 is greater than CCR1=350, the current pulse has been completely output, 1 is added on the basis of the cumulative reference value 0 of transmitted pulses, and the actual transmitted quantity of transmitted pulses is 1 (in fact, it means that the 150th of the 150 pulses has been cumulatively completed, combined with the number of 150 pulses transmitted in this segment, the total count is 150).

[0160] (VIII) DMA Acceleration Extension As an optional extension scheme for this example, it is suitable for situations where the microcontroller does not have internal programmable logic units and no external AND gates are configured. In addition to triggering an interrupt, the comparison event of the second timer channel can also be configured to trigger a DMA (Direct Memory Access) request. In response to this request, the DMA controller directly modifies the control register of either the first or second timer channel to stop the pulse. This stopping process is a purely hardware operation and requires no software processing. After the DMA transfer is completed, a DMA transfer completion interrupt is triggered, and the interrupt service routine performs the software configuration for the next segment's transmission. This extension scheme is suitable for situations where the microcontroller lacks internal hardware gating or external AND gates.

[0161] (ix) Multi-channel pulse output expansion In this example, each independent pulse output occupies only one timer resource. In scenarios requiring multiple outputs, multiple timers can run independently, with each channel achieving quantitative pulse transmission and automatic cutoff using the method described in this example. Since transmission and cutoff are both hardware processes, and each pulse does not require an interrupt, software resource contention between pulse channels and with other functional modules is not significant, making this suitable for PLC (Programmable Logic Controller) products with 6 to 12 pulse outputs.

[0162] It is important to emphasize that in commercially available small and medium-sized PLC products, low-end products generally use the single-timer interrupt method, which is extremely low in cost but has very limited application scenarios, typically used for applications with simple functions, few control channels, and low frequency. Mid-range products generally use the dual-timer linkage method, but due to the limited number and functions of timer peripherals in mainstream MCUs, the number of channels can only reach 4 to 6, and it often competes with the PLC's input counting function for timer resources. High-end products use FPGA solutions, which have no limit on the number of channels, but material and development and maintenance costs increase significantly. The solution of this invention can achieve stable multi-channel independent pulse output at a controllable cost, and can be applied to PLC products with 6 to 12 pulse outputs. It fills the mid-range market gap between pulse-type motion control products and bus-type motion control products, solving the problem of insufficient channel count in pulse products without FPGAs and avoiding the problem of excessively high prices for bus-type products. Specifically, each independent pulse output channel of this invention only occupies two channels of one timer, without the need for additional hardware counters or FPGAs, and can be implemented on mainstream MCUs, significantly reducing hardware costs in multi-channel pulse output scenarios.

[0163] A third aspect of the present invention provides a microcontroller, which includes a processor, a pulse output circuit, and a bus. The pulse output circuit is the output circuit of the first aspect of the present invention. The bus is used to couple the processor to the pulse output circuit; a system clock is used to drive a first timer channel and a second timer channel of the pulse output circuit to count. The processor is configured to execute the method of the second aspect of the present invention.

[0164] The embodiments of the present invention have been described in detail above with reference to the accompanying drawings. However, the present invention is not limited to the above embodiments, and various changes can be made within the scope of knowledge possessed by those skilled in the art without departing from the spirit of the present invention. Furthermore, the embodiments of the present invention and the features thereof can be combined with each other unless otherwise specified.

Claims

1. A pulse output circuit characterized by comprising: include: The timer includes a first timer channel, a second timer channel, and a synchronization control unit. The synchronization control unit is coupled to the first timer channel and the second timer channel, respectively. The synchronization control unit is used to control the first timer channel and the second timer channel to start counting synchronously. The first timer channel and the second timer channel are configured to count driven by the system clock. The first timer channel is configured to count cyclically with a first channel target period, and output a first valid level and a first invalid level that toggle at a first target duration interval within each first channel target period; The second timer channel is configured to output a second valid level after synchronously starting the count with the first timer channel, and to flip to a second invalid level after a second target time interval from the start of synchronous start, and to maintain the second invalid level after flipping; The logic operation module is coupled to the first timer channel and the second timer channel respectively, and is used to output a pulse valid level when the first timer channel outputs the first valid level and the second timer channel outputs the second valid level, and to output a pulse invalid level when the first timer channel outputs the first invalid level or the second timer channel outputs the second invalid level. The logic output terminal of the logic operation module is used to electrically connect to the target load device. The second target duration interval is greater than the first channel target period.

2. The pulse output circuit according to claim 1, characterized in that, The first timer channel includes a first counter, a first comparator, a first comparison register, and a first cycle register; the first counter is coupled to the first comparator and the first cycle register respectively, the first comparator is coupled to the first comparison register, and the first comparator is also coupled to the logic operation module; The first period register is used to store the target period of the first channel; The first comparison register is used to store the first comparison setting value; The first counter is configured in a cyclic counting mode to increment from a first initial value to the target period of the first channel, and automatically reloads to the first initial value when the target period of the first channel is reached, so as to enter the next cycle; The first comparator is used to compare the first current count value of the first counter with the first comparison setting value, and when the two match, it triggers the output level of the first timer channel to toggle between the first valid level and the first invalid level; The first timer channel is triggered by the cyclic counting of the first counter and the matching of the first comparator, and periodically toggles its output between the first valid level and the first invalid level.

3. The pulse output circuit according to claim 1 or 2, characterized in that, The second timer channel includes a second counter, a second comparator, a second comparison register, and a second cycle register; the second counter is coupled to the second comparator and the second cycle register respectively, the second comparator is coupled to the second comparison register, and the second comparator is also coupled to the logic operation module; The second cycle register is used to store a second cycle setting value, which is greater than the second comparison setting value stored in the second comparison register. The second comparison register is used to store the second comparison setting value corresponding to the second target duration interval; The second counter is configured in single-count mode for incrementing from a second initial value; The second comparator is used to compare the second current count value of the second counter with the second comparison setting value, and trigger the output level of the second timer channel to flip from the second valid level to the second invalid level when the two match; The reload function of the second cycle register is disabled so that the second counter stops counting after reaching the second cycle set value, and the second invalid level is maintained.

4. A pulse output method, applied to the pulse output circuit according to any one of claims 1-3, characterized in that, include: Obtain the inherent count limit value of the second timer channel, as well as the load target pulse setting number, load target pulse period, load level detection threshold, and load inherent response delay of the target load device; The first channel target period of the first timer channel is determined based on the load target pulse period; Based on the load level detection threshold, the first target duration interval of the first timer channel is determined; The upper limit of the number of pulses sent in a single pulse is determined based on the inherent upper limit of the count and the inherent response delay of the load. Get the actual number of pulses sent; The actual set number of pulses to be sent in this instance is determined based on the upper limit of the number of pulses to be sent in a single instance, the actual number of pulses already sent, and the set number of target pulses for the load. Based on the actual number of pulses sent in this transmission, determine the second target duration interval of the second timer channel; Configure the first channel target period and the first target duration interval to the first timer channel; Configure the second target duration interval to the second timer channel; After configuring the first timer channel and the second timer channel, a start command is sent to the synchronization control unit so that the logic output terminal of the logic operation module outputs a pulse valid level or a pulse invalid level to the target load device.

5. The method according to claim 4, characterized in that, The step of obtaining the actual number of transmitted pulses includes: Read the cumulative reference value of transmitted pulses; When the second counter of the second timer channel reaches the count value corresponding to the second target duration interval, a target interrupt is triggered. In response to the target interrupt, read the first current count value of the first timer channel and the second current count value of the second timer channel; The cumulative reference value of transmitted pulses is corrected based on the comparison result between the first current count value and the count value corresponding to the first target duration interval, and the comparison result between the second current count value and the count value corresponding to the second target duration interval. The corrected cumulative reference value of the transmitted pulses is used as the actual number of transmitted pulses.

6. The method according to claim 4, characterized in that, The step of determining the actual set number of pulses to be sent in this instance based on the upper limit of the number of pulses to be sent in a single instance, the actual number of pulses already sent, and the set number of target pulses for the load includes: Based on the target number of pulses set for the load and the actual number of pulses already sent, the number of remaining pulses to be sent is obtained; The remaining number of pulses to be sent is compared with the upper limit of the number of pulses to be sent in a single pulse. When the number of remaining pulses to be sent is greater than the upper limit of the number of pulses to be sent in a single pulse, the upper limit of the number of pulses to be sent in a single pulse is determined as the set number of pulses to be sent in this pulse. When the number of remaining pulses to be sent is not greater than the upper limit of the number of pulses to be sent in a single pulse, the number of remaining pulses to be sent is determined as the set number of pulses to be sent in this pulse.

7. The method according to claim 6, characterized in that, When the number of remaining pulses to be sent is greater than the upper limit of the number of pulses to be sent in a single pulse, the method further includes: After this pulse transmission is completed, the target interrupt is triggered by the second timer channel; In response to the target interruption, the actual number of pulses already transmitted is updated according to the set number of pulses actually transmitted in this pulse; Based on the updated actual number of pulses already sent, the actual number of pulses to be sent in this pulse is re-determined. Based on the redefined actual number of pulses sent in this pulse, the steps of determining the second target duration interval, configuring the first timer channel, configuring the second timer channel, and sending a start command to the synchronization control unit are repeated.

8. The method according to claim 4, characterized in that, Based on the inherent count upper limit and the inherent load response delay, the upper limit for the number of pulses transmitted in a single pulse is determined, including: Based on the inherent upper limit of the count, determine the theoretical maximum number of pulses that can be sent; Based on the inherent response delay of the load, determine the margin for the count value; Based on the theoretical maximum number of pulses that can be sent and the margin reserved for the count value, the upper limit of the number of pulses sent in a single instance is determined.

9. The method according to claim 4, characterized in that, The step of determining the second target duration interval of the second timer channel based on the actual number of pulses sent in this pulse includes: The reference duration interval is determined based on the actual number of pulses sent in this pulse and the target pulse period of the load. Obtain the preset safety margin; The second target duration interval is determined based on the difference between the baseline duration interval and the preset safety margin; Wherein, the preset safety margin is less than the target pulse period of the load.

10. A microcontroller unit, characterized in that, include: processor; The pulse output circuit according to any one of claims 1-3; A bus is used to couple the processor to the pulse output circuit; The system clock is used to drive the counting of the first and second timer channels of the pulse output circuit. The processor is configured to perform the method of any one of claims 4-9.

Citation Information

Patent Citations

  • Timer and control device

    CN117792352A

  • Module interlocking starting control system and method based on hardware state machine

    CN122111524A