0
0
Cnc-programmingHow-ToBeginner · 3 min read

How to Configure ADC on STM32: Step-by-Step Guide

To configure the ADC on an STM32, first enable the ADC clock and configure the GPIO pin as analog input. Then initialize the ADC parameters like resolution and sampling time, and start the ADC conversion using the HAL library functions.
📐

Syntax

The basic steps to configure ADC on STM32 using HAL library are:

  • Enable ADC peripheral clock.
  • Configure GPIO pin as analog mode.
  • Initialize ADC handle with settings like resolution and scan mode.
  • Configure ADC channel and sampling time.
  • Start ADC conversion and read the value.
c
ADC_HandleTypeDef hadc1;

void ADC_Config(void) {
    __HAL_RCC_ADC1_CLK_ENABLE();

    GPIO_InitTypeDef GPIO_InitStruct = {0};
    __HAL_RCC_GPIOA_CLK_ENABLE();
    GPIO_InitStruct.Pin = GPIO_PIN_0;
    GPIO_InitStruct.Mode = GPIO_MODE_ANALOG;
    GPIO_InitStruct.Pull = GPIO_NOPULL;
    HAL_GPIO_Init(GPIOA, &GPIO_InitStruct);

    hadc1.Instance = ADC1;
    hadc1.Init.Resolution = ADC_RESOLUTION_12B;
    hadc1.Init.ScanConvMode = DISABLE;
    hadc1.Init.ContinuousConvMode = DISABLE;
    hadc1.Init.DiscontinuousConvMode = DISABLE;
    hadc1.Init.ExternalTrigConv = ADC_SOFTWARE_START;
    hadc1.Init.DataAlign = ADC_DATAALIGN_RIGHT;
    hadc1.Init.NbrOfConversion = 1;
    HAL_ADC_Init(&hadc1);

    ADC_ChannelConfTypeDef sConfig = {0};
    sConfig.Channel = ADC_CHANNEL_0;
    sConfig.Rank = ADC_REGULAR_RANK_1;
    sConfig.SamplingTime = ADC_SAMPLETIME_3CYCLES;
    HAL_ADC_ConfigChannel(&hadc1, &sConfig);
}
💻

Example

This example shows how to configure ADC1 on pin PA0, start a single conversion, and read the converted value.

c
#include "stm32f1xx_hal.h"

ADC_HandleTypeDef hadc1;

void SystemClock_Config(void);
void ADC_Config(void);

int main(void) {
    HAL_Init();
    SystemClock_Config();
    ADC_Config();

    HAL_ADC_Start(&hadc1);
    if(HAL_ADC_PollForConversion(&hadc1, 100) == HAL_OK) {
        uint32_t adcValue = HAL_ADC_GetValue(&hadc1);
        // adcValue now holds the ADC reading from PA0
    }
    HAL_ADC_Stop(&hadc1);

    while(1) {}
}

void ADC_Config(void) {
    __HAL_RCC_ADC1_CLK_ENABLE();

    GPIO_InitTypeDef GPIO_InitStruct = {0};
    __HAL_RCC_GPIOA_CLK_ENABLE();
    GPIO_InitStruct.Pin = GPIO_PIN_0;
    GPIO_InitStruct.Mode = GPIO_MODE_ANALOG;
    GPIO_InitStruct.Pull = GPIO_NOPULL;
    HAL_GPIO_Init(GPIOA, &GPIO_InitStruct);

    hadc1.Instance = ADC1;
    hadc1.Init.Resolution = ADC_RESOLUTION_12B;
    hadc1.Init.ScanConvMode = DISABLE;
    hadc1.Init.ContinuousConvMode = DISABLE;
    hadc1.Init.DiscontinuousConvMode = DISABLE;
    hadc1.Init.ExternalTrigConv = ADC_SOFTWARE_START;
    hadc1.Init.DataAlign = ADC_DATAALIGN_RIGHT;
    hadc1.Init.NbrOfConversion = 1;
    HAL_ADC_Init(&hadc1);

    ADC_ChannelConfTypeDef sConfig = {0};
    sConfig.Channel = ADC_CHANNEL_0;
    sConfig.Rank = ADC_REGULAR_RANK_1;
    sConfig.SamplingTime = ADC_SAMPLETIME_3CYCLES;
    HAL_ADC_ConfigChannel(&hadc1, &sConfig);
}

void SystemClock_Config(void) {
    // System clock configuration code here (depends on your board)
}
Output
No visible output; adcValue variable contains the ADC reading (0-4095 for 12-bit resolution).
⚠️

Common Pitfalls

Common mistakes when configuring ADC on STM32 include:

  • Not enabling the ADC and GPIO clocks before configuration.
  • Forgetting to set the GPIO pin to analog mode, which can cause incorrect readings.
  • Not initializing the ADC handle properly or missing channel configuration.
  • Starting ADC conversion without polling or waiting for conversion completion.
  • Using wrong sampling time or resolution settings for your application.
c
/* Wrong: GPIO pin not set to analog mode */
GPIO_InitStruct.Mode = GPIO_MODE_INPUT; // Incorrect for ADC

/* Right: GPIO pin set to analog mode */
GPIO_InitStruct.Mode = GPIO_MODE_ANALOG; // Correct for ADC
📊

Quick Reference

Remember these key points when configuring ADC on STM32:

  • Enable clocks for ADC and GPIO ports.
  • Configure GPIO pins as analog inputs.
  • Initialize ADC with desired resolution and conversion mode.
  • Configure ADC channel and sampling time carefully.
  • Start conversion and wait for completion before reading value.

Key Takeaways

Always enable ADC and GPIO clocks before configuration.
Set GPIO pins to analog mode to get accurate ADC readings.
Initialize ADC handle and configure channels properly.
Start ADC conversion and wait for it to complete before reading data.
Choose sampling time and resolution based on your application needs.