cancel
Showing results for 
Search instead for 
Did you mean: 

How to set up the ADC channel correctly? When I set up the channel as ADC_CHANNEL_5 I get the readings from the pin ADC_IN0(PA0). Changing Channels switches reading between CH0 and CH1. I am using STM32G030K8T on a custom board and FW_g0_v1.2.0

ABarn.1
Associate

__HAL_RCC_GPIOA_CLK_ENABLE();

GPIO_InitTypeDef agpio;

agpio.Mode = GPIO_MODE_ANALOG;

agpio.Pull = GPIO_NOPULL;

agpio.Pin = GPIO_PIN_5;

HAL_GPIO_Init(GPIOA, &agpio);

__HAL_RCC_ADC_CLK_ENABLE();

RCC_PeriphCLKInitTypeDef adc_clk;

adc_clk.PeriphClockSelection = RCC_PERIPHCLK_ADC;

adc_clk.AdcClockSelection = RCC_ADCCLKSOURCE_SYSCLK;

HAL_RCCEx_PeriphCLKConfig(&adc_clk);

ADC_HandleTypeDef adc;

adc.Init.ClockPrescaler    = ADC_CLOCK_SYNC_PCLK_DIV4;

adc.Init.Resolution      = ADC_RESOLUTION_12B;

adc.Init.DataAlign       = ADC_DATAALIGN_RIGHT;

adc.Init.ScanConvMode     = ADC_SCAN_DISABLE;

adc.Init.EOCSelection     = ADC_EOC_SINGLE_CONV;

adc.Init.LowPowerAutoWait   = DISABLE;

adc.Init.LowPowerAutoPowerOff = DISABLE;

adc.Init.ContinuousConvMode  = ENABLE;

adc.Init.NbrOfConversion    = 1;

adc.Init.DiscontinuousConvMode = DISABLE;

adc.Init.ExternalTrigConv   = ADC_SOFTWARE_START;

adc.Init.ExternalTrigConvEdge = ADC_EXTERNALTRIGCONVEDGE_NONE;

adc.Init.DMAContinuousRequests = DISABLE;

adc.Init.Overrun        = ADC_OVR_DATA_PRESERVED;

adc.Init.SamplingTimeCommon1  = ADC_SAMPLETIME_1CYCLE_5;

adc.Init.SamplingTimeCommon2  = ADC_SAMPLETIME_1CYCLE_5;

adc.Init.OversamplingMode   = DISABLE;

adc.Init.TriggerFrequencyMode = ADC_TRIGGER_FREQ_HIGH;

HAL_ADC_Init(&adc);

HAL_ADCEx_Calibration_Start(&adc);

ADC_ChannelConfTypeDef adc_ch;

adc_ch.Channel = ADC_CHANNEL_5; //4 -- 1 || 5 -- 0 || 3 -- 0 || 2 -- 1 ||

adc_ch.Rank = ADC_REGULAR_RANK_1;

adc_ch.SamplingTime = ADC_SAMPLINGTIME_COMMON_1;

HAL_StatusTypeDef res = HAL_ADC_ConfigChannel(&adc, &adc_ch);

res = HAL_ADC_Start(&adc);

HAL_ADC_PollForConversion(&adc, 10);

uint32_t value = HAL_ADC_GetValue(&adc);

1 ACCEPTED SOLUTION

Accepted Solutions
ABarn.1
Associate

Hello,

I found a solution.

ADC_HandleTypeDef hadc1 = {0};

When I initialize the handler with zero values all seems to work fine.

View solution in original post

1 REPLY 1
ABarn.1
Associate

Hello,

I found a solution.

ADC_HandleTypeDef hadc1 = {0};

When I initialize the handler with zero values all seems to work fine.