3
votes

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;
}
4
The big eval boards usually use a larger package which would have VRef brought out on a pin. Are you sure your 12-bits are representing 0v - 3.3v and not something like 0v - 2.5v?rjp

4 Answers

3
votes

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.

2
votes

You do have to wait for the conversion to complete:

ADC_SoftwareStartConv(ADC3);

while( ADC_GetFlagStatus( ADC3, ADC_FLAG_EOC ) == RESET )
{
    // do nothing (or something useful perhaps)
}

temp_value = ADC_GetConversionValue(ADC3);
0
votes

Did you measure the voltage at the temperature sensor and the voltage on your ACD input and VCC voltage on your device? It is better to include their run-time values at the moment you are taking this 5000+ output.

Did you try to feed known in advance voltage (say 3.3 V) to the same ADC input and compare measured/obtained values? Because it seems the voltage you measure taking 5000+ is like 4+V.

As for code, it is probably better to mask returning value in order to take only 12 bit into account:

return (uint16_t) ((ADCx->DR) & 0xFFF)

0
votes

The following code has worked for me:


#define HW_DIVIDE      2
#define VREF_IN_mV     2500
#define MAX_RESOLUTION 255

static void _setup_rcc()
{
    // Enable the ADC interface clock
    RCC_APB2PeriphClockCmd(RCC_APB2Periph_ADC1, ENABLE);

    // Enable the clock for the ADC GPIOs
    RCC_AHB1PeriphClockCmd( RCC_AHB1Periph_GPIOA, ENABLE);
}

static void _setup_gpio()
{
    GPIO_InitTypeDef GPIO_InitStructure;

    // Configure source 1
    GPIO_InitStructure.GPIO_Pin = GPIO_Pin_4;
    GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AN;
    GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_NOPULL;
    GPIO_Init(GPIOA, &GPIO_InitStructure);

    // Configure source 2
    GPIO_InitStructure.GPIO_Pin = GPIO_Pin_5;
    GPIO_Init(GPIOA, &GPIO_InitStructure);

    // Configure source 3
    GPIO_InitStructure.GPIO_Pin = GPIO_Pin_6;
    GPIO_Init(GPIOA, &GPIO_InitStructure);

    // Configure source 4
    GPIO_InitStructure.GPIO_Pin = GPIO_Pin_7;
    GPIO_Init(GPIOA, &GPIO_InitStructure);
}

static void _setup_adc()
{
    ADC_InitTypeDef ADC_InitStructure;
    ADC_DeInit();
    ADC_InitStructure.ADC_DataAlign = ADC_DataAlign_Right;
    ADC_InitStructure.ADC_Resolution = ADC_Resolution_8b;
    ADC_InitStructure.ADC_ContinuousConvMode = ENABLE;
    ADC_InitStructure.ADC_ExternalTrigConvEdge = ADC_ExternalTrigConvEdge_None;
    ADC_InitStructure.ADC_DataAlign = ADC_DataAlign_Right;
    ADC_InitStructure.ADC_NbrOfConversion = 16;
    ADC_InitStructure.ADC_ScanConvMode = DISABLE;
    ADC_Init(ADC1, &ADC_InitStructure);

    // Enable ADC
    ADC_Cmd(ADC1, ENABLE);
}

void drv_adc_init()
{
    _setup_rcc();
    _setup_gpio();
    _setup_adc();
}

uint16_t drv_adc_get_voltage(ADC_CHANNEL_IN type)
{
    uint16_t val;

    switch (type)
    {
    case ADC_3V3:
        ADC_RegularChannelConfig(ADC1,ADC_Channel_4,1,ADC_SampleTime_15Cycles);
        break;
    case ADC_1V2:
        ADC_RegularChannelConfig(ADC1,ADC_Channel_5,1,ADC_SampleTime_15Cycles);
        break;
    case ADC_3VA:
        ADC_RegularChannelConfig(ADC1,ADC_Channel_6,1,ADC_SampleTime_15Cycles);
        break;
    case VIB_CUR:
        ADC_RegularChannelConfig(ADC1,ADC_Channel_7,1,ADC_SampleTime_15Cycles);
        break;
    }

    do
    {
        // Start the conversion
        ADC_SoftwareStartConv(ADC1);

        // Processing the conversion
        if (ADC_GetFlagStatus(ADC1, ADC_FLAG_OVR))
        {
            ADC1->SR &= ~ADC_FLAG_OVR;
        }
    }
    while(!ADC_GetFlagStatus(ADC1, ADC_FLAG_EOC));

    // Calculate the converted data
    val = ADC_GetConversionValue(ADC1);

    // Return voltage in millivolts
    return HW_DIVIDE * VREF_IN_mV * val / MAX_RESOLUTION;
}