2012-09-10 02:10 AM
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_3unsigned 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 VoltOn PA1 I've 2,28 VoltWhen 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-f02012-09-10 05:53 PM
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, Hal2012-09-10 11:23 PM
I'm sorry for my english..
if I call APS_GetVCH();APS_GetTemp();ACDValue[0] -> 3000ADCValue[1] -> 1800if I invert the order of call like APS_GetTemp();APS_GetVCH();ACDValue[0] -> 1800ADCValue[1] -> 3000there is something wrong on the multiplexer or timing but I don't know how can be solved..Is it more clear, Hal?2012-09-11 08:13 AM
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, Hal2012-09-11 08:34 AM
/* Initialize ADC structure */
ADC_StructInit(&ADC_InitStructure);
2012-09-11 12:10 PM
No, I mean this:
typedef enum{ ADC_VCH, ADC_TEMP, ADC_REF, NofAnalogIn } t_AnalogIn; Cheers, Hal2012-09-12 02:02 AM
It's an enum 0,1,2,3,4 ecc...
2012-09-12 04:27 PM
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