AnsweredAssumed Answered

MB1034B STM32F0DISCOVERY Problems on ADC multichannel

Question asked by Bandini.Matteo on Sep 10, 2012
Latest reply on Sep 13, 2012 by baird.hal.001
I've a problems to use the multiplexer on analog channel:

this is my init procedure:

typedef enum{
    ADC_VCH,
    ADC_TEMP,
    ADC_REF,
    NofAnalogIn
} t_AnalogIn;

unsigned short ACDValue[NofAnalogIn];


#define RCC_PORT_AIN_VCH           RCC_AHBPeriph_GPIOA
#define GPIO_PORT_AIN_VCH      GPIOA
#define GPIO_PIN_AIN_VCH           GPIO_Pin_1
#define RCC_ADC_AIN_VCH               RCC_APB2Periph_ADC1
#define ADC_CHANNEL_VCH           ADC_Channel_1


#define RCC_PORT_AIN_TEMP      RCC_AHBPeriph_GPIOA
#define GPIO_PORT_AIN_TEMP      GPIOA
#define GPIO_PIN_AIN_TEMP      GPIO_Pin_2
#define RCC_ADC_AIN_TEMP           RCC_APB2Periph_ADC1
#define ADC_CHANNEL_TEMP           ADC_Channel_2


#define RCC_PORT_AIN_REF           RCC_AHBPeriph_GPIOA
#define GPIO_PORT_AIN_REF      GPIOA
#define GPIO_PIN_AIN_REF           GPIO_Pin_3
#define RCC_ADC_AIN_REF           RCC_APB2Periph_ADC1
#define ADC_CHANNEL_REF           ADC_Channel_3




unsigned char InitAnalog(void) {
     
ADC_InitTypeDef     ADC_InitStructure;
GPIO_InitTypeDef    GPIO_InitStructure;     
     
     
  /* GPIOC Periph clock enable */
  RCC_AHBPeriphClockCmd(RCC_PORT_AIN_VCH | RCC_PORT_AIN_TEMP | RCC_PORT_AIN_REF, ENABLE);
  
  /* ADC1 Periph clock enable */
  RCC_APB2PeriphClockCmd(RCC_ADC_AIN_VCH | RCC_ADC_AIN_TEMP | RCC_ADC_AIN_REF, ENABLE);
  
  /* Configure ADC VCH as analog input */
  GPIO_InitStructure.GPIO_Pin = GPIO_PIN_AIN_VCH ;
  GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AN;
  GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_NOPULL ;
  GPIO_Init(GPIO_PORT_AIN_VCH, &GPIO_InitStructure);
  /* Configure ADC TEMP as analog input */
  GPIO_InitStructure.GPIO_Pin = GPIO_PIN_AIN_TEMP ;
  GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AN;
  GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_NOPULL ;
  GPIO_Init(GPIO_PORT_AIN_TEMP, &GPIO_InitStructure);
  /* Configure ADC REF as analog input */
  GPIO_InitStructure.GPIO_Pin = GPIO_PIN_AIN_REF ;
  GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AN;
  GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_NOPULL ;
  GPIO_Init(GPIO_PORT_AIN_REF, &GPIO_InitStructure);  
  /* ADCs DeInit */  
  ADC_DeInit(ADC1);
  
  /* 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 = DISABLE; 
  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 */
  if (ADC_GetCalibrationFactor(ADC1) == 0) 
          return 0;
  
  /* Enable ADCperipheral[PerIdx] */
  ADC_Cmd(ADC1, ENABLE);


  /* Wait the ADCEN falg */
  while(!ADC_GetFlagStatus(ADC1, ADC_FLAG_ADEN)); 
  
  return 1;
}

unsigned short APS_GetTemp(void){
          
/* ADC1 regular Software Start Conv */ 
     ADC_ChannelConfig(ADC1, ADC_CHANNEL_TEMP , ADC_SampleTime_239_5Cycles);   
// Start the conversion 
     ADC_StartOfConversion(ADC1);
/* Test EOC flag */
  while(ADC_GetFlagStatus(ADC1, ADC_FLAG_EOC) == RESET);
  /* Get ADC1 converted data */
  ACDValue[ADC_TEMP] =ADC_GetConversionValue(ADC1);    
  return ACDValue[ADC_TEMP];
}


unsigned short APS_GetVCH(void){
     
  ADC_ChannelConfig(ADC1, ADC_CHANNEL_VCH , ADC_SampleTime_239_5Cycles);   
/* ADC1 regular Software Start Conv */ 
  ADC_StartOfConversion(ADC1);
/* Test EOC flag */
  while(ADC_GetFlagStatus(ADC1, ADC_FLAG_EOC) == RESET);
/* Get ADC1 converted data */
  ACDValue[ADC_VCH] =ADC_GetConversionValue(ADC1);
  
  return ACDValue[ADC_VCH];
}




On Pa2 I've 1,45 Volt
On PA1 I've 2,28 Volt


When I call the two function APS_GetVCH and APS_GetTemp 
some time I see 3000 - 1800 If I call GetVCH before GetTemp,
 but if I call GetTemp Before I see 1800 - 3000 ????
 
 What's wrong in my code?

Outcomes