Voltage detection

The tutorial demonstrates ADC (ADC12_IN5) to obtain the real-time voltage of the battery pack and print the voltage value through the serial port.

Hardware connection

image-20240825204131635

PeripheralsDevelopment boardDescription
Battery packPA5The battery pack DC interface needs to be connected to the development board

Control principle

The converted value of the battery BAT pin is obtained through a single ADC, and the value is converted into actual voltage information and printed out through USART1.

STM32F103RCT6 has three 12-bit analog-to-digital converters embedded, and each ADC controller has up to 16 channels.

Only introduce the ADC channel corresponding to the battery pack interface.

Main control chipPinMain function (after reset)Default multiplexing functionRedefine function
STM32F103RCT6PA5PA5SPI1_SCK/DAC_OUT2/ADC12_IN5 

The ADC of STM32F103RCT6 is a 12-bit successive approximation analog-to-digital converter with 12-bit resolution;

ADCvaluerange=0212=04095

The converted value of ADC can be stored in 16-bit data register in left-aligned or right-aligned mode.

The A/D conversion of each ADC channel can be performed in single, continuous, scan or intermittent mode.

ModeFunction
Single conversion modeADC performs only one conversion
Continuous conversion modeStarts another conversion as soon as the previous ADC conversion is completed
Scan modeUsed to scan a group of ADC channels
Intermittent modeConverts multiple ADC channels in groups until the entire sequence is converted

The input clock of the ADC must not exceed 14MHz, which is generated by the frequency division of PCLK2.

Totalconversiontime=TCONV=samplingtime+12.5clockcycles

Conversion time in this tutorial: ADC clock frequency 12MHz, sampling time 239.5 clock cycles

Totalconversiontime=TCONV=samplingtime+12.5clockcycles=(239.5+12.5)112000000=21us
VBAT=ValueADCconvertedvalue3.34096

Referring to the hardware schematic diagram, we can see: Current equality principle

Icurrent=VBATR16=VMR16+R14VBAT3.3=VM10+3.3VM=VBAT10+3.33.3
Thatis,theactualvoltage=VM=VBAT10+3.33.3

Pin definition

Main control chipPinMain function (after reset)Default multiplexing functionRedefine function
STM32F103RCT6PA5PA5SPI1_SCK/DAC_OUT2/ADC12_IN5 

Software code

Since the default function of the PA5 pin is a normal IO pin function, we need to use the multiplexing function.

Control function

The tutorial only briefly introduces the code, and you can open the project source code to read it in detail.

Battery_init

Battery_Get

Battery_Get_Average

Get_Measure_Volotage

Get_Battery_Volotage

Experimental phenomenon

The ADC.hex file generated by the project compilation is located in the OBJ folder of the ADC project. Find the ADC.hex file corresponding to the project and use the FlyMcu software to download the program to the development board.

After the program is successfully downloaded: the LED switches on and off every 500ms; using the serial port debugging assistant, you can see the serial port returns the battery pack voltage information.

image-20240730190226793