cancel
Showing results for 
Search instead for 
Did you mean: 

Multichannel ADC

muliku
Associate II
Posted on May 06, 2015 at 19:17

Hi, I'm developing application on STM32F0 where I use two gas sensors (CO and CO2), so I need two channels on ADC. In my main.c I only call two functions and on beginig of each function I change channel like so:

while(1){ 
CO(); 
CO2(); 
} 
void CO() 
{double ADC1ConvertedValue; 
ADC1->CHSELR |= ADC_CHSELR_CHSEL1; 
//AD conv. and sending data to usart
} 
void CO2() 
{double ADC1ConvertedValue; 
ADC1->CHSELR |= ADC_CHSELR_CHSEL4; 
//AD conv. and sending data to usart}

When I comment on of those two functions it runs just fine (CO gives me value around 750 and CO2 gives me value around 1000 until I blow into it or something). But when I uncomment both function and wanna measure both at once it gives me weird numbers, for example CO is 800 and in very next measurment it is 2500 and then back to 800. It seems like mcu cannot handle switching the channels or what. Can anyone explain where is the problem? I'm beginer at programming STM32 and I read something about DMA that it is best way to do it, but I didnt manage to make it functional. Will this switching of channel work or do I need to use another method? btw ADC1 conf. is below:

void ADC1_Configuration(void)
{
ADC_InitTypeDef ADC_InitStructure;
/* ADC1 DeInit */
ADC_DeInit(ADC1);
/* ADC1 Periph clock enable */
RCC->APB2ENR |= (1<<9);
/* Initialize ADC structure */
ADC_StructInit(&ADC_InitStructure);
/* Configure the ADC1 in continous mode withe a resolutuion equal to 12 bits */
ADC_InitStructure.ADC_Resolution = ADC_Resolution_12b;
ADC_InitStructure.ADC_ContinuousConvMode = ENABLE;
ADC_InitStructure.ADC_ExternalTrigConvEdge = ADC_ExternalTrigConvEdge_None;
ADC_InitStructure.ADC_DataAlign = ADC_DataAlign_Right;
ADC_InitStructure.ADC_ScanDirection = ADC_ScanDirection_Upward;
ADC_Init(ADC1, &ADC_InitStructure);
/* ADC Calibration */
ADC_GetCalibrationFactor(ADC1);
/* Enable ADC1 */
ADC_Cmd(ADC1, ENABLE);
/* Wait the ADCEN falg */
while(!ADC_GetFlagStatus(ADC1, ADC_FLAG_ADEN));
/* ADC1 regular Software Start Conv */
ADC_StartOfConversion(ADC1);
}

#adc #stm32f0 #no-hablo-hal #multichannel
10 REPLIES 10
Posted on May 13, 2016 at 16:02

The ST model for multichannel ADC is to use DMA, you can set it up so the start of conversion is triggered by you, and you use the DMA TC interrupt in place of the ADC EOC.

It is likely more efficient to read the two or three samples into an array, and ignore the values you don't need, than constantly faff around with the configuration and wait for the EOC

Tips, Buy me a coffee, or three.. PayPal Venmo
Up vote any posts that you find helpful, it shows what's working..