AnsweredAssumed Answered

STM32F042C6T6 ADC Reading 8-10% Low

Question asked by rissar on Jun 9, 2015
Latest reply on Jun 11, 2015 by rissar
I have the ADC reading the voltage drop across a 10k thermistor setup in a voltage divider w/ a 57k resistor. If I probe the board itself I can easily read the voltage drop across the thermistor and to the appropriate input pin of the STM32. This reading is consistently 8-10% higher than the 12 bit reading the ADC outputs (so the ADC is reading low). I am using the HAL ADC drivers and the ADC is reading in single conversion mode. I tried adjusting the sampling time to be longer, but it had no impact. Any idea what it could be? I inserted the code I am using for the ADC below (these functions are called by the main program):

#include "stm32f0xx_hal.h"
#include "adc.h"
 
ADC_HandleTypeDef hadc;
 
/* ADC init function */
void adc_init(void)
{
 
    ADC_ChannelConfTypeDef sConfig;
 
    /**Configure the global features of the ADC (Clock, Resolution, Data Alignment and number of conversion)
     */
    hadc.Instance = ADC1;
    hadc.Init.ClockPrescaler = ADC_CLOCK_ASYNC;
    hadc.Init.Resolution = ADC_RESOLUTION12b;
    hadc.Init.DataAlign = ADC_DATAALIGN_RIGHT;
    hadc.Init.ScanConvMode = ADC_SCAN_DIRECTION_FORWARD;
    hadc.Init.EOCSelection = EOC_SINGLE_CONV;
    hadc.Init.LowPowerAutoWait = DISABLE;
    hadc.Init.LowPowerAutoPowerOff = DISABLE;
    hadc.Init.ContinuousConvMode = DISABLE;
    hadc.Init.DiscontinuousConvMode = DISABLE;
    hadc.Init.ExternalTrigConvEdge = ADC_EXTERNALTRIGCONVEDGE_NONE;
    hadc.Init.DMAContinuousRequests = DISABLE;
    hadc.Init.Overrun = OVR_DATA_PRESERVED;
    HAL_ADC_Init(&hadc);
 
    /**Configure for the selected ADC regular channel to be converted.
     */
    sConfig.Channel = ADC_CHANNEL_0;
    sConfig.Rank = 1;
    sConfig.SamplingTime = ADC_SAMPLETIME_13CYCLES_5;
    HAL_ADC_ConfigChannel(&hadc, &sConfig);
 
    sConfig.Channel = ADC_CHANNEL_2;
    sConfig.Rank = 1;
    sConfig.SamplingTime = ADC_SAMPLETIME_13CYCLES_5;
    HAL_ADC_ConfigChannel(&hadc, &sConfig);
}
 
uint32_t adc_read(uint32_t channel) {
    HAL_StatusTypeDef adc_status;
    //ADC_ChannelConfTypeDef sConfig;
 
    //sConfig.Channel = channel;
    //HAL_ADC_ConfigChannel(&hadc, &sConfig);
    hadc.Instance->CHSELR = 1 << channel;
 
    HAL_ADC_Start(&hadc);
    adc_status = HAL_ADC_PollForConversion(&hadc, 1000);
    if (adc_status == HAL_OK) {
        return HAL_ADC_GetValue(&hadc);
    }
    return 0;
}

Outcomes