STM32 ADC values reading too high

rayray picture rayray · Jul 17, 2014 · Viewed 7.7k times · Source

I am using an STM3240G-EVAL board to read in values from the ADC. I print the values from the ADC to the LCD on my board using the print function below. I physically connected the temperature sensor to 3.3V, ADC3, and GND on the eval board. The values that are being returned are too large. The ADC Resolution is supposed to be 12 bit so 4096 should be the maximum value output by the ADC_GetConversionValue function. I am receiving 5000+ values at room temperature! Does anyone have any intuition as to why the ADC values could be getting scaled?

////// stm324xg_eval.c
// to configure the ADC
void STM_EVAL_TEMPInit(Temp_TypeDef Temp) 
{

RCC_PCLK2Config(RCC_HCLK_Div8);
RCC_APB2PeriphClockCmd(RCC_APB2Periph_ADC3, ENABLE);
/* Enable the GPIO_TEMP Clock */
RCC_AHB1PeriphClockCmd(GPIO_TEMP_CLK[Temp], ENABLE);

/* Configure the GPIO_TEMP pin */
GPIO_InitTypeDef  GPIO_InitStructure;
GPIO_InitStructure.GPIO_Pin = GPIO_TEMP_PIN[Temp];
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AN;
GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_NOPULL;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_Init(GPIO_TEMP_PORT[Temp], &GPIO_InitStructure);


/* Enable ADC3 Clock */
ADC_InitTypeDef           ADC_InitStructure;
ADC_CommonInitTypeDef     ADC_CommonInitStructure;

ADC_CommonInitStructure.ADC_Mode = ADC_Mode_Independent;
ADC_CommonInitStructure.ADC_Prescaler = ADC_Prescaler_Div4;
ADC_CommonInitStructure.ADC_DMAAccessMode = ADC_DMAAccessMode_Disabled;
ADC_CommonInitStructure.ADC_TwoSamplingDelay = 0;
ADC_CommonInit(&ADC_CommonInitStructure);

ADC_InitStructure.ADC_ContinuousConvMode = DISABLE;
ADC_InitStructure.ADC_Resolution = ADC_Resolution_12b;
ADC_InitStructure.ADC_ScanConvMode = DISABLE;
ADC_InitStructure.ADC_ExternalTrigConvEdge = ADC_ExternalTrigConvEdge_None;
ADC_InitStructure.ADC_DataAlign = ADC_DataAlign_Right;      
ADC_Init(ADC3, &ADC_InitStructure);

ADC_RegularChannelConfig(ADC3, ADC_Channel_4, 1, ADC_SampleTime_144Cycles);
ADC_Cmd(ADC3, ENABLE);
}


////// main.cpp
// to print to lcd  
ADC_SoftwareStartConv(ADC3);
temp_value = ADC_GetConversionValue(ADC3);
uint8_t mymsg[20];
sprintf((char *)mymsg, "ADC = %d",(int)temp_value);
LCD_DisplayStringLine(Line6, mymsg);


////// stm32f4xx_adc.c
// ADC_GetConversionValue function
/**
* @brief  Returns the last ADCx conversion result data for regular channel.
* @param  ADCx: where x can be 1, 2 or 3 to select the ADC peripheral.
* @retval The Data conversion value.
*/
uint16_t ADC_GetConversionValue(ADC_TypeDef* ADCx)
{
   /* Check the parameters */
   assert_param(IS_ADC_ALL_PERIPH(ADCx));
   /* Return the selected ADC conversion value */
   return (uint16_t) ADCx->DR;
}

Answer

rayray picture rayray · Aug 5, 2014

I was missing an ADC configuration;

ADC_InitStructure.ADC_NbrOfConversion = 1;

Make sure when you are using the ADC you are using all the configurations and not missing any. After including this I was able to get the same voltage using a multimeter as the ADC values read by the sensor.

Added description :

And this is because of you have defined the following structures as a local one :

ADC_InitTypeDef           ADC_InitStructure;

ADC_CommonInitTypeDef     ADC_CommonInitStructure;

and a local variable has an unreliable(random) initial value so that it may cause for example ADC_NbrOfConversion becomes inappropriate number that makes such a problem when it has been written in the corresponding register.