STM32 ADC值读数过高

时间:2014-07-17 00:12:26

标签: c++ c embedded stm32 adc

我正在使用STM3240G-EVAL板读取ADC的值。我使用下面的打印功能将ADC中的值打印到我板上的LCD上。我将温度传感器物理连接到评估板上的3.3V,ADC3和GND。返回的值太大。 ADC分辨率应为12位,因此4096应为ADC_GetConversionValue函数输出的最大值。我在室温下接收5000+值!有没有人对为什么ADC值可以缩放有任何直觉?

////// 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 个答案:

答案 0 :(得分:3)

我错过了ADC配置;

ADC_InitStructure.ADC_NbrOfConversion = 1;

确保在使用ADC时,您正在使用所有配置而不会遗漏任何配置。在包括这个之后,我能够使用万用表获得与传感器读取的ADC值相同的电压。

添加说明:

这是因为您已将以下结构定义为本地

ADC_InitTypeDef           ADC_InitStructure;

ADC_CommonInitTypeDef     ADC_CommonInitStructure;

局部变量具有不可靠(随机)初始值,因此可能导致例如 ADC_NbrOfConversion 变为不合适的数字将其写入相应的寄存器时出现问题。

答案 1 :(得分:2)

您必须等待转换完成:

ADC_SoftwareStartConv(ADC3);

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

temp_value = ADC_GetConversionValue(ADC3);

答案 2 :(得分:0)

您是否测量了温度传感器上的电压以及设备上ACD输入电压和VCC电压?在您使用5000+输出时,最好包括它们的运行时值。

您是否尝试将已知的提前电压(例如3.3 V)提供给相同的ADC输入并比较测量/获得的值?因为看起来你测量的电压达到5000 +就像是4 + V.

至于代码,最好掩盖返回值以便只考虑12位:

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

答案 3 :(得分:0)

以下代码对我有用:


#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;
}