Since our topic at the time was not only measuring the frequency, it was more troublesome to measure the duty cycle. The two measurement methods are closely related, so the method of measuring the duty cycle is also written.
Use platform: official STM32F429DISCOVERY development board, 180MHz main frequency, timer frequency 90MHz.
Related topics:
(1) The pulse signal frequency f_O is measured, the frequency range is 10 Hz to 2 MHz, and the absolute value of the measurement error is not more than 0.1%. (15 marks)
(2) Measure the duty cycle D of the pulse signal, the measurement range is 10% to 90%, and the absolute value of the measurement error is not more than 2%. (15 marks)
Idea 1: External interruption
Idea: This method is easy to think of and works for almost all MCUs (even 51). The method is also very simple, declare a count variable TIM_cnt, each time a rising edge / falling edge enters an interrupt, for TIM_cnt++, then timing statistics can be. If a duty cycle is required, then another timer can be used to count the time between the rising and falling edges.
Disadvantages: The defect is obvious. When the frequency is increased, it will frequently enter the interrupt and take up a lot of time. When the frequency exceeds 100 kHz, the interrupt program time will even exceed the pulse period, causing a huge error. At the same time, more importantly, the duty cycle that you want to measure will be more and more error due to the interrupt program.
Summary: We PASSed this plan for the first time, there is no relevant code (this code is very simple). However, this method can be used to measure the frequency at lower frequencies (below 10K). At lower frequencies, the duty cycle can be measured.
Idea 2: PWM input mode
Idea: Turn through the ST reference manual, there is such a mode in the timer:
In short, in theory, this mode allows direct measurement of frequency and duty cycle in hardware. At the time we found this model to be rejoicing, thinking that we could solve this problem in one step.
However, after measurement, it is found that the test data of this method is unstable and inaccurate, the data keeps beating, and the actual value is very different. These functions of ST often have such problems, such as the encoder mode of the timer, which may be stuck when frequent positive and negative transitions at 0 o'clock. Although these methods are trouble-free, the stability is not very good.
After linear compensation, the error can be reduced to some extent (parameters are different in different situations):
Freq=Frequency*2.2118-47.05;
Idea 3: Input Capture
Idea: In general, the altar friends who have a certain understanding of STM32 often think of using input capture in the measurement of frequency. First set to the rising edge trigger, when entering the interrupt (rising) the interval between the recording and the last interrupt (rising_last) (cycle, the reciprocal is the frequency). Set to the falling edge again, the difference between the time after entering the interrupt and the rising edge is the high-level time (falling-rising_last), and the high-level time dividing period is the duty ratio.
This method is especially accurate at low to medium frequencies (<100 kHz).
Disadvantages: A slightly experienced friend should be able to see that this method still brings a very high frequency of interruption. Under high frequency, the CPU time is completely occupied first. In addition, more importantly, interrupting the program for too long often leads to one or more interrupt signals being missed. The performance is the actual value and actual value of the measured value. The actual value is ×3 and so on. In the actual measurement, the highest frequency can be measured to be about 400 kHz.
Summary: This method has good accuracy at low frequencies (<100kHz). It is recommended to use this method at 10kHz, taking into account other programs. At the same time, you can refer to the following improved procedures to reduce the CPU load.
Improve:
In the aforesaid problem, the main factor limiting the frequency increase is the excessive interruption time (under the general application scenario, there are other program parts). So make the following improvements:
1. Use 2 channels, one to measure only the rising edge and the other to measure only the falling edge. This can reduce the delay of switching the trigger edge, the disadvantage is that an IO port is used.
2. Use registers to simplify the program
The reason for switching to TIM2 is because CH1 (PA0) of TIM5 is still a key input pin. Originally, I should have nothing to do with this. If the button is not pressed, it is not open. But later found that there is still an RC filter on the official development board...
So, before using someone else's program, be sure to check the circuit diagram carefully.
In this way, the highest frequency can reach about 1.1MHz, which is a small improvement. However, the underlying problem - interrupts too often - still exists.
The solution to the problem also exists. Essentially, we actually only need to read the CCR1 and CCR2 registers. In the process of memory copying, what do we think of when it comes to the transfer of large amounts of data? Obviously, we can easily think of it - using DMA. So, we use the input capture event to trigger the DMA to move the registers instead of triggering the interrupt, then store the data in an array and cycle through it. This way, we can view the data and calculate the frequency at any time.
Suggestions for improvement are listed below:
1. It can be set that only channel 2 performs the falling edge capture and triggers the interrupt, while channel 1 captures the rising edge without triggering the interrupt. Among the interrupt functions, CCR1 and CCR2 are read at a time. This can save a lot of time.
2. It is possible to perform a measurement first, and change the prescaler value PSC according to the measured value to improve the accuracy.
3. Interval sampling. For example, sampling every 10ms for 10ms.
Such an improvement should be able to increase the maximum sampling frequency to 2M. However, further improvements in frequency are still not possible. Because the main contradiction at this time is that the interrupt function takes too long, and the CPU is still processing the interrupt, this cycle ends, so that the final measured frequency is about an integer multiple of the real frequency. The schematic is as follows:
Therefore, the following methods are still recommended at high frequencies.
Idea 4: Use an external clock counter
This method is the recommended method when I answer questions in the past few days. The idea is to configure two timers. Timer a is set to external clock counter mode, timer b is set to timer (such as 50ms overflow, software timer can also be used), then timer b interrupt function is used to count timer a. In this time increment, simply calculate.
Disadvantages:
1. The duty cycle cannot be measured. The high frequency duty cycle measurement method is described below.
2. In the case of low frequency, the measurement accuracy is not as good as idea 3 (because the measurement period is 100ms, if the pulse period is 200ms...).
3. The input amplitude must exceed 3V. If it is not enough or exceeds, you need to add a preamplifier.
Summary: This method is very accurate. The measured error is 30Hz at 2MHz, which is 0.0015% (caused by the interrupt service routine, which can be corrected by linear compensation). Under 25MHz, the error is about 30Hz (it is impossible to achieve higher). The reason is that the maximum output frequency of the waveform generator is 25MHz^_^). At the same time, the problem of excessive interruption frequency is fundamentally solved. Due to low frequency problems, it is recommended to increase the sampling interval (change the TIM7 period) at low frequencies or use the input capture of Idea 3.
In addition, there is an inexplicable problem. If you do not include sprintf (str, "%3.3f", TIM_ExtCntFreq/1000.0), TIM_ExtCntFreq is always 0. I guess the problem is optimization, but adding volatile is useless, and time is not enough to ignore it.
Idea 5: ADC sampling measurement (probability measurement method)
For high-end oscilloscopes, measuring frequency is the method. In short, a series of data is sampled at high speed and then obtained by spectral analysis (such as Fast Fourier Transform FFT). F4 has FPU and DSP instructions, and the calculation speed is acceptable. However, the sampling frequency of the ADC is far from reaching. The official manual states that under the three-channel alternate sampling + DMA, the sampling rate can be up to 8.4M. However, according to the Shannon sampling theorem, the sampling frequency must be at least 2 times the signal. The 2M signal and the sampling rate of 8.4M, even if it can be calculated, the error is unacceptable. Therefore, ADC sampling cannot measure frequencies, especially high frequency.
However, the frequency cannot be measured, but the duty cycle, and even the overshoot and rise time (the time the signal rises from 10% to 90%) can be measured! The principle is also very simple. The basic principle of probability is mentioned in the university probability class:
When the number of samples n tends to infinity, the probability of event A approaches the frequency of statistics. Therefore, when the number of samples is larger, the sampled high level accounts for the frequency of the total number of samples, which is close to the probability - duty cycle!
Therefore, the basic idea is to sample at equal intervals (speed does not matter, but must be guaranteed to be equal probability sampling), and store the data in an array and refresh it repeatedly. In this way, the data in the array can be counted at any time to obtain the duty cycle data.
Disadvantages:
1. Low precision: The measured error is about 1.3% at 2MHz. It can't be counted at low frequency (for example, the frequency is 10Hz, and the ADC sampling time is 50ms. At this time, if the sampling time is just high level, the duty ratio is 1...) .
2. Large memory footprint: The data pool size is 65536, which occupies 64KB of memory.
3. There is a response delay: the measured "average duty cycle" instead of "instantaneous duty cycle". Since I used a waveform generator when testing, the output waveform is quite stable (the price of 1W+ is reasonable after all...), and in practical applications, it is generally impossible to achieve such a level, which is bound to bring a response delay (accurately speaking Is the sampling system integral inertia is larger).
4. The amplitude is too low (0.3V) and cannot be measured. If it is too high, it exceeds the maximum allowable value of the ADC. Therefore, different preamplifiers must be used as appropriate.
In fact, how to choose when using, you need to look at the actual situation. After all, this is just a low-cost solution.
In summary, make a summary of these methods:
External interrupt: easy to write and versatile. The disadvantage is that the interruption is frequent and the error is large.
PWM input: Full hardware completion, small CPU load, easy to write. The disadvantage is instability and large errors.
Input capture: up to approximately 400kHz. Low frequency accuracy is high, 10Hz can reach 0.01% or less, and 400kHz also has 3%. The disadvantage is that the interruption is frequent, the high frequency cannot be measured, and the amplitude must be between 3.3 and 5V.
External clock counter (preferred): Very high frequency (in theory should be 90MHz) and very low error (0.0015% at 2MHz and linear compensation). The disadvantage is that the low frequency accuracy is low, and the same amplitude must be between 3.3 and 5V.
ADC sampling frequency measurement method: It is difficult to measure the frequency. At high frequency, there is acceptable measurement accuracy for the duty cycle and rise time (about 1.3% at 2MHz), which cannot be measured at low frequencies. The amplitude is 0.3~3.3V, and the amplitude is arbitrary when added to the preamplifier.
ADC sampling spectrum analysis: dedicated for high-end oscilloscopes, STM32 abandonment therapy.
The method I used was: first, the ADC measured the amplitude and changed the preamplifier amplification factor accordingly, adjusted the amplitude to 3.3V, and measured the reference duty cycle. The frequency is then measured using an external clock counter. If it is high (>10000), it is confirmed as frequency data, and the ADC measurement duty is confirmed as duty data. Otherwise, the frequency and duty cycle data are measured using the input capture method.
For the linearity error of each method, linear compensation is used to improve the accuracy. In general, the data stored in the ROM is used as a parameter. When correction is required, the following correction ideas are used:
The waveform generator generates some preset parameter waveforms (eg 10Hz, 10%; 100K, 50%; 2M, 90%...), and the data is measured multiple times in different intervals, then the original data is x, and the real data is y. After removing the abnormal data, a linear regression of y=f(x) is performed, and the highest correlation coefficient is taken as a new parameter, and is stored in the ROM.
RJ45 connectors mostly used for data communication . SVLEC provide Male Female straight angle .RJ45 shielded connector with 4-pole 8-pole , Protection degree IP20 with Quick connection technology .Plastic or metal housings with good resistance against chemicals and oils. The resistance to aggressive media should be individually tested for your application. Further details on request.
Rj45 Connector,Rj45 Cat5 Connector,8Pin Rj45 Connector,RJ45 Shielded Connector, RJ45 connector 4 pin
Kunshan SVL Electric Co.,Ltd , https://www.svlelectric.com