2024-01-20 1:37 AM
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();
}
}
Solved! Go to Solution.
2024-01-20 1:53 AM - edited 2024-01-20 1:55 AM
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.
2024-01-20 1:53 AM - edited 2024-01-20 1:55 AM
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.
2024-01-21 5:23 AM
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
2024-01-21 5:56 AM - edited 2024-01-21 6:15 AM
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 :
So if using the sequence scan mode, you need to use dma anyway and so always sequence is stored correct .
2024-01-21 6:30 AM
Or use the injected conversion ->
-> have 4 register for corresponding conversion :