cancel
Showing results for 
Search instead for 
Did you mean: 

Simple ADC STM32L475

DSabo
Associate III

I'm trying to get the ADC set up on the IoT discovery board. I did this successfully on the F411 so I thought it would be easy to move the code over, but I can't get it working.

I'm trying to use PC0.

The conversion is always returning 334 or 335, regardless of the pot value I have connected, so I'm sure it's just garbage.

Anything wrong with my configuration?

GPIO_InitTypeDef gpioInit;
	    ADC_HandleTypeDef g_AdcHandle;
	    __GPIOC_CLK_ENABLE();
	    __HAL_RCC_ADC_CLK_ENABLE();
 
	    gpioInit.Pin = GPIO_PIN_0;
	    gpioInit.Mode = GPIO_MODE_ANALOG;
	    gpioInit.Pull = GPIO_NOPULL;
	    HAL_GPIO_Init(GPIOC, &gpioInit);
 
	    ADC_ChannelConfTypeDef adcChannel;
 
	    g_AdcHandle.Instance = ADC1;
 
	    g_AdcHandle.Init.ClockPrescaler = ADC_CLOCKPRESCALER_PCLK_DIV2;
	    g_AdcHandle.Init.Resolution = ADC_RESOLUTION_12B;
	    g_AdcHandle.Init.ScanConvMode = DISABLE;
	    g_AdcHandle.Init.ContinuousConvMode = ENABLE;
	    g_AdcHandle.Init.DiscontinuousConvMode = DISABLE;
	    g_AdcHandle.Init.NbrOfDiscConversion = 0;
	    g_AdcHandle.Init.ExternalTrigConvEdge = ADC_EXTERNALTRIGCONVEDGE_NONE;
	    g_AdcHandle.Init.ExternalTrigConv = ADC_SOFTWARE_START;
	    g_AdcHandle.Init.DataAlign = ADC_DATAALIGN_RIGHT;
	    g_AdcHandle.Init.NbrOfConversion = 1;
	    g_AdcHandle.Init.DMAContinuousRequests = DISABLE;
	    g_AdcHandle.Init.EOCSelection = DISABLE;
 
	    HAL_ADC_Init(&g_AdcHandle);
 
	    adcChannel.Channel = ADC_CHANNEL_1;
	    adcChannel.Rank = 1;
	    adcChannel.SamplingTime = ADC_SAMPLETIME_247CYCLES_5;
	    adcChannel.Offset = 0;
 
	    if (HAL_ADC_ConfigChannel(&g_AdcHandle, &adcChannel) != HAL_OK)
	    {
	    	AZURE_PRINTF("ADC Error\r\n");
	    	while(1){};
	    }
	    volatile uint32_t g_ADCValue;
	    volatile int g_MeasurementNumber;
	    HAL_ADC_Start(&g_AdcHandle);
	        for (;;)
	        {
	            if (HAL_ADC_PollForConversion(&g_AdcHandle, 1000000) == HAL_OK)
	            {
	                g_ADCValue = HAL_ADC_GetValue(&g_AdcHandle);
	                AZURE_PRINTF("%u\r\n", g_ADCValue);
	                g_MeasurementNumber++;
	            }
	        }

2 REPLIES 2

Did you fill in all fields of the initialization structs? Better zero them explicitly.

If in doubts, read out and check the ADC registers content.

JW

All the registers have the value I assigned... I think I have a configuration incorrect or missing