AnsweredAssumed Answered

Change ADC channel

Question asked by Lukasz Przenioslo on Feb 1, 2016
Hello there,
I am using STM32F4. I am trying to use ADC1 to meassure 2 voltages (on channel 8 and channel 7). I want to do it one by one, in polling mode, but I cant figure out how to tell the MCU to change the channel.

This is my configuration:
/* ADC1 init function */
void MX_ADC1_Init(void)
{
  ADC_ChannelConfTypeDef sConfig;
 
    /**Configure the global features of the ADC (Clock, Resolution, Data Alignment and number of conversion)
    */
  hadc1.Instance = ADC1;
  hadc1.Init.ClockPrescaler = ADC_CLOCKPRESCALER_PCLK_DIV4;
  hadc1.Init.Resolution = ADC_RESOLUTION12b;
  hadc1.Init.ScanConvMode = DISABLE;
  hadc1.Init.ContinuousConvMode = DISABLE;
  hadc1.Init.DiscontinuousConvMode = DISABLE;
  hadc1.Init.ExternalTrigConvEdge = ADC_EXTERNALTRIGCONVEDGE_NONE;
  hadc1.Init.DataAlign = ADC_DATAALIGN_RIGHT;
  hadc1.Init.NbrOfConversion = 2;
  hadc1.Init.DMAContinuousRequests = DISABLE;
  hadc1.Init.EOCSelection = EOC_SINGLE_CONV;
  HAL_ADC_Init(&hadc1);
 
    /**Configure for the selected ADC regular channel its corresponding rank in the sequencer and its sample time.
    */
  sConfig.Channel = ADC_CHANNEL_8;
  sConfig.Rank = 1;
  sConfig.SamplingTime = ADC_SAMPLETIME_3CYCLES;
  HAL_ADC_ConfigChannel(&hadc1, &sConfig);
 
    /**Configure for the selected ADC regular channel its corresponding rank in the sequencer and its sample time.
    */
  sConfig.Channel = ADC_CHANNEL_7;
  sConfig.Rank = 2;
  HAL_ADC_ConfigChannel(&hadc1, &sConfig);
 
}

To measure the voltage I do (test function)

void adc_Init(ADC_HandleTypeDef* handle)
{
    assert_param(handle);
    p_adc_Handle = handle;
    float adcVal = 0;
    ADC_ChannelConfTypeDef sConfig;
 
    sConfig.Channel = ADC_CHANNEL_8;
    sConfig.Rank = 1;
    sConfig.SamplingTime = ADC_SAMPLETIME_3CYCLES;
    HAL_ADC_ConfigChannel(p_adc_Handle, &sConfig);
    HAL_ADC_Start(p_adc_Handle);
    HAL_ADC_PollForConversion(p_adc_Handle, 1);
 
    if ((HAL_ADC_GetState(p_adc_Handle) & HAL_ADC_STATE_EOC_REG) == HAL_ADC_STATE_EOC_REG)
        adcVal = adc_GetVoltage(HAL_ADC_GetValue(p_adc_Handle));
 
    sConfig.Channel = ADC_CHANNEL_7;
    sConfig.Rank = 2;
    HAL_ADC_ConfigChannel(p_adc_Handle, &sConfig);
    HAL_ADC_Start(p_adc_Handle);
    HAL_ADC_PollForConversion(p_adc_Handle, 1);
 
    if ((HAL_ADC_GetState(p_adc_Handle) & HAL_ADC_STATE_EOC_REG) == HAL_ADC_STATE_EOC_REG)
        adcVal = adc_GetVoltage(HAL_ADC_GetValue(p_adc_Handle));
 
    // DODAC pomiar obu kanalow
 
    return;
}

The problem is that after I change to channel 7 from channel 8, I still measure on channel 8. I know that because I measured the 2 voltages with multimeter.
What am I missing here? I would apreciate all help!

Outcomes