mikroSDK Reference Manual

ADC Driver API Reference. More...

Functions list

void analog_in_configure_default (analog_in_config_t *config)
 Configure ADC configuration structure. More...
 
err_t analog_in_open (analog_in_t *obj, analog_in_config_t *config)
 Open the ADC driver object on selected pin. More...
 
err_t analog_in_set_resolution (analog_in_t *obj, analog_in_resolution_t resolution)
 Set ADC driver sample resolution. More...
 
err_t analog_in_set_vref_input (analog_in_t *obj, analog_in_vref_t vref)
 Set ADC driver voltage reference source. More...
 
err_t analog_in_set_vref_value (analog_in_t *obj, float vref_value)
 Set ADC driver voltage reference value. More...
 
err_t analog_in_read (analog_in_t *obj, uint16_t *readDatabuf)
 Read analog value from pin. More...
 
err_t analog_in_read_voltage (analog_in_t *obj, float *readDatabuf)
 Read analog voltage value from pin. More...
 
void analog_in_close (analog_in_t *obj)
 Close ADC driver context object. More...
 

Detailed Description

The ADC driver provides standard ADC functionality including setting the analog pin and reading the ADC value from it. It also allows configuration of ADC resolution, voltage reference source and value.

Function Documentation

◆ analog_in_configure_default()

void analog_in_configure_default ( analog_in_config_t config)

Configures ADC configuration structure to default initialization values. Take into consideration that this is just structure variable initial values setting. Values need to be redefined by user.

Parameters
[in,out]configADC driver configuration settings. See analog_in_config_t structure definition for detailed explanation.

Default values:

Function Default value
Input pin 0xFFFFFFFF (invalid pin)
Resolution 12 bit
Vref input External reference voltage
Vref value -1 (invalid reference voltage value)
Returns
Nothing.

Example

// Analog input driver configuration structure.
analog_in_config_t analog_in_cfg;
// Fill structure with default values.
analog_in_configure_default( &analog_in_cfg );

◆ analog_in_open()

err_t analog_in_open ( analog_in_t obj,
analog_in_config_t config 
)

Opens the ADC driver object on selected pins. Allocates memory and pin for specified object.

Parameters
[in,out]objADC driver object. See analog_in_t structure definition for detailed explanation.
[in]configADC driver configuration settings. See analog_in_config_t structure definition for detailed explanation.
Returns
The function can return one of the values defined by analog_in_err_t, which is size dependant on the architecture.
Precondition
Make sure that config structure has been adequately populated beforehand. See analog_in_configure_default definition for detailed explanation.
Note
It is recommended to check return value for error.

Example

// Analog input driver context structure.
analog_in_t analog_in;
// Analog input driver configuration structure.
analog_in_config_t analog_in_cfg;
// Fill structure with default values.
analog_in_configure_default( &analog_in_cfg );
// Fill analog input pin.
analog_in_cfg.pin = MIKROBUS_1_AN;
// Open the driver on the selected pin.
analog_in_open( &analog_in, &analog_in_cfg );

◆ analog_in_set_resolution()

err_t analog_in_set_resolution ( analog_in_t obj,
analog_in_resolution_t  resolution 
)

Sets ADC driver resolution to passed value if possible. If not possible, returns error. Make sure to check for return value. Take into consideration that the driver will be re-initialized on the hardware level.

Parameters
[in]objADC driver context object.
[in]resolutionADC driver sample resolution value. See analog_in_resolution_t for valid values.
Returns
The function can return one of the values defined by analog_in_err_t, which is size dependant on the architecture.
Precondition
Before calling this function, the user is expected to call analog_in_open
Postcondition
This function sets ADC resolution.
Note
Driver will be re-initialized on the hardware level

Example

// Analog input driver context structure.
analog_in_t analog_in;
// Analog input driver configuration structure.
analog_in_config_t analog_in_cfg;
// Fill sample resolution.
// Set sample resolution.
analog_in_set_resolution( &analog_in, analog_in_cfg.resolution );

◆ analog_in_set_vref_input()

err_t analog_in_set_vref_input ( analog_in_t obj,
analog_in_vref_t  vref 
)

Sets ADC driver voltage reference source to passed value if possible. If not possible, returns error. Make sure to check for return value. Take into consideration that the driver will be re-initialized on the hardware level.

Parameters
[in]objADC driver context object.
[in]vrefADC driver voltage reference source value. See analog_in_vref_t for valid values.
Returns
The function can return one of the values defined by analog_in_err_t, which is size dependant on the architecture.
Precondition
Before calling this function, the user is expected to call analog_in_open
Postcondition
This function sets ADC voltage reference source.
Note
Driver will be re-initialized on the hardware level

Example

// Analog input driver context structure.
analog_in_t analog_in;
// Analog input driver configuration structure.
analog_in_config_t analog_in_cfg;
// Fill voltage reference source.
// Set voltage reference source.
analog_in_set_vref_input( &analog_in, analog_in_cfg.vref );

◆ analog_in_set_vref_value()

err_t analog_in_set_vref_value ( analog_in_t obj,
float  vref_value 
)

Sets ADC driver voltage reference value to passed value if possible. If not possible, returns error. Make sure to check for return value. Take into consideration that the driver will be re-initialized on the hardware level.

Parameters
[in]objADC driver context object.
[in]vref_valueADC driver voltage reference value.
Returns
The function can return one of the values defined by analog_in_err_t, which is size dependant on the architecture.
Precondition
Before calling this function, the user is expected to call analog_in_open
Postcondition
This function sets ADC voltage reference value neccessary for voltage calculation.
Note
Setting wrong voltage reference value will result with bad voltage calculation.

Example

// Analog input driver context structure.
analog_in_t analog_in;
// Analog input driver configuration structure.
analog_in_config_t analog_in_cfg;
// Fill voltage reference value.
analog_in_cfg.vref_value = 2.048;
// Set voltage reference value.
analog_in_set_vref_value( &analog_in, analog_in_cfg.vref_value );

◆ analog_in_read()

err_t analog_in_read ( analog_in_t obj,
uint16_t *  readDatabuf 
)

Function perform a read on the configured analog pin.

Parameters
[in]objAnalog input driver object. See analog_in_t structure definition for detailed explanation.
[out]readDatabufData buffer to place read data in.
Returns
The function can return one of the values defined by analog_in_err_t, which is size dependant on the architecture.
Note
The ADC driver needs to be initialized so that the read can be done.

Example

// Analog input driver context structure.
analog_in_t analog_in;
// ADC data buffer.
float *readDatabuf;
// Read analog value and store it to buffer.
analog_in_read( &analog_in, &readDatabuf );

◆ analog_in_read_voltage()

err_t analog_in_read_voltage ( analog_in_t obj,
float *  readDatabuf 
)

Function perform a read on the configured analog pin and converts it into voltage.

Parameters
[in]objAnalog input driver object. See analog_in_t structure definition for detailed explanation.
[out]readDatabufData buffer to place read data in.
Returns
The function can return one of the values defined by analog_in_err_t, which is size dependant on the architecture.
Note
The ADC driver needs to be initialized so that the read can be done. User should set right reference voltage value in order to get correct voltage value from this function.

Example

// Analog input driver context structure.
analog_in_t analog_in;
// ADC data buffer.
float *readDatabuf;
// Read analog voltage value and store to buffer.
analog_in_read_voltage( &analog_in, &readDatabuf );

◆ analog_in_close()

void analog_in_close ( analog_in_t obj)

De-allocates hardware resources for specific driver object.

Parameters
[in]objADC driver object.
Returns
Nothing.

Example

// Analog input driver context structure.
analog_in_t analog_in;
// Close ADC driver context object.
analog_in_close( &analog_in );
analog_in_config_t
Analog input driver configuration structure.
Definition: drv_analog_in.h:106
analog_in_config_t::resolution
analog_in_resolution_t resolution
Definition: drv_analog_in.h:109
ANALOG_IN_VREF_EXTERNAL
Definition: drv_analog_in.h:68
analog_in_t
Analog input driver context structure, consisted of the following fields :
Definition: drv_analog_in.h:126
analog_in_set_vref_input
err_t analog_in_set_vref_input(analog_in_t *obj, analog_in_vref_t vref)
Set ADC driver voltage reference source.
analog_in_set_vref_value
err_t analog_in_set_vref_value(analog_in_t *obj, float vref_value)
Set ADC driver voltage reference value.
analog_in_config_t::vref_input
analog_in_vref_t vref_input
Definition: drv_analog_in.h:110
analog_in_read
err_t analog_in_read(analog_in_t *obj, uint16_t *readDatabuf)
Read analog value from pin.
analog_in_config_t::vref_value
float vref_value
Definition: drv_analog_in.h:111
analog_in_open
err_t analog_in_open(analog_in_t *obj, analog_in_config_t *config)
Open the ADC driver object on selected pin.
analog_in_read_voltage
err_t analog_in_read_voltage(analog_in_t *obj, float *readDatabuf)
Read analog voltage value from pin.
analog_in_close
void analog_in_close(analog_in_t *obj)
Close ADC driver context object.
analog_in_configure_default
void analog_in_configure_default(analog_in_config_t *config)
Configure ADC configuration structure.
analog_in_set_resolution
err_t analog_in_set_resolution(analog_in_t *obj, analog_in_resolution_t resolution)
Set ADC driver sample resolution.
ANALOG_IN_RESOLUTION_10_BIT
Definition: drv_analog_in.h:80