2021-01-05 01:28 AM
__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);
Solved! Go to Solution.
2021-01-05 07:39 AM
Hello,
I found a solution.
ADC_HandleTypeDef hadc1 = {0};
When I initialize the handler with zero values all seems to work fine.
2021-01-05 07:39 AM
Hello,
I found a solution.
ADC_HandleTypeDef hadc1 = {0};
When I initialize the handler with zero values all seems to work fine.