How can I find out which ADC input channel the data in the ADC register belongs to?

44 Views Asked by At

In the ADC scan mode in STM32 microcontrollers, the digitized value of all input channels is dumped into ADCx->DR. Is there a register or flags to find out which ADC channel the data currently in the ADCx->DR register

void HAL_ADC_ConvCpltCallback(ADC_HandleTypeDef* hadc)
{
    if(hadc->Instance == ADC1)
    {
        uint16_t adc_data;
        
        // Read the converted data from the ADC data register
        adc_data = ADC1->DR; //I don't know which ADC channel this data is related to
        
        // Process the ADC data as needed
        // ...
    }
}

void ADC_Init()
{
    ADC_ChannelConfTypeDef sConfig;

    hadc.Instance = ADC1;
    hadc.Init.ScanConvMode = ENABLE;
    hadc.Init.ContinuousConvMode = ENABLE;
    hadc.Init.NbrOfConversion = 2; // Number of channels to convert
    hadc.Init.DiscontinuousConvMode = DISABLE;
    hadc.Init.ExternalTrigConv = ADC_SOFTWARE_START;
    hadc.Init.DataAlign = ADC_DATAALIGN_RIGHT;
    hadc.Init.NbrOfDiscConversion = 1;
    if (HAL_ADC_Init(&hadc) != HAL_OK)
    {
        Error_Handler();
    }

    // Configure the ADC channels to be converted
    sConfig.Channel = ADC_CHANNEL_0;
    sConfig.Rank = 1;
    sConfig.SamplingTime = ADC_SAMPLETIME_13CYCLES_5;
    if (HAL_ADC_ConfigChannel(&hadc, &sConfig) != HAL_OK)
    {
        Error_Handler();
    }

    sConfig.Channel = ADC_CHANNEL_1;
    sConfig.Rank = 2;
    if (HAL_ADC_ConfigChannel(&hadc, &sConfig) != HAL_OK)
    {
        Error_Handler();
    }
}

0

There are 0 best solutions below