cancel
Showing results for 
Search instead for 
Did you mean: 

I am trying to read 3 ADC channels via DMA. One weird thing happen: if I use a breakpoint on the line of the readings, I get the correct values from the ADC. If I don't use a breakpoint, the values are always 0 or random. Can you help me ?

LScar.1
Associate

I tried using delays, but they don't solve the problem.

Data are retrieved using

HAL_ADC_Start_DMA(&hadc1, value, DATA_LENGTH)

HAL_ADC_Stop_DMA(&hadc1);

here below my intialization and function:

void MX_ADC1_Init(void)

{

    ADC_ChannelConfTypeDef sConfig = {0};

    hadc1.Instance = ADC1;

    hadc1.Init.ClockPrescaler = ADC_CLOCK_ASYNC_DIV4;

    hadc1.Init.Resolution = ADC_RESOLUTION_12B;

    hadc1.Init.DataAlign = ADC_DATAALIGN_RIGHT;

    hadc1.Init.ScanConvMode = ADC_SCAN_ENABLE;

    hadc1.Init.EOCSelection = ADC_EOC_SINGLE_CONV;

    hadc1.Init.LowPowerAutoWait = DISABLE;

    hadc1.Init.ContinuousConvMode = DISABLE;

    hadc1.Init.NbrOfConversion = 3;

    hadc1.Init.DiscontinuousConvMode = DISABLE;

    hadc1.Init.ExternalTrigConv = ADC_SOFTWARE_START;

    hadc1.Init.ExternalTrigConvEdge = ADC_EXTERNALTRIGCONVEDGE_NONE;

    hadc1.Init.DMAContinuousRequests = DISABLE;

    hadc1.Init.Overrun = ADC_OVR_DATA_OVERWRITTEN;

    hadc1.Init.OversamplingMode = DISABLE;

    if (HAL_ADC_Init(&hadc1) != HAL_OK)

    {

        Error_Handler();

    }

    /** Configure Regular Channel

     */

    sConfig.Channel = ADC_CHANNEL_5;

    sConfig.Rank = ADC_REGULAR_RANK_1;

    sConfig.SamplingTime = ADC_SAMPLETIME_24CYCLES_5;

    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_6;

    sConfig.Rank = ADC_REGULAR_RANK_2;

    sConfig.SamplingTime = ADC_SAMPLETIME_24CYCLES_5;

    if (HAL_ADC_ConfigChannel(&hadc1, &sConfig) != HAL_OK)

    {

        Error_Handler();

    }

    /** Configure Regular Channel

     */

    sConfig.Channel = ADC_CHANNEL_11;

    sConfig.Rank = ADC_REGULAR_RANK_3;

    sConfig.SamplingTime =  ADC_SAMPLETIME_24CYCLES_5;

    sConfig.SingleDiff = ADC_SINGLE_ENDED;

    sConfig.OffsetNumber = ADC_OFFSET_NONE;

    sConfig.Offset = 0;

    if (HAL_ADC_ConfigChannel(&hadc1, &sConfig) != HAL_OK)

    {

        Error_Handler();

    }

}

1 REPLY 1
Simon.T
ST Employee

Hello @LScar.1​ ,

The error may come from fact of you are STOP the DMA just after the conversion.

Can you give me the reason why you are stopping the ADC just after the start ? Because as the ADC is configured in ADC_SOFTWARE_START, it will do the 3 conversions and wait for the next software start.

Is it possible to try just without the HAL_ADC_Stop_DMA(&hadc1); ?

Best regards,

Simon