This is the 19th day of my participation in the August Wenwen Challenge.More challenges in August

Current sampling is very important in switching power supply. The common calculation methods of current sampling are average value sampling and effective value sampling. The two current sampling algorithms are analyzed and compared.

Hardware connection diagram

Ac 220V is changed into DC 310V after full-bridge rectification filtering, dc 310V supplies power to the back-end load, and the sampling resistance Rs is connected in series in the dc circuit ground wire.

When the device is operating, the current flows from the positive pole of the rectifier bridge through the load, and then back to the negative pole of the rectifier bridge through the sampling resistor Rs. Because the resistance value of the sampling resistance Rs is very small, the pressure drop on the sampling resistance is only a few tenths of a volt, so the sampling resistance will not affect the load. Since the voltage drop on the sampling resistor is very small, MCU cannot use it directly. Therefore, by amplifying the voltage on the sampling resistor to a range that MCU can directly recognize, and then reading the voltage signal from the amplifier circuit directly through the ADC port on MCU, the value of the input current can be calculated. In this way, the input current of power supply can be calculated only through a sampling resistor combined with the algorithm of software, which simplifies the hardware circuit, saves the development cost, and shortens the development cycle.

Average sampling:

Through the ADC input port of MCU, the voltage value sampled can be read directly. Take the sample to N data and find the arithmetic mean directly.

The software process is as follows:

When the number of samples is less than N, continue to sample. When the number of samples is greater than N, calculate the sum of N sample values, and then calculate the arithmetic mean value of N sample data. Then reset the number of samples counter to continue with the next sample. This average can then be used to calculate the current value of the power input.

The sampling number N is selected according to different waveforms. The selection principle is to ensure that the sampling value must contain at least one period of the input current waveform. In this way, the maximum and minimum values in the waveform can be sampled and the calculated average value can be more accurate.

The software core code is as follows:

u16 get_ave( void )
{
    static  u8  cnt = 0;
    static u32 sum = 0;
    static u16 ave = 0;

    if( cnt < N)
    {
        sum += ReadVol_CH4();                 // Summation of sample values
        cnt++;
    }
    if( cnt >= N)
    {
        ave = sum / N;                                // Calculate the arithmetic average value
        cnt = 0;
        sum = 0;
    }
    return ave;
}
Copy the code

Sampling data analysis:

A sinusoidal wave with an amplitude of 0.2V- 2V and a frequency of 100Hz is generated by the function generator. It is used to simulate the power input current waveform, and the sine wave generated by the function generator is directly sent to the ADC sampling port of MCU.

Use oscilloscope to observe the input waveform as shown below:

Since the data sampled by MCU is difficult to observe, the data sampled by ADC port is sent out through the serial port, and then the data is drawn into a curve, and the stability of the sampling value is judged by observing the fluctuation of the curve.

MCU sampling data and curves are shown below:

The maximum value and minimum value of data sampled by MCU are 472 and 467 respectively. The ADC resolution of MCU is 10 bits, and the maximum sampling value of ADC can be calculated as 2^10=1024. MCU is supplied with 5V power, and the voltage of sampling value can be calculated as

472/10245 = 467/10245 = 2.2802734375 2.3046875

According to the calculation, the ADC sampling value fluctuates between 2.28V and 2.3V.

According to the Vavg measured on the oscilloscope in Figure 3 =2.22V, the average waveform sent to the ADC port is 2.22V, and the value calculated by MCU through average sampling is between 2.28V and 2.3V. It indicates that the values calculated by MCU are basically accurate. It can also be seen from the green data sampling waveform in FIG. 4 that the average value calculated by MCU fluctuates slightly.

Reduce the input waveform amplitude to 0.5V–1.5V for testing, and the input waveform is shown as follows:

MCU sampling data and curves are shown below:

The maximum value of MCU sampling is 425, and the minimum value is 421, which is converted to the voltage value

425/1024 * 5 = 2.0751953125 421/1024 * 5 = 2.0556640625

MCU sampling values are between 2.05V and 2.07V. According to Vavg=2.02V measured by oscilloscope in FIG. 5, it can be seen that the average waveform sent into ADC port is 2.02V. It can be seen that the values sampled by MCU are close to the actual values. It can be seen from the green data waveform in FIG. 6 that the average fluctuation is relatively small.

By comparing the above two groups of tests, it can be found that when the amplitude of the ADC port input waveform decreases, the average value calculated by MCU is closer to the real average value of the input waveform.

Sampling time analysis:

MCU sampling requires accuracy and real-time performance. That is to say, not only should the sampling be accurate, but also the sampling speed should be fast, and the input current changes can be timely reflected. The accuracy of sampling has been verified above, and the real-time of sampling is verified next. Due to the fast sampling speed of MCU, it is difficult to directly observe how long it takes for a sampling. Here, the on-off LED indicator is used to indirectly observe the time required for an ADC sampling. LED light – sampling data at a time — – > > LED out the sampling data at a time — – > – > LED light – > sampling data at a time, so have been circulating. Before each sampling, let the state of the LED indicator light change once, so that the LED light from on to off, from off to on is a sampling of data. Then use oscilloscope to observe the LED lamp high and low level of time, you can see indirectly, sampling a time of data. The relevant codes are as follows:

While(1)
{
    LED3 = 1;                    / / the LED light
    val = get_ave();               / / sampling
    LED3 = 0;                    / / the LED out
    val = get_ave();               / / sampling
}
Copy the code

Oscilloscope test LED waveform as shown below:

It can be seen from the oscilloscope measurement value that + WID = 60US – WID =59us indicates that the high level duration of LED is 60us and the low level duration is 59us. It can be seen indirectly from the LED level that it takes about 60us to sample a data MCU.

Effective value sampling:

Through the ADC input port of MCU, the voltage value sampled can be read directly. Take the summation of the data sampled in one period, and then take the average of the summation, and finally take the square root. The result is the root mean square value, which is the effective value.

The software process is as follows:

After the program starts sampling, the timer starts timing, and continues sampling when the timing time is less than T. When the timing time is greater than T, the sampling ends. And then you take the square of the sample, you take the sum, and then you take the sum and you take the square root. The value is the root mean square value of the current waveform within the sampling time T, which is the effective value. This RMS value can then be used to calculate the RMS value of the current input current.

The value of sampling time T is selected according to different waveforms. The selection principle is to ensure that the sampling value is a period of the input current waveform. The calculated value is the periodic RMS value, which is closest to the true effective value of the input waveform.

The software core code is as follows:

u16 get_rms( void )
{
    static u16 ave_value = 0;
    u16 tem_time = 0;
    u8 cnt = 0, num = 0;
    u32 sum = 0, value = 0;
    u16 rms = 0;

    while( cnt <= T)                            // Sample a period
    {
        if( tem_time ! = time_cnt ) { tem_time = time_cnt; cnt++; } value = ReadVol_CH4(); sum += value * value; num++; } rms = (u16)sqrt( sum / num );                 // find the root mean square value
    ave_value = rms;
    return ave_value;
}
Copy the code

Sampling data analysis:

A sinusoidal wave with an amplitude of 0.2V- 2V and a frequency of 100Hz is generated by the function generator. It is used to simulate the power input current waveform, and the sine wave generated by the function generator is directly sent to the ADC sampling port of MCU.

Use oscilloscope to observe the input waveform as shown below:

MCU sampling data and curves are shown below:

MCU sampling maximum value is 536, minimum value is 504. Calculate the sampling voltage value as

536/10245 = 504/10245 = 2.4609375 2.6171875

It can be seen from the calculation that the MCU sampling value fluctuates between 2.62V and 2.46V.

As can be seen from The oscilloscope waveform in Figure 9, the effective value of the input waveform Vrms=2.56V, and the effective value calculated by MCU sampling fluctuates around the real effective value. The fluctuation of sampling value can be seen from the green waveform in Figure 10.

Reduce the input waveform amplitude to 0.5V–1.5V for testing, and the input waveform is shown as follows:

MCU sampling data curve is shown in the figure below:

The maximum value of MCU sampling was 447 and the minimum value was 428. Calculate the sampling voltage value as

447/1024 * 5 = 2.1826171875 428/1024 * 5 = 2.08984375

It can be seen from the calculation that the MCU sampling value fluctuates between 2.18V and 2.08V.

As can be seen from The oscilloscope waveform in Figure 9, the effective value of the input waveform Vrms=2.15V, and the effective value calculated by MCU sampling fluctuates around the real effective value. The fluctuation of sampling value can be seen from the green waveform in Figure 11.

By comparing the above two groups of tests, it can be found that when the amplitude of the ADC port input waveform decreases, the accuracy of the RMS calculated by MCU does not change significantly, and the calculated RMS fluctuate around the real RMS. The calculation of RMS is not affected by waveform amplitude.

Sampling time analysis:

As MCU sampling speed is relatively fast, it is difficult to directly observe how long it takes to sample once. Here, LED indicator light is also used to indirectly observe how long it takes to sample once. LED light – sampling data at a time — – > > LED out the sampling data at a time — – > – > LED light – > sampling data at a time. Before each sampling, let the state of the LED indicator light change once, so that the LED light from on to off, from off to on is a sampling of data. Then use oscilloscope to observe the LED lamp high and low level of time, you can see indirectly, sampling a time of data. The relevant codes are as follows:

while( 1 )
{
    LED3 = 1;                    / / the LED light
    val = get_rms();                   / / sampling
    LED3 = 0;                    / / the LED out
    val = get_rms();                   / / sampling
}
Copy the code

Oscilloscope test LED waveform as shown below:

Through oscilloscope observation, it can be seen that + WID =10.4ms – WID =10ms indicates that the high level duration of LED is 10.4ms and the low level duration is 10ms. It can be seen indirectly from the LED level that it takes about 10ms to sample a data MCU.

Conclusion:

This can be seen from the above analysis and comparison of mean and effective values samples. The average sampling time is short and the sampling speed is fast, but it has a large relationship with the input waveform. The smaller the amplitude of the input waveform is, the smaller the error between the sampling value and the actual value is. The sampling time of effective values is long and the sampling speed is slow, but it is not affected by the amplitude of the input waveform.

According to the application experience in practical projects, if the waveform of sampling is DC or AC with small distortion and the real-time requirement of the system is relatively high, the average sampling method can be considered. If the operating environment of the equipment is complex, the input waveform will have different distortion in different environments, and the real-time requirement of the system is not high, the effective value sampling method can be considered. RMS sampling is equivalent to calculating the area of the input waveform, so no matter how complex the field environment, how serious the input waveform distortion. The current value calculated by the effective value sampling method is closest to the true value. Therefore, the current value calculated by RMS sampling method is more stable than that calculated by mean sampling method in switching power supply circuit.