cancel
Showing results for 
Search instead for 
Did you mean: 

STM32F407 ADC Issue

XLei.3
Associate II

Hello all,

I'm using STM32F407 ADC1 with DMA enabled for sampling on 4 channels at same time, the configuration for these 4 channels are all same with DIV4 clock and 12Bit Resolution, but only 3 of them are working fine, and one channel always returns 4095 to sample the square 60HZ waveform, occasionally it returns some small number 0 ~ 10.

Here is the code for ADC1 configuration:

 

   ADC_ChannelConfTypeDef sConfig = {0};

    /** Configure the global features of the ADC (Clock, Resolution, Data Alignment and number of conversion)
    */
    hadc1.Instance = ADC1;
    hadc1.Init.ClockPrescaler = ADC_CLOCK_SYNC_PCLK_DIV4;
    hadc1.Init.Resolution = ADC_RESOLUTION_12B;
    hadc1.Init.ScanConvMode = ENABLE;
    hadc1.Init.ContinuousConvMode = ENABLE;
    hadc1.Init.DiscontinuousConvMode = DISABLE;
    hadc1.Init.ExternalTrigConvEdge = ADC_EXTERNALTRIGCONVEDGE_NONE;
    hadc1.Init.ExternalTrigConv = ADC_SOFTWARE_START;
    hadc1.Init.DataAlign = ADC_DATAALIGN_RIGHT;
    hadc1.Init.NbrOfConversion = ADC1_CHANNEL_NUM;
    hadc1.Init.DMAContinuousRequests = ENABLE;
    hadc1.Init.EOCSelection = ADC_EOC_SEQ_CONV;

    if (HAL_ADC_Init(&hadc1) != HAL_OK) {
        LOGE("Failed to call HAL_ADC_Init hadc1");
        return -1;
    }
    /** Configure for the selected ADC regular channel its corresponding rank in the sequencer and its sample time.
    */
    sConfig.Channel = ADC_CHANNEL_4;
    sConfig.Rank = 1;
    sConfig.SamplingTime = ADC_SAMPLETIME_84CYCLES;
    if (HAL_ADC_ConfigChannel(&hadc1, &sConfig) != HAL_OK) {
        LOGE("Failed to call HAL_ADC_ConfigChannel hadc1");
        return -1;
    }

    /** Configure for the selected ADC regular channel its corresponding rank in the sequencer and its sample time.
    */
    sConfig.Channel = ADC_CHANNEL_6;
    sConfig.Rank = 2;
    if (HAL_ADC_ConfigChannel(&hadc1, &sConfig) != HAL_OK) {
        LOGE("Failed to call HAL_ADC_ConfigChannel hadc1");
        return -1;
    }
    /** Configure for the selected ADC regular channel its corresponding rank in the sequencer and its sample time.
    */
    sConfig.Channel = ADC_CHANNEL_8;
    sConfig.Rank = 3;
    if (HAL_ADC_ConfigChannel(&hadc1, &sConfig) != HAL_OK) {
        LOGE("Failed to call HAL_ADC_ConfigChannel hadc1");
        return -1;
    }
    /** Configure for the selected ADC regular channel its corresponding rank in the sequencer and its sample time.
    */
    sConfig.Channel = ADC_CHANNEL_9;
    sConfig.Rank = 4;
    if (HAL_ADC_ConfigChannel(&hadc1, &sConfig) != HAL_OK) {
        LOGE("Failed to call HAL_ADC_ConfigChannel hadc1");
        return -1;
    }

 

The failed channel is CHANNEL_6. Could anyone give me some clue about some errors in the code?

Just FYI, the system Clock is set to 160MHZ, and PCLK2 is 80MHZ.

Any feedback will be appreciated.

 

8 REPLIES 8
RhSilicon
Lead

for sampling on 4 channels at same time

Interesting to be able to read 4 channels at the same time, if the MCU only has 3 ADC.

raptorhal2
Lead

What is the sampling frequency? Channel 6 may be in sync with a multiple of the 60 Hz.

TDK
Guru

Nothing wrong with the initialization code. Swap that channel to another rank and see if the result is the same.

Show the 60Hz waveform on a scope to verify. The ADC is returning 4095 and values close to 0. Simplest and most likely explanation is that's what's actually on the pin.

Other channels are working so the ADC is fine.

If you feel a post has answered your question, please click "Accept as Solution".

The sampling frequency is 25HZ now. This might be a problem, let me have a quick verification. Thanks.

XLei.3
Associate II

I used the scope on the pin, it's showing 60HZ waveform. Yes, I agreed with you, ADC is fine. Actually I'm also using ADC3 to sample 3 more channels, all 6 changes are fine except for this one.

I changed the sampling rate to 240HZ or even higher,  but no lucks to get good result.

XLei.3
Associate II

I swapped the rank, but same result as before.

Thanks to the powerful MCU, each ADC can supports multiple channels