# **USB-Meter-Pro Project Description** ## **Overview** --- Lichuang Training Camp|Lichuang Electronic Design Competition Works, the work was completed on 2022/08/26. This work uses national technology N32G430C8L7 as the main control MCU. The other main components include TFT color screen, W25 memory chip, ina199 current sampling chip, DCDC step-down chip, etc. Implement current and voltage detection, voltmeter, small game console and other functions of consumer products with typec interface. The hardware design was studied with reference to many big names in the open source platform. The program was designed independently, the size is relatively small, and the appearance is acceptable. ![1.png]
## **Function introduction** --- 1. After powering on, the main menu column will be displayed. There are two touch buttons embedded above the power meter (hereinafter referred to as "up and left buttons"). Short Press to switch up and down to select the cursor position. Short press the embedded button on the lower right (hereinafter referred to as "down button") to enter the selected function interface. Press and hold the button on the corresponding function interface to return to the previous menu; ![2.png ]

2. [power meter], corresponding to the type-c power meter function, the interface displays the charging current and voltage (with corresponding color curves to display changes and fluctuations), charging power, charging time, as well as the sampling time interval (upd) and curve display time range, and you can change the sampling time interval and its corresponding curve time range by pressing the up and left keys (short press to increase or decrease time, long press to quickly increase or decrease time multiple times); ![3.png]

3. [play game ], literally means "game". This is a fish-catching function. It is a simple game designed with reference to Google's Dinosaur Mini Game. The dinosaur keeps running. The player needs to make the dinosaur jump (right-click) to avoid the cactus obstacles on the road. The player's progress time The longer it is, the higher the score will be, and the obstacles will move faster to increase the difficulty of the game. After the game is over, the score will be automatically saved. You can enter the "record" page to view the score. The system will record several latest scores and the highest score in history, which will be stored in the w25 chip and will not be lost in case of power failure; ![4.png]

4 . [ex voltage], voltmeter function (0-30V), the power meter reserves a curved pin on the side to lead to the debugging download interface and a pwm output port (not yet developed) and adc voltage detection pin, through two male to female By connecting gnd and adc with a Dupont line, it can be used as a simple voltmeter and can display voltage changes in a curve (it also has the time adjustment function during power measurement);! [5.png]

5. [author infor], show the author (this Rookie) related information, you are welcome to give me some advice, or interested friends to come and have fun together (づ ̄ 3 ̄)づ; ![6.png]

6. Is there an onboard LED red light indicator? Power supply, LED blue light indicates whether a button is pressed;
## **Hardware Design** --- - ### mcu: National Technology **N32G430C8L7** Pin function assignment: |pin | application| pin |application| |:---:| :--: | :---------:|:-----: | |PD14 |osc_in| PA2 |adc_in3(ina199)| |PD15 |osc_out| PA1 |adc_in1(voltage) | |PA13 |swdio | PB12 |spi_nss(w25)| |PA14 |swclk | PB13 |spi_sck(w25)| |PA9 |debug_uart_tx| PB14 |spi_miso(w25)| |PA10 |debug_uart_rx| PB14 |spi_miso (w25)| |PA0 |pwm(servo) | PB8 |BLK(tft) | |PB11 |adc_in4(voltmeter) |PB3 |CS(tft)| |NRST | RST | PB6 |DC(tft)| |PB0 | sw1 | PB7 |RES(tft) | |PB1 | sw2 | PB5 | SDA(tft)| |PB2 | sw3 | PB4 | SCL(tft)| |PB10 |led |
- ### External interface - typec male and female one Right - 2x4 curved pin header: debugging serial port, sw download port, pwm (servo), adc capture (voltmeter), 3.3V, GND pin header - three function buttons - there is a pair of pads on the lower right side of the motherboard for shorting Pull rst low to reset. There is also a pair of pads on the upper right that are integrated into the VBUS and GND of the type-c input interface for testing.
- ### Current sampling: **INA199A3DCKT** 200 voltage amplification, direct analog output - ### Voltage sampling: resistor voltage division (1/10) AD detection literal meaning, resistor series voltage division - ### 1.14 inch TFT color screen **ST7789V** adopts plug-in type, welding fpc socket on pcb (0.5mm, not easy to weld) - ### DC-DC step-down **SY8303AIC** type-c connected to cc pin line, It supports PD fast charging, and the voltage is relatively high, so a dcdc step-down circuit is used to output 3.3V power supply to other chips - ### Memory chip **W25Q32JVSSIQ**
## **Software design** --- ### Underlying driver: 1 . The three adc channels detect the current analog signal, charging voltage, and external detection voltage output by INA199 respectively; 2. Zhongjingyuan driver of tft screen cv, adapted to ordinary io as analog SPI communication; 3. Configure hardware spi Communication driver w25; 4. Enable channel2 of TIM2 as PWM output; 5. Use TIM6 timer interrupt as key detection; ### Upper layer application logic: 1. Basic flow chart! [7.png]

2. The core part That is, the menu control function (srv_menu.c), where all basic functions are combined and called together, and human-computer interaction is realized through buttons and screens. A combination of enumerations and structures is used to encapsulate each menu interface and its corresponding interactive functions. Each submenu function is independent. The interfaces can be jumped to each other through ID numbers, and control rights are transferred without interfering with each other. It can be compared Convenient cropping and expansion of submenus and their corresponding functions. For specific implementation, please see the code file (I have not organized it in a hurry, so it may be a little messy);![8.png]
## **Learning | Pitfall records** --- 1. I forgot to set the grounding on the mcu boot0 pin, and it was extremely painful to fly the mcu pin (the welder was not good, it took up to two hours). ![9.jpg]

2. The link cannot be downloaded, and the pop-up window displays "RDDI-DAP ERROR". The reason is that the DuPont cable of the downloader is too long (0.8m) and the signal cannot be transmitted stably. 3. ADC initialization, external crystal oscillator cannot be selected for 1M clock configuration. Reason: temporarily unknown (update: the reason why the external crystal oscillator is initialized normally). 4. The Taobao IPS screen pin display is in the opposite order to the actual product. It plugs in and burns. After arguing with the Taobao seller, they sent me a 1.14-inch IPS screen (original 0.96) for free. I worked overtime overnight to redesign the new PCB. Proofing. 5. I found the W25q32 chip driver code on gitee and debugged it for a day. Finally, with the help of a boss, I found that the driver was imperfect, there was no delay in erasing, and the reading was misaligned. Mentality exploded. . . . . (Update: Supplementary, if the system clock is configured to 128M Hz, W25 cannot be read and written. You can lower the frequency. The metaphysical reason is unknown) 6. When configuring the timer, it is found that the external crystal oscillator has not been initialized successfully. The reason is unknown (Update: Welding problem, re-solder. After normal startup). 7. The charging current reading of the new board was zero. After a long time of troubleshooting, I found that the signal line of the op amp ina199 was connected backwards, and I was forced to cut the copper flying wire. ![10.jpg]
## **Supplement** --- ### References: - [Lichuang official case](https://oshwhub.com/limengmeng12345/ji-chu-ying-usb-dian- ya-biao#P1) - Upload the code to the gitee warehouse: [gitee warehouse](https://gitee.com/dead-poem/usb-meter-pro.git) - Bilibili Video: [Video Portal] (https://www.bilibili.com/video/BV17G4y1k7Yc?vd_source=6c6ddc65aa9a516f6055f95eb2779c53) - Additional pictures: ![11.jpg]

![12.jpg]

![13.jpg]

![14.jpg]

![15 .jpg]

![16.jpg]