cancel
Showing results for 
Search instead for 
Did you mean: 

ADC configuration to get 1 value

fiulala
Associate II
Posted on October 01, 2015 at 16:03

Hi, I think this would be very easy but I can't get just 1 value from ADC on stm32f4-disco. I configure ADC like this:

void ADC_Configuration(void)
{
ADC_CommonInitTypeDef ADC_CommonInitStructure;
ADC_InitTypeDef ADC_InitStructure;
/* ADC Common Init */
ADC_CommonInitStructure.ADC_Mode = ADC_Mode_Independent;
ADC_CommonInitStructure.ADC_Prescaler = ADC_Prescaler_Div2;
ADC_CommonInitStructure.ADC_DMAAccessMode = ADC_DMAAccessMode_Disabled;
ADC_CommonInitStructure.ADC_TwoSamplingDelay = ADC_TwoSamplingDelay_5Cycles;
ADC_CommonInit(&ADC_CommonInitStructure);
ADC_InitStructure.ADC_Resolution = ADC_Resolution_12b;
ADC_InitStructure.ADC_ScanConvMode = DISABLE; // 1 Channel
ADC_InitStructure.ADC_ContinuousConvMode = DISABLE;
ADC_InitStructure.ADC_ExternalTrigConv = DISABLE;
ADC_InitStructure.ADC_DataAlign = ADC_DataAlign_Right;
ADC_InitStructure.ADC_NbrOfConversion = 1;
ADC_Init(ADC2, &ADC_InitStructure);
ADC_Cmd(ADC2, ENABLE);
/* ADC2 regular channel 12 configuration */
ADC_RegularChannelConfig(ADC2, ADC_Channel_12, 2, ADC_SampleTime_15Cycles); // PC2
}

And I do in main:

int main(void){
RCC_Configuration();
GPIO_Configuration();
ADC_Configuration();
ADC_SoftwareStartConv(ADC2); //Start ADC2 software conversion
while(ADC_GetFlagStatus(ADC2, ADC_FLAG_EOC) == RESET); //Wait for conversion complete
AD_value = ADC_GetConversionValue(ADC2); //Read ADC value
ADC_ClearFlag(ADC2, ADC_FLAG_EOC); //Clear EOC flag
while(1){
}
}

The problem is that I get random values. For instance if I connect PC2 (pin associate with ADC2CH12) to 3V I get 200 instead of 4
1 REPLY 1
fiulala
Associate II
Posted on October 01, 2015 at 16:13

I changed from:

ADC_RegularChannelConfig(ADC2, ADC_Channel_12, 2, ADC_SampleTime_15Cycles); // PC2

to

ADC_RegularChannelConfig(ADC2, ADC_Channel_12, 1, ADC_SampleTime_15Cycles); // PC2

And it seems it's working now, connecting the 3V of stm32f4 to it I get values between 4040 and 4095