cancel
Showing results for 
Search instead for 
Did you mean: 

ADC of stm32l4

Namita
Associate II

I configured ADC Channel 9 on STM32L4-Nuceo board its working but If change the ADC channel to 10 Or 13 its not working.Please find the below ADC Configuration.

/* Definition for ADCx clock resources */

#define ADCx              ADC1

#define ADCx_CLK_ENABLE()        __HAL_RCC_ADC_CLK_ENABLE()

#define ADCx_CHANNEL_GPIO_CLK_ENABLE() __HAL_RCC_GPIOA_CLK_ENABLE()

#define ADCx_FORCE_RESET()       __HAL_RCC_ADC_FORCE_RESET()

#define ADCx_RELEASE_RESET()      __HAL_RCC_ADC_RELEASE_RESET()

/* Definition for ADCx Channel Pin */

#define ADCx_CHANNEL_PIN        GPIO_PIN_4

#define ADCx_CHANNEL_GPIO_PORT     GPIOA

/* Definition for ADCx's Channel */

#define ADCx_CHANNEL          ADC_CHANNEL_VBAT//ADC_CHANNEL_9

#define SAMPLINGTIME          ADC_SAMPLETIME_6CYCLES_5

void ADC_Config(void)

{

 AdcHandle.Instance     = ADCx;

  

 if (HAL_ADC_DeInit(&AdcHandle) != HAL_OK)

 {

  /* ADC de-initialization Error */

  Error_Handler();

 }

  

 AdcHandle.Init.ClockPrescaler    = ADC_CLOCK_ASYNC_DIV1;     /* Asynchronous clock mode, input ADC clock not divided */

 AdcHandle.Init.Resolution      = ADC_RESOLUTION_12B;      /* 12-bit resolution for converted data */

 AdcHandle.Init.DataAlign       = ADC_DATAALIGN_RIGHT;      /* Right-alignment for converted data */

 AdcHandle.Init.ScanConvMode     = DISABLE;            /* Sequencer disabled (ADC conversion on only 1 channel: channel set on rank 1) */

 AdcHandle.Init.EOCSelection     = ADC_EOC_SINGLE_CONV;      /* EOC flag picked-up to indicate conversion end */

 AdcHandle.Init.LowPowerAutoWait   = DISABLE;            /* Auto-delayed conversion feature disabled */

 AdcHandle.Init.ContinuousConvMode  = DISABLE;            /* Continuous mode disabled to have only 1 conversion at each conversion trig */

 AdcHandle.Init.NbrOfConversion    = 1;               /* Parameter discarded because sequencer is disabled */

 AdcHandle.Init.DiscontinuousConvMode = DISABLE;            /* Parameter discarded because sequencer is disabled */

 AdcHandle.Init.NbrOfDiscConversion  = 1;               /* Parameter discarded because sequencer is disabled */

 AdcHandle.Init.ExternalTrigConv   = ADC_SOFTWARE_START;      /* Software start to trig the 1st conversion manually, without external event */

 AdcHandle.Init.ExternalTrigConvEdge = ADC_EXTERNALTRIGCONVEDGE_NONE; /* Parameter discarded because software trigger chosen */

 AdcHandle.Init.DMAContinuousRequests = DISABLE;            /* DMA one-shot mode selected (not applied to this example) */

 AdcHandle.Init.Overrun        = ADC_OVR_DATA_OVERWRITTEN;   /* DR register is overwritten with the last conversion result in case of overrun */

 AdcHandle.Init.OversamplingMode   = DISABLE;            /* No oversampling */

 if (HAL_ADC_Init(&AdcHandle) != HAL_OK)

 {

  /* ADC initialization Error */

  Error_Handler();

 }

 /*##-2- Configure ADC regular channel ######################################*/

 sConfig.Channel   = ADCx_CHANNEL;        /* Sampled channel number */

 sConfig.Rank     = ADC_REGULAR_RANK_1;     /* Rank of sampled channel number ADCx_CHANNEL */

 sConfig.SamplingTime = ADC_SAMPLETIME_6CYCLES_5;  /* Sampling time (number of clock cycles unit) */

 sConfig.SingleDiff  = ADC_SINGLE_ENDED;      /* Single-ended input channel */

 sConfig.OffsetNumber = ADC_OFFSET_NONE;       /* No offset subtraction */ 

 sConfig.Offset = 0;                 /* Parameter discarded because offset correction is disabled */

}

// ADC_Enable(&AdcHandle);

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

 {

  /* Channel Configuration Error */

  Error_Handler();

 }

 /* Run the ADC calibration in single-ended mode */

 if (HAL_ADCEx_Calibration_Start(&AdcHandle, ADC_SINGLE_ENDED) != HAL_OK)

 {

  /* Calibration Error */

  Error_Handler();

 }

  

 /*##-3- Start the conversion process #######################################*/

 if (HAL_ADC_Start(&AdcHandle) != HAL_OK)

 {

  /* Start Conversation Error */

  Error_Handler();

 }

 /*##-4- Wait for the end of conversion #####################################*/

 /* For simplicity reasons, this example is just waiting till the end of the

   conversion, but application may perform other tasks while conversion

   operation is ongoing. */

 if (HAL_ADC_PollForConversion(&AdcHandle, 10) != HAL_OK)

 {

  /* End Of Conversion flag not set on time */

  Error_Handler();

 }

 else

 {

  /* ADC conversion completed */

  /*##-5- Get the converted value of regular channel ########################*/

  uhADCxConvertedValue = HAL_ADC_GetValue(&AdcHandle);

 }

8 REPLIES 8
GwenoleB
ST Employee

Hello @Namita​,

When cubeMX is generating ADC init, GPIOs used for ADC channels are initialized in hal_msp_init .c file.

If under cubeMX you have only declared channel 9, then the code generated will only configure the GPIO related to ADC channel 9.

Make sure, channel 10 & 13 are configured in analog mode and enabled before using them.

Best Regards,

Gwénolé

Now want to set Vref+ to  2.5 v by vbuff .but voltage on vref+pin is at 1.67v on custom hardware board having STM32L496RET6 MCU.

Please find below configuration of vbuff.Kindly Reply.

HAL_SYSCFG_EnableVREFBUF();

 HAL_SYSCFG_VREFBUF_HighImpedanceConfig(SYSCFG_VREFBUF_HIGH_IMPEDANCE_DISABLE);

 HAL_SYSCFG_VREFBUF_VoltageScalingConfig( SYSCFG_VREFBUF_VOLTAGE_SCALE1);

GwenoleB
ST Employee

Hello,

Make sure you have enabled SYSCLK and PWR_CLK:

  __HAL_RCC_SYSCFG_CLK_ENABLE();
  __HAL_RCC_PWR_CLK_ENABLE();

Then, you can configure VREFBUF registers:

  /** Configure the internal voltage reference buffer voltage scale
  */
  HAL_SYSCFG_VREFBUF_VoltageScalingConfig(SYSCFG_VREFBUF_VOLTAGE_SCALE1);
 
  /** Configure the internal voltage reference buffer high impedance mode
  */
  HAL_SYSCFG_VREFBUF_HighImpedanceConfig(SYSCFG_VREFBUF_HIGH_IMPEDANCE_DISABLE);
 
  /** Enable the Internal Voltage Reference buffer
  */
  HAL_SYSCFG_EnableVREFBUF();

Registers cannot be written without enabling related clock before.

Regards,

Gwénolé

We configure the Vbuff as you shared configuration deatils,but 2.5 voltage is not generated on vref Pin 12 and 14 pin (VREF-) of STM32L496RETB are shorted .so is it Pin 13 and 19 pin (VREF+) of STM32L496RETB are shorted internally ???

GwenoleB
ST Employee

Hello,

What Nucleo board do you use?

On STM32L496RET device, the VREF+ pin is double bonded with VDDA, that means the internal voltage reference buffer is not available.

Regards,

Gwénolé

On STM32L496RET device,there is VREF+ pin at 19 of MCU is we externally connected to 3.3v .But adc is not working properly. AS we connect external vref at 13 of MCU to 3.3v then adc is working properly.what is the difference between vref+ at 13 and 19 of MCU

> On STM32L496RET device,there is VREF+ pin at 19 of MCU

No there is not. VDD is on pin 19.

0693W00000NsS4aQAF.pngJW

ok.