cancel
Showing results for 
Search instead for 
Did you mean: 

STM32WB55 and read ADC from 4 channel using LL library

mPona
Associate II

Hi everyone,

I am trying to read ADC values from 4 different channels

 PC0  ------> ADC1_IN1

 PC1  ------> ADC1_IN2

 PC2  ------> ADC1_IN3

 PC3  ------> ADC1_IN4

I am able to get different values from different channel for the 1st iteration. but from the 2nd iteration i am getting same values from the last read channel in the 1st iteration.

Please help me to figure out this issue.

static void MX_ADC1_Init(void)

{

 /* USER CODE BEGIN ADC1_Init 0 */

 /* USER CODE END ADC1_Init 0 */

 LL_ADC_CommonInitTypeDef ADC_CommonInitStruct = {0};

 LL_ADC_InitTypeDef ADC_InitStruct = {0};

 LL_ADC_REG_InitTypeDef ADC_REG_InitStruct = {0};

 LL_GPIO_InitTypeDef GPIO_InitStruct = {0};

 /* Peripheral clock enable */

 LL_AHB2_GRP1_EnableClock(LL_AHB2_GRP1_PERIPH_ADC);

  

 LL_AHB2_GRP1_EnableClock(LL_AHB2_GRP1_PERIPH_GPIOC);

 /**ADC1 GPIO Configuration  

 PC0  ------> ADC1_IN1

 PC1  ------> ADC1_IN2

 PC2  ------> ADC1_IN3

 PC3  ------> ADC1_IN4 

 */

 GPIO_InitStruct.Pin = LL_GPIO_PIN_0|LL_GPIO_PIN_1|LL_GPIO_PIN_2|LL_GPIO_PIN_3;

 GPIO_InitStruct.Mode = LL_GPIO_MODE_ANALOG;

 GPIO_InitStruct.Pull = LL_GPIO_PULL_NO;

 LL_GPIO_Init(GPIOC, &GPIO_InitStruct);

 /* USER CODE BEGIN ADC1_Init 1 */

 /*## Configuration of NVIC #################################################*/

 /* Configure NVIC to enable ADC1 interruptions */

 NVIC_SetPriority(ADC1_IRQn, 0);

 NVIC_EnableIRQ(ADC1_IRQn);

 /* USER CODE END ADC1_Init 1 */

 /** Common config 

 */

 ADC_CommonInitStruct.CommonClock = LL_ADC_CLOCK_SYNC_PCLK_DIV2;

 LL_ADC_CommonInit(__LL_ADC_COMMON_INSTANCE(ADC1), &ADC_CommonInitStruct);

 ADC_InitStruct.Resolution = LL_ADC_RESOLUTION_12B;

 ADC_InitStruct.DataAlignment = LL_ADC_DATA_ALIGN_RIGHT;

 ADC_InitStruct.LowPowerMode = LL_ADC_LP_AUTOWAIT;

 LL_ADC_Init(ADC1, &ADC_InitStruct);

 ADC_REG_InitStruct.TriggerSource = LL_ADC_REG_TRIG_SOFTWARE;

 ADC_REG_InitStruct.SequencerLength =LL_ADC_REG_SEQ_SCAN_ENABLE_4RANKS;

 ADC_REG_InitStruct.SequencerDiscont = LL_ADC_REG_SEQ_DISCONT_DISABLE;

 ADC_REG_InitStruct.ContinuousMode =LL_ADC_REG_CONV_SINGLE;

 ADC_REG_InitStruct.DMATransfer = LL_ADC_REG_DMA_TRANSFER_NONE;

 ADC_REG_InitStruct.Overrun = LL_ADC_REG_OVR_DATA_OVERWRITTEN;

 LL_ADC_REG_Init(ADC1, &ADC_REG_InitStruct);

 LL_ADC_SetOverSamplingScope(ADC1, LL_ADC_OVS_DISABLE);

LL_ADC_DisableIT_EOC(ADC1);

LL_ADC_DisableIT_EOS(ADC1);

 /** Configure Regular Channel 

 */

 LL_ADC_REG_SetSequencerRanks(ADC1, LL_ADC_REG_RANK_1, LL_ADC_CHANNEL_4);

 LL_ADC_SetChannelSamplingTime(ADC1, LL_ADC_CHANNEL_4, LL_ADC_SAMPLINGTIME_2CYCLES_5);

 LL_ADC_SetChannelSingleDiff(ADC1, LL_ADC_CHANNEL_4, LL_ADC_SINGLE_ENDED);

 /** Configure Regular Channel 

 */

 LL_ADC_REG_SetSequencerRanks(ADC1, LL_ADC_REG_RANK_2, LL_ADC_CHANNEL_1);

 LL_ADC_SetChannelSamplingTime(ADC1, LL_ADC_CHANNEL_1, LL_ADC_SAMPLINGTIME_2CYCLES_5);

 LL_ADC_SetChannelSingleDiff(ADC1, LL_ADC_CHANNEL_1, LL_ADC_SINGLE_ENDED);

 /** Configure Regular Channel 

 */

 LL_ADC_REG_SetSequencerRanks(ADC1, LL_ADC_REG_RANK_3, LL_ADC_CHANNEL_2);

 LL_ADC_SetChannelSamplingTime(ADC1, LL_ADC_CHANNEL_2, LL_ADC_SAMPLINGTIME_2CYCLES_5);

 LL_ADC_SetChannelSingleDiff(ADC1, LL_ADC_CHANNEL_2, LL_ADC_SINGLE_ENDED);

 /** Configure Regular Channel 

 */

 LL_ADC_REG_SetSequencerRanks(ADC1, LL_ADC_REG_RANK_4, LL_ADC_CHANNEL_3);

 LL_ADC_SetChannelSamplingTime(ADC1, LL_ADC_CHANNEL_3, LL_ADC_SAMPLINGTIME_2CYCLES_5);

 LL_ADC_SetChannelSingleDiff(ADC1, LL_ADC_CHANNEL_3, LL_ADC_SINGLE_ENDED);

 /* USER CODE BEGIN ADC1_Init 2 */

 /* USER CODE END ADC1_Init 2 */

}

void Activate_ADC(void)

{

 if (LL_ADC_IsEnabled(ADC1) == 0)

 {

  /* Disable ADC deep power down (enabled by default after reset state) */

  LL_ADC_DisableDeepPowerDown(ADC1);

  /* Enable ADC internal voltage regulator */

  LL_ADC_EnableInternalRegulator(ADC1);

  wait_loop_index = ((LL_ADC_DELAY_INTERNAL_REGUL_STAB_US * (SystemCoreClock / (100000 * 2))) / 10);

  while(wait_loop_index != 0)

  {

   wait_loop_index--;

  }

  /* Run ADC self calibration */

  LL_ADC_StartCalibration(ADC1, LL_ADC_SINGLE_ENDED);

  while (LL_ADC_IsCalibrationOnGoing(ADC1) != 0)

  {

  }

  /* Delay between ADC end of calibration and ADC enable.          */

  while(wait_loop_index != 0)

  {

   wait_loop_index--;

  }

  /* Enable ADC */

  LL_ADC_Enable(ADC1);

  while (LL_ADC_IsActiveFlag_ADRDY(ADC1) == 0)

  {

    }

 }

}

uint8_t adc_read()

{

uint8_t ADC_value1,ADC_value2,ADC_value3,ADC_value4;

LL_ADC_REG_SetSequencerRanks(ADC1, LL_ADC_REG_RANK_1, LL_ADC_CHANNEL_4);

LL_ADC_SetChannelSamplingTime(ADC1, LL_ADC_CHANNEL_4, LL_ADC_SAMPLINGTIME_2CYCLES_5);

LL_ADC_SetChannelSingleDiff(ADC1, LL_ADC_CHANNEL_4, LL_ADC_SINGLE_ENDED);

  Configure_ADC();

  Activate_ADC();

ADC_value1 = LL_ADC_REG_ReadConversionData12(ADC1);

LL_ADC_REG_StopConversion(ADC1);

 LL_ADC_REG_SetSequencerRanks(ADC1, LL_ADC_REG_RANK_2, LL_ADC_CHANNEL_1);

 LL_ADC_SetChannelSamplingTime(ADC1, LL_ADC_CHANNEL_1, LL_ADC_SAMPLINGTIME_2CYCLES_5);

 LL_ADC_SetChannelSingleDiff(ADC1, LL_ADC_CHANNEL_1, LL_ADC_SINGLE_ENDED);

 Configure_ADC();

 Activate_ADC();

ADC_value2 = LL_ADC_REG_ReadConversionData12(ADC1);

LL_ADC_REG_StopConversion(ADC1);

  LL_ADC_REG_SetSequencerRanks(ADC1, LL_ADC_REG_RANK_3, LL_ADC_CHANNEL_2);

   LL_ADC_SetChannelSamplingTime(ADC1, LL_ADC_CHANNEL_2, LL_ADC_SAMPLINGTIME_2CYCLES_5);

   LL_ADC_SetChannelSingleDiff(ADC1, LL_ADC_CHANNEL_2, LL_ADC_SINGLE_ENDED);

   Configure_ADC();

   Activate_ADC();

ADC_value3 = LL_ADC_REG_ReadConversionData12(ADC1);

LL_ADC_REG_StopConversion(ADC1);

  LL_ADC_REG_SetSequencerRanks(ADC1, LL_ADC_REG_RANK_4, LL_ADC_CHANNEL_3);

  LL_ADC_SetChannelSamplingTime(ADC1, LL_ADC_CHANNEL_3, LL_ADC_SAMPLINGTIME_2CYCLES_5);

  LL_ADC_SetChannelSingleDiff(ADC1, LL_ADC_CHANNEL_3, LL_ADC_SINGLE_ENDED);

  Configure_ADC();

 Activate_ADC();

ADC_value4= LL_ADC_REG_ReadConversionData12(ADC1);

LL_ADC_REG_StopConversion(ADC1);

}

Please help me to resolve this issue.

1 REPLY 1
eos1d3
Associate III

Try:

ADC_REG_InitStruct.ContinuousMode = LL_ADC_REG_CONV_CONTINUOUS;