cancel
Showing results for 
Search instead for 
Did you mean: 

Problem with ADC and DMA

Jan Nowak_2
Associate II
Posted on July 13, 2018 at 22:03

Hello community! I've got a problem with DMA and ADC (multi channel). I can't find solution why function 

HAL_ADC_Start_DMA returns 0's to the 'adc_value' array. I have connected to Nucleo F466RE board 3 sensors, and i want to measure and get 3 values. Thanks for any prompts. 

#include 'stm32f4xx.h'

#include 'stm32f4xx_nucleo.h'

DMA_HandleTypeDef dma;

UART_HandleTypeDef uart;

uint32_t adc_value[3];

char result[20]='';

int main(void)

{

HAL_Init();

__HAL_RCC_DMA1_CLK_ENABLE();

__GPIOA_CLK_ENABLE();

__USART2_CLK_ENABLE();

__ADC1_CLK_ENABLE();

GPIO_InitTypeDef gpioANALOG;

gpioANALOG.Pin = GPIO_PIN_0|GPIO_PIN_4|GPIO_PIN_1;

gpioANALOG.Mode = GPIO_MODE_ANALOG;

gpioANALOG.Pull = GPIO_NOPULL;

HAL_GPIO_Init(GPIOA, &gpioANALOG);

GPIO_InitTypeDef gpioUART;

gpioUART.Pin = GPIO_PIN_3|GPIO_PIN_2;

gpioUART.Mode = GPIO_MODE_AF_PP;

gpioUART.Alternate = GPIO_AF7_USART2;

gpioUART.Speed = GPIO_SPEED_HIGH;

gpioUART.Pull = GPIO_NOPULL;

HAL_GPIO_Init(GPIOA, &gpioUART);

uart.Instance = USART2;

uart.Init.BaudRate = 9600;

uart.Init.WordLength = UART_WORDLENGTH_8B;

uart.Init.StopBits = UART_STOPBITS_1;

uart.Init.Parity = UART_PARITY_NONE;

uart.Init.Mode = UART_MODE_TX_RX;

uart.Init.HwFlowCtl = UART_HWCONTROL_NONE;

uart.Init.OverSampling = UART_OVERSAMPLING_16;

HAL_UART_Init(&uart);

ADC_HandleTypeDef adc;

adc.Instance = ADC1;

adc.Init.ContinuousConvMode = ENABLE;

adc.Init.ExternalTrigConv = ADC_SOFTWARE_START;

adc.Init.DataAlign = ADC_DATAALIGN_RIGHT;

adc.Init.ScanConvMode = ENABLE;

adc.Init.DiscontinuousConvMode = DISABLE;

adc.Init.NbrOfConversion = 3;

HAL_ADC_Init(&adc);

ADC_ChannelConfTypeDef adcChannel;

adcChannel.Channel = ADC_CHANNEL_0;

adcChannel.Rank = 1;

adcChannel.SamplingTime = ADC_SAMPLETIME_480CYCLES;

HAL_ADC_ConfigChannel(&adc, &adcChannel);

adcChannel.Channel = ADC_CHANNEL_1;

adcChannel.Rank = 2;

adcChannel.SamplingTime = ADC_SAMPLETIME_480CYCLES;

HAL_ADC_ConfigChannel(&adc, &adcChannel);

adcChannel.Channel = ADC_CHANNEL_2;

adcChannel.Rank = 3;

adcChannel.SamplingTime = ADC_SAMPLETIME_480CYCLES;

HAL_ADC_ConfigChannel(&adc, &adcChannel);

dma.Instance = DMA1_Stream1;

dma.Init.Direction = DMA_PERIPH_TO_MEMORY;

dma.Init.PeriphInc = DMA_PINC_ENABLE;

dma.Init.MemInc = DMA_MINC_ENABLE;

dma.Init.PeriphDataAlignment = DMA_PDATAALIGN_HALFWORD;

dma.Init.MemDataAlignment = DMA_MDATAALIGN_HALFWORD;

dma.Init.Mode = DMA_CIRCULAR;

dma.Init.Priority = DMA_PRIORITY_HIGH;

HAL_DMA_Init(&dma);

__HAL_LINKDMA(&adc,DMA_Handle,dma);

HAL_ADC_Start_DMA(&adc,(uint32_t*)adc_value,3);

sprintf(result,'ADC: %lu \n\r',adc_value[0]);

HAL_UART_Transmit(&uart,result,sizeof(result),100);

while(1)

{

}

}
1 REPLY 1

Does it wait for completion? Should DMA buffers be volatile.

Review examples under HAL/Cube trees

Tips, Buy me a coffee, or three.. PayPal Venmo
Up vote any posts that you find helpful, it shows what's working..