cancel
Showing results for 
Search instead for 
Did you mean: 

Can't change ADC channel on STL32L011F4P6, it always measure from channel 0

dvhx
Associate

I want to measure ADC on multiple pins. I don't need DMA, just measure PA0 then measure PA2. Measuring PA0 works, but when I switch the channel to 2 it still measures from PA0:

In my main loop I do this:

  // Configure channel
  ADC_ChannelConfTypeDef sConfig = {0};
  sConfig.Channel = ADC_CHANNEL_2;
  sConfig.Rank = ADC_RANK_CHANNEL_NUMBER;
  if (HAL_ADC_ConfigChannel(&hadc, &sConfig) != HAL_OK) {
    return 0;
  }
  // Read one ADC value
  HAL_ADC_Start(&hadc);
  HAL_ADC_PollForConversion(&hadc, HAL_MAX_DELAY);
  uint32_t adcValue = HAL_ADC_GetValue(&hadc);
  HAL_ADC_Stop(&hadc);

For rank I tried 0, 1, 2, ADC_RANK_CHANNEL_NUMBER. Still only measures PA0 not PA2.

Here is my MX_ADC_Init

 
static void MX_ADC_Init(void)
{
  ADC_ChannelConfTypeDef sConfig = {0};
  hadc.Instance = ADC1;
  hadc.Init.OversamplingMode = DISABLE;
  hadc.Init.ClockPrescaler = ADC_CLOCK_SYNC_PCLK_DIV1;
  hadc.Init.Resolution = ADC_RESOLUTION_12B;
  hadc.Init.SamplingTime = ADC_SAMPLETIME_160CYCLES_5;
  hadc.Init.ScanConvMode = ADC_SCAN_DIRECTION_FORWARD;
  hadc.Init.DataAlign = ADC_DATAALIGN_RIGHT;
  hadc.Init.ContinuousConvMode = DISABLE;
  hadc.Init.DiscontinuousConvMode = DISABLE;
  hadc.Init.ExternalTrigConvEdge = ADC_EXTERNALTRIGCONVEDGE_NONE;
  hadc.Init.ExternalTrigConv = ADC_SOFTWARE_START;
  hadc.Init.DMAContinuousRequests = DISABLE;
  hadc.Init.EOCSelection = ADC_EOC_SINGLE_CONV;
  hadc.Init.Overrun = ADC_OVR_DATA_PRESERVED;
  hadc.Init.LowPowerAutoWait = DISABLE;
  hadc.Init.LowPowerFrequencyMode = DISABLE;
  hadc.Init.LowPowerAutoPowerOff = DISABLE;
  if (HAL_ADC_Init(&hadc) != HAL_OK)
  {
    Error_Handler();
  }
  sConfig.Channel = ADC_CHANNEL_0;
  sConfig.Rank = ADC_RANK_CHANNEL_NUMBER;
  if (HAL_ADC_ConfigChannel(&hadc, &sConfig) != HAL_OK)
  {
    Error_Handler();
  }
  sConfig.Channel = ADC_CHANNEL_1;
  if (HAL_ADC_ConfigChannel(&hadc, &sConfig) != HAL_OK)
  {
    Error_Handler();
  }
  sConfig.Channel = ADC_CHANNEL_2;
  if (HAL_ADC_ConfigChannel(&hadc, &sConfig) != HAL_OK)
  {
    Error_Handler();
  }
  ...

 

I searched forum and found similar post: https://community.st.com/t5/stm32-mcus-wireless/adc-cannot-change-channel/m-p/149087

and the solution was to change "Number of Conversions" but I don't have "Number of Conversions" in CubeIDE ADC settings for STM32L011F4P6.

 

1 ACCEPTED SOLUTION

Accepted Solutions
TDK
Super User

Without DMA, you can only convert one channel at a time. When you reconfigure for channel 2, you need to disable channel 0.

Easiest way would be to modify CHSELR directly to have a 1 in the bit you want to convert and 0 everywhere else.

TDK_0-1748896594394.png

 

If you feel a post has answered your question, please click "Accept as Solution".

View solution in original post

2 REPLIES 2
TDK
Super User

Without DMA, you can only convert one channel at a time. When you reconfigure for channel 2, you need to disable channel 0.

Easiest way would be to modify CHSELR directly to have a 1 in the bit you want to convert and 0 everywhere else.

TDK_0-1748896594394.png

 

If you feel a post has answered your question, please click "Accept as Solution".

I only need to measure 1 channel at a time, so this is what I used and it works now:

CLEAR_BIT(ADC1->CR, ADC_CR_ADEN);
ADC1->CHSELR = ADC_CHSELR_CHSEL2;
SET_BIT(ADC1->CR, ADC_CR_ADEN);