2024-05-21 07:28 AM
Hello everyone, im trying to understand how the configuration and the use of the ADC is usued on a NUCLEO401RE. I've read the datasheet and reference Manuel but still its not clear.
I have configure the pin A0 on CubeIDE to be an ADC input. But now i don't now how to handle the software part and which functions (HAL or not) to use to read this entry and get their value.
Could someone help me code this ? or give me some indications maybe ?
Im thanking you in advance for your kind help.
Solved! Go to Solution.
2024-05-21 08:36 AM
Hi,
1. use Cube + HAL (best for beginning to do something...)
2. how to use the peripherals , just look in the HAL lib (in your generated project -> /Drivers/STM32xx_HAL_Driver/
then open , what you look for, here ADC : stm32xx_hal_adc.c .
Here you see all "standard" calls for this, e.g. HAL_ADC_Start(..) and short description:
/**
* @brief Enable ADC, start conversion of regular group.
* @note Interruptions enabled in this function: None.
* @note Case of multimode enabled (when multimode feature is available):
* if ADC is Slave, ADC is enabled but conversion is not started,
* if ADC is master, ADC is enabled and multimode conversion is started.
* @PAram hadc ADC handle
* @retval HAL status
*/
HAL_StatusTypeDef HAL_ADC_Start(ADC_HandleTypeDef *hadc)
{
and also the variants, to use it with INT or DMA .
And you can search on github, many projects there, to get some "inspiration" .
(But dont expect click and ready examples, like on Arduino...)
2024-05-21 07:39 AM
Hello,
You can inspire from the example provided in the STM32CubeF4 package under this link:
It does use DMA to transfer converted values in the RAM.
2024-05-21 08:14 AM
Thank you for your response, however i the files uploaded on this github, i can't find any particular code that would help me, in the main.c file, besides the ADC configuration that are automatically done by CubeMX, I don't see any kind of software exemple that describe this process...
2024-05-21 08:26 AM - edited 2024-05-21 08:28 AM
Sorry I didn't understand what do you mean by:
besides the ADC configuration that are automatically done by CubeMX, I don't see any kind of software exemple that describe this process...
I pointed you out to an example which was not generated by CubeMx. It's a complete example, you can refer to the readme file to understand how it works.
You can generate the code with CubeMx and add your code from the example I provided to you in / User code start */ / User code end */ section.
2024-05-21 08:36 AM
Hi,
1. use Cube + HAL (best for beginning to do something...)
2. how to use the peripherals , just look in the HAL lib (in your generated project -> /Drivers/STM32xx_HAL_Driver/
then open , what you look for, here ADC : stm32xx_hal_adc.c .
Here you see all "standard" calls for this, e.g. HAL_ADC_Start(..) and short description:
/**
* @brief Enable ADC, start conversion of regular group.
* @note Interruptions enabled in this function: None.
* @note Case of multimode enabled (when multimode feature is available):
* if ADC is Slave, ADC is enabled but conversion is not started,
* if ADC is master, ADC is enabled and multimode conversion is started.
* @PAram hadc ADC handle
* @retval HAL status
*/
HAL_StatusTypeDef HAL_ADC_Start(ADC_HandleTypeDef *hadc)
{
and also the variants, to use it with INT or DMA .
And you can search on github, many projects there, to get some "inspiration" .
(But dont expect click and ready examples, like on Arduino...)
2024-05-23 12:51 AM
Thank you @SofLit and @AScha.3, I have finally manage to do it in deed thnaks to the examples and the HAL library which provide good functions to easily to that. However theres no HAL_Functions that allows to only check if the ADC measure done by HAL_ADC_Start(). Theres 2 possibilities with HAL library :
- HAL_ADC_PoolForConversion(...) : this function is working with a TimeOut which does not allow to execute other tasks while this period is not over.
- HAL_ADC_GetValue(...) : this function only get the result of the HAL_ADC_Start() measure. But theres no way to check if the measuring process is over before getting the value or not, with the HAL_Library.
Anyway thank you very much for your help, and maybe see you soon on other STM32 forums.
2024-05-23 01:18 AM
Right, HAL_ADC_PollForConversion(...), is waiting , as its name suggests.
But think: if you have the ADC converting at 1 Msps, it will just wait 1 us for one conversion...not sooo long time. :)
If you just want look "is it busy or ready now", try : HAL_ADC_GetState() .
+
If doing a lot of conversions at hi speed, think about using HAL_ADC_Start_DMA() , then you get just an INT, when the (new) ADC data block is finished.
2024-05-23 01:22 AM - edited 2024-05-23 01:22 AM
Thanks a lot for your suggestion about HAL_ADC_GetState(), i'll try it asap.
In deed, 1 us is not a long time at all but this ADC process is part of another one bigger that has to be run every 5ms and there's a lot of other task to run so the idea is to be the most effecient possible ;)
I'll investigate about the use of DMA for that. Again, thank you very much for your helpfull answers !
2024-05-23 06:55 AM - edited 2024-05-23 06:57 AM
@AScha.3 if somehow you are still curious about how to read the value only when the measurement is over, the process i finally used is by gettinf rid of the HAL_functions and to use directly the flags of the ADC and to right my code with the process of a state diagram ( iwould enjoy to show it but i can't because of confidentiality policy)
2024-05-23 07:59 AM - edited 2024-05-23 08:03 AM
Perfect !
Or try the LL lib, that is fast, but maybe not so comfortable as HAL (at not available for everything).
If you want/need something really fast, write/read the registers direct - you know.
And using HAL , just look how its done - or read in rm, what register you need/can use .
So you get something working really in short time with using HAL , but this is a "bloated" complex lib, that might need a microsecond to set a port pin...just leave it as it is, if you only switch on a LED with this.
But if you want something really fast, write to the pin and do it at cpu speed : 4ns on a H563 at 250M -> e.g.
GPIOG->BSRR = GPIO_PIN_4;
This is the way !