* 1. Introduction to project functions
A simple USB power consumption tester was made to detect the current on the USB device. By using TI's INA180A2 current sensing amplifier, the voltage on the current-sensing resistor is amplified, and then the amplified voltage is collected through the ADC on the N32G430, and then the current passing through the current-sensing resistor is solved by the program.
*2. Project attributes
This project is the first time to participate in the joint summer training camp of National Technology and LiChuang.
* 3. Open source agreement
Altruism is self-interest,
embracing open source and giving the project unlimited value. The project is 100% open source;
*4. Hardware part
The power supply of the microcontroller uses ME6211C33 LDO as the power supply of the entire system. If this chip is not available, the common RT9013-3.3 can also be used as the power chip.
These two capacitors need at least 1uF of capacitance.

Then it is the design of the minimum system .
The minimum system has a reset circuit, a crystal oscillator circuit, and a boot selection circuit.
The reset circuit mainly makes the reset pin of the microcontroller generate a low level for several clock cycles. No external pull-up resistor is used here to ensure that the reset can be successful. The main reason is that the microcontroller already has a pull-up resistor inside. If an external pull-up resistor is connected, the resistance of the equivalent pull-up resistor will become smaller. Then, according to the zero-state response of the first-order circuit, the NRST pin may not be able to generate a low level time that allows the microcontroller to reset effectively. However, some experienced engineers can calculate that the microcontroller can be quickly reset while also ensuring the reset function. This requires everyone to study more information about the reset circuit in depth.

Crystal oscillator circuit
The design of the crystal oscillator circuit is also very sophisticated. Generally, the crystal oscillator circuit used by everyone is modeled after other circuits. Choose an 8MHz crystal oscillator and then add two 22pF load capacitors to complete it. But why choose a 22pF load capacitor instead of a 20pF? What problems will the load capacitor affect?

The size of the load capacitance will directly affect the frequency of the crystal oscillator. The load capacitance is the sum of the capacitances connected at both ends of the crystal oscillator. The load capacitance consists of the external matching capacitance, the parasitic capacitance on the PCB, and the parasitic capacitance between the crystal oscillator pins. The capacitance after the three are matched is the load capacitance. The size of the matching capacitance should be calculated according to the requirements in the specification, so that the crystal oscillator can output the signal as small as possible without deviation.
The formula for load capacitance is as follows:

Among them, CS is the parasitic capacitance between the two pins of the crystal, and CD represents the total capacitance from the output pin of the crystal oscillator circuit to the ground, including the PCB trace capacitance CPCB, the chip pin parasitic capacitance CO, and the external matching capacitance CL2, that is, CD=CPCB+CO+CL2. CG represents the total capacitance from the input pin of the crystal oscillator circuit to the ground, including the PCB trace capacitance CPCB, the chip pin parasitic capacitance CI, and the external matching capacitance CL1, that is, CG=CPCB+CI+CL1. Generally, CS is about 1pF, CI and CO are generally several picofarads. For details, please refer to the data sheet of the crystal oscillator to obtain CS=0.8pF, CI=CO=5pF, and CPCB=4pF. The load capacitance value on the crystal oscillator data sheet is 18pF, so

CD=CG=34.4pF, and the calculated matching capacitance value CL1=CL2=25pF.
Haha, is it different from directly using 22pF?
If you want to know more about crystal oscillator oscillation, you can take a look at the design of oscillation circuits in high-frequency electronic circuits, which is much more detailed than mine. The choice of
boot selection circuit
BOOT is actually to select the code in which area to run after the microcontroller is powered on. Assuming that the microcontroller is a mobile phone, the function of BOOT is similar to which APP you open first when you turn on your mobile phone in the morning.
Here, the design of the BOOT circuit is completed according to the official schematic diagram.

After completing the design of the minimum system, we have to start designing the sampling circuit. I did not copy the official sampling circuit in full. The main reason is that I have about 10 INA180A2 current sensing amplifiers. In order not to waste them, I still use the existing amplifiers on hand.

You can see that the circuit I circled in red is actually a filter circuit, because when collecting voltage signals, a lot of high-frequency noise will be introduced, and the sampling rate of the microcontroller is assumed to be 100K. After the FFT analysis of the collected signal, the maximum spectrum is 50K, so the noise outside 50KHz cannot be restored. Therefore, the front end of the sampling circuit will generally add an analog front-end circuit. This filter is to reduce the interference of high-frequency noise.
The output of INA180 is connected to the ADC of the microcontroller, and the ADC of the microcontroller is high impedance. The best filter is to connect an RC low-pass filter to the output of INA180, which will greatly reduce the output common-mode noise and the interference in the INA180 power supply voltage. However, adding an RC filter to the output will cause output attenuation, and before the amplifier is a differential signal, the best way to deal with differential signals is to start filtering at the input stage. The specific RC filter parameters can be referred to the figure below.

The addition of external series resistance will produce additional errors in the measurement; therefore, in order to reduce the impact on accuracy, the value of these series resistors must be kept below 10Ω. When a differential voltage is applied between the input pins, a mismatch in bias current will occur at the input pins. If additional external series filter resistors are added to the circuit, the mismatch in bias current causes a mismatch in the voltage drop across the filter resistors. This mismatch will produce a differential error voltage that will be subtracted from the voltage generated across the shunt resistor. This error will cause the voltage at the input pin of the device to be different from the voltage generated across the shunt resistor. If there is no additional series resistance, the mismatch in input bias current has little effect on the operation of the device. The amount of error added to the measurement by these external filter resistors can be calculated using the following formula:

Where the gain error factor is calculated using the following equation.

The amount of change in the differential voltage present at the input relative to the voltage generated at the shunt resistor is based on the value of the external series resistor (RF) and the internal input resistor RINT, as shown in the schematic diagram above. When the output voltage is compared to the voltage across the shunt resistor, the reduction in the shunt voltage reaching the device input pin is manifested as a gain error. A coefficient can be calculated to determine the amount of gain error introduced by adding the external series resistor. Calculate the expected deviation between the shunt voltage and the measured value at the device input pin.
I used the INA180A2 current sensing amplifier, which has a gain GAIN of 50, an internal resistor RINT of 10KΩ, and Rf of 10Ω. The Gain Error Factor formula gives:

Then using this filter will cause some gain error, but don't worry, you can calibrate the error later in the program.
.*5. Please enter the content for the software part
...
Note: If your project involves software development, please upload the corresponding engineering source code in the attachment. Here you can explain your software flow chart, functional module block diagram, explanation or popular science of related algorithms, source code structure, compilation environment construction and configuration, source code compilation method, program burning method, etc. in detail. It is recommended to introduce your ideas to others in the form of pictures and text.
*6. BOM list
Note: BOM list involved in the project. Please upload a screenshot of the BOM in this location. Please upload the list details in PDF format to the attachment. It is recommended to include model, brand, name, package, procurement channel, purpose, etc. The specific content and form should be based on the clear expression of the project composition.
*7. Competition LOGO verification


* 8. Demonstrate your project and record it into a video to upload.

Video requirements: Please shoot horizontally, with a resolution of no less than 1280×720, in Mp4/Mov format, and a single video size limited to 100M;
Video title: Lichuang Electric Competition: {Project Name}-{Video Module Name}; For example, Lichuang Electric Competition: "Autonomous Driving"-Team Introduction.
More details: https://diy.szlcsc.com/posts/d76d9cb41705430e9a54e7a5feed07a5