1. Project Function Introduction:
This project was submitted to the LCSC training camp.
Maximum input 20V 3A. Implements USB power and ripple detection. Supports USB 2.0 passthrough or QC3.0 decoy. DC buck current limiting output.
The power supply section uses a DC-DC to 3.3V converter to power the system. It uses the MT2492, which, although nominally rated at 12V, can still output 3.3V normally at 19V in actual testing.
Voltage detection uses an internal 12-bit ADC, and current detection uses an INA199 with a 10mΩ resistor differential-mode amplified 50 times before being converted to an internal 12-bit ADC.
2. Project Attributes
: First public disclosure
. Original reference to this project:
[Open Source] High-Precision QC3.0 Decoy_Bilibili_bilibili
Xie Ming used the line drawing function of this graphics library. Microcontroller Graphics Library Open Source Sharing 32 51 OLED_Bilibili_bilibili
3. Hardware Part:
Hardware Structure Diagram

A. First is the minimum system of the single chip.

This is relatively standard; just follow the reference materials. It's worth mentioning that, to save costs, a reference power supply IC was not used; instead, a filter was added before connecting it to the ADC reference input. The BOOT0 boot partition selection and RST are only soldered with 0603 resistors, with external buttons attached. This allows for disassembly and adjustment after program debugging, improving the appearance.
B. The module power supply section


drops from 20V to approximately 0.5V, demonstrating excellent MCU protection. It has a sufficiently wide voltage tolerance and is fully compatible with QC3.0's 12V.
C. For programming and interactive

programming, UART serial programming and SWD are introduced. SWD is recommended for one-click programming in Keil. UART requires downloading the National Technology download software. Furthermore, UART cannot be debugged.
For human-machine interaction, four buttons are used. A better solution is to connect one end of each button to I/O and the other end to ground, referencing National Technology's development board. A 100nF capacitor can be connected in parallel with each button for filtering.
A 0.91µm SPI screen is used for display. It has to be said that the experience of 128x32 is significantly worse than 128x64. The drawing area is halved, and many displays are rather haphazard. The driver used is the sample project software SPI from the purchased website, and I rewrote the character drawing and other functions myself. Thanks again to the graphics library provided by the developer; I copied and adapted a line drawing function from it. The screen hardware is actually connected to the MCU hardware SPI pin; I'll work on that later when I have time.
D. The core sampling function

calculates power by collecting the input voltage and the current on the bus.
The voltage is divided by resistors and then connected to the ADC pin.
The current is amplified 50 times by the differential mode voltage of the 10 milliohm sampling resistor using the INA199 before entering the ADC pin. A small design detail: the MCU system is connected after the low-end sampling resistor to avoid the influence of the microcontroller system on the load power sampling.


Using the voltage range of a multimeter, we can measure the voltage difference between any two points. Note that it's the voltage difference between two points. For example, in the above diagram, we would say that Uab voltage is 6.66V, and Ubc = 3.333V.
In Uxx, we use the latter voltage as a reference, such as Uab, which is actually Ua-Ub.
What is Ua?
It's the voltage at point a relative to 0V
. Where is 0V defined? Generally, it's anchored to the negative terminal of the power supply. (When doing calculations, you can arbitrarily set the voltage at a certain point to 0V and use this anchor point for calculations).
Therefore, in this electronic system, you'll find the representation in the diagram on the right to be so natural.
If you still don't understand, you can think of it as always keeping the black probe of the multimeter pressed against the negative terminal of the battery.
The voltage at point test can be calculated using voltage divider:
Utest = VCC * 5 / (10 + 5)
. If VCC = 10, then Utest = 3.33V. (The calculation approach here is based on a series resistor; the current is constant, and Ohm's law is used to derive the calculation.)
Conversely, if Utest = 1V is known... Therefore,
VCC = Utest ÷ [5/(10+5)] = 3V.
The microcontroller's ADC is like "the black probe of the multimeter always touching the negative terminal of the battery," and the ADC pin is the red probe.
Now, let's formally explain the calculation coefficient.
Using my example,

as shown in the figure, the actual ADC sampling value is 679.

What does this ADC value mean?

So, the ADC=679 comes from

ADC=Utest/3.3 *4095= 679.
We can work backwards to get

Utest=ADC ÷4096*3.3=0.547V.
Then, combining this with the previous resistor voltage divider,
Vcc= Utest÷[10/(10+82)]=5.03V, we
can further refine this:
Vcc= Utest÷[10/(10+82)]
Vcc= (ADC ÷4096*3.3) ÷[10/(10+82)].
Then, we simplify the constants:
Vcc=ADC* (7.4121X10^-3); Vcc=ADC*0.0074121 (volts);
If you have prior programming experience, you'll know that floating-point calculations have errors. Therefore, we multiply the coefficient by 1000, changing the output unit from volts to millivolts.
Vcc=ADC*7.4121; This is very close to the example. But why is it still different? That's because the MCU's ADC sampling has a bias. For an explanation, see page 74 of the datasheet (not the user manual; there are two PDFs with different focuses)
. Also, make good use of search engines.

Next is current acquisition. Here, the INA199's 50x differential voltage amplification is used. "Differential mode" can be understood as the voltage measured by pressing the multimeter probes across a resistor.
Knowing the voltage across the resistor, the current can be calculated using I=U/R; I won't elaborate further here.

To correct the bias, I used an adjustable power supply coefficient correction. It's quite close to the theoretical calculation. (To achieve this, search for Excel fitting curves yourself.)
D. QC Deception and Channel Switching

. On the left is a USB 2.0 analog switch IC. Using a USB 3.0 male connector ensures the system powers on first and starts working, defaulting to USB 2.0 input/output. As you can see in the schematic, my positive and negative terminals don't match the IC; this is for easier routing in the physical PCB. The datasheet specifies rail-to-rail, which is why I dared to do it this way. Regarding the QC3.0 charging protocol, please search for information yourself.
4.
The main structure of the software is roughly as follows: A detailed demonstration video

will be provided on Bilibili later .
[LCSC Training Camp USB Power Meter - Bilibili] https://b23.tv/eCdln06
END. Project and LCSC Electronics Contest logo display.


