Skip to main content
Visitor II
September 10, 2025
Solved

STM32u5 ADC not reading 4 channels properly using polling mode/ADC scan mode)

  • September 10, 2025
  • 2 replies
  • 851 views

Hello,

I am working on simple program to read ADC using the STM32u585QII microcontroller.

I am using polling method in Scan mode on 4 channels. All set as 14 bit but understood the readin will be in 12 bit.

The problem is the values I am getting are not reflecting the real value at the input. I am receiving something like 

1586 cnts on 1st and 3rd channel and 2395 cnts on 2nd and 4th channel. Values make no sense assuming I have stable (verified with oscilloscope) voltage 1.61V. Vref is 2.5V external.

Also when I am trying change voltage at the inputs the reading doesn't change.

Can someone point me the possible issue with the configuration.

/* USER CODE BEGIN Header */
/**
 ******************************************************************************
 * @file : main.c
 * @brief : Main program body
 ******************************************************************************
 * @attention
 *
 * Copyright (c) 2025 STMicroelectronics.
 * All rights reserved.
 *
 * This software is licensed under terms that can be found in the LICENSE file
 * in the root directory of this software component.
 * If no LICENSE file comes with this software, it is provided AS-IS.
 *
 ******************************************************************************
 */
/* USER CODE END Header */
/* Includes ------------------------------------------------------------------*/
#include "main.h"

/* Private includes ----------------------------------------------------------*/
/* USER CODE BEGIN Includes */

/* USER CODE END Includes */

/* Private typedef -----------------------------------------------------------*/
/* USER CODE BEGIN PTD */

/* USER CODE END PTD */

/* Private define ------------------------------------------------------------*/
/* USER CODE BEGIN PD */

/* USER CODE END PD */

/* Private macro -------------------------------------------------------------*/
/* USER CODE BEGIN PM */

/* USER CODE END PM */

/* Private variables ---------------------------------------------------------*/
ADC_HandleTypeDef hadc1;

DCACHE_HandleTypeDef hdcache1;

/* USER CODE BEGIN PV */

/* USER CODE END PV */

/* Private function prototypes -----------------------------------------------*/
void SystemClock_Config(void);
static void MX_GPIO_Init(void);
static void MX_ADC1_Init(void);
static void MX_DCACHE1_Init(void);
static void MX_ICACHE_Init(void);
/* USER CODE BEGIN PFP */

/* USER CODE END PFP */

/* Private user code ---------------------------------------------------------*/
/* USER CODE BEGIN 0 */
	int isADCfinished = 1;
	uint32_t ADC_Data[4];
	uint32_t Singl_Read;
	uint32_t timestamp;
	int i = 0;

	int _write(int32_t file, uint8_t *ptr, int32_t len)
	{
	 for (int i = 0; i < len; i++)
	 {
	 ITM_SendChar(*ptr++);
	 }
	 return len;
	}


/* USER CODE END 0 */

/**
 * @brief The application entry point.
 * @retval int
 */
int main(void)
{

 /* 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();

 /* USER CODE BEGIN SysInit */

 /* USER CODE END SysInit */

 /* Initialize all configured peripherals */
 MX_GPIO_Init();
 MX_ADC1_Init();
 MX_DCACHE1_Init();
 MX_ICACHE_Init();
 /* USER CODE BEGIN 2 */
// HAL_ADC_Start(&hadc1)

 HAL_PWREx_EnableVddA();
 HAL_PWREx_EnableVddIO2();
 HAL_PWREx_EnablePullUpPullDownConfig();
 HAL_ADCEx_Calibration_Start(&hadc1, ADC_CALIB_OFFSET, ADC_SINGLE_ENDED);
 printf("Hello World!!\n");
 /* USER CODE END 2 */

 /* Infinite loop */
 /* USER CODE BEGIN WHILE */
 while (1)
 {
	 for(i=0; i<4; i++)
	 	 {
	 		 HAL_ADC_Start(&hadc1);
	 		 HAL_Delay(10);
	 		 HAL_ADC_PollForConversion(&hadc1, 1);
	 //		 HAL_Delay(10);
	 		 Singl_Read = HAL_ADC_GetValue(&hadc1);
	 		 switch(i)
	 		 {
	 		 	 case 0:
	 		 		ADC_Data[0] = Singl_Read;
	 		 		 break;
	 		 	 case 1:
	 		 		ADC_Data[1] = Singl_Read;
	 		 		 break;
	 		 	 case 2:
	 		 		ADC_Data[2] = Singl_Read;
	 		 		 break;
	 		 	 case 3:
	 		 		ADC_Data[3] = Singl_Read;
	 		 		 break;

	 		 }
	 	 }

	 HAL_Delay(5);
	 	 printf("SIN_A = %lu COS_A = %lu SIN_B = %lu COS_B = %lu\n", ADC_Data[0],ADC_Data[1],ADC_Data[2],ADC_Data[3]);
	 	 for(i=0; i<4; i++)
	 	 {
	 		 ADC_Data[i]=0;
	 	 }
	 	 HAL_Delay(5);
 /* USER CODE END WHILE */

 /* USER CODE BEGIN 3 */
 }
 /* USER CODE END 3 */
}

/**
 * @brief System Clock Configuration
 * @retval None
 */
void SystemClock_Config(void)
{
 RCC_OscInitTypeDef RCC_OscInitStruct = {0};
 RCC_ClkInitTypeDef RCC_ClkInitStruct = {0};

 /** Configure the main internal regulator output voltage
 */
 if (HAL_PWREx_ControlVoltageScaling(PWR_REGULATOR_VOLTAGE_SCALE1) != HAL_OK)
 {
 Error_Handler();
 }

 /** Initializes the CPU, AHB and APB buses clocks
 */
 RCC_OscInitStruct.OscillatorType = RCC_OSCILLATORTYPE_HSI|RCC_OSCILLATORTYPE_HSE;
 RCC_OscInitStruct.HSEState = RCC_HSE_ON;
 RCC_OscInitStruct.HSIState = RCC_HSI_ON;
 RCC_OscInitStruct.HSICalibrationValue = RCC_HSICALIBRATION_DEFAULT;
 RCC_OscInitStruct.PLL.PLLState = RCC_PLL_ON;
 RCC_OscInitStruct.PLL.PLLSource = RCC_PLLSOURCE_HSE;
 RCC_OscInitStruct.PLL.PLLMBOOST = RCC_PLLMBOOST_DIV1;
 RCC_OscInitStruct.PLL.PLLM = 1;
 RCC_OscInitStruct.PLL.PLLN = 20;
 RCC_OscInitStruct.PLL.PLLP = 2;
 RCC_OscInitStruct.PLL.PLLQ = 2;
 RCC_OscInitStruct.PLL.PLLR = 1;
 RCC_OscInitStruct.PLL.PLLRGE = RCC_PLLVCIRANGE_1;
 RCC_OscInitStruct.PLL.PLLFRACN = 0;
 if (HAL_RCC_OscConfig(&RCC_OscInitStruct) != HAL_OK)
 {
 Error_Handler();
 }

 /** Initializes the CPU, AHB and APB buses clocks
 */
 RCC_ClkInitStruct.ClockType = RCC_CLOCKTYPE_HCLK|RCC_CLOCKTYPE_SYSCLK
 |RCC_CLOCKTYPE_PCLK1|RCC_CLOCKTYPE_PCLK2
 |RCC_CLOCKTYPE_PCLK3;
 RCC_ClkInitStruct.SYSCLKSource = RCC_SYSCLKSOURCE_PLLCLK;
 RCC_ClkInitStruct.AHBCLKDivider = RCC_SYSCLK_DIV1;
 RCC_ClkInitStruct.APB1CLKDivider = RCC_HCLK_DIV2;
 RCC_ClkInitStruct.APB2CLKDivider = RCC_HCLK_DIV1;
 RCC_ClkInitStruct.APB3CLKDivider = RCC_HCLK_DIV1;

 if (HAL_RCC_ClockConfig(&RCC_ClkInitStruct, FLASH_LATENCY_4) != HAL_OK)
 {
 Error_Handler();
 }
}

/**
 * @brief ADC1 Initialization Function
 * None
 * @retval None
 */
static void MX_ADC1_Init(void)
{

 /* USER CODE BEGIN ADC1_Init 0 */

 /* USER CODE END ADC1_Init 0 */

 ADC_ChannelConfTypeDef sConfig = {0};

 /* USER CODE BEGIN ADC1_Init 1 */

 /* USER CODE END ADC1_Init 1 */

 /** Common config
 */
 hadc1.Instance = ADC1;
 hadc1.Init.ClockPrescaler = ADC_CLOCK_ASYNC_DIV1;
 hadc1.Init.Resolution = ADC_RESOLUTION_14B;
 hadc1.Init.GainCompensation = 0;
 hadc1.Init.ScanConvMode = ADC_SCAN_ENABLE;
 hadc1.Init.EOCSelection = ADC_EOC_SINGLE_CONV;
 hadc1.Init.LowPowerAutoWait = DISABLE;
 hadc1.Init.ContinuousConvMode = DISABLE;
 hadc1.Init.NbrOfConversion = 4;
 hadc1.Init.DiscontinuousConvMode = DISABLE;
 hadc1.Init.ExternalTrigConv = ADC_SOFTWARE_START;
 hadc1.Init.ExternalTrigConvEdge = ADC_EXTERNALTRIGCONVEDGE_NONE;
 hadc1.Init.DMAContinuousRequests = DISABLE;
 hadc1.Init.TriggerFrequencyMode = ADC_TRIGGER_FREQ_HIGH;
 hadc1.Init.Overrun = ADC_OVR_DATA_PRESERVED;
 hadc1.Init.LeftBitShift = ADC_LEFTBITSHIFT_NONE;
 hadc1.Init.ConversionDataManagement = ADC_CONVERSIONDATA_DR;
 hadc1.Init.OversamplingMode = DISABLE;
 if (HAL_ADC_Init(&hadc1) != HAL_OK)
 {
 Error_Handler();
 }

 /** Configure Regular Channel
 */
 sConfig.Channel = ADC_CHANNEL_7;
 sConfig.Rank = ADC_REGULAR_RANK_1;
 sConfig.SamplingTime = ADC_SAMPLETIME_5CYCLE;
 sConfig.SingleDiff = ADC_SINGLE_ENDED;
 sConfig.OffsetNumber = ADC_OFFSET_NONE;
 sConfig.Offset = 0;
 if (HAL_ADC_ConfigChannel(&hadc1, &sConfig) != HAL_OK)
 {
 Error_Handler();
 }

 /** Configure Regular Channel
 */
 sConfig.Channel = ADC_CHANNEL_8;
 sConfig.Rank = ADC_REGULAR_RANK_2;
 if (HAL_ADC_ConfigChannel(&hadc1, &sConfig) != HAL_OK)
 {
 Error_Handler();
 }

 /** Configure Regular Channel
 */
 sConfig.Channel = ADC_CHANNEL_10;
 sConfig.Rank = ADC_REGULAR_RANK_3;
 if (HAL_ADC_ConfigChannel(&hadc1, &sConfig) != HAL_OK)
 {
 Error_Handler();
 }

 /** Configure Regular Channel
 */
 sConfig.Channel = ADC_CHANNEL_11;
 sConfig.Rank = ADC_REGULAR_RANK_4;
 if (HAL_ADC_ConfigChannel(&hadc1, &sConfig) != HAL_OK)
 {
 Error_Handler();
 }
 /* USER CODE BEGIN ADC1_Init 2 */

 /* USER CODE END ADC1_Init 2 */

}

/**
 * @brief DCACHE1 Initialization Function
 * None
 * @retval None
 */
static void MX_DCACHE1_Init(void)
{

 /* USER CODE BEGIN DCACHE1_Init 0 */

 /* USER CODE END DCACHE1_Init 0 */

 /* USER CODE BEGIN DCACHE1_Init 1 */

 /* USER CODE END DCACHE1_Init 1 */
 hdcache1.Instance = DCACHE1;
 hdcache1.Init.ReadBurstType = DCACHE_READ_BURST_WRAP;
 if (HAL_DCACHE_Init(&hdcache1) != HAL_OK)
 {
 Error_Handler();
 }
 /* USER CODE BEGIN DCACHE1_Init 2 */

 /* USER CODE END DCACHE1_Init 2 */

}

/**
 * @brief ICACHE Initialization Function
 * None
 * @retval None
 */
static void MX_ICACHE_Init(void)
{

 /* USER CODE BEGIN ICACHE_Init 0 */

 /* USER CODE END ICACHE_Init 0 */

 /* USER CODE BEGIN ICACHE_Init 1 */

 /* USER CODE END ICACHE_Init 1 */
 /* USER CODE BEGIN ICACHE_Init 2 */

 /* USER CODE END ICACHE_Init 2 */

}

/**
 * @brief GPIO Initialization Function
 * None
 * @retval None
 */
static void MX_GPIO_Init(void)
{
/* USER CODE BEGIN MX_GPIO_Init_1 */
/* USER CODE END MX_GPIO_Init_1 */

 /* GPIO Ports Clock Enable */
 __HAL_RCC_GPIOH_CLK_ENABLE();
 __HAL_RCC_GPIOA_CLK_ENABLE();

/* USER CODE BEGIN MX_GPIO_Init_2 */
/* USER CODE END MX_GPIO_Init_2 */
}

/* USER CODE BEGIN 4 */

/* USER CODE END 4 */

/**
 * @brief This function is executed in case of error occurrence.
 * @retval None
 */
void Error_Handler(void)
{
 /* USER CODE BEGIN Error_Handler_Debug */
 /* User can add his own implementation to report the HAL error return state */
 __disable_irq();
 while (1)
 {
 }
 /* USER CODE END Error_Handler_Debug */
}

#ifdef USE_FULL_ASSERT
/**
 * @brief Reports the name of the source file and the source line number
 * where the assert_param error has occurred.
 * file: pointer to the source file name
 * line: assert_param error line source number
 * @retval None
 */
void assert_failed(uint8_t *file, uint32_t line)
{
 /* USER CODE BEGIN 6 */
 /* User can add his own implementation to report the file name and line number,
 ex: printf("Wrong parameters value: file %s on line %d\r\n", file, line) */
 /* USER CODE END 6 */
}
#endif /* USE_FULL_ASSERT */

 

    This topic has been closed for replies.
    Best answer by mƎALLEm

    Hello,

    You need to use DMA for that. You can't read sequentially the data from the ADC like that:

    	 for(i=0; i<4; i++)
    	 	 {
    	 		 HAL_ADC_Start(&hadc1);
    	 		 HAL_Delay(10);
    	 		 HAL_ADC_PollForConversion(&hadc1, 1);
    	 //		 HAL_Delay(10);
    	 		 Singl_Read = HAL_ADC_GetValue(&hadc1);
    	 		 switch(i)
    	 		 {
    	 		 	 case 0:
    	 		 		ADC_Data[0] = Singl_Read;
    	 		 		 break;
    	 		 	 case 1:
    	 		 		ADC_Data[1] = Singl_Read;
    	 		 		 break;
    	 		 	 case 2:
    	 		 		ADC_Data[2] = Singl_Read;
    	 		 		 break;
    	 		 	 case 3:
    	 		 		ADC_Data[3] = Singl_Read;
    	 		 		 break;
    
    	 		 }
    	 	 }

    There is only one register to read the ADC value from and you are reading 4 channels that are stored in the same ADC register and you don't have a good synchronization (moreover using HAL_Delay()) in your implementation. This will completely introduce errors in the reading. Using DMA will manage that sequence automatically and it will fill your ADC_Data[] table with the correct values in their respective order.

    2 replies

    Super User
    September 10, 2025

    Welcome to the forum.

    Please see How to write your question to maximize your chances to find a solution for best results.

    In particular, please give full details of your hardware setup.

     


    @aristot wrote:

    assuming I have stable (verified with oscilloscope) voltage 1.61V. 


    Sure, we can assume that - but have you verified that it is actually true?

    Have you measured directly at the MCU pins?

     

    aristotAuthor
    Visitor II
    September 10, 2025

    Sorry for being not clear.

    Yes I veriffied it using the oscilloscope directly on MCU pins.

    I also verify that VREF is stable.

    Super User
    September 10, 2025

    You still haven't given any details of your hardware setup:

    • schematics, etc;
    • details of whatever is sourcing the signals to the ADCs
    • some good, clear photos may help...

     

    Super User
    September 10, 2025

    Perhaps start with converting a single channel and verifying it is working correctly before you move to multiple channels. Multiple channels will require the use of DMA.