multiple channel adc reads the same value in all the channel in stm32
HI all i have been working in stm32L072CBT6 controller i have the code to read the adc multiple channel and i can read the values for different channels individually by using separate code for each channel but if im reading all the four channels altogether the the value in one channel is reflecting in all the channels. And i have also checked whether the voltage in each channel is same and found that all the values are different though im getting the same values for example channel1 values reflecting in channel 0 any thought on this issue kindly share me. I have attached the code below.
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_SYNC_PCLK_DIV4;
hadc1.Init.Resolution = ADC_RESOLUTION_12B;
hadc1.Init.ScanConvMode = ENABLE;
hadc1.Init.ContinuousConvMode = ENABLE;
hadc1.Init.DiscontinuousConvMode = DISABLE;
hadc1.Init.ExternalTrigConvEdge = ADC_EXTERNALTRIGCONVEDGE_NONE;
hadc1.Init.ExternalTrigConv = ADC_SOFTWARE_START;
hadc1.Init.DataAlign = ADC_DATAALIGN_RIGHT;
hadc1.Init.DMAContinuousRequests = DISABLE;
hadc1.Init.EOCSelection = ADC_EOC_SINGLE_CONV;//ADC_EOC_SINGLE_CONV;
//naga
hadc.Init.SamplingTime = ADC_SAMPLETIME_160CYCLES_5;
if (HAL_ADC_Init(&hadc1) != HAL_OK)
{
Error_Handler();
}
}
void ADC_Select_CH0 (void)
{
ADC_ChannelConfTypeDef sConfig = {0};
/** Configure for the selected ADC regular channel its corresponding rank in the sequencer and its sample time.
*/
sConfig.Channel = ADC_CHANNEL_0;
sConfig.Rank = ADC_RANK_CHANNEL_NUMBER;
//sConfig.SamplingTime = ADC_SAMPLETIME_160CYCLES_5;
if (HAL_ADC_ConfigChannel(&hadc1, &sConfig) != HAL_OK)
{
Error_Handler();
}
}
void ADC_Select_CH1 (void)
{
ADC_ChannelConfTypeDef sConfig = {0};
/** Configure for the selected ADC regular channel its corresponding rank in the sequencer and its sample time.
*/
sConfig.Channel = ADC_CHANNEL_1;
sConfig.Rank = ADC_RANK_CHANNEL_NUMBER;
//sConfig.SamplingTime = ADC_SAMPLETIME_84CYCLES;
if (HAL_ADC_ConfigChannel(&hadc1, &sConfig) != HAL_OK)
{
Error_Handler();
}
}
int main(void)
{
HAL_Init();
SystemClock_Config();
/* Initialize all configured peripherals */
MX_GPIO_Init();
MX_USART1_UART_Init();
MX_USART2_UART_Init();
MX_ADC1_Init();
// MX_ADC_Init();
/* Infinite loop */
/* USER CODE BEGIN WHILE */
while (1)
{
AD_RES0 = 0;
AD_RES1 = 0;
// HAL_UART_Transmit(&huart1,Buf, sizeof(Buf), 100);
// HAL_GPIO_WritePin(BUZZER_GPIO_Port, BUZZER_Pin, GPIO_PIN_RESET);
ADC_Select_CH0();
HAL_ADC_Start(&hadc1);
HAL_ADC_PollForConversion(&hadc1, 1000);
AD_RES0 = HAL_ADC_GetValue(&hadc1);
HAL_ADC_Stop(&hadc1);
sprintf(Buf,"adc0 %u \r\n", AD_RES0);
HAL_UART_Transmit(&huart1,Buf, sizeof(Buf), 100);
// ADC->CCR |= (1<<ADC->ADEN);
HAL_Delay(10);
ADC_Select_CH1();
HAL_ADC_Start(&hadc1);
HAL_ADC_PollForConversion(&hadc1, 1000);
AD_RES1 = HAL_ADC_GetValue(&hadc1);
HAL_ADC_Stop(&hadc1);
sprintf(Buf," adc1 %u \r\n", AD_RES1);
HAL_UART_Transmit(&huart1,Buf, sizeof(Buf), 100);
HAL_Delay(10);
}
}
