cancel
Showing results for 
Search instead for 
Did you mean: 

How can I get adc value in main statement?

lee_daeun
Associate II

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!

1 ACCEPTED SOLUTION

Accepted Solutions
SofLit
ST Employee

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 on "Accept as Solution" on the reply which solved your issue or answered your question.

View solution in original post

8 REPLIES 8

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
Guru

If you want to use polling mode, see here for an example:

STM32CubeF4/Projects/STM32F413ZH-Nucleo/Examples/ADC/ADC_RegularConversion_Polling/Src/main.c at 82738194a5e85ee5331975b04905daa0a17053ec · STMicroelectronics/STM32CubeF4 (github.com)

 

If you want to use DMA, see here for an example:

STM32CubeF4/Projects/STM32F446ZE-Nucleo/Examples/ADC/ADC_RegularConversion_DMA/Src/main.c at 82738194a5e85ee5331975b04905daa0a17053ec · STMicroelectronics/STM32CubeF4 (github.com)

Conversions are not complete until HAL_ADC_ConvCpltCallback is called from an interrupt.

 

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

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

SofLit
ST Employee

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 on "Accept as Solution" on the reply which solved your issue or answered your question.

@SofLit 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.

Indeed, I didn't express myself well ..

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

Thanks a lot! I got an answer

The key learning from this is to understand the "asynchronous" key word introduced by @waclawek.jan 

This is a general concept in all forms of computing - not just specific to your ADC or DMA.

The distinction between synchronous (aka "blocking") and  asynchronous (aka "non-blocking") APIs:

  • Synchronous: the function does not return until the entire operation has completed, and all results are available.
    Thus, the function blocks the rest of your code from running until it is done - hence the alternate name, "blocking".
     
  • Asychronous: the function simply starts the process, and returns immediately while the process continues in the background. Therefore the results are not available immediately.
    Thus, the function does not block the rest of your code from running - hence the alternate name, "non-blocking".
    This can improve the performance of your application, but does bring some added complexity - there now needs to be some extra mechanism for your code to know when the operation is completed; ie, when the results are available.
     

Example - illustrating the generality:

https://www.techtarget.com/whatis/definition/synchronous-asynchronous-API

 

#BlockingCall #NonBlockingCall #SynchronousAPI #AsynchronousAPI