cancel
Showing results for 
Search instead for 
Did you mean: 

Vbat of STM32G0 is not working Always it is giving fixed value . Is it usable as adc ???

SDwiv.1
Associate II
 
11 REPLIES 11
TDK
Guru

Yes, VBAT can be measured from the ADC.

What value are you getting and what value do you expect? How are things configured?

If you feel a post has answered your question, please click "Accept as Solution".
SDwiv.1
Associate II

static void MX_ADC1_Init(void)

{

 /* USER CODE BEGIN ADC1_Init 0 */

 /* USER CODE END ADC1_Init 0 */

 ADC_ChannelConfTypeDef sConfig = {0};

 /* USER CODE BEGIN ADC1_Init 1 */

 /* USER CODE END ADC1_Init 1 */

 /** Configure the global features of the ADC (Clock, Resolution, Data Alignment and number of conversion)

 */

 hadc1.Instance = ADC1;

 hadc1.Init.ClockPrescaler = ADC_CLOCK_ASYNC_DIV1;

 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.LowPowerAutoPowerOff = DISABLE;

 hadc1.Init.ContinuousConvMode = DISABLE;

 hadc1.Init.NbrOfConversion = 4;

 hadc1.Init.DiscontinuousConvMode = ENABLE;

 hadc1.Init.ExternalTrigConv = ADC_SOFTWARE_START;

 hadc1.Init.ExternalTrigConvEdge = ADC_EXTERNALTRIGCONVEDGE_NONE;

 hadc1.Init.DMAContinuousRequests = DISABLE;

 hadc1.Init.Overrun = ADC_OVR_DATA_OVERWRITTEN;

 hadc1.Init.SamplingTimeCommon1 = ADC_SAMPLETIME_12CYCLES_5;

 hadc1.Init.SamplingTimeCommon2 = ADC_SAMPLETIME_160CYCLES_5;

 hadc1.Init.OversamplingMode = DISABLE;

 hadc1.Init.TriggerFrequencyMode = ADC_TRIGGER_FREQ_HIGH;

 if (HAL_ADC_Init(&hadc1) != HAL_OK)

 {

  Error_Handler();

 }

 /** Configure Regular Channel

 */

 sConfig.Channel = ADC_CHANNEL_4;

 sConfig.Rank = ADC_REGULAR_RANK_4;

 sConfig.SamplingTime = ADC_SAMPLINGTIME_COMMON_2;

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

 {

  Error_Handler();

 }

 /** Configure Regular Channel

 */

 sConfig.Channel = ADC_CHANNEL_VREFINT;

 sConfig.Rank = ADC_REGULAR_RANK_2;

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

 {

  Error_Handler();

 }

 /** Configure Regular Channel

 */

 sConfig.Channel = ADC_CHANNEL_TEMPSENSOR;

 sConfig.Rank = ADC_REGULAR_RANK_3;

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

 {

  Error_Handler();

 }

 /** Configure Regular Channel

 */

 sConfig.Channel = ADC_CHANNEL_VBAT;

 sConfig.Rank = ADC_REGULAR_RANK_1;

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

 {

  Error_Handler();

 }

 /* USER CODE BEGIN ADC1_Init 2 */

 /* USER CODE END ADC1_Init 2 */

}

SDwiv.1
Associate II

Getting value 1380

Getting value always 1380

TDK
Guru

Why did you post the MX_ADC1_Init code 4 times?

What's wrong with the value 1380? Seems reasonable for a VBAT/3 measurement to me. Do the values of the other channels change?

If you feel a post has answered your question, please click "Accept as Solution".

And WHAT voltage do you measure there with a meter?

Have you tested with multiple different levels?

Explain your test methodology so we have some clue as to what you're doing.

Tips, Buy me a coffee, or three.. PayPal Venmo
Up vote any posts that you find helpful, it shows what's working..
SDwiv.1
Associate II

When i am giving other voltage or o volt ,there is no count is changing ,always it is giving around 1380

SDwiv.1
Associate II

For other channel is working fine

SDwiv.1
Associate II

using dma to start adc