AnsweredAssumed Answered

Multichannel ADC

Question asked by nerold.adam on May 6, 2015
Latest reply on May 13, 2016 by Clive One
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);
}

Outcomes