The present disclosure relates to a field of automobile technologies, specifically to a method for acquiring a pulse width modulation (PWM) signal, a computer device, and a computer-readable storage medium.
A pulse width modulation (PWM) signal technology is widely applied in various fields such as industrial automation, precision computer numerical control (CNC) machine tools, photoelectric instruments and aerospace. Key parameters for acquisition and measurement of a PWM signal wave are a duty cycle and a frequency.
In a first aspect, a method for acquiring a pulse width modulation (PWM) signal is provided according to embodiments of the present disclosure, and includes:
In a second aspect, a computer device including one or more processors and a storage device is further provided according to embodiments of the disclosure;
In a third aspect, a computer-readable storage medium with a computer program computer program stored thereon is provided according to embodiments of the disclosure. The computer program implements a method for acquiring a PWM signal when executed by a processor. The method includes:
In order to explain the technical solutions in embodiments of the present disclosure more clearly, attached drawings described in the embodiments or in the related art will be briefly introduced below. Obviously, the diagrams described as below are only some embodiments of the present disclosure. Those skilled in the art may obtain other illustrative creative work.
In order to understand the above purpose, features and advantages of the present disclosure more clearly, the present disclosure may be further described in combination with attached drawings and embodiments. It may be understood that, embodiments are a part of embodiments of the disclosure, rather than all embodiments. The specific embodiments described here are merely used for explaining the present disclosure, rather than a limitation of embodiments of the present disclosure. On the basis of embodiments of the present disclosure, all other embodiments obtained by those skilled in the art are within the protection scope of the present disclosure.
It should be noted that relational terms such as “first” and “second” are used herein to distinguish one entity or operation from another entity or operation without necessarily requiring or implying any such actual relationship or order between such entities or operations.
Currently, acquisition of a duty ratio and a frequency of a PWM signal is generally calculated by capturing count values of a rising edge and a falling edge of the PWM signal by an input capture unit (ICU) integrated in a microcontroller (MCU). For example, a period of the PWM signal is calculated by recording a count value between two adjacent rising edges in the PWM signal, and the duty ratio is calculated by recording a count value between the rising edge and the falling edge of the PWM signal. Since a fixed frequency division coefficient is adopted during counting and the counting is limited by a bit number of a timer of the ICU module, when a high-frequency PWM signal is acquired, the count value is inaccurate, an acquisition frequency will be jittered, and the jitter will be larger as the frequency is higher. Therefore, in the related art, the PWM signal in a wide frequency range cannot be accurately acquired.
At S110, an initial count value during a pulse period of the PWM signal is acquired based on an initial frequency division coefficient.
In embodiments of the disclosure, after detection of the PWM signal is started, the initial frequency division coefficient may be acquired first. The initial frequency division coefficient, may be, for example pre-stored by default. The initial frequency division coefficient generally limits a counting frequency of a timer. When count is performed during the pulse period of the PWM signal, a clock source is generally selected. A frequency of a clock source does not change, and a counting frequency may be determined based on the frequency of the clock source and the initial frequency division coefficient. The pulse period of the PWM signal refers to a period of one PWM signal, for example, a time between adjacent rising edges or a time between adjacent falling edges. The initial count value during the pulse period of the PWM signal refers to a corresponding count value within a time interval between two adjacent rising edges or two adjacent falling edges of the PWM signal based on the initial frequency division coefficient. The counting frequency indicates a counting speed of the timer. In the present disclosure, the counting frequency may be determined based on the initial frequency division coefficient, and an initial count value of the period of one PWM signal may be acquired based on the counting frequency.
At S120, a predicted frequency of the PWM signal is determined based on the initial count value during the pulse period of the PWM signal.
When the initial count value during the pulse period of the PWM signal is acquired based on the initial frequency division coefficient, the predicted frequency of the PWM signal may be obtained by predicting the frequency of the PWM signal based on the initial count value.
At S130, a corresponding target frequency division coefficient is determined based on the predicted frequency of the PWM signal.
The target frequency division coefficient corresponding to the predicted frequency of the PWM signal is searched based on the predicted frequency of the PWM signal determined at S120, so that the frequency division coefficient may be adjusted for PWM signals of different frequencies, and a corresponding target frequency division coefficient may be endowed.
At S140, a target count value during the pulse period of the PWM signal and a target count value during a pulse width time of the PWM signal are acquired based on the target frequency division coefficient, an actual frequency of the PWM signal is determined based on the target count value during the pulse period of the PWM signal, and an actual duty ratio of the PWM signal is determined based on the target count value during the pulse period of the PWM signal and the target count value during the pulse width time of the PWM signal.
After corresponding target frequency division coefficients are endowed for PWM signals of different frequencies, a count value is counted again based on the endowed target frequency division coefficient. That is, the target count value during the pulse period of the PWM signal and the target count value during the pulse width time of the PWM signal are acquired based on the target frequency division coefficient, the actual frequency of the PWM signal is determined based on the target count value during the pulse period of the PWM signal, and the actual duty ratio of the PWM signal is determined based on the target count value during the pulse period of the PWM signal and the target count value during the pulse width time of the PWM signal. The pulse width time of the PWM signal means an effective pulse width time in one pulse period of the PWM signal. For example, a high level of the PWM signal is an active pulse, then a duration of the high level in the one pulse period, i.e., a time between a rising edge and an adjacent falling edge is the pulse width time.
It should be noted that, the method for determining the actual frequency of the PWM signal based on the target count value during the pulse period of the PWM signal and determining the actual duty ratio of the PWM signal based on the target count value during the pulse period of the PWM signal and the target count value during the pulse width time of the PWM signal may adopt any existing method for calculating the actual frequency and the actual duty ratio of the PWM signal, which is not limited in embodiments of the present disclosure. Illustratively, for example, the target count value during the pulse period of the PWM signal acquired based on the target frequency division coefficient is X, the target count value during the pulse width time of the PWM signal acquired based on the target frequency division coefficient is Y, and the counting frequency of the timer is f based on the target frequency division coefficient, in this case, the actual frequency of the PWM signal is f/X, and the duty ratio of the PWM signal is Y/X.
In the disclosure, the frequency division coefficient may be self-adaptively adjusted based on the frequency of the input PWM signal, the target count value during the pulse period of the PWM signal and the target count value during the pulse width time of the PWM signal may be acquired based on the adjusted target frequency division coefficient, and the actual frequency of the PWM signal may be determined based on the target count value during the pulse period of the PWM signal, and the actual duty ratio of the PWM signal may be determined based on the target count value during the pulse period of the PWM signal and the target count value during the pulse width time of the PWM signal. Therefore, in embodiments of the disclosure, the PWM signal in each frequency range may be accurately acquired.
In an embodiment, in addition to a preset fixed initial frequency division coefficient, the initial frequency division coefficient may be determined based on an intermediate frequency of an acquisition frequency band of the PWM signal and the frequency of the clock source in embodiments of the disclosure.
When the rising edge or the falling edge of the PWM signal is captured, and the count value during the pulse period of the PWM signal is counted, one clock source generally needs to be selected, and the frequency of the clock source and the frequency division coefficient decide the count frequency. Since a frequency of the input PWM signal is unknown, in order to avoid affecting a calculation result caused by calculating the initial frequency division coefficient by using an over small frequency of the PWM signal or an over large frequency of the PWM signal, the initial frequency division coefficient is determined based on the intermediate frequency of the acquisition frequency band of the PWM signal and the frequency of the clock source in embodiments of the disclosure. For example, the acquisition frequency band of the PWM signal is 1 Hz-10 KHz, in this case, the initial frequency division coefficient may be determined based on a 5000 HZ PWM signal and the frequency of the clock source.
In an embodiment, the initial frequency division coefficient may be determined based on an equation Div0=f1/(f2*N), where f1 is a frequency of a clock source, f2 is an intermediate frequency of an acquisition frequency band of the PWM signal, Div0 is an initial frequency division coefficient, and N is a maximum count value of a timer.
In an embodiment, as illustrated in
At S101, an operation of acquiring the initial count value during the pulse period of the PWM signal based on the initial frequency division coefficient is triggered to be executed in response to detecting an input of the PWM signal.
In embodiments of the disclosure, it is first determined whether a function of capturing the PWM signal is activated, that is, it is detected whether there is an input of the PWM signal. The operation of acquiring the initial count value during the pulse period of the PWM signal based on the initial frequency division coefficient is triggered to be executed in response to determining the input of the PWM signal.
In an embodiment, as illustrated in
At S102, the operation of acquiring the initial count value during the pulse period of the PWM signal based on the initial frequency division coefficient is triggered to be executed in response to determining that count does not overflow.
The timer will automatically clear to zero based on count overflow, and the count during the pulse period of the PWM signal cannot be adopted for subsequent calculation. Therefore, in embodiments of the disclosure, it is first determined whether the count overflows or not, and the operation of acquiring the initial count value during the pulse period of the PWM signal based on the initial frequency division coefficient is triggered to be executed in response to determining that the count does not overflow.
In an embodiment, determining the corresponding target frequency division coefficient based on the predicted frequency of the PWM signal includes:
In embodiments of the disclosure, a correspondence table between an acquisition frequency band of a PWM signal and a frequency division coefficient may be preset. For example, acquisition frequency bands of the PWM signal include a band 1, a band 2 and a band 3. The band 1 is 1 HZ-500 HZ, the band 2 is 501 HZ-1000 HZ, and the band 3 is 1001 HZ-1500 HZ. A frequency division coefficient corresponding to the band 1 is Div1=64, a frequency division coefficient corresponding to the band 2 is Div2=32, and a frequency division coefficient corresponding to the band 3 is Div3=1. The frequency band to which the predicted frequency of the PWM signal belongs is searched from a preset correspondence table between the acquisition frequency band of the PWM signal and the frequency division coefficient, and the frequency division coefficient corresponding to the frequency band to which the predicted frequency of the PWM signal belongs is determined as the target frequency division coefficient of the PWM signal. For example, the predicted frequency of the PWM signal is 800 HZ, which belongs to the band 2, and the frequency division coefficient corresponding to the band 2 is 32, in this case, the target frequency division coefficient of the PWM signal is 32. The predicted frequency of the PWM signal is 100 HZ, which belongs to the band 1, and the frequency division coefficient corresponding to the band 1 is 64, in this case, the target frequency division coefficient of the PWM signal is 64.
In an embodiment, before determining the frequency division coefficient corresponding to the frequency band to which the predicted frequency of the PWM signal belongs as the target frequency division coefficient of the PWM signal, the method may further include: triggering an operation of determining the frequency division coefficient corresponding to the frequency band to which the predicted frequency of the PWM signal belongs as the target frequency division coefficient of the PWM signal in response to determining that the frequency division coefficient corresponding to the frequency band to which the predicted frequency of the PWM signal belongs is different from the initial frequency division coefficient.
In embodiments of the disclosure, before determining the frequency division coefficient corresponding to the frequency band to which the predicted frequency of the PWM signal belongs as the target frequency division coefficient of the PWM signal, it is first determined whether the frequency division coefficient corresponding to the frequency band to which the predicted frequency of the PWM signal belongs is same as the initial frequency division coefficient. When the frequency division coefficient corresponding to the frequency band to which the predicted frequency of the PWM signal belongs is different from the initial frequency division coefficient, the frequency division coefficient is readjusted then, which avoids that counting statistics is performed again when the frequency division coefficient corresponding to the frequency band to which the predicted frequency of the PWM signal is the same as the initial frequency division coefficient.
Correspondingly, on the basic of the frequency division coefficient corresponding to the frequency band to which the predicted frequency of the PWM signal belongs being the same as the initial frequency division coefficient, the predicted frequency of the PWM signal is taken as the actual frequency of the PWM signal, and a predicted duty ratio of the PWM signal determined based on the initial count value during the pulse period of the PWM signal and the initial count value during the pulse width time of the PWM signal is taken as the actual duty ratio of the PWM signal. An initial count value during the pulse width time of the PWM signal is acquired based on the initial frequency division coefficient.
For example, the predicted frequency of the PWM signal is 300 HZ, which belongs to the band 1 (1 Hz-500 Hz), the frequency division coefficient corresponding to the band 1 is 64. Based on the initial frequency division coefficient Div0=64, the 300 HZ predicted frequency of the PWM signal is directly taken as the actual frequency of the PWM signal without an operation of adjusting the frequency division coefficient. The predicted duty ratio of the PWM signal determined based on the initial count value during the pulse period of the PWM signal and the initial count value during the pulse width time of the PWM signal is taken as the actual duty ratio of the PWM signal, and the initial count value during the pulse width time of the PWM signal is acquired based on the initial frequency division coefficient Div. In a case that the predicted frequency of the PWM signal is 800 HZ, which belongs to the band 2 (501 Hz-1000 Hz), the frequency division coefficient corresponding to the band 2 is 32. The operation of adjusting the frequency division coefficient is required to be performed based on the initial frequency division coefficient Div0=64, and the frequency division coefficient 32 corresponding to the frequency band to which the predicted frequency of the PWM signal 300 HZ belongs is determined as the target frequency division coefficient of the PWM signal. In such a configuration, re-counting by using the same frequency division coefficient may be avoided, and repetition operation may be avoided.
In an embodiment, the higher the predicted frequency of the PWM signal, the smaller the target frequency division coefficient.
For example, a correspondence between a predicted frequency of a PWM signal and a target frequency division coefficient may be preset. By verification, it is found the higher the predicted frequency of the PWM signal, the smaller the target frequency division coefficient, so that the acquired frequency of the PWM signal and the duty ratio are relatively accurate.
In an embodiment, acquiring the target count value during the pulse period of the PWM signal and the target count value during the pulse width time of the PWM signal based on the target frequency division coefficient, determining the actual frequency of the PWM signal based on the target count value during the pulse period of the PWM signal, and determining the actual duty ratio of the PWM signal based on the target count value during the pulse period of the PWM signal and the target count value during the pulse width time of the PWM signal includes:
In embodiments of the disclosure, after the initial frequency division coefficient is adjusted to the target frequency division coefficient, abnormal counting may occur in a process of switching the frequency division coefficient. Therefore, in embodiments of the disclosure, after the initial frequency division coefficient is adjusted to the target frequency division coefficient, the target count value during the pulse period of the PWM signal acquired based on the target frequency division coefficient for the first time and the target count value during the pulse width time of the PWM signal acquired based on the target frequency division coefficient for the first time are discarded, the actual frequency of the PWM signal is determined based on the target count value during the pulse period of the PWM signal acquired based on the target frequency division coefficient for a second time, and the actual duty ratio of the PWM signal is determined based on the target count value during the pulse period of the PWM signal and the target count value during the pulse width time of the PWM signal acquired based on the target frequency division coefficient for a second time.
In an embodiment, acquiring the target count value during the pulse period of the PWM signal and the target count value during the pulse width time of the PWM signal based on the target frequency division coefficient, determining the actual frequency of the PWM signal based on the target count value during the pulse period of the PWM signal, and determining the actual duty ratio of the PWM signal based on the target count value during the pulse period of the PWM signal and the target count value during the pulse width time of the PWM signal includes:
In embodiments of the disclosure, after the initial frequency division coefficient is adjusted to the target frequency division coefficient, in order to avoid abnormal counting that may occur in the process of switching the frequency division coefficient, resulting in inaccurately acquiring the actual frequency of the PWM signal and the actual duty ratio of the PWM signal, and the actual duty ratio of the PWM signal determined based on the target count value during the pulse period of the PWM signal acquired based on the target frequency division coefficient for the first time may be compared with the predicted frequency of the PWM signal. When the difference value between the actual duty ratio of the PWM signal determined based on the target count value during the pulse period of the PWM signal acquired based on the target frequency division coefficient for the first time and the predicted frequency of the PWM signal is greater than the preset threshold, it indicates that the actual frequency of the PWM signal determined based on the target count value during the pulse period of the PWM signal acquired based on the target frequency division coefficient for the first time is inaccurate, the actual frequency of the PWM signal is determined based on the target count value during the pulse period of the PWM signal acquired based on the target frequency division coefficient for the second time, and the actual duty ratio of the PWM signal is determined based on the target count value during the pulse period of the PWM signal and the target count value during the pulse width time of the PWM signal acquired based on the target frequency division coefficient for the second time. When the difference value between the actual frequency of the PWM signal determined based on the target count value during the pulse period of the PWM signal acquired based on the target frequency division coefficient for the first time and the predicted frequency of the PWM signal is less than or equal to the preset threshold, the actual frequency of the PWM signal determined based on the target count value during the pulse period of the PWM signal acquired based on the target frequency division coefficient for the first time may be taken as a finally actual frequency of the PWM signal, and the actual duty ratio of the PWM signal determined based on the target count value during the pulse period of the PWM signal and the target count value during the pulse width time of the PWM signal acquired based on the target frequency division coefficient for the second time is taken as a final actual duty ratio of the PWM signal.
An apparatus for acquiring a pulse width modulation (PWM) signal is provided according to embodiments of the present disclosure.
The initial count value acquiring module 11 is configured to acquire an initial count value during a pulse period of the PWM signal based on an initial frequency division coefficient. The frequency prediction module 12 is configured to determine a predicted frequency of the PWM signal based on the initial count value during the pulse period of the PWM signal. The target frequency division coefficient determining module 13 is configured to a corresponding target frequency division coefficient based on the predicted frequency of the PWM signal. The actual frequency and actual duty radio determining module 14 is configured to acquire a target count value during the pulse period of the PWM signal and a target count value during a pulse width time of the PWM signal based on the target frequency division coefficient, determine an actual frequency of the PWM signal based on the target count value during the pulse period of the PWM signal, and determine an actual duty ratio of the PWM signal based on the target count value during the pulse period of the PWM signal and the target count value during the pulse width time of the PWM signal.
It needs to be noted that, the foregoing explanation of the method embodiment for acquiring the PWM signal is also applicable to the apparatus embodiment for acquiring the PWM signal, which will not be repeated here. The specific way in which each module performs the operation in apparatus embodiments for acquiring the PWM signal has been described in the method embodiments and will not be elaborated here.
In an embodiment, the apparatus for acquiring the PWM signal further includes an initial frequency division determining module. The initial frequency division determining module is configured to determine the initial frequency division coefficient based on an intermediate frequency of an acquisition frequency band of the PWM signal and a frequency of a clock source.
In an embodiment, the initial frequency division coefficient determining module is specifically configured to determine the initial frequency division coefficient based on an equation:
Div0=f1/(f2*N);
In an embodiment, the apparatus for acquiring the PWM signal further includes a PWM signal input detection module. The PWM signal input detection module is configured to trigger the initial count value acquiring module to execute an operation of acquiring the initial count value during one PWM signal based on the initial frequency division coefficient in response to detecting an input of the PWM signal.
In an embodiment, the apparatus for acquiring the PWM signal further includes a count overflow determining module. The count overflow determining module is configured to trigger the initial count value acquiring module to execute the operation of acquiring the initial count value during one pulse period of the PWM signal based on the initial frequency division coefficient in response to determining that count does not overflow.
In an embodiment, the target frequency division coefficient determining module is specifically configured to determine a frequency band to which the predicted frequency of the PWM signal belongs; and determine the frequency division coefficient corresponding to the frequency band to which the predicted frequency of the PWM signal belongs as the target frequency division coefficient of the PWM signal.
In an embodiment, the apparatus for acquiring the PWM signal further includes a frequency division coefficient comparison module. The frequency division coefficient comparison module is configured to trigger an operation of determining the frequency division coefficient corresponding to the frequency band to which the predicted frequency of the PWM signal belongs as the target frequency division coefficient of the PWM signal in response to determining that the frequency division coefficient corresponding to the frequency band to which the predicted frequency of the PWM signal belongs is different from the initial frequency division coefficient.
In an embodiment, the frequency division coefficient comparison module is configured to, in response to determining that the frequency division coefficient corresponding to the frequency band to which the predicted frequency of the PWM signal belongs is same as the initial frequency division coefficient, trigger the actual frequency and actual duty ratio determining module to perform operations of taking the predicted frequency of the PWM signal as the actual frequency of the PWM signal, and taking the predicted duty ratio of the PWM signal determined based on the initial count value during the pulse period of the PWM signal to the initial count value during the pulse width time of the PWM signal as the actual duty ratio of the PWM signal.
The initial count value during the pulse period of the PWM signal is acquired based on the initial frequency division coefficient.
In an embodiment, the higher the predicted frequency of the PWM signal, the smaller the target frequency division coefficient.
In an embodiment, the actual frequency and actual duty ratio determining module is specifically configured to:
In an embodiment, the actual frequency and actual duty ratio determining module is specifically configured to:
A computer device including one or more processors and a storage device is further provided according to embodiments of the disclosure. The storage device is configured to store one or more programs. When the one or more programs are executed by the one or more processors, the one or more processors are caused to implement the method for acquiring the PWM signal as described in the first aspect of any embodiment.
As illustrated in
The bus 23 represents one or more of several types of bus structures, including a memory bus or a memory controller, a peripheral bus, a graphics acceleration port, a processor, or a local bus with any of a plurality of bus structures. For example, the architectures include, but are not limited to, an industry standard architecture (ISA) bus, a micro channel architecture (MAC) bus, an enhanced ISA bus, a video electronics standards association (hereinafter referred to as VESA) local bus and a peripheral component interconnection (PCI) bus.
In some implementation solutions, the computer device includes a variety of computer system readable media. The media may be any available media that may be accessed by the computer device, including volatile and non-volatile media, and removable and non-removable media.
The memory 22 may include a computer system readable medium in the form of a volatile memory, for example, a random-access memory (RAM) and/or a cache memory. The computer device may further include other volatile and non-volatile media, and removable and non-removable media. As an example only, a storage system may be configured to read and write a non-removable and non-volatile magnetic medium (not shown in
A computer-readable storage medium with a computer program stored thereon is provided according to embodiments of the disclosure. The computer program implements the method for acquiring the PWM signal as described in any embodiment of the present disclosure when executed by a processor.
A computer program product including a computer program is further provided according to embodiments of the disclosure. When the computer program is executed by a processor, the method for acquiring the PWM signal as described in the first aspect of any embodiment of the present disclosure is implemented.
A computer program including a computer program code is further provided according to embodiments of the disclosure. When the computer program code is running on a computer, the computer is caused to perform the method for acquiring the PWM signal as described in the first aspect of any embodiment of the present disclosure.
It needs to be noted that, the foregoing explanation of the method embodiments and the apparatus embodiments is also applicable to the device, the computer-readable storage medium, the computer program product and the computer program, which will not be repeated here.
In the above embodiments, functions may be wholly or partially implemented by a software, a hardware, a firmware, or any combination thereof. When implemented by a software, the functions may be implemented in whole or in part in the form of a computer program product. The computer program product includes one or more computer instructions. Procedures or functions according to embodiments of the present disclosure are wholly or partially generated when the computer program instruction is loaded and executed on a computer.
Any reference to a memory, a database or other media used in embodiments provided in the application may include at least one of a non-volatile memory or a volatile memory. The non-volatile memory may include a read-only memory (ROM), a magnetic disk, a floppy disk, an optical disk or an optical memory. The volatile memory may include a random-access memory (RAM) or an external high-speed cache memory. As an illustration rather than a limitation, the RAM is available in a plurality of forms, such as a static random-access memory (SRAM) and a dynamic random-access memory (DRAM).
Technical features of the above embodiments may be combined arbitrarily. For the purpose of a brief description, all possible combinations of the technical features of the above embodiments are not described. However, as long as there is no contradiction in the combination of the technical features, it shall be deemed to be the scope of the description.
The above embodiments only express several implementations of the disclosure, and the description is specific and detailed, but it cannot be understood as a limitation of the scope of the invention patent. It should be noted that, for those skilled in the art, several improvements and modifications may be made without departing from the principles of the present disclosure, and these improvements and modifications are also considered to be within the protection scope of the present disclosure. Therefore, the protection scope of the present disclosure shall be subject to the protection scope of claims.
All embodiments of the disclosure may be executed separately or in combination with other embodiments, and are deemed within a protection scope of the disclosure.
Number | Date | Country | Kind |
---|---|---|---|
202111070278.2 | Sep 2021 | CN | national |
This application is a US national phase application of International Application No. PCT/CN2022/117112, filed on Sep. 5, 2022, which claims priority to Patent Application No. 202111070278.2, filed on Sep. 13, 2021, the entire contents of which are incorporated herein by reference.
Filing Document | Filing Date | Country | Kind |
---|---|---|---|
PCT/CN2022/117112 | 9/5/2022 | WO |