AnsweredAssumed Answered

Adc channel problem

Question asked by Kauppi.Teijo on Nov 5, 2013
Latest reply on Nov 5, 2013 by Kauppi.Teijo
Hello,

I'm using stm32f417vg chip and arm-none-eabi-gcc compiler(and gdb). I try to get adc to read 14 analog input channels. Channels 1, 2, 3 values are fine, but when i read channel 4 value doesn't change at all. I use code below:

typedef struct {
    GPIO_TypeDef* port;
    uint32_t pin;
}gpioConfigs_t;

static gpioConfigs_t gpioConfig[] = {
    {GPIOA,GPIO_Pin_1},
    {GPIOA,GPIO_Pin_2},
    {GPIOA,GPIO_Pin_3},
    {GPIOA,GPIO_Pin_4},
    {GPIOA,GPIO_Pin_5},
    {GPIOA,GPIO_Pin_6},
    {GPIOA,GPIO_Pin_7},
    {GPIOB,GPIO_Pin_0},
    {GPIOB,GPIO_Pin_1},
    {GPIOC,GPIO_Pin_0},
    {GPIOC,GPIO_Pin_1},
    {GPIOC,GPIO_Pin_2},
    {GPIOC,GPIO_Pin_3},
    {GPIOC,GPIO_Pin_4}
};

void internalAdc_init2(void)
{

    GPIO_InitTypeDef      GPIO_InitStructure;
    ADC_CommonInitTypeDef ADC_CommonInitStructure;
    ADC_InitTypeDef       ADC_InitStructure;

    RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOA, ENABLE);
    RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOB, ENABLE);
    RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOC, ENABLE);
    RCC_APB2PeriphClockCmd(RCC_APB2Periph_ADC3, ENABLE);

    for(i = 0; i < INTERNAL_ADC_CHANNELS; i++){
            GPIO_InitStructure.GPIO_Pin = gpioConfig[i].pin;
            GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AN;
            GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_NOPULL ;
            GPIO_Init(gpioConfig[i].port, &GPIO_InitStructure);
        }

    ADC_CommonInitStructure.ADC_Mode = ADC_Mode_Independent;
    ADC_CommonInitStructure.ADC_Prescaler = ADC_Prescaler_Div2;
    ADC_CommonInitStructure.ADC_DMAAccessMode = ADC_DMAAccessMode_Disabled;
    ADC_CommonInitStructure.ADC_TwoSamplingDelay = ADC_TwoSamplingDelay_5Cycles;
    ADC_CommonInit(&ADC_CommonInitStructure);

    /* ADC3 Init */
    ADC_InitStructure.ADC_Resolution = ADC_Resolution_12b;
    ADC_InitStructure.ADC_ScanConvMode = DISABLE;
    ADC_InitStructure.ADC_ContinuousConvMode = DISABLE;
    ADC_InitStructure.ADC_ExternalTrigConvEdge = ADC_ExternalTrigConvEdge_None;
    ADC_InitStructure.ADC_DataAlign = ADC_DataAlign_Right;
    ADC_InitStructure.ADC_NbrOfConversion = 1;
    ADC_Init(ADC3, &ADC_InitStructure);

    /* Enable ADC3 */
    ADC_Cmd(ADC3, ENABLE);
}
uint16_t readAdc(uint8_t channel)
{
    uint16_t res, val = 0;
    ADC_RegularChannelConfig(ADC3, channel, 1, ADC_SampleTime_3Cycles);
    ADC_SoftwareStartConv(ADC3);
    while(ADC_GetFlagStatus(ADC3, ADC_FLAG_EOC) == RESET);
    val = ADC_GetConversionValue(ADC3);
    res = val >> 4;

    return res;

}

from app ->
uint16_t testData;
testData = readAdc(1); /* ok */
testData = readAdc(2); /* ok */
testData = readAdc(3);/* ok */
testData = readAdc(4);/* This value does not change */

Outcomes