2021-11-11 02:14 AM
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?
2021-11-11 05:37 AM
You may need HAL_ADC_Stop at the end, before reconfiguring the channel.
2021-11-11 07:55 AM
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);