STM32 Guide: ADC
Image for post

Prerequisites

This is tutorial is not intended to be a guide for learning C language or about the STM32 platform. It’s primary target is to provide developers a concise guide about integrating peripheral modules and features into active applications.

If you are a beginner, I would recommend you look into an STM32 Project Setup guide like this one.

https://medium.com/vicara-hardware-university/smt32-project-setup-with-cubeide-947974baf713

ADC

Analog to Digital Converters are a very essential component of embedded devices. Our world operates in analog format and correspondingly the sensors we develop read analog signals. However, our processors are all digital and not a good fit for handling analog data.

ADC reads analog signals and converts them into a usable format ie; digital for use in microcontrollers and microprocessors.

ADC in STM32

The initialization process for STM32 peripherals is handled by the Cube IDE. Once that is done, the code generator gives a main.c file with the ADC_Init() function configured as per the settings you mention in the Cube IDE File.

Image for post

In the F407 Dev Kit we have ADC1 enabled by default. The generated code will therefore have ADC1_Init() function in the main.c file and also have it called.

The configuration will follow the same parameters you have set in the Peripheral Config Tab.

The generator will also create an ADC typedef handle. We can now use ADC API functions by using this handle as a reference.

ADC HAL API Functions

Start ADC Conversion

HAL_ADC_Start(ADC_HandleTypeDef *hadc);

Parameters:

Parameters
* hadc: pointer to a ADC_HandleTypeDef structure that
contains the configuration information for the specified ADC.
Return values
* HAL: status.

Stop ADC Conversion

HAL_ADC_Stop (ADC_HandleTypeDef *hadc);

Parameters:

Parameters
* hadc: pointer to a ADC_HandleTypeDef structure that
contains the configuration information for the specified ADC.
Return values
* HAL: status.

Conclusion

In this tutorial, we discussed the methods for using ADC API from CMSIS-HAL library for reading and writing to ADC devices.

Sources