cancel
Showing results forย 
Search instead forย 
Did you mean:ย 

Hello everyone, I am trying to code for multichannel ADC in STM32F103RB. I have 9 analog inputs. I am using polling method to get values. The values get shifted when i read the adc values. Please help me find my mistake.

sne_123
Associate III

I have configured 9 channels as follows:- channel 5,8,9,10,11,12,13,14,15. Now Sampling time for all channels is ADC_SAMPLETIME_1CYCLE_5. I have attached potentiometer for as input voltage. Now when I change pot of channel 9, it gets reflected in channel 5 instead of channel 9. what might be the reason. Below is my configuration for the code:-

void MX_ADC1_Init(void)

{

 ADC_ChannelConfTypeDef sConfig;

  /**Common config 

  */

 hadc1.Instance = ADC1;

 hadc1.Init.ScanConvMode = ADC_SCAN_ENABLE;

 hadc1.Init.ContinuousConvMode = ENABLE;

 hadc1.Init.DiscontinuousConvMode = DISABLE;

 hadc1.Init.ExternalTrigConv = ADC_SOFTWARE_START;

 hadc1.Init.DataAlign = ADC_DATAALIGN_RIGHT;

 hadc1.Init.NbrOfConversion = 9;

 if (HAL_ADC_Init(&hadc1) != HAL_OK)

 {

  _Error_Handler(__FILE__, __LINE__);

 }

   

  /**Configure Regular Channel 

  */

 sConfig.Channel = ADC_CHANNEL_5;

 sConfig.Rank = ADC_REGULAR_RANK_1;

 sConfig.SamplingTime = ADC_SAMPLETIME_1CYCLE_5;

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

 {

  _Error_Handler(__FILE__, __LINE__);

 }

  /**Configure Regular Channel 

  */

 sConfig.Channel = ADC_CHANNEL_8;

 sConfig.Rank = ADC_REGULAR_RANK_2;

 sConfig.SamplingTime = ADC_SAMPLETIME_1CYCLE_5;

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

 {

  _Error_Handler(__FILE__, __LINE__);

 }

  /**Configure Regular Channel 

  */

 sConfig.Channel = ADC_CHANNEL_9;

 sConfig.Rank = ADC_REGULAR_RANK_3;

 sConfig.SamplingTime = ADC_SAMPLETIME_1CYCLE_5;

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

 {

  _Error_Handler(__FILE__, __LINE__);

 }

  /**Configure Regular Channel 

  */

 sConfig.Channel = ADC_CHANNEL_10;

 sConfig.Rank = ADC_REGULAR_RANK_4;

 sConfig.SamplingTime = ADC_SAMPLETIME_1CYCLE_5;

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

 {

  _Error_Handler(__FILE__, __LINE__);

 }

  /**Configure Regular Channel 

  */

 sConfig.Channel = ADC_CHANNEL_11;

 sConfig.Rank = ADC_REGULAR_RANK_5;

 sConfig.SamplingTime = ADC_SAMPLETIME_1CYCLE_5;

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

 {

  _Error_Handler(__FILE__, __LINE__);

 }

  /**Configure Regular Channel 

  */

 sConfig.Channel = ADC_CHANNEL_12;

 sConfig.Rank = ADC_REGULAR_RANK_6;

 sConfig.SamplingTime = ADC_SAMPLETIME_1CYCLE_5;

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

 {

  _Error_Handler(__FILE__, __LINE__);

 }

  /**Configure Regular Channel 

  */

 sConfig.Channel = ADC_CHANNEL_13;

 sConfig.Rank = ADC_REGULAR_RANK_7;

 sConfig.SamplingTime = ADC_SAMPLETIME_1CYCLE_5;

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

 {

  _Error_Handler(__FILE__, __LINE__);

 }

  /**Configure Regular Channel 

  */

 sConfig.Channel = ADC_CHANNEL_14;

 sConfig.Rank = ADC_REGULAR_RANK_8;

 sConfig.SamplingTime = ADC_SAMPLETIME_1CYCLE_5;

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

 {

  _Error_Handler(__FILE__, __LINE__);

 }

  /**Configure Regular Channel 

  */

 sConfig.Channel = ADC_CHANNEL_15;

 sConfig.Rank = ADC_REGULAR_RANK_9;

 sConfig.SamplingTime = ADC_SAMPLETIME_1CYCLE_5;

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

 {

  _Error_Handler(__FILE__, __LINE__);

 }

}

void HAL_ADC_MspInit(ADC_HandleTypeDef* adcHandle)

{

 GPIO_InitTypeDef GPIO_InitStruct;

 if(adcHandle->Instance==ADC1)

 {

 /* USER CODE BEGIN ADC1_MspInit 0 */

 /* USER CODE END ADC1_MspInit 0 */

  /* ADC1 clock enable */

  __HAL_RCC_ADC1_CLK_ENABLE();

  

  /**ADC1 GPIO Configuration   

  PA4   ------> ADC1_IN4

  PA5   ------> ADC1_IN5 

  */

  GPIO_InitStruct.Pin = GPIO_PIN_5;

  GPIO_InitStruct.Mode = GPIO_MODE_ANALOG;

  HAL_GPIO_Init(GPIOA, &GPIO_InitStruct);

  GPIO_InitStruct.Pin = GPIO_PIN_0|GPIO_PIN_1|GPIO_PIN_2|GPIO_PIN_3|GPIO_PIN_4|GPIO_PIN_5;

  GPIO_InitStruct.Mode = GPIO_MODE_ANALOG;

  HAL_GPIO_Init(GPIOC, &GPIO_InitStruct);

  GPIO_InitStruct.Pin = GPIO_PIN_0|GPIO_PIN_1;

  GPIO_InitStruct.Mode = GPIO_MODE_ANALOG;

  HAL_GPIO_Init(GPIOB, &GPIO_InitStruct);

}

}

In main.c file....

int main()

{

HAL_Init();

 SystemClock_Config();

 MX_GPIO_Init();

 MX_ADC1_Init();

while(1)

{

if (HAL_ADCEx_Calibration_Start(&hadc1) != HAL_OK)

{

/* Calibration Error */

Error_Handler();

}

HAL_Delay(1000);

HAL_ADC_Start(&hadc1);

if(HAL_ADC_PollForConversion(&hadc1,100)== HAL_OK)

{

adc5 = HAL_ADC_GetValue(&hadc1);

}

HAL_Delay(1000);

if(HAL_ADC_PollForConversion(&hadc1,100)== HAL_OK)

{

adc8 = HAL_ADC_GetValue(&hadc1);

}

HAL_Delay(1000);

if(HAL_ADC_PollForConversion(&hadc1,100)== HAL_OK)

{

adc9 = HAL_ADC_GetValue(&hadc1);

}

HAL_Delay(1000);

if(HAL_ADC_PollForConversion(&hadc1,100)== HAL_OK)

{

adc10 = HAL_ADC_GetValue(&hadc1);

}

HAL_Delay(1000);

if(HAL_ADC_PollForConversion(&hadc1,100)== HAL_OK)

{

adc11 = HAL_ADC_GetValue(&hadc1);

}

HAL_Delay(1000);

if(HAL_ADC_PollForConversion(&hadc1,100)== HAL_OK)

{

adc12 = HAL_ADC_GetValue(&hadc1);

}

HAL_Delay(1000);

if(HAL_ADC_PollForConversion(&hadc1,100)== HAL_OK)

{

adc13 = HAL_ADC_GetValue(&hadc1);

}

HAL_Delay(1000);

if(HAL_ADC_PollForConversion(&hadc1,100)== HAL_OK)

{

adc14 = HAL_ADC_GetValue(&hadc1);

}

HAL_Delay(1000);

if(HAL_ADC_PollForConversion(&hadc1,10)== HAL_OK)

{

adc15 = HAL_ADC_GetValue(&hadc1);

}

HAL_ADC_Stop(&hadc1);

HAL_Delay(1000);

}

}

1 REPLY 1
sne_123
Associate III

NO Answers :unamused_face: :unamused_face:

Tried to debug a lot in polling mode but it doesnt work properly....Got fed up & I configured ADC in DMA mode . Its working properly now.:D =)

But I dont understand whats the problem in polling method !!!:( :|