Skip to content

Reading analog input

Analog input tutorial will guide you through initializing and working with the ADC module inside your MCU using mikroSDK V2.
This tutorial includes the port driver, so it is recommended that you have already familiarized yourself with it, in the Digital Input/Output tutorial.

In this example, we will read the analog value from a pin via the ADC module and display that value on a port.

To begin, we need to include the analog_in and port drivers:

#include "drv_analog_in.h"
#include "drv_port.h"

As well as define context structures and variables we will use later:

analog_in_t         analog_in;      // Analog input driver context structure.
analog_in_config_t  analog_in_cfg;  // ADC init configuration structure.
uint16_t          * read_value;     // Variable for read ADC value.

static port_t       led_port;       // Port driver context structure.
uint16_t mask   =   0xFFFF;         /* Mask for setting the entire port as
                                       digital output */

Remember the naming convention for context structures - <module_name>_t.
In case of the analog input driver (and most other drivers), we will also need to define a configuration structure.

Configuration structures

Module configuration structures are used for configuring the initial state of modules.
Usually they also contain pin names required for the driver, in addition to other configuration data.
The naming convention for configuration structures is <module_name>_cfg_t.

In order to store the read ADC data, we declared a variable called read_value.

We move next onto the application_init(), where we initialize configuration structure and the module itself.

void application_init ( void )
{
    port_init( &led_port, PORT_D, mask, PIN_DIRECTION_DIGITAL_OUTPUT ); /* Initialize
    PORT_D as output. */
    analog_in_configure_default( &analog_in_cfg ); /* Initialize analog input
    configuration structure to default values. */
    analog_in_cfg.input_pin = PA3;               // Set AN pin.
    analog_in_cfg.channel = ADC_CHANNEL_0;       // Initialize ADC channel.
    analog_in_set_vref_value( &analog_in, 3.3);  // Set 12-bit ADC resolution.
    analog_in_set_resolution( &analog_in,
    ANALOG_IN_RESOLUTION_10_BIT);                // Set Vref
}

Let us break down the above code.
Firstly, we initialized PORTD as digital output, using the port_init() function, as this is where we will display the read ADC value:

port_init( &led_port, PORT_D, mask, PIN_DIRECTION_DIGITAL_OUTPUT  ); /* Initialize
PORT_D as output. */

Moving on, we initialized the analog_in configuration structure. Like always, we have to specify the configuration structure we created earlier.

analog_in_configure_default( &analog_in_cfg ); /* Initialize analog input
configuration structure to default values. */

Other thing that analog_in driver needs in the configuration structure is the analog pin. This is where you should consult the datasheet of your MCU to see the available analog pins and which ADC channel they are multiplexed to. Setting other parameters such as ADC resolution and VREF are optional.

analog_in_cfg.pin = PA3;  // Set AN pin.

We can then call the analog_in_open() function:

analog_in_open( &analog_in, &analog_in_cfg );

Finally, we can move onto the application_task(), which will read the ADC value and display it on our output port.

void application_task ( void )
{
    analog_in_read( &analog_in, &read_value ); // Read ADC value.
    port_write( &led_port, read_value );       // Write read value to GPIO port.
}

Using the analog_in_read() function, we can get the ADC value and store it in the variable read_value we created earlier and then, using the port_write() function, write that value to our output port.

Below is the entire code:

#include "drv_analog_in.h"
#include "drv_port.h"

analog_in_t         analog_in;    
analog_in_config_t  analog_in_cfg;
uint16_t          * read_value;   

static port_t       led_port;     
uint16_t mask   =   0xFFFF;                                 

void application_init ( void )
{
    port_init( &led_port, PORT_D, mask, PIN_DIRECTION_DIGITAL_OUTPUT  );

    analog_in_configure_default( &analog_in_cfg );
    analog_in_cfg.pin = PA3;

    analog_in_open( &analog_in, &analog_in_cfg );
}

void application_task ( void )
{
    analog_in_read( &analog_in, &read_value );
    port_write( &led_port, read_value );  
}

void main ( void )
{

    application_init( );

    for ( ; ; )
    {
        application_task( );
    }
}