cancel
Showing results for 
Search instead for 
Did you mean: 

MB1034B STM32F0DISCOVERY Problems on ADC multichannel

matteo239955_st
Associate II
Posted on September 10, 2012 at 11:10

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];

&sharpdefine RCC_PORT_AIN_VCH RCC_AHBPeriph_GPIOA

&sharpdefine GPIO_PORT_AIN_VCH GPIOA

&sharpdefine GPIO_PIN_AIN_VCH GPIO_Pin_1

&sharpdefine RCC_ADC_AIN_VCH RCC_APB2Periph_ADC1

&sharpdefine ADC_CHANNEL_VCH ADC_Channel_1

&sharpdefine RCC_PORT_AIN_TEMP RCC_AHBPeriph_GPIOA

&sharpdefine GPIO_PORT_AIN_TEMP GPIOA

&sharpdefine GPIO_PIN_AIN_TEMP GPIO_Pin_2

&sharpdefine RCC_ADC_AIN_TEMP RCC_APB2Periph_ADC1

&sharpdefine ADC_CHANNEL_TEMP ADC_Channel_2

&sharpdefine RCC_PORT_AIN_REF RCC_AHBPeriph_GPIOA

&sharpdefine GPIO_PORT_AIN_REF GPIOA

&sharpdefine GPIO_PIN_AIN_REF GPIO_Pin_3

&sharpdefine RCC_ADC_AIN_REF RCC_APB2Periph_ADC1

&sharpdefine 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?

#adc-smt32-f0
7 REPLIES 7
raptorhal2
Lead
Posted on September 11, 2012 at 02:53

What's wrong in my code?

Perhaps nothing.

VCH is PA1 and has the higher input voltage, so all is well as far as I can tell.

Regards, Hal

matteo239955_st
Associate II
Posted on September 11, 2012 at 08:23

I'm sorry for my english..

if I call 

APS_GetVCH();

APS_GetTemp();

ACDValue[0] -> 3000

ADCValue[1] -> 1800

if I invert the order of call like 

APS_GetTemp();

APS_GetVCH();

ACDValue[0] -> 1800

ADCValue[1] -> 3000

there is something wrong on the multiplexer or timing but I don't know how can be solved..

Is it more clear, Hal?

raptorhal2
Lead
Posted on September 11, 2012 at 17:13

Perhaps the AnalogIn structure is not initialized.

You have two copies of unsigned short APS_GetTemp(void){. Is that just a copy/paste to forum oversight?

Cheers, Hal

matteo239955_st
Associate II
Posted on September 11, 2012 at 17:34

Do you mean this?

  /* Initialize ADC structure */

  ADC_StructInit(&ADC_InitStructure);

raptorhal2
Lead
Posted on September 11, 2012 at 21:10

No, I mean this:

typedef enum{

    ADC_VCH,

    ADC_TEMP,

    ADC_REF,

    NofAnalogIn

} t_AnalogIn;

Cheers, Hal

matteo239955_st
Associate II
Posted on September 12, 2012 at 11:02

It's an enum 0,1,2,3,4 ecc...

raptorhal2
Lead
Posted on September 13, 2012 at 01:27

You are right, I am wrong.

Other than not running the ADC continuously, you are following the F0 library basic ADC example code. I can't spot any problem and I don't have an F0 to try your code on. My suggestion is to step through the code and make sure that items like ADC_TEMP and the converted values are what you expect.

Possibly someone else can spot something.

Cheers, Hal