cancel
Showing results for 
Search instead for 
Did you mean: 

ADC read with STM32L433

YChun.15
Associate

would like to read ADC value for vbat consecutively.

First read Vbat

Second read VREFINT.

implementation is like below.

 hadc1.Instance = ADC1;

 hadc1.Init.ClockPrescaler = ADC_CLOCK_ASYNC_DIV4;

 hadc1.Init.Resolution = ADC_RESOLUTION_12B;

 hadc1.Init.DataAlign = ADC_DATAALIGN_RIGHT;

 hadc1.Init.ScanConvMode = ADC_SCAN_ENABLE;

 hadc1.Init.EOCSelection = ADC_EOC_SINGLE_CONV;

 hadc1.Init.LowPowerAutoWait = DISABLE;

 hadc1.Init.ContinuousConvMode = ENABLE;

 hadc1.Init.NbrOfConversion = 1;

 hadc1.Init.DiscontinuousConvMode = DISABLE;

 hadc1.Init.ExternalTrigConv = ADC_SOFTWARE_START;

 hadc1.Init.ExternalTrigConvEdge = ADC_EXTERNALTRIGCONVEDGE_NONE;

 hadc1.Init.DMAContinuousRequests = DISABLE;

 hadc1.Init.Overrun = ADC_OVR_DATA_PRESERVED;

 hadc1.Init.OversamplingMode = DISABLE;

 if (HAL_ADC_Init(&hadc1) != HAL_OK)

 {

  Error_Handler();

 }

 /** Configure Regular Channel 

 */

 sConfig.Channel = ADC_CHANNEL_8; <-- vbat channel

 sConfig.Rank = ADC_REGULAR_RANK_1;

 sConfig.SamplingTime = ADC_SAMPLETIME_6CYCLES_5;

 sConfig.SingleDiff = ADC_SINGLE_ENDED;

 sConfig.OffsetNumber = ADC_OFFSET_NONE;

 sConfig.Offset = 0;

 if (HAL_ADC_ConfigChannel(&hadc1, &sConfig) != HAL_OK)

 {

  Error_Handler();

 }

 /** Configure Regular Channel 

 */

 sConfig.Channel = ADC_CHANNEL_VREFINT;  

 if (HAL_ADC_ConfigChannel(&hadc1, &sConfig) != HAL_OK)

 {

  Error_Handler();

 }

read vbat and vrefint

HAL_ADC_Start(&hadc1);

/* Get 1st channel conv.value (ADC_IN8 - VDD) */

res = HAL_ADC_PollForConversion(&hadc1, POLLING_CONVERSION_TIME);

if (res != HAL_OK) {

 goto VbatMeasureError;

}

vdd = HAL_ADC_GetValue(&hadc1);

HAL_ADC_Stop(&hadc1);

/* Get 2nd channel conv.value (ADC_VREFINT) */

res = HAL_ADC_PollForConversion(&hadc1, POLLING_CONVERSION_TIME);

if (res != HAL_OK) {

 goto VbatMeasureError;

}

vrefint = HAL_ADC_GetValue(&hadc1);

HAL_ADC_Stop(&hadc1);

but the results are not expected values.

vdd is 1502, vrefint is 1501.

Looks ADC seems to read vrefint always.

How can I read ADC both vbat and vrefint?

4 REPLIES 4
Ozone
Lead II

> sConfig.SamplingTime = ADC_SAMPLETIME_6CYCLES_5;

That's a really short sampling time.

Bombadil
Associate II

VBAT should be on channel 18 not 8

Did you use an external low drop diode between VBAT and VCC?

raptorhal2
Lead

HAL L4 library version 1.14.0 for L4R5ZI projects has an ADC_Sequencer example that you can easily adapt.

Cheers, Hal

S.Ma
Principal

What is your Vbat voltage? Is it withing the ADC vaild range?