2025-06-02 1:13 PM
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
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.
Solved! Go to Solution.
2025-06-02 1:36 PM
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.
2025-06-02 1:36 PM
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.
2025-06-02 1:49 PM
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);