cancel
Showing results for 
Search instead for 
Did you mean: 

[Fixed] Cant get ADC to work

KK.2
Associate III

Trying to read out a resistive touchscreen right now. To keep everything as simple as possible for testing I created an empty project in CubeMX just for testing this and set up all the pins in a static fashion (No reconfiguration at runtime)

Measuring the ADC input with a multimeter I correctly see the voltage depending on where I touch, however no matter what I try, the ADC always either outputs a static number (Usually the max value of my configured resolution) or at random will (E.g. with 10 bit) Output a value like 0, 255, 512 or 1024.

To eliminate the variable of the touchscreen I've also tried to remove the Touchscreen and connect a potentiometer directly which is connected between ground and 3v3 which made no difference.

I tried pretty much all combinations of resolutions, sample times, Interrupt or not, its always the same result. I have also tried to use ADC2 / ADC3 instead of ADC1 with the same result.

My code looks like this:

HAL_ADC_Start(&hadc1);
HAL_ADC_PollForConversion(&hadc1, HAL_MAX_DELAY);
uint16_t xpos = HAL_ADC_GetValue(&hadc1);
HAL_ADC_Stop(&hadc1);

Continuous conversion is enabled, leaving out the ADC Stop makes no difference. I pretty much have no idea any more, if somebody can help me that would be appreciated.

1 ACCEPTED SOLUTION

Accepted Solutions
KK.2
Associate III

Like I've said, Ive tested a lot of things, different values for `HAL_MAX_DELAY` is one of them, regardless in my case it is a hardware issue as vref+ was not connected to vdd / vdda due to poor soldering. Having corrected that it is now working.

View solution in original post

2 REPLIES 2
LMI2
Lead

Several people have posted working ADC code. If you search for instance HAL_ADC_PollForConversion

you'll get a working examples elswhere, too. I wonder why you use HAL_MAX_DELAY, what I remeber 1000 or so is enough. And Where is Getvalue command, I have used that. This may have worked, it is old code, so I'm not 100% sure.

  while (1)
  {
    /* USER CODE END WHILE */
 
    /* USER CODE BEGIN 3 */
		//K15_Pin|K1_Pin|K3_Pin|K16_Pin
		HAL_GPIO_TogglePin(GPIOD,K16_Pin);
		HAL_GPIO_TogglePin(GPIOD,K15_Pin);
		HAL_GPIO_TogglePin(GPIOD,K1_Pin);
		HAL_GPIO_TogglePin(GPIOD,K3_Pin);
 
	while	(HAL_ADC_PollForConversion(&hadc1,0)!=HAL_OK)
	{
luku=HAL_GPIO_ReadPin(GPIOD, GPIO_PIN_4);//
luku=HAL_GPIO_ReadPin(GPIOD, GPIO_PIN_5);//
luku=HAL_GPIO_ReadPin(GPIOD, GPIO_PIN_6);//
luku=HAL_GPIO_ReadPin(GPIOD, GPIO_PIN_7);//		
	}
luku = HAL_ADC_GetValue(&hadc1);
		sprintf(msg ,"Welcome %d",luku);
HAL_UART_Transmit(&huart1, (uint8_t*)msg, strlen(msg), 1000);
		HAL_ADC_Stop(&hadc1);
	HAL_ADC_Start(&hadc1);
		HAL_Delay(5000);
  }

KK.2
Associate III

Like I've said, Ive tested a lot of things, different values for `HAL_MAX_DELAY` is one of them, regardless in my case it is a hardware issue as vref+ was not connected to vdd / vdda due to poor soldering. Having corrected that it is now working.