A kind of PWM ripple acquisition methods based on timer
Technical field
The present invention provides a kind of PWM ripple acquisition methods based on timer, and it is long that it is related to a kind of pulsewidth based on timer
Spend the acquisition method of modulated signal PWM ripples, it is proposed that a kind of method for the collection that PWM ripples are realized using timer.This base
In the PWM ripple acquisition methods of timer, the collection that pwm signal is realized in various types of timers can be generalized to, it is adaptable to
A variety of industrial control fields.Example explanation is carried out by example of CDT2000 boards in the present invention, the PWM ripples gathered are to use
Come the 14ms cycle PWM ripples of the motor-driven work of control flaps, 1.1~1.9ms of pulsewidth length range.
Background technology
PWM ripples (PWM, i.e. Pulse Width Modulation, pulse width modulation) are a kind of industry of technology maturation
Control signal, in terms of being widely used in the Industry Controls such as motor speed control, actuator Stroke Control, be using numeral output come
A kind of very effective technology being controlled to analog circuit.
The oscillogram of PWM ripples is shown in Fig. 1, tcFor pulse period, t1、t2To characterize the pulsewidth length of PWM ripple effective informations.This
The described PWM ripples of invention derive from the demand of steering wheel start control, it is necessary to gather a kind of cycle 14ms, the scope of pulsewidth length
1.1~1.9ms pulse-width signal.
The collection of PWM ripples is carried out, the pulsewidth measurement of length of PWM ripples is exactly realized using some equipment.
Traditional PWM ripple acquisition methods, using respectively record pulse width signal rising edge and trailing edge the generation moment, then
Two moment are made the difference and try to achieve pulsewidth length.The shortcoming of this method is the spread of voltage or jagged when pulse width signal
When, error occurs in the judgement of rising edge and trailing edge, causes signal acquisition to make a mistake.
PWM ripple acquisition methods used in the present invention, using PWM ripples signal as the trigger source of timer, set timer
Triggered for high level, then take design to interrupt the count value that timer is read in service function timing.Utilize the meter in timer
The variable quantity of numerical value calculates the duration of high level, the interference that will not thus be brought by spread of voltage and burr,
With very strong antijamming capability.
The content of the invention
<1>Goal of the invention
Present invention aims at a kind of acquisition method of the PWM ripples based on timer is designed, using timer as main hardware
Equipment, by certain physical connection and interrupts the coding of service function, realizes and is applicable to various different cycles and pulsewidth
The collection of PWM ripples.
<2>Technical scheme
A kind of PWM ripple acquisition methods based on timer of the present invention, its step is as follows:
Step one:It is cycle count mode of operation to set a timer, and each cycle triggers a tick interrupt;
Step 2:It is provided for the timer of PWM ripples collection and triggers the mode of operation that subtrahend is counted, PWM ripples for high level
The input of collection timer is connected with the PWM ripple signals to be gathered;
Step 3:Interruption service function is write, tick interrupt is responded after each cycle count terminates.Wherein, in step
" mode of operation that high level triggering subtrahend is counted " described in two, is another mode of operation that can be set of timer, should
The function of mode of operation is:Timing can be triggered when the high level that the input that PWM ripples gather timer is PWM ripple signals arrives
Terminate to subtract certainly when device progress subtracts certainly, low level arrives.
Wherein, " the cycle count mode of operation " described in step one, is a kind of work that can be configured of timer
Operation mode, gives a setting value, and then it can turn again to this value and continue to subtract certainly from this value from reducing to zero, Zhou Erfu
Begin, cycle count period=setting value/cycle count timer clock frequency;When triggering one when counting and reducing to zero certainly every time
Clock is interrupted;In order to realize the collection of PWM ripples, cycle count period is necessarily less than the two of PWM ripple low duration minimum values
/ mono-, so just can ensure that " interruption service function " described in step 3 can read described in step 2 " PWM ripples are adopted
Collection timer " counts count value when stopping.
Wherein, " the interruption service function " described in step 3, is at the end of each cycle count timer period
A called function is interrupted in triggering, and its function is:Each cycle count timer period reads a PWM ripples collection timing
The count value of device, judges that the PWM ripples in two neighboring cycle gather the count value of timer;If the PWM ripples in continuous two cycles
The count value of collection timer is equal and when being not equal to initial count value, shows that pulse high level is over, pulsewidth length
=(initial count value-PWM ripples of PWM ripples collection timer gather the counting end value of timer)/PWM ripples collection timing
Device clock frequency, reads pulsewidth length value, and PWM ripples are gathered into timer heavy duty;Otherwise it is without any processing.
The timing diagram of PWM ripples is gathered with timer as shown in Figure 2.
<3>Advantage effect
PWM ripple acquisition methods based on timer, timer count rather than pulse capture are triggered as a result of high level
Acquisition mode, even in signal occur in that such as burr or other bounce, on collection result also almost do not influence so that significantly
Improve the antijamming capability of PWM collections.
Brief description of the drawings
The present invention is further described below in conjunction with the accompanying drawings.
Fig. 1 is a kind of waveform diagram of PWM ripples.
Fig. 2 is the timing diagram that PWM ripples are gathered with timer, the explanation for having correlated variables in remarks;Wherein, 1, circulation
Counter timer counts cycle T0It is necessarily less than 1/2nd of PWM ripple low level length maximums;2nd, in continuous two interruptions
Inside read identical and be not equal to TmaxCount value, then it is assumed that high level has terminated;3rd, the interruption that high level terminates is being determined
It is interior, read PWM ripple pulsewidths t1=(Tmax-T1)/fTimer clock frequency, the count value of heavily loaded PWM ripples collection timer is Tmax。
Fig. 3 is that the circuit and signal for realizing the collection of PWM ripples using CDT2000 timer function in application example are connected
Figure.
Fig. 4 is using CDT2000 timer function to carry out the timing diagram of PWM ripple collections in application example.
Fig. 5 the method for the invention FB(flow block)s.
Embodiment
Implementation steps
As Figure 1-5, a kind of PWM ripple acquisition methods based on timer of the present invention, as shown in Figure 5, its step is as follows:
Step one:It is cycle count mode of operation to set a timer, and each cycle triggers a tick interrupt, circulation meter
It is necessarily less than 1/2nd of PWM ripple low duration minimum values one number time.
Step 2:It is the counting of high level triggering subtrahend, rising edge flip-flop number weight to be provided for the timer of PWM ripples collection
The mode of operation of load, the input of PWM ripples collection timer is connected with the PWM ripple signals to be gathered.
Step 3:Interruption service function is write, tick interrupt is responded after each cycle count terminates;Interrupt service function
Each cycle reads the count value that a PWM ripple gathers timer, judges the timer value in two neighboring cycle;If continuous two
The numerical value in individual cycle is equal and when being not equal to initial count value, shows that pulse high level is over, pulsewidth length=(initial
Count value-counting end value)/timer clock frequency, reads pulsewidth length value, and the collection timing of PWM ripples is thought highly of
Carry.
Application example
The example explained below comes from actual application case, using CDT2000 boards as timer device, with PC/104
Embedded computer realizes the response for interrupting service function.In this case, it is necessary to gather 6 road PWM ripples.6 road PWM of collection
Both from the remote signal of Futaba receivers, wherein it is original servos control letter that 1 tunnel, which is mode switching signal, 5 road PWM ripples,
Number.
The PWM ripples for needing collection are a kind of cycle 14ms, 1.1~1.9ms of pulsewidth length range pulse-width signal.
Specific practice is as follows:
1st, runs pattern is set
Four 82C54 timing controllers of CDT2000 boards, it is each that there are three independent timers, 12 are obtained altogether
Individual independent timer, carries out the collection of PWM ripples and has only used wherein seven timers, as shown in Figure 4.The connection of each timer and
The setting of working method is expressed as follows:
Timer 1-0:Counted by loop cycle of 2ms, and produce interruption.
Timer 3-0~timer 4-2:GATE mouthfuls start counting up when being high level, and low level stops counting.
Wherein, timer 1-0 is as the triggering timer for interrupting service function, and the setting work period is 2ms, i.e., weekly
Phase initial count value is set as 16000.
2nd, the description of service function function is interrupted
The principle gathered on PWM ripples in service function is interrupted to be expressed as follows (timing diagram of PWM ripples collection is shown in Fig. 5):
1) PWM collection timers 3-0~timer 4-2 count value is read by the cycle of 2ms;
If 2) count results are not 65535 and equal to continuous adjacent twice, the pulsewidth that this count value is T, then PWM ripples is remembered
As t=(65535-T)/(8 × 106), it is maximum (65535) by the heavy duty of this timer;
If 3) count results are unequal twice for continuous adjacent, without any processing.