cancel
Showing results for 
Search instead for 
Did you mean: 

stm32 ADC scann mdoe

AliKavari76
Associate

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();
    }
}
1 ACCEPTED SOLUTION

Accepted Solutions
AScha.3
Chief

You set up the sequence, the ADC is converting - right ?

So you should know , whats the first channel/result and whats the next - and so on.

Results are coming just in the sequence order, you set up - so if you set sequence to convert 

ch1 - ch3 - ch4  the results are for ch1 - ch3 - ch4 -  ch1 - ch3 - ch4  ....      . Thats it.

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

View solution in original post

4 REPLIES 4
AScha.3
Chief

You set up the sequence, the ADC is converting - right ?

So you should know , whats the first channel/result and whats the next - and so on.

Results are coming just in the sequence order, you set up - so if you set sequence to convert 

ch1 - ch3 - ch4  the results are for ch1 - ch3 - ch4 -  ch1 - ch3 - ch4  ....      . Thats it.

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

Yes, I know the order of the channels, but suppose for some reason  ADC interrupt does not run (for example, due to the occurrence of a simultaneous interrupt with a higher priority). After that, we may not be able to distinguish which channel the current data belongs to

No, still the next value you get/read , is the next in the sequence.

You always do only one conversion, then read the result.

So give high enough priority to the adc interrupt , to be sure, you can keep the speed, you want.

 

Or do the (adc sequence) -> [data array]  with the DMA, then you cannot have problems about loosing a result (and the "position" in the sequence). For using the adc at hi speed and/or big array of results you should use the DMA anyway.

ed.:

from F1 rm :

AScha3_0-1705846122968.png

So if using the sequence scan mode, you need to use dma anyway and so always sequence is stored correct .

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

Or use the injected conversion ->

AScha3_0-1705847284267.png

-> have 4 register for corresponding conversion :

AScha3_1-1705847413946.png

 

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