Random behaviour while using ADC on STM32f030cc
Hello.
I'm using STM32f030CC in which we have to use 2 ADC channels.
Following is the Initialization part.
ADC_InitTypeDef stADC_InitStructure;
GPIO_InitTypeDef GPIO_InitStructure;
RCC_AHBPeriphClockCmd(RCC_AHBPeriph_GPIOB , ENABLE);
/********************* GPIO INITIALIZATION***************/
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_1 | GPIO_Pin_0;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AN;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_2MHz;
GPIO_InitStructure.GPIO_OType = GPIO_OType_PP;
GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_NOPULL;
GPIO_Init(GPIOB, &GPIO_InitStructure);
/********************* ADC1 INITIALIZATION***************/
RCC_APB2PeriphClockCmd(RCC_APB2Periph_ADC1, ENABLE);
/*ADC Deinit*/
ADC_DeInit(ADC1);
/*ADC Structure Initialization*/
ADC_StructInit(&stADC_InitStructure);
stADC_InitStructure.ADC_Resolution = ADC_Resolution_12b;
stADC_InitStructure.ADC_ContinuousConvMode = DISABLE;;
stADC_InitStructure.ADC_ExternalTrigConvEdge = ADC_ExternalTrigConvEdge_None;
stADC_InitStructure.ADC_DataAlign = ADC_DataAlign_Right;
stADC_InitStructure.ADC_ScanDirection=ADC_ScanDirection_Upward;
ADC_Init(ADC1, &stADC_InitStructure);
/* Enable the ADC peripheral */
ADC_Cmd(ADC1, ENABLE);
/* Wait the ADRDY flag */
while(!ADC_GetFlagStatus(ADC1, ADC_FLAG_ADRDY));
//while(ADC_GetCalibrationStatus(ADC1) == SET);Now in the phantom loop we're continuously sampling the ADC channels sequentially as follows.
if(uiSamplingCount <= NO_OF_SAMPLES)
{
val1 = fnA();
val2 = fnB();
uiSamplingCount++;
}
else
{
uiSamplingCount=0;
/* Averaging the values along with calibration */
/* Print the values */
}Definition for the above functions are as follows:
void fnA(void)
{
/* Configure channel */
ADC_ChannelConfig(ADC1, ADC_Channel_9, ADC_SampleTime_55_5Cycles);
/* Start the conversion */
ADC_StartOfConversion(ADC1);
/* Wait until conversion completion */
while(ADC_GetFlagStatus(ADC1, ADC_FLAG_EOC) == RESET);
/* Get the conversion value */
return ADC_GetConversionValue(ADC1);
}
void fnB(void)
{
/* Configure channel */
ADC_ChannelConfig(ADC1, ADC_Channel_8, ADC_SampleTime_55_5Cycles);
/* Start the conversion */
ADC_StartOfConversion(ADC1);
/* Wait until conversion completion */
while(ADC_GetFlagStatus(ADC1, ADC_FLAG_EOC) == RESET);
/* Get the conversion value */
return ADC_GetConversionValue(ADC1);
}Now the issue is we're getting offset values when we use the above sequence.
Second test case:
We're reading only individual channel value and printing the same.
Output is proper.
Third test case:
fnB() is executed first and then fnA() ie Channel 8 first and then Channel 9.
In this case after 5 to 6 loops it stats giving proper values.
Fourth test case:
At a time we're either taking 1000 samples from Channel 8 or 1000 samples from Channel 9 with a delay of 4 secs in between them.
My question is, Is there a dependency between the channels we're using ?
Is there an issue of some settling time in ADC so some error in the sequence that we're missing?
Regards,
Aatif.
@Community member
@Community member
@Community member
@Pavel A.
@henry.dick
