Skip to main content
Graduate
August 27, 2024
Solved

How can I get adc value in main statement?

  • August 27, 2024
  • 4 replies
  • 1750 views

I am using 5 adc values (emg[5]) and can get the value of them.

lee_daeun_0-1724738352736.png

lee_daeun_2-1724738424817.png

 

I want to use these values in the main statement, so I assigned it.

However I can't get the value. It's always zero.  (good[0] = emg[0])

lee_daeun_1-1724738390885.png

lee_daeun_3-1724738449569.png

 

 

Can I know what's the matter of this,,? Thanks for help!

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

    Hello @lee_daeun and welcome to the community,

    First, as stated by @Andrew Neil , you need to copy paste your code using the button </> instead of posting the screenshots.

    Second, as stated by @waclawek.jan ,

    The DMA transfer is not starting immediately after calling HAL_ADC_Start_DMA():

     

    HAL_ADC_Start_DMA(&hadc1, (uint32_t*) emg, 5);
    good[0] = emg[0];

     

    So you need to use interrupt callback HAL_ADC_ConvCpltCallback() and read emg ADC buffer there.

    4 replies

    Super User
    August 27, 2024

    HAL_ADC_Start_DMA() is asynchronous, it means, that it starts the hardware process with ADC and DMA and returns immediately, not waiting for the result. It takes time until the hardware - ADC and DMA - performs the conversions, so you can't read out the results immediately after that function was called.

    DMA signals when it's finished by the Transfer Complete interrupt, the is a callback in Cube/HAL which is called at that point and may be a good point to read the results. I don't use Cube/HAL, refer to is documentation or to the examples.

    JW

    Super User
    August 27, 2024
    Super User
    August 27, 2024

    Please see the Posting Tips for how to properly post source code - not as screenshots:

    https://community.st.com/t5/community-guidelines/how-to-write-your-question-to-maximize-your-chances-to-find-a/ta-p/575228

    mƎALLEmAnswer
    Technical Moderator
    August 27, 2024

    Hello @lee_daeun and welcome to the community,

    First, as stated by @Andrew Neil , you need to copy paste your code using the button </> instead of posting the screenshots.

    Second, as stated by @waclawek.jan ,

    The DMA transfer is not starting immediately after calling HAL_ADC_Start_DMA():

     

    HAL_ADC_Start_DMA(&hadc1, (uint32_t*) emg, 5);
    good[0] = emg[0];

     

    So you need to use interrupt callback HAL_ADC_ConvCpltCallback() and read emg ADC buffer there.

    Super User
    August 27, 2024

    @mƎALLEm wrote:

    The DMA transfer is not starting immediately after calling HAL_ADC_Start_DMA():.


    It should be starting, but it won't have finished - therefore the results are not yet available.

    Technical Moderator
    August 27, 2024

    Indeed, I didn't express myself well ..