Skip to main content
Associate III
November 21, 2023
Question

ADC multichannel DMA

  • November 21, 2023
  • 9 replies
  • 6891 views

with all your help I tried to change multichannel using DMA but I cant able to read the values can anyone please help

 

ADC_HandleTypeDef hadc1;
DMA_HandleTypeDef hdma_adc1;

UART_HandleTypeDef huart2;

/* USER CODE BEGIN PV */
uint16_t mains=0;
uint16_t battV=0;
uint16_t tsense=0;
uint16_t battI=0;
uint16_t ledI=0;
uint16_t opv=0;
uint16_t batMon=0;
uint32_t getADCvalues[7];
char msg[100];
***********

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

/* USER CODE END PFP */

/* Private user code ---------------------------------------------------------*/
/* USER CODE BEGIN 0 */
uint8_t convCompleted=0;
void HAL_ADC_ConvCpltCallback(ADC_HandleTypeDef *hadc)
{
	convCompleted=1;
}




/* 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_DMA_Init();
 MX_ADC1_Init();
 MX_USART2_UART_Init();
 /* USER CODE BEGIN 2 */
 HAL_ADC_Start_DMA(&hadc1, (uint32_t *)getADCvalues, 7);
 /* USER CODE END 2 */

 /* Infinite loop */
 /* USER CODE BEGIN WHILE */
 while (1)
 {
 /* USER CODE END WHILE */

 /* USER CODE BEGIN 3 */
	 while(!convCompleted);
	 for(uint8_t i =0;i< hadc1.Init.NbrOfConversion;i++)
	 {
		 tsense=getADCvalues[0];
		 ledI=getADCvalues[1];
		 battI=getADCvalues[2];
		 mains=getADCvalues[3];
		 opv=getADCvalues[4];
		 batMon=getADCvalues[5];
		 battV=getADCvalues[6];

	 }
static void MX_USART2_UART_Init(void)
{

 /* USER CODE BEGIN USART2_Init 0 */

 /* USER CODE END USART2_Init 0 */

 /* USER CODE BEGIN USART2_Init 1 */

 /* USER CODE END USART2_Init 1 */
 huart2.Instance = USART2;
 huart2.Init.BaudRate = 115200;
 huart2.Init.WordLength = UART_WORDLENGTH_8B;
 huart2.Init.StopBits = UART_STOPBITS_1;
 huart2.Init.Parity = UART_PARITY_NONE;
 huart2.Init.Mode = UART_MODE_TX_RX;
 huart2.Init.HwFlowCtl = UART_HWCONTROL_NONE;
 huart2.Init.OverSampling = UART_OVERSAMPLING_16;
 huart2.Init.OneBitSampling = UART_ONE_BIT_SAMPLE_DISABLE;
 huart2.Init.ClockPrescaler = UART_PRESCALER_DIV1;
 huart2.AdvancedInit.AdvFeatureInit = UART_ADVFEATURE_NO_INIT;
 if (HAL_UART_Init(&huart2) != HAL_OK)
 {
 Error_Handler();
 }
 /* USER CODE BEGIN USART2_Init 2 */

 /* USER CODE END USART2_Init 2 */

}

/**
 * Enable DMA controller clock
 */
static void MX_DMA_Init(void)
{

 /* DMA controller clock enable */
 __HAL_RCC_DMA1_CLK_ENABLE();

 /* DMA interrupt init */
 /* DMA1_Channel1_IRQn interrupt configuration */
 HAL_NVIC_SetPriority(DMA1_Channel1_IRQn, 0, 0);
 HAL_NVIC_EnableIRQ(DMA1_Channel1_IRQn);

}

 

how to read these values in debugger with Live expression I couldn't get that 

This topic has been closed for replies.

9 replies

TDK
November 21, 2023

> I cant able to read the values

Why cant you able to read the values? What happens when you try?

ADC values should probably be uint16_t, not uint32_t. They should also be marked volatile, as they're modified outside of the program.

"If you feel a post has answered your question, please click ""Accept as Solution""."
meenaAuthor
Associate III
November 21, 2023

the values are still 0 for all 

 

TDK
November 21, 2023

Debug your program. Hit pause, see where execution is at and examine the state of the variables in a non-live expression context.

"If you feel a post has answered your question, please click ""Accept as Solution""."
Visitor II
November 21, 2023

make sure that your DMA configuration, especially the memory address where ADC values are being stored, is correct. The getADCvalues array should be the correct destination for DMA to store the ADC values.

TDK
November 22, 2023

Your MX_ADC1_Init is set up to initialize the ADC for 6 conversions, but then you're trying to do single conversions. Might be other issues as well.

Perhaps go off of a working example code from the MX repository.

Here's an example for multiple conversions using DMA:

https://github.com/STMicroelectronics/STM32CubeG0/tree/master/Projects/NUCLEO-G070RB/Examples/ADC/ADC_MultiChannelSingleConversion

There are quite a few others available in the Example Selector within CubeMX for the G0 series.

"If you feel a post has answered your question, please click ""Accept as Solution""."
meenaAuthor
Associate III
November 27, 2023

these examples i tried as a result reading same values for all channels