How can I get adc value in main statement?
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Email to a Friend
- Report Inappropriate Content
‎2024-08-26 11:02 PM
I am using 5 adc values (emg[5]) and can get the value of them.
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])
Can I know what's the matter of this,,? Thanks for help!
Solved! Go to Solution.
- Labels:
-
ADC
-
STM32F4 Series
Accepted Solutions
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Email to a Friend
- Report Inappropriate Content
‎2024-08-27 7:26 AM - edited ‎2024-08-27 7:26 AM
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.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Email to a Friend
- Report Inappropriate Content
‎2024-08-26 11:44 PM
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
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Email to a Friend
- Report Inappropriate Content
‎2024-08-27 5:42 AM
If you want to use polling mode, see here for an example:
If you want to use DMA, see here for an example:
Conversions are not complete until HAL_ADC_ConvCpltCallback is called from an interrupt.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Email to a Friend
- Report Inappropriate Content
‎2024-08-27 5:52 AM
Please see the Posting Tips for how to properly post source code - not as screenshots:
A complex system designed from scratch never works and cannot be patched up to make it work.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Email to a Friend
- Report Inappropriate Content
‎2024-08-27 7:26 AM - edited ‎2024-08-27 7:26 AM
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.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Email to a Friend
- Report Inappropriate Content
‎2024-08-27 7:37 AM
@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 designed from scratch never works and cannot be patched up to make it work.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Email to a Friend
- Report Inappropriate Content
‎2024-08-27 8:18 AM
Indeed, I didn't express myself well ..
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Email to a Friend
- Report Inappropriate Content
‎2024-08-27 6:48 PM
Thanks a lot! I got an answer
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Email to a Friend
- Report Inappropriate Content
‎2024-08-28 2:18 AM
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
A complex system designed from scratch never works and cannot be patched up to make it work.
