AnsweredAssumed Answered

Switching/selecting ADC source

Question asked by snyder.joe on Apr 26, 2015
Latest reply on Apr 27, 2015 by snyder.joe
I am using Cube generated code, exported to EWARM, running on a Nucleo L053R8 board.  My question is how to select the active ADC source outside of the ADC initialization code.  For instance, with channel 12 and 13 selected in Cube, the ADC initialization is:

/* ADC init function */
void MX_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.OversamplingMode = DISABLE;
  hadc.Init.ClockPrescaler = ADC_CLOCKPRESCALER_PCLK_DIV1;
  hadc.Init.Resolution = ADC_RESOLUTION12b;
  hadc.Init.SamplingTime = ADC_SAMPLETIME_1CYCLE_5;
  hadc.Init.ScanDirection = ADC_SCAN_DIRECTION_UPWARD;
  hadc.Init.DataAlign = ADC_DATAALIGN_RIGHT;
  hadc.Init.ContinuousConvMode = DISABLE;
  hadc.Init.DiscontinuousConvMode = DISABLE;
  hadc.Init.ExternalTrigConvEdge = ADC_EXTERNALTRIG_EDGE_NONE;
  hadc.Init.DMAContinuousRequests = DISABLE;
  hadc.Init.EOCSelection = EOC_SINGLE_CONV;
  hadc.Init.Overrun = OVR_DATA_PRESERVED;
  hadc.Init.LowPowerAutoWait = DISABLE;
  hadc.Init.LowPowerFrequencyMode = DISABLE;
  hadc.Init.LowPowerAutoOff = DISABLE;
  HAL_ADC_Init(&hadc);
 
    /**Configure for the selected ADC regular channel to be converted.
    */
  sConfig.Channel = ADC_CHANNEL_12;
  HAL_ADC_ConfigChannel(&hadc, &sConfig);
 
    /**Configure for the selected ADC regular channel to be converted.
    */
  sConfig.Channel = ADC_CHANNEL_13;
  HAL_ADC_ConfigChannel(&hadc, &sConfig);
 
}
 
I have my ADC temp variable defined as:
__IO uint32_t ADCVal = 0;

I'm trying to switch between channels 12 and 13 by doing the following:

while(1)
 {
    ADC_ChannelConfTypeDef ADCChan;
 
    ADCChan.Channel = ADC_CHANNEL_12;
    HAL_ADC_ConfigChannel(&hadc, &ADCChan);
    HAL_Delay(1);   //give it time to settle
    HAL_ADC_Start(&hadc);
    HAL_ADC_PollForConversion(&hadc, 10);    //poll for conversion
   if(HAL_ADC_GetState(&hadc) == HAL_ADC_STATE_EOC)
    {
      //Get the converted value of regular channel  
      ADCVal = HAL_ADC_GetValue(&hadc);
    }
 
  ....output ADCVal (channel 12) to LCD and pause for a second....
    
    ADCChan.Channel = ADC_CHANNEL_13;
    HAL_ADC_ConfigChannel(&hadc, &ADCChan);
    HAL_Delay(1);   //give it time to settle
    HAL_ADC_Start(&hadc);
    HAL_ADC_PollForConversion(&hadc, 10);    //poll for conversion
   if(HAL_ADC_GetState(&hadc) == HAL_ADC_STATE_EOC)
    {
      //Get the converted value of regular channel  
      ADCVal = HAL_ADC_GetValue(&hadc);
    }
 
...output ADCVal (channel 13) to LCD and pause for a second....
 }
 

I have a potentiometer hooked to channel 13 (PC3) and channel 12 (PC2) is grounded. The LCD is outputting fine, but it is only showing the value of channel 13.  i.e. with 3.3V on channel 13 and 0V on channel 12, both are reading 4095. 

Any ideas what I am doing wrong trying to switch between the two?  Thanks for your time.  

Outcomes