cancel
Showing results for 
Search instead for 
Did you mean: 

Why my ADC polling project created by me not working but when I put my code in ST ADC example it can work?

LHuan.4
Associate II

Hi I created a very simple ADC polling project using Nucleo-U575ZI-Q board. The settings of CubeMX is attached below. The code is very simple, just start the ADC1 and wait for polling then read the result from ADC1_IN8. The ADC polling code in my own project is not working. But when putting the same code in the ST ADC example project, it can work. I assume some settings must be missed but I checked all the settings and functions used and they are the same for my project and example project. Can anyone figure out which key steps are missed? Thanks.

0693W00000YALAIQA5.jpg0693W00000YALASQA5.jpg 

6 REPLIES 6
victagayun
Senior III

Check whether you may need some extra codes...

  /* USER CODE BEGIN 1 */
 
  /* USER CODE END 1 */
 
  /* MCU Configuration--------------------------------------------------------*/
 
  /* Reset of all peripherals, Initializes the Flash interface and the Systick. */
  HAL_Init();
 
  /* USER CODE BEGIN Init */
 
  /* USER CODE END Init */
 
  /* Configure the system clock */
  SystemClock_Config();
 
  /* Configure the System Power */
  SystemPower_Config();
 
  /* USER CODE BEGIN SysInit */
 
  /* USER CODE END SysInit */
 
  /* Initialize all configured peripherals */
  MX_GPIO_Init();
  MX_ICACHE_Init(); // check if needed
  MX_ADC1_Init();
  /* USER CODE BEGIN 2 */
 
  /* Initialize LED on board */
  BSP_LED_Init(LED1);
 
  /* Perform ADC calibration */
  if (HAL_ADCEx_Calibration_Start(&hadc1, ADC_CALIB_OFFSET, ADC_SINGLE_ENDED) != HAL_OK)
  {
    /* Calibration Error */
    Error_Handler();
  }

I don't think it needs extra code, as the code I run in my main.c is the same as the one in ST example project. When I debug I found my code is stuck in  

HAL_ADC_PollForConversion(&hadc1, HAL_MAX_DELAY);

So I guess there must be some settings wrong/missed in my project. But I cannot figure it out as I followed the exact steps for ADC polling project.

victagayun
Senior III

Is this not needed?

  if (HAL_ADCEx_Calibration_Start(&hadc1, ADC_CALIB_OFFSET, ADC_SINGLE_ENDED) != HAL_OK)

Yes, not needed. This is for accuracy. But for simple use of ADC, this is not necessary.

This is the only difference between all the examples and your code.

Maybe you try to add it.

All the examples have this code except yours.

Some examples are inside the MX_ADC_Init() ending user code section.

  /* USER CODE BEGIN ADC4_Init 2 */
  if (HAL_ADCEx_Calibration_Start(&hadc4, ADC_CALIB_OFFSET, ADC_SINGLE_ENDED) != HAL_OK)
  {
    Error_Handler();
  }
  /* USER CODE END ADC4_Init 2 */

Yes, I know. But when I delete these lines of code the ADC can still work. So I assume the issue is not there.