AnsweredAssumed Answered

too high value on one channel adc

Question asked by belloc.thomas on Jul 6, 2015
Latest reply on Jul 8, 2015 by baird.hal.001
hi everybody!
I'm developing on st32f0 a device with two analogics sensors.
After the initialization, I can read properly one value but the other is always at it s maximum value (4095 for 12 bits of data). I can see my bad values in co2_value or temp_value.
Do someone know where my problem is from.
Thank you a lot.

void ADC1_Config(void)     
{    ADC_InitTypeDef          ADC_InitStructure;
     GPIO_InitTypeDef         GPIO_InitStructure;
     TIM_TimeBaseInitTypeDef  TIM_TimeBaseStructure;
      TIM_OCInitTypeDef        TIM_OCInitStructure;
 
/* GPIOB Periph clock enable */
RCC_AHBPeriphClockCmd(RCC_AHBPeriph_GPIOB, ENABLE);
 
/* ADC1 Periph clock enable */
RCC_APB2PeriphClockCmd(RCC_APB2Periph_ADC1, ENABLE);
 
RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM3, ENABLE);
 
 
/* Configure ADC Channel11 as analog input */
    GPIO_InitStructure.GPIO_Pin = GPIO_Pin_0 |GPIO_Pin_1;
    GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AN;
    GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_NOPULL ;
    GPIO_Init(GPIOB, &GPIO_InitStructure); 
 
/* ADC1 Configuration *******************************************************/
/* ADCs DeInit */
ADC_DeInit(ADC1);
 
 
/* Configure the ADC1 in continous mode with a resolutuion equal to 8 bits*/
ADC_InitStructure.ADC_Resolution = ADC_Resolution_12b;
ADC_InitStructure.ADC_ContinuousConvMode = ENABLE;
ADC_InitStructure.ADC_ExternalTrigConvEdge = ADC_ExternalTrigConvEdge_None;
ADC_InitStructure.ADC_ExternalTrigConv = ADC_ExternalTrigConv_T3_TRGO;
ADC_InitStructure.ADC_DataAlign = ADC_DataAlign_Right;
ADC_InitStructure.ADC_ScanDirection = ADC_ScanDirection_Backward;
 
ADC_Init(ADC1, &ADC_InitStructure);
 
/* Convert the ADC1 Channel 11 with 239.5 Cycles as sampling time */
ADC_ChannelConfig(ADC1, ADC_Channel_8 , ADC_SampleTime_71_5Cycles);
ADC_ChannelConfig(ADC1, ADC_Channel_9 , ADC_SampleTime_71_5Cycles);
 
/* ADC Calibration */
ADC_GetCalibrationFactor(ADC1);
 
/* Enable the auto delay feature */
ADC_WaitModeCmd(ADC1, ENABLE);
 
/* Enable the Auto power off mode */
ADC_AutoPowerOffCmd(ADC1, ENABLE);
 
/* Enable ADCperipheral[PerIdx] */
ADC_Cmd(ADC1, ENABLE);
 
/* Wait the ADCEN falg */
while(!ADC_GetFlagStatus(ADC1, ADC_FLAG_ADEN));
 
ADC_StartOfConversion(ADC1);
 
 
 
float read_temp()
{
    uint32_t calibration;
    float temp_value[SAMPLES];
    float volt_value=-1;
    float intern_value=-1;
    float intern_volt_value=-1;
    float tempconv=-1;
    int overrun=0;
    int releve=0;
    float temperature=-1;
 
    ADC_StartOfConversion(ADC1);
 
    delay_10_us();
    int i;
 
    for(i=0;i<SAMPLES;i++)
    {
        delay_ms(100);
    temp_value[i]=ADC_GetConversionValue(ADC1);
    }
 
    for(i=0;i<SAMPLES;i++){
 
        if(i%2==1)
        {
            releve++;
            tempconv=tempconv+temp_value[i];
        }
    }
 
    tempconv=tempconv/releve;
    volt_value=(tempconv*2.978)/4095;
    calibration=ADC_GetCalibrationFactor(ADC1);
    temperature=volt_value*67.126742-119.772;
 
    ADC_StopOfConversion(ADC1);
     
    return temperature;
}
 
float read_co2()
{
 
    uint32_t calibration;
        float co2_value[SAMPLES];
        float volt_value=-1;
        float intern_value=-1;
        float intern_volt_value=-1;
        float co2conv=-1;
        int overrun=0;
        int releve=0;
        float co2=-1;
 
        ADC_StartOfConversion(ADC1);
 
        delay_10_us();
        int i;
 
        for(i=0;i<SAMPLES;i++){
            delay_ms(100);
        co2_value[i]=ADC_GetConversionValue(ADC1);
        }
 
        for(i=0;i<SAMPLES;i++){
            if(i%2==0)
            {
                releve++;
                co2conv=co2conv+co2_value[i];
            }
        }
 
        co2conv=co2conv/releve;
        volt_value=(co2conv*2.978)/4095;
        calibration=ADC_GetCalibrationFactor(ADC1);
        co2=volt_value*2000/2;
        ADC_StopOfConversion(ADC1);
        return co2;
 
}

Outcomes