Skip to main content
Visitor II
January 20, 2024
Solved

stm32 ADC scann mdoe

  • January 20, 2024
  • 2 replies
  • 1651 views

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();
 }
}
    This topic has been closed for replies.
    Best answer by AScha.3

    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.

    2 replies

    AScha.3Answer
    Super User
    January 20, 2024

    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.

    Visitor II
    January 21, 2024

    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

    Super User
    January 21, 2024

    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 .