Skip to content

ADC

Introduction

The MikroSDK.Driver.ADC library simplifies the process of reading analog voltage values from ADC pins.

It abstracts the low-level hardware details, providing a straightforward API to initialize ADC peripherals and read their values.

This documentation will guide you through setting up the environment, initializing the ADC driver, and performing analog readings.

API Name

  • MikroSDK.Driver.ADC

API Files

Prerequisites

  • Library Manager:

    • Select Driver.ADC to add the library to your project using the Library Manager in NECTO Studio.
  • Headers:

    • Include the Driver.ADC header in your source files to access the GPIO output functions.
    • This header pops up in the lower right part of NECTO Studio once you perform the previous task (while selecting Driver.ADC in Library Manager)
    • Alternatively, include the drv_analog_in.h header in your source files to access the GPIO output functions.
  • CMakeLists:

    • You do not have to perform anything, configuration (CMake) file is already taken care of automatically while you performed the first task in this Prerequisites!
    • You can ensure that your CMakeLists.txt includes the necessary configurations to link against the MikroSDK.Driver.ADC library.

Code Examples

  • How do I initialize the ADC driver using drv_analog_in.h?
/* Project name:
 *   How do I initialize the ADC driver?
 * Copyright:
 *   (c) MIKROE, 2024.
 * Description:
 *   This example demonstrates the functionality of ADC (Analog-to-Digital Converter)
 *   using mikroSDK v2.0. ADC shall be initialized, ADC value shall be read as well as
 *   actual voltage on ADC pin.
 * How to test this code example?
 *   - Run debug mode by pressing F9 on your keyboard;
 *   - Insert `read_data_buffer` and `read_data_buffer_voltage` variables in Debug Watch window;
 *   - Step Over (F8 on your keyboard) to read ADC values.
 * Library dependencies?
 *   - Make sure `Driver.ADC`, `Driver.GPIO.Port` and `Board` libraries are enabled in NECTO's
 *     Library Manager to ensure a successful build.
 */

// ------------------------------------------------------------------ INCLUDES
/**
 * Any initialization code needed for MCU to function properly.
 * Do not remove this line or clock might not be set correctly.
 */
#ifdef PREINIT_SUPPORTED
#include "preinit.h"
#endif

#include "drv_analog_in.h"  // API for Analog input driver.
#include "board.h"          // Main board pin mapping.

// -------------------------------------------------------------------- MACROS
// Voltage reference value.
#define ADC_VREF_VALUE 3.3f // It can be changed in CODEGRIP Suite: POWER -> Outputs -> Voltage Reference -> Set Voltage.

// ------------------------------------------------------------------ VARIABLES
analog_in_t adc;            // Analog input driver context structure.
analog_in_config_t adc_cfg; // Analog input configuration structure.

uint16_t read_data_buffer;  // Analog input data buffer.
float read_data_buffer_voltage; // Analog input data buffer (voltage).

int main(void)
{
    /* Do not remove this line or clock might not be set correctly. */
    #ifdef PREINIT_SUPPORTED
    preinit();
    #endif

    // Initialize ADC configuration structure to default values
    analog_in_configure_default(&adc_cfg);

    // Set the correct input pin here (analog pin on mikroBUS 1 slot is currently set)
    adc_cfg.input_pin = MIKROBUS_1_AN; // example: `PA0` or `MIKROBUS_4_AN` or `HAL_PIN_NC` (`Pin not connected` state)

    // Set the correct ADC resolution
    adc_cfg.resolution = ANALOG_IN_RESOLUTION_12_BIT;

    // Set the desired reference voltage
    adc_cfg.vref_value = ADC_VREF_VALUE;

    // Initialize the ADC
    if( ACQUIRE_FAIL == analog_in_open( &adc, &adc_cfg ) ) {
        // Error handling
    }

    // Configure ADC resolution
    if( ADC_ERROR == analog_in_set_resolution( &adc, adc_cfg.resolution ) ) {
        // Error handling
    }

    // Configure ADC voltage reference input source
    if( ADC_ERROR == analog_in_set_vref_input( &adc, ANALOG_IN_VREF_EXTERNAL ) ) {
        // Error handling
    }

    // Configure ADC voltage reference value
    if( ADC_ERROR == analog_in_set_vref_value( &adc, ADC_VREF_VALUE ) ) {
        // Error handling
    }

    // Finally, read the analog value (which depends on ADC resolution)
    if( ADC_ERROR == analog_in_read( &adc, &read_data_buffer ) ) {
        // Error handling
    }

    Delay_ms(500); // Delay for 500 milliseconds

    // You are able to read the actual voltage on the pin as well
    if( ADC_ERROR == analog_in_read_voltage( &adc, &read_data_buffer_voltage ) ) {
        // Error handling
    }

    // Close ADC driver context object
    if ( ADC_SUCCESS == analog_in_close( &adc ) ) // De-allocates hardware resources for the driver object
    {
        // No error
    } else {
        // Handle the error
    }

    return 0;
}
  • How do I set a reference voltage value for the ADC?
/* Project name:
 *   How do I set a reference voltage value for the ADC?
 * Copyright:
 *   (c) MIKROE, 2024.
 * Description:
 *   This example demonstrates the configuration of a reference voltage
 *   value for the ADC (Analog-to-Digital Converter) using mikroSDK v2.0.
 *   ADC shall be initialized, reference voltage shall be set.
 * Library dependencies?
 *   - Make sure `Driver.ADC`, `Driver.GPIO.Port` and `Board` libraries are enabled in NECTO's
 *     Library Manager to ensure a successful build.
 */

// ------------------------------------------------------------------ INCLUDES
/**
 * Any initialization code needed for MCU to function properly.
 * Do not remove this line or clock might not be set correctly.
 */
#ifdef PREINIT_SUPPORTED
#include "preinit.h"
#endif

#include "drv_analog_in.h"  // API for Analog input driver.
#include "board.h"          // Main board pin mapping.

// -------------------------------------------------------------------- MACROS
// Voltage reference value.
#define ADC_VREF_VALUE 3.3f // It can be changed in CODEGRIP Suite: POWER -> Outputs -> Voltage Reference -> Set Voltage.

// ------------------------------------------------------------------ VARIABLES
analog_in_t adc;            // Analog input driver context structure.
analog_in_config_t adc_cfg; // Analog input configuration structure.

int main(void)
{
    /* Do not remove this line or clock might not be set correctly. */
    #ifdef PREINIT_SUPPORTED
    preinit();
    #endif

    // Set the desired reference voltage
    adc_cfg.vref_value = ADC_VREF_VALUE;

    // Initialize the ADC
    if( ACQUIRE_FAIL == analog_in_open( &adc, &adc_cfg ) ) {
        // Error handling
    }

    // Configure ADC voltage reference value
    if( ADC_ERROR == analog_in_set_vref_value( &adc, ADC_VREF_VALUE ) ) {
        // Error handling
    }

    return 0;
}
  • How do I read raw ADC values from the input pin?
/* Project name:
 *   How do I read raw ADC values from the input pin?
 * Copyright:
 *   (c) MIKROE, 2024.
 * Description:
 *   This example demonstrates reading raw ADC values from the input pin of an ADC
 *   (Analog-to-Digital Converter) using mikroSDK v2.0.
 *   ADC should be initialized and raw ADC value shall be read.
 * How to test this code example?
 *   - Run debug mode by pressing F9 on your keyboard;
 *   - Insert `read_data_buffer` variable in Debug Watch window;
 *   - Step Over (F8 on your keyboard) to read raw ADC value.
 * Library dependencies?
 *   - Make sure `Driver.ADC`, `Driver.GPIO.Port` and `Board` libraries are enabled in NECTO's
 *     Library Manager to ensure a successful build.
 */

// ------------------------------------------------------------------ INCLUDES
/**
 * Any initialization code needed for MCU to function properly.
 * Do not remove this line or clock might not be set correctly.
 */
#ifdef PREINIT_SUPPORTED
#include "preinit.h"
#endif

#include "drv_analog_in.h"  // API for Analog input driver.
#include "board.h"          // Main board pin mapping.

// -------------------------------------------------------------------- MACROS
// Voltage reference value.
#define ADC_VREF_VALUE 3.3f // It can be changed in CODEGRIP Suite: POWER -> Outputs -> Voltage Reference -> Set Voltage.

// ------------------------------------------------------------------ VARIABLES
analog_in_t adc;            // Analog input driver context structure.
analog_in_config_t adc_cfg; // Analog input configuration structure.

uint16_t read_data_buffer;  // Analog input data buffer.

int main(void)
{
    /* Do not remove this line or clock might not be set correctly. */
    #ifdef PREINIT_SUPPORTED
    preinit();
    #endif

    // Initialize ADC configuration structure to default values
    analog_in_configure_default(&adc_cfg);

    // Set the correct input pin here (analog pin on mikroBUS 1 slot is currently set)
    adc_cfg.input_pin = MIKROBUS_1_AN; // example: `PA0` or `MIKROBUS_4_AN` or `HAL_PIN_NC` (`Pin not connected` state)

    // Set the correct ADC resolution
    adc_cfg.resolution = ANALOG_IN_RESOLUTION_12_BIT;

    // Set the desired reference voltage
    adc_cfg.vref_value = ADC_VREF_VALUE;

    // Initialize the ADC
    if( ACQUIRE_FAIL == analog_in_open( &adc, &adc_cfg ) ) {
        // Error handling
    }

    // Configure ADC resolution
    if( ADC_ERROR == analog_in_set_resolution( &adc, adc_cfg.resolution ) ) {
        // Error handling
    }

    // Configure ADC voltage reference input source
    if( ADC_ERROR == analog_in_set_vref_input( &adc, ANALOG_IN_VREF_EXTERNAL ) ) {
        // Error handling
    }

    // Configure ADC voltage reference value
    if( ADC_ERROR == analog_in_set_vref_value( &adc, ADC_VREF_VALUE ) ) {
        // Error handling
    }

    // Finally, read the raw ADC value (which depends on ADC resolution)
    if( ADC_ERROR == analog_in_read( &adc, &read_data_buffer ) ) {
        // Error handling
    }

    return 0;
}
  • How do I read a converted voltage from the ADC input pin?
/* Project name:
 *   How do I read a converted voltage from the ADC input pin?
 * Copyright:
 *   (c) MIKROE, 2024.
 * Description:
 *   This example demonstrates reading converted ADC voltage
 *   from the ADC input pin of an ADC (Analog-to-Digital Converter)
 *   using mikroSDK v2.0. ADC should be initialized and raw ADC
 *   value shall be read.
 * How to test this code example?
 *   - Run debug mode by pressing F9 on your keyboard;
 *   - Insert `read_data_buffer_voltage` variable in Debug Watch window;
 *   - Step Over (F8 on your keyboard) to read ADC voltage.
 * Library dependencies?
 *   - Make sure `Driver.ADC`, `Driver.GPIO.Port` and `Board` libraries are enabled in NECTO's
 *     Library Manager to ensure a successful build.
 */

// ------------------------------------------------------------------ INCLUDES
/**
 * Any initialization code needed for MCU to function properly.
 * Do not remove this line or clock might not be set correctly.
 */
#ifdef PREINIT_SUPPORTED
#include "preinit.h"
#endif

#include "drv_analog_in.h"  // API for Analog input driver.
#include "board.h"          // Main board pin mapping.

// -------------------------------------------------------------------- MACROS
// Voltage reference value.
#define ADC_VREF_VALUE 3.3f // It can be changed in CODEGRIP Suite: POWER -> Outputs -> Voltage Reference -> Set Voltage.

// ------------------------------------------------------------------ VARIABLES
analog_in_t adc;            // Analog input driver context structure.
analog_in_config_t adc_cfg; // Analog input configuration structure.

float read_data_buffer_voltage; // Analog input data buffer (voltage).

int main(void)
{
    /* Do not remove this line or clock might not be set correctly. */
    #ifdef PREINIT_SUPPORTED
    preinit();
    #endif

    // Initialize ADC configuration structure to default values
    analog_in_configure_default(&adc_cfg);

    // Set the correct input pin here (analog pin on mikroBUS 1 slot is currently set)
    adc_cfg.input_pin = MIKROBUS_1_AN; // example: `PA0` or `MIKROBUS_4_AN` or `HAL_PIN_NC` (`Pin not connected` state)

    // Set the correct ADC resolution
    adc_cfg.resolution = ANALOG_IN_RESOLUTION_12_BIT;

    // Set the desired reference voltage
    adc_cfg.vref_value = ADC_VREF_VALUE;

    // Initialize the ADC
    if( ACQUIRE_FAIL == analog_in_open( &adc, &adc_cfg ) ) {
        // Error handling
    }

    // Configure ADC resolution
    if( ADC_ERROR == analog_in_set_resolution( &adc, adc_cfg.resolution ) ) {
        // Error handling
    }

    // Configure ADC voltage reference input source
    if( ADC_ERROR == analog_in_set_vref_input( &adc, ANALOG_IN_VREF_EXTERNAL ) ) {
        // Error handling
    }

    // Configure ADC voltage reference value
    if( ADC_ERROR == analog_in_set_vref_value( &adc, ADC_VREF_VALUE ) ) {
        // Error handling
    }

    // Finally, you are able to read the actual voltage on the pin
    if( ADC_ERROR == analog_in_read_voltage( &adc, &read_data_buffer_voltage ) ) {
        // Error handling
    }

    return 0;
}
  • How do I close the ADC driver?
/* Project name:
 *   How do I close the ADC driver?
 * Copyright:
 *   (c) MIKROE, 2024.
 * Description:
 *   This example demonstrates the functionality of ADC (Analog-to-Digital Converter)
 *   using mikroSDK v2.0. ADC shall be initialized, ADC value shall be read as well as
 *   actual voltage on ADC pin, and finally closing the ADC driver shall be called.
 * How to test this code example?
 *   - Toggle breakpoint on the line where `analog_in_close` functionality is called;
 *   - Run debug mode by pressing F9 on your keyboard;
 *   - Continue debug (F6 on your keyboard) to get to the line;
 *   - Insert `adc` analog input driver context structure in Debug Watch window;
 *   - Step Over (F8 on your keyboard) to see the de-allocation in action.
 * Library dependencies?
 *   - Make sure `Driver.ADC`, `Driver.GPIO.Port` and `Board` libraries are enabled in NECTO's
 *     Library Manager to ensure a successful build.
 */

// ------------------------------------------------------------------ INCLUDES
/**
 * Any initialization code needed for MCU to function properly.
 * Do not remove this line or clock might not be set correctly.
 */
#ifdef PREINIT_SUPPORTED
#include "preinit.h"
#endif

#include "drv_analog_in.h"  // API for Analog input driver.
#include "board.h"          // Main board pin mapping.

// -------------------------------------------------------------------- MACROS
// Voltage reference value.
#define ADC_VREF_VALUE 3.3f // It can be changed in CODEGRIP Suite: POWER -> Outputs -> Voltage Reference -> Set Voltage.

// ------------------------------------------------------------------ VARIABLES
analog_in_t adc;            // Analog input driver context structure.
analog_in_config_t adc_cfg; // Analog input configuration structure.

uint16_t read_data_buffer;  // Analog input data buffer.
float read_data_buffer_voltage; // Analog input data buffer (voltage).

int main(void)
{
    /* Do not remove this line or clock might not be set correctly. */
    #ifdef PREINIT_SUPPORTED
    preinit();
    #endif

    // Initialize ADC configuration structure to default values
    analog_in_configure_default(&adc_cfg);

    // Set the correct input pin here (analog pin on mikroBUS 1 slot is currently set)
    adc_cfg.input_pin = MIKROBUS_1_AN; // example: `PA0` or `MIKROBUS_4_AN` or `HAL_PIN_NC` (`Pin not connected` state)

    // Set the correct ADC resolution
    adc_cfg.resolution = ANALOG_IN_RESOLUTION_12_BIT;

    // Set the desired reference voltage
    adc_cfg.vref_value = ADC_VREF_VALUE;

    // Initialize the ADC
    if( ACQUIRE_FAIL == analog_in_open( &adc, &adc_cfg ) ) {
        // Error handling
    }

    // Configure ADC resolution
    if( ADC_ERROR == analog_in_set_resolution( &adc, adc_cfg.resolution ) ) {
        // Error handling
    }

    // Configure ADC voltage reference input source
    if( ADC_ERROR == analog_in_set_vref_input( &adc, ANALOG_IN_VREF_EXTERNAL ) ) {
        // Error handling
    }

    // Configure ADC voltage reference value
    if( ADC_ERROR == analog_in_set_vref_value( &adc, ADC_VREF_VALUE ) ) {
        // Error handling
    }

    // Finally, read the analog value (which depends on ADC resolution)
    if( ADC_ERROR == analog_in_read( &adc, &read_data_buffer ) ) {
        // Error handling
    }

    Delay_ms(500); // Delay for 500 milliseconds

    // You are able to read the actual voltage on the pin as well
    if( ADC_ERROR == analog_in_read_voltage( &adc, &read_data_buffer_voltage ) ) {
        // Error handling
    }

    // Close ADC driver context object
    if ( ADC_SUCCESS == analog_in_close( &adc ) ) // De-allocates hardware resources for the driver object
    {
        // No error
    } else {
        // Handle the error
    }

    return 0;
}
  • How do I reconfigure the ADC driver after it has been initialized?
/* Project name:
 *   How do I reconfigure the ADC driver after it has been initialized?
 * Copyright:
 *   (c) MIKROE, 2024.
 * Description:
 *   This example demonstrates the functionality of ADC (Analog-to-Digital Converter)
 *   using mikroSDK v2.0.
 *   ADC shall be initialized, ADC value shall be read as well as actual voltage
 *   on ADC pin, closing the ADC driver shall be called followed by
 *   ADC reconfiguration (ADC resolution had been changed) and finally another
 *   opening of the ADC object shall be called.
 * How to test this code example?
 *   - Toggle breakpoint on the line where `analog_in_close` functionality is called;
 *   - Run debug mode by pressing F9 on your keyboard;
 *   - Continue debug (F6 on your keyboard) to get to the line;
 *   - Insert `adc` analog input driver context structure in Debug Watch window;
 *   - Step Over (F8 on your keyboard) to see the de-allocation and allocation in action.
 * Library dependencies?
 *   - Make sure `Driver.ADC`, `Driver.GPIO.Port` and `Board` libraries are enabled in NECTO's
 *     Library Manager to ensure a successful build.
 */

// ------------------------------------------------------------------ INCLUDES
/**
 * Any initialization code needed for MCU to function properly.
 * Do not remove this line or clock might not be set correctly.
 */
#ifdef PREINIT_SUPPORTED
#include "preinit.h"
#endif

#include "drv_analog_in.h"  // API for Analog input driver.
#include "board.h"          // Main board pin mapping.

// -------------------------------------------------------------------- MACROS
// Voltage reference value.
#define ADC_VREF_VALUE 3.3f // It can be changed in CODEGRIP Suite: POWER -> Outputs -> Voltage Reference -> Set Voltage.

// ------------------------------------------------------------------ VARIABLES
analog_in_t adc;            // Analog input driver context structure.
analog_in_config_t adc_cfg; // Analog input configuration structure.

uint16_t read_data_buffer;  // Analog input data buffer.
float read_data_buffer_voltage; // Analog input data buffer (voltage).

int main(void)
{
    /* Do not remove this line or clock might not be set correctly. */
    #ifdef PREINIT_SUPPORTED
    preinit();
    #endif

    // Initialize ADC configuration structure to default values
    analog_in_configure_default(&adc_cfg);

    // Set the correct input pin here (analog pin on mikroBUS 1 slot is currently set)
    adc_cfg.input_pin = MIKROBUS_1_AN; // example: `PA0` or `MIKROBUS_4_AN` or `HAL_PIN_NC` (`Pin not connected` state)

    // Set the correct ADC resolution
    adc_cfg.resolution = ANALOG_IN_RESOLUTION_12_BIT;

    // Set the desired reference voltage
    adc_cfg.vref_value = ADC_VREF_VALUE;

    // Initialize the ADC
    if( ACQUIRE_FAIL == analog_in_open( &adc, &adc_cfg ) ) {
        // Error handling
    }

    // Configure ADC resolution
    if( ADC_ERROR == analog_in_set_resolution( &adc, adc_cfg.resolution ) ) {
        // Error handling
    }

    // Configure ADC voltage reference input source
    if( ADC_ERROR == analog_in_set_vref_input( &adc, ANALOG_IN_VREF_EXTERNAL ) ) {
        // Error handling
    }

    // Configure ADC voltage reference value
    if( ADC_ERROR == analog_in_set_vref_value( &adc, ADC_VREF_VALUE ) ) {
        // Error handling
    }

    // Finally, read the analog value (which depends on ADC resolution)
    if( ADC_ERROR == analog_in_read( &adc, &read_data_buffer ) ) {
        // Error handling
    }

    Delay_ms(500); // Delay for 500 milliseconds

    // You are able to read the actual voltage on the pin as well
    if( ADC_ERROR == analog_in_read_voltage( &adc, &read_data_buffer_voltage ) ) {
        // Error handling
    }

    // Close ADC driver context object
    if ( ADC_SUCCESS == analog_in_close( &adc ) ) // De-allocates hardware resources for the driver object
    {
        // No error
    } else {
        // Handle the error
    }

    // Reconfigure some of the ADC parameters (ADC resolution for example)
    adc_cfg.resolution = ANALOG_IN_RESOLUTION_10_BIT;

    // Now perform `analog_in_open` to allocate newly configured hardware resources
    if( ACQUIRE_FAIL == analog_in_open( &adc, &adc_cfg ) ) {
        // Error handling
    }

    return 0;
}
  • How do I configure the ADC to use a specific input pin?
/* Project name:
 *   How do I configure the ADC to use a specific input pin?
 * Copyright:
 *   (c) MIKROE, 2024.
 * Description:
 *   This example demonstrates the functionality of ADC (Analog-to-Digital Converter)
 *   using mikroSDK v2.0.
 *   ADC specific input pin shall be configured and ADC shall be initialized.
 * Library dependencies?
 *   - Make sure `Driver.ADC`, `Driver.GPIO.Port` and `Board` libraries are enabled in NECTO's
 *     Library Manager to ensure a successful build.
 */

// ------------------------------------------------------------------ INCLUDES
/**
 * Any initialization code needed for MCU to function properly.
 * Do not remove this line or clock might not be set correctly.
 */
#ifdef PREINIT_SUPPORTED
#include "preinit.h"
#endif

#include "drv_analog_in.h"  // API for Analog input driver.
#include "board.h"          // Main board pin mapping.

// ------------------------------------------------------------------ VARIABLES
analog_in_t adc;            // Analog input driver context structure.
analog_in_config_t adc_cfg; // Analog input configuration structure.

int main(void)
{
    /* Do not remove this line or clock might not be set correctly. */
    #ifdef PREINIT_SUPPORTED
    preinit();
    #endif

    // Initialize ADC configuration structure to default values
    analog_in_configure_default(&adc_cfg);

    // Set the specific input pin here (analog pin on mikroBUS 1 slot is currently set)
    adc_cfg.input_pin = MIKROBUS_1_AN; // example: `PA0` or `MIKROBUS_4_AN` or `HAL_PIN_NC` (`Pin not connected` state)

    // Initialize the ADC
    if( ACQUIRE_FAIL == analog_in_open( &adc, &adc_cfg ) ) {
        // Error handling
    }

    return 0;
}
  • How can I check if the ADC driver is properly initialized?
/* Project name:
 *   How can I check if the ADC driver is properly initialized?
 * Copyright:
 *   (c) MIKROE, 2024.
 * Description:
 *   This example demonstrates the functionality of ADC (Analog-to-Digital Converter)
 *   using mikroSDK v2.0.
 *   ADC specific input pin shall be configured and ADC shall be initialized.
 * How to test this code example?
 *   - Run debug mode by pressing F9 on your keyboard;
 *   - Continue debug (F6 on your keyboard);
 *   - Monitor the `Application Output` in the lower part of the NECTO's environment
 *     to check proper initialization of an ADC driver.
 * Library dependencies?
 *   - Make sure `Driver.ADC`, `Log`, `Driver.GPIO.Port` and `Board` libraries are enabled in NECTO's
 *     Library Manager to ensure a successful build.
 */

// ------------------------------------------------------------------ INCLUDES
/**
 * Any initialization code needed for MCU to function properly.
 * Do not remove this line or clock might not be set correctly.
 */
#ifdef PREINIT_SUPPORTED
#include "preinit.h"
#endif

#include "drv_analog_in.h"  // API for Analog input driver.
#include "board.h"          // Main board pin mapping.
#include "log.h"            // API for Logger driver.

// -------------------------------------------------------------------- MACROS
// Voltage reference value.
#define ADC_VREF_VALUE 3.3f // It can be changed in CODEGRIP Suite: POWER -> Outputs -> Voltage Reference -> Set Voltage.

// ------------------------------------------------------------------ VARIABLES
analog_in_t adc;            // Analog input driver context structure.
analog_in_config_t adc_cfg; // Analog input configuration structure.

static log_t logger;        // Logger driver object.

uint16_t read_data_buffer;  // Analog input data buffer.
float read_data_buffer_voltage; // Analog input data buffer (voltage).

int main(void)
{
    /* Do not remove this line or clock might not be set correctly. */
    #ifdef PREINIT_SUPPORTED
    preinit();
    #endif

    log_cfg_t log_cfg;              // Logger configuration structure instance.
    log_init( &logger, &log_cfg );  // Initialize Logger.
    log_printf( &logger, "Logger initialized.\n" );

    // Initialize ADC configuration structure to default values
    analog_in_configure_default(&adc_cfg);
    log_printf( &logger, "Configured default ADC settings.\n" );

    // Set the correct input pin here (analog pin on mikroBUS 1 slot is currently set)
    adc_cfg.input_pin = MIKROBUS_1_AN; // example: `PA0` or `MIKROBUS_4_AN` or `HAL_PIN_NC` (`Pin not connected` state)
    log_printf( &logger, "Selected `MIKROBUS_1_AN` as an ADC pin.\n" );

    // Set the correct ADC resolution
    adc_cfg.resolution = ANALOG_IN_RESOLUTION_12_BIT;
    log_printf( &logger, "Selected 12-bit ADC resolution.\n" );

    // Set the desired reference voltage
    adc_cfg.vref_value = ADC_VREF_VALUE;
    log_printf( &logger, "Selected 3.3 volts as a reference voltage.\n" );

    // Initialize the ADC
    if ( ADC_ERROR == analog_in_open( &adc, &adc_cfg ) ) {
        log_printf( &logger, "ADC open failed.\n" );
    } else {
        log_printf( &logger, "ADC open successful.\n" );
    }

    return 0;
}
  • How do I reset the ADC to its default configuration?
analog_in_configure_default(&adc_cfg);
  • How do I check if the ADC input pin is set?
if (adc_cfg.input_pin != HAL_PIN_NC) {
    // Input pin is set
} else {
    // Input pin is NOT set
}