Skip to main content
Graduate II
May 21, 2024
Solved

ADC on Nucleo 401RE

  • May 21, 2024
  • 3 replies
  • 2622 views

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.

    This topic has been closed for replies.
    Best answer by AScha.3

    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...)

    3 replies

    Technical Moderator
    May 21, 2024

    Hello,

    You can inspire from the example provided in the STM32CubeF4 package under this link:

    https://github.com/STMicroelectronics/STM32CubeF4/tree/master/Projects/STM32F401-Discovery/Examples/ADC/ADC_RegularConversion_DMA

    It does use DMA to transfer converted values in the RAM.

    willowAuthor
    Graduate II
    May 21, 2024

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

    Technical Moderator
    May 21, 2024

    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.

     

    AScha.3Answer
    Super User
    May 21, 2024

    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...)

    willowAuthor
    Graduate II
    May 23, 2024

    Thank you @mƎALLEm 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.

    Super User
    May 23, 2024

    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.

    willowAuthor
    Graduate II
    May 23, 2024

    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 !