## 1. Question requirements:
Use a microcontroller to design a power tester. There is no limit on appearance. It is required to be able to display voltage, current, and power, and the maximum input power is more than 100W. No plagiarism or use of finished modules.
## 2. Question analysis
The question requires the design of a power tester. I chose to design a power meter that uses a USB interface and can test mobile phone fast charging, which has a certain degree of portability.
## 3. Design plan
! [Screenshot 2022-03-02 114921.jpg]
## 4. Schematic design description #### 4.1 Buck circuit The
buck part provides 3.3V power supply for MCU, INA220, OLED screen, etc. , since the maximum current consumed by the entire system will not exceed 60mA, and the efficiency of ordinary DC-DC converters is low under low load, it is best to choose a chip with light load energy-saving functions such as burst, skip, and pfm. The stand-alone converter used here is The RY9320AT6 purchased from Chuang Mall has an upper/lower tube internal resistance of 100/50mΩ and a frequency of 500k. It works normally in PSM mode and enters PFM mode under light load. And it only costs 1.5 yuan a piece, which is very cost-effective.
When measured at 5V power supply, only 9mA current is required from VUBS, while in most fast charging 9-12V range, only about 4mA current is required, which greatly reduces the measurement error. ![Step-down]

![Step-down actual thing]

#### 4.2 Add a decoupling capacitor to the voltage and current sampling INA220 power supply. A0 and A1 are grounded to set the address. ![INA220]

![INA220]

#### 4.3 Interface part
A port uses a 5PIN customized interface, which is used to test Xiaomi, OPPO’s fast charging and QC’s fast charging that uses ordinary A port. The Type-C interface is mainly used for testing PD fast charging.
This kind of A port actually multiplexes the GND pin of USB3.0 into the identification pin of the private protocol, and VBUS and GND are widened. Theoretically, it can also be recognized by directly using the USB3.0 interface, but the USB3.0 interface cannot pass too much current and there is a risk. The female base is available in Lichuang Mall, but the male one in Lichuang Mall is always out of stock, so I bought it from Taobao. For Type-C, just find a high-current one at Lichuang Mall. Be careful not to use the "high-current" one with only 6 PINs, because it lacks a pair of VBUS and GND. ![Interface]

![Interface actual.jpg]

#### 4.4 Quick charge deception This design has a simple QC fast charge deception function, which can be used as a simple voltage-regulated power supply. The QC2.0 and QC3.0 protocols require three voltages: 0, 0.6V, and 3.3V, which can be directly simulated using the IO port. QC2.0 supports four gears: 5V, 9V, 12V, and 20V. The 20V gear is only supported by a very few charging heads that support QC2.0 ClassB. This is why QC2.0 20V cannot be tricked in the demonstration video later. QC3.0 supports continuous voltage regulation in steps of 200mV and can output 4.4-20V. Most of Xiaomi's QC3.0 fast charging heads above 30W can support up to 20V, which is to power their own wireless chargers. ![QC deception]

![QC deception]

#### 4.5 Serial port download Serial port download uses CH340K, and uses RTS and DTR to achieve one-click download. In fact, it’s okay not to use CH340K. During debugging, connect three wires from the board to the CH340 module. Remove the flying wires after debugging. ![Serial download]

![ch340]

#### 4.6 Main control The main control uses stm32g030f6p6, which is small in size and high in performance. It is probably the cheapest one among stm32 currently. It only costs 2-3 yuan on Taobao, and maybe one or two yuan if you collect a red envelope. The g030 series may be due to the use of more advanced processes. More dies can be cut out of one wafer. It has stronger performance and is even cheaper than the f030 series. ![Master Control]

![Main Control Actual]

#### 4.7 The schematic diagram of the OLED screen is drawn directly according to the recommended design of the datasheet of the driver chip ssd1315. ![OLED]

![OLED Actual 1]

![OLED Actual 2]

#### 4.8 eeprom is used to store the recorded voltage, current, and curve. You don’t need to add it if you don’t need this function. ![eeprom]

## PCB design instructions PCB uses a four-layer board. The four-layer board has stronger anti-interference ability and can reduce the difficulty of wiring. Open the VBUS window and hang tin to increase the current carrying capacity. The sampling resistor wiring uses the Kelvin connection method. The OLED screen is placed on another board and connected to the motherboard through an FFC cable.
![PCB1]

![PCB2]

Used Easy EDA Professional Edition to design a 3D printed housing. But the day before the courier arrived, there was an epidemic here and the courier could not come in, so there was no outer shell in the physical display later. I will make up for it after the epidemic passes![Shell]

![Shell]

## Software description: The IDE uses STM32CubeIDE, the firmware library uses the HAL library, and the initialization code is generated using STM32CubeMX. All codes are in the attachment, basically with comments. If you have any questions, you can ask me in the comment area **Multi-level menu** ``` key_table table[30]= { {0,0,0,1,(*fun0) }, //Level 0, displays the main interface {1,4,2,5,(*fun1)}, //Level 1, displays [Brightness Setting], fast charge deception, data recording, return {2,1 ,3,6,(*fun2)}, //The first layer, display brightness setting, [Fast charge deception], data recording, return {3,2,4,9,(*fun3)}, //The first Layer, display brightness setting, fast charge deception, [data record], return {4,3,1,0,(*fun4)}, //The first layer, display brightness setting, fast charge deception, data record, [return ] {5,5,5,1,(*fun5)}, //Second layer, brightness setting {6,8,7,10,(*fun6)}, //Second layer, under the fast charge deception layer Display [QC 2.0], QC3.0, return {7,6,8,11,(*fun7)}, //The second layer, display QC 2.0, [QC3.0], return {8 under the fast charge deception layer ,7,6,2,(*fun8)}, //The second layer, QC 2.0, QC3.0, [Return] {9,9,9,3,(*fun9)}, is displayed under the fast charge deception layer. //Second layer, data record {10,10,10,6,(*fun10)}, //Third layer, fast charge deception QC 2.0 {11,11,11,7,(*fun11)},/ /Third layer, fast charge deception QC 3.0 }; ``` **Main function loop** ``` while (1) { OLED_Refresh();//Refresh the video memory if(time_1s_ok) { time_1s_ok=0; voltage = INA220_GetVoltage (); current = INA220_GetCurrent(); power = INA220_GetPower(); energy_temp = energy_temp + power*0.00027777; energy = energy_temp; if(power!=0&&mem_addr_base<0xE00) { HAL_I2C_Mem_Write(&hi2c1,0xA0,mem_addr_base,I2C_MEMADD_SIZE _16BIT,(u8* )ina220_data,4,100); mem_addr_base=mem_addr_base+4; } } /**********************find index************** ******************/ if((key_enter==1)||(key_up==1)||(key_down==1)) { if(key_up==1) { func_index =table[func_index].up; //up key_up=0; } if(key_down==1) { func_index=table[func_index].down; //down key_down=0; } if(key_enter==1) { OLED_Clear (); func_index=table[func_index].enter; //enter key_enter=0; } } if(key_back==1) { key_back=0; if(oled_on==1) oled_on=0; else oled_on=1; } current_operation_index=table[func_index].current_operation; (*current_operation_index)();//Execute the current operation function HAL_Delay(10); } } ``` **OLED screen uses hardware I2C driver and directly calls HAL_I2C_Mem_Write() in the HAL library This enables continuous writing of large amounts of video memory. ** **This function was originally used to read and write eeprom, but other applications that need to write to two addresses can also be used. ** ``` //Refresh the video memory void OLED_Refresh(void) { u8 i; for(i=0;i<8;i++) { OLED_WR_Byte(0xb0+i,OLED_CMD); //Set the row starting address OLED_WR_Byte(0x00 ,OLED_CMD); //Set the low column starting address OLED_WR_Byte(0x10,OLED_CMD); //Set the high column starting address HAL_I2C_Mem_Write(&hi2c2,OLED_ADDRESS,0x40,I2C_MEMADD_SIZE_8BIT,&OLED_GRAM[i][0],128,100); // Write to video memory} } ``` ** Since reading and writing of INA220 also requires writing to two addresses, you can also directly use the HAL_I2C_Mem_R/W() function to read and write, so you don’t need to spend additional time to implement it. ** **But since the register in INA220 is 16 bits, this function can only read and write u8, so you need to manually combine two u8 into one u16. ** ``` //INA220 initialization void INA220_Init(void) { u8 CFG[2] = {0x27,0xff}; //Configuration register 0x21D7 u8 CAL[2] = {0x20,0x00}; //Calibration register 0x5000, 1ma/bit HAL_I2C_Mem_Write(&hi2c1,INA220_ADDR,CFG_REG,I2C_MEMADD_SIZE_8BIT,&CFG[0],2,100);//Set the configuration register HAL_I2C_Mem_Write(&hi2c1,INA220_ADDR,CAL_REG,I2C_MEMADD_SIZE_8BIT, &CAL[0],2,100);//Set calibration register} ``` ## The physical display shows that
due to the epidemic, the printed casing cannot be put in. Here, a multi-screen board is used as the lower cover, and a previously printed black PCB is used as the upper cover to protect the screen.
![Real object]

![Real object]

![Real object]
The front of the motherboard.
![Physical display 1]
The back of the motherboard.
![Physical display 2]

![Physical display 3]

## Notes
When using the STM32G0 chip for the first time, be careful not to brick it! !
Because the startup mode of G0 is not determined by the boot0 pin by default, but by the internal nboot0 bit register, and its default value is 1. If you directly burn a program into it, you will not be able to boot from the System memory. You must first use STM32CubeProgrammer to uncheck nBOOT_SEL in Option bytes-User Configuration. This is somewhat similar to the configuration fuse of the AVR microcontroller. ![User Configuration]

## In addition to power detection and fast charge deception, other attachment uploads also provide screen brightness adjustment and simple voltage and current curve recording functions. ![Brightness]

![Curve]

## Voltage and current sampling part of the selection guide. Both INA220 and INA226 can be used directly without changing the circuit. 226 has 16 bits and has higher accuracy. Taobao 220 is about half cheaper than 226, and 12 bits is almost enough. It can measure 8A with a 5mΩ sampling resistor. The sampling resistor is C723507, with 0.5% accuracy. If it is replaced with another 1%, it should not be a big problem. The formula for the maximum current range of ina220 is 40mV/(R)mΩ. The STM32G030F6P6 used in the MCU is mainly cheap, only two or three yuan on Taobao. The disadvantage is that when you use it for the first time, don’t forget to configure the registers first to prevent bricking; also because it is relatively new, you can only use the STM32CubeProgrammer for serial port programming. One-click programming will waste half of it. There are bugs in version 2.9, so you need to use 2.8. ; It is more recommended to use SWD to burn directly. It is recommended not to leave the serial port part empty for the reasons mentioned above. If you need to download the serial port or debug printf, fly the wire directly from the reserved programming pad. Interface part. The USB-A female connector is C708966 from Lichuang Mall; the male connector is purchased from Taobao Ruixin for 4 cents each, but it costs 6 yuan for shipping. You can buy more. You can also buy the female connector from the same store, so they are all green. . USB-C is C963413. For OLED screens, be careful to choose the new model SSD1315, which is 24.7 inches wide, while the SSD1306 model is a bit larger and does not match the casing. ESD diodes, whether you need them or not. EEPROM is used to record curves and is a bit crude. If you don't need it, you can just skip it. For the voltage reduction part, I use the RY9320 from Lichuang Mall. The measured efficiency is very high. If you want to replace it with another one, it is best to choose one with light load energy saving, otherwise the efficiency will be extremely low. Pay attention to the withstand voltage of the input capacitor; and the material must be X5R or X7R, because when the DC bias voltage of other materials such as Y5V is close to the withstand voltage, its capacity may only be one-tenth. The cost of the entire design is still relatively low. If you already have commonly used resistors and capacitors, you can get it for about 20 yuan not including shipping costs. Taobao's ones that can measure such high power and support the Xiaomi OPPO protocol are much more expensive. ## In the demonstration video, the first key from left to right is the selection key; the second and third keys are the up and down keys; the last one is an additional function key, which can temporarily turn on/off the display on the main interface to prevent the OLED screen from being damaged by long-term measurements. Screen burn-in, voltage, current, and power can be switched in the data recording interface. Video 1 is a 65W charging test using Redmi note10pro, which is my highest power device. There should be no problem when testing 120W charging. After all, the current is the same as 65W at 6A. But for the later 160W charging, the current has increased to 8A. Although this tester can measure 8A, the A port is 6A and the C port is 5A. It is estimated that it will be ok for a short time, but it may become hot after a long time. . Video 2 is a demonstration of fast charging deception. At the same time, the output is connected to a multimeter to compare the accuracy. The multimeter has been calibrated using the REF5010 voltage reference. It can be seen that the accuracy of voltage measurement is quite good. Video 3 is a demonstration of current accuracy. A 10Ω aluminum shell resistor is connected in series with a multimeter. The current measured by the two is basically the same. Due to the internal resistance measured by the multimeter, the current is low.