How to use ADC for battery operated application?
The STM32 microcontrollers have an internal voltage reference source, and that can be used to determine an unknown VDDA, VDD value. The necessary informations are found in the datasheet, for example for the STM32F030 series:



it is 1.23 V typically. Now, given that this reference voltage is known to be 1.23 V you can calculate the ADC reference voltage VDDA by reading the internal reference's valute with the ADC.

The formula is the following:

$$ \frac{V_{REFINT} \:[V]}{VDDA\: [V]} \times 4096  =  digital_{VREFINT}$$

In this equation the following values are known:

$$ V_{REFINT} = 1.23\:V $$
$$ digital_{VREFINT} = \text{from reading the internal ADC channel}$$

So the VDDA, aka battery voltage can be calculated:

$$ \frac{V_{REFINT} \:[V]}{digital_{VREFINT}} \times 4096  =  VDDA\: [V]$$

Performing this calculation before your normal ADC readings will give you the reference voltage value you need.



A more accurate value can be calculated using the internal voltage reference's calibration value which is individually measured for each part by ST during production test and stored in the system memory area. It is 
accessible in read-only mode. Here are the specs:



It is actually the digital value of the internal reference voltage that was measured with VDDA = 3.3 V. The actual VDDA can be calculated as follows:

$$ VDDA = 3.3 \: V \times  \frac{digital_{VREFINTCAL}}{digital_{VREFINT}} $$

3.3 V is known from the table above, the calibration value from the memory and the actual value by reading the appropriate internal ADC channel.

Generally this is the most cost effective and the easiest way to use the ADC with not constant VDDA.