cancel
Showing results for 
Search instead for 
Did you mean: 

Multi channel ADC: am I sensing right?

DU2
Associate II

Hello, I am using L031K6 with its board

I am trying to sense multi- adc port. One is ADC 1 channel 3, other one is ADC1 channel 7

I open example code of ADC_DMA and overwrite it, for making my MCU sense these ADC channels, CH 3 & CH 7

but problem is, when I connect 3.3V to CH3 , CH7 is also increasing its ADC sense value , vice versa.

precisely, when I give 3.3v to ADC 1 CH 7, the CH3 adc value returns 4096 and it is correct because L031K6 ADC bits is 12bit. but the CH3 - which is not physically related - returns about 3000 ( check the screenshots, First one is default status, second is connecting to CH7 , When connecting to CH 3 then even number index array is 4096, others are about 3000 )

​is it because these port are same ADC? or Do I set something wrong ?

thanks for reading.

below is my code:

//msp

void HAL_ADC_MspInit(ADC_HandleTypeDef *hadc)

{

 GPIO_InitTypeDef     GPIO_InitStruct;

 static DMA_HandleTypeDef     DmaHandle;

  

 /*##-1- Enable peripherals and GPIO Clocks #################################*/

 /* Enable GPIO clock ****************************************/

 __HAL_RCC_GPIOA_CLK_ENABLE();

 /* ADC1 Periph clock enable */

 ADCx_CLK_ENABLE();

 /* Enable DMA1 clock */

 __HAL_RCC_DMA1_CLK_ENABLE();

  

 /*##- 2- Configure peripheral GPIO #########################################*/

 /* ADC Channel GPIO pin configuration */

 GPIO_InitStruct.Pin = GPIO_PIN_3 | GPIO_PIN_7;

 GPIO_InitStruct.Mode = GPIO_MODE_ANALOG;

 GPIO_InitStruct.Pull = GPIO_NOPULL;

 HAL_GPIO_Init(ADCx_CHANNEL_GPIO_PORT, &GPIO_InitStruct);

 /*##- 3- Configure DMA #####################################################*/ 

 /*********************** Configure DMA parameters ***************************/

 DmaHandle.Instance         = DMA1_Channel1;

 DmaHandle.Init.Direction      = DMA_PERIPH_TO_MEMORY;

 DmaHandle.Init.PeriphInc      = DMA_PINC_DISABLE;

 DmaHandle.Init.MemInc       = DMA_MINC_ENABLE;

 DmaHandle.Init.PeriphDataAlignment = DMA_PDATAALIGN_HALFWORD;

 DmaHandle.Init.MemDataAlignment  = DMA_MDATAALIGN_HALFWORD;

 DmaHandle.Init.Mode        = DMA_CIRCULAR;

 DmaHandle.Init.Priority      = DMA_PRIORITY_MEDIUM;

 DmaHandle.Init.Request       = DMA_REQUEST_0;

 /* Deinitialize & Initialize the DMA for new transfer */

 HAL_DMA_DeInit(&DmaHandle);

 HAL_DMA_Init(&DmaHandle);

  

 /* Associate the DMA handle */

 __HAL_LINKDMA(hadc, DMA_Handle, DmaHandle);

 /* NVIC configuration for DMA Input data interrupt */

 HAL_NVIC_SetPriority(DMA1_Channel1_IRQn, 1, 0);

 HAL_NVIC_EnableIRQ(DMA1_Channel1_IRQn);

}

//main

 /* ### - 1 - Initialize ADC peripheral #################################### */

  

 AdcHandle.Instance     = ADCx;

 if (HAL_ADC_DeInit(&AdcHandle) != HAL_OK)

 {

  /* ADC de-initialization Error */

  Error_Handler();

 }

 AdcHandle.Init.ClockPrescaler    = ADC_CLOCK_SYNC_PCLK_DIV2;   /* Synchronous clock mode, input ADC clock with prscaler 2 */

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

 AdcHandle.Init.LowPowerFrequencyMode = DISABLE;

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

 AdcHandle.Init.ContinuousConvMode  = ENABLE;            /* Continuous mode enabled (automatic conversion restart after each conversion) */

 AdcHandle.Init.DiscontinuousConvMode = DISABLE;            /* 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 = ENABLE;            /* ADC DMA continuous request to match with DMA circular mode */

 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 */

 AdcHandle.Init.SamplingTime     = ADC_SAMPLETIME_7CYCLES_5;

 /* Initialize ADC peripheral according to the passed parameters */

 if (HAL_ADC_Init(&AdcHandle) != HAL_OK)

 {

  Error_Handler();

 }

  

  

 /* ### - 2 - Start calibration ############################################ */

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

 {

  Error_Handler();

 }

  

 /* ### - 3 - Channel configuration ######################################## */

 sConfig.Channel   = ADC_CHANNEL_3;        /* Channel to be converted */

 sConfig.Rank     = ADC_RANK_CHANNEL_NUMBER;

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

 {

  Error_Handler();

 }

  

  sConfig.Channel   = ADC_CHANNEL_7;        /* Channel to be converted */

 sConfig.Rank     = ADC_RANK_CHANNEL_NUMBER;

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

 {

  Error_Handler();

 }

  

 /* ### - 4 - Start conversion in DMA mode ################################# */

 if (HAL_ADC_Start_DMA(&AdcHandle,

            (uint32_t *)aADCxConvertedData, //this is my ADC data[]

            ADC_CONVERTED_DATA_BUFFER_SIZE //32 but essentially 2

            ) != HAL_OK)

 {

  Error_Handler();

 }

  

 /* Infinite Loop */

 while (1)

 { 

 }

1 REPLY 1
TDK
Guru

> When connecting to CH 3 then even number index array is 4096, others are about 3000 )

Okay. How are the other channels connected?

There is going to be some cross talk between a channel and the next converted channel. Increase sampling time to mitigate.

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