Battery voltage detection (ADC)

The tutorial demonstrates the low voltage alarm function of the development board.

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.

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, you can open the project source code to read it.

Battery_init

Battery_Get

Battery_Get_Average

Get_Measure_Volotage

Get_Battery_Volotage

Experimental phenomenon

The Battery.hex file generated by the project compilation is located in the OBJ folder of the Battery project. Find the Battery.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 program will enter the corresponding state according to the obtained voltage and print the real-time battery voltage information on the serial port:

Voltage is less than 9.6V: The buzzer keeps beeping and the LED is off;

Voltage is greater than or equal to 9.6V: The LED flashes

image-20240808192520824