Skip to main content
Associate II
August 27, 2024
Solved

How can I get adc value in main statement?

  • August 27, 2024
  • 4 replies
  • 3851 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!

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

waclawek.jan
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

TDK
Super User
August 27, 2024
"If you feel a post has answered your question, please click ""Accept as Solution""."
Andrew Neil
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

A complex system that works is invariably found to have evolved from a simple system that worked.A complex system designed from scratch never works and cannot be patched up to make it work.
mƎALLEm
mƎALLEmBest answer
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.

To give better visibility on the answered topics, please click "Best answer" on the reply which solved your issue or answered your question.
Andrew Neil
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.

A complex system that works is invariably found to have evolved from a simple system that worked.A complex system designed from scratch never works and cannot be patched up to make it work.
mƎALLEm
Technical Moderator
August 27, 2024

Indeed, I didn't express myself well ..

To give better visibility on the answered topics, please click "Best answer" on the reply which solved your issue or answered your question.