cancel
Showing results for 
Search instead for 
Did you mean: 

Two channels from the same ADC

DCarr.1
Senior

Hello,

I have a custom STM32F439VITx based board, with two analog sources wired to PC0 (ADC1_IN10) and PC1 (ADC1_IN11). The sources need to be read infrequently, on the order of every five to ten seconds, and independently of each other (so sometimes I read one, sometimes the other sometimes neither) and in-between readings I need to turn as much off as possible to save power.

I configured the ADC and channels in CubeIDE pretty much by hit-and-miss and copying various tutorials and examples but when it actually comes to taking the readings I really don't know how to convince the ADC which channel it should read. My code to read one of the channels is pretty much like this:

static ADC_ChannelConfTypeDef channel = {
    .Channel = ADC_CHANNEL_11,
    .Rank = 0,
    .SamplingTime = ADC_SAMPLETIME_480CYCLES,
    .Offset = 0
};
 
HAL_StatusTypeDef hal = HAL_ADC_ConfigChannel(&hadc1, &channel);
hal = hal == HAL_OK? HAL_ADC_Start(&hadc1): hal;
hal = hal == HAL_OK? HAL_ADC_PollForConversion(&hadc1, HAL_MAX_DELAY): hal;
uint32_t adc = hal == HAL_OK? HAL_ADC_GetValue(&hadc1): 0;

Assuming I can live with the relatively inefficient polling, is this the correct way to select which channel I want to read?

2 REPLIES 2
TDK
Guru

You may need HAL_ADC_Stop at the end, before reconfiguring the channel.

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

In Stdlibs example code HAL have similar...

	ADC1->CHSELR = 0; //reset none chan selected 
 
	ADC_ChannelConfig(ADC1, ADC_Channel_3, ADC_SampleTime_13_5Cycles);   
	ADC_StartOfConversion(ADC1);
	while(ADC_GetFlagStatus(ADC1, ADC_FLAG_EOC) == RESET);
	samp[0]=ADC_GetConversionValue(ADC1);