Analog to Digital Converter (ADC) Interface

Some USB-GPIO Interface Adapters have several analog inputs connected to an Analog to Digital Converter (ADC). Analog to digital conversion has many applications. You can acquire data from various analog sensors and save it to your PC for further processing. You can also implement a real time analog data processing in your software. Combined with other USB-GPIO adapter modules you can implement feedback control over your hardware. Analog to digital conversion is well utilized for external analog signal reading such as current, voltage, temperature, distance, pressure, or even color information.

Analog to Digital Converter inputs can generate events. If you adjust low or high threshold value, the adapter will send events once this threshold is crossed.

DLN-4M and DLN-4S each have 2 ADC modules. The first one has 4 10-bit channels and the second one has 4 12-bit channels. DLN-4M or DLN-4S USB-GPIO adapter can be used to measure voltage from 0 V to VDD (positive supply voltage). Since VDD can be configured as 3.3 V, the ADC can be used to measure voltage from 0 V to 3.3 V. You can use either VDD or external supply as the reference voltage.

Rating: 
Average: 3.3 (4 votes)

Simple ADC Module Example

This example shows how to enable ADC module, setup its resolution and measure voltage from the ADC channel. You can find the complete example in the “..\Program Files\Diolan\DLN\ examples\c_cpp\examples\simple” folder after DLN setup package installation.

C/C++
#include "..\..\..\common\dln_generic.h"
#include "..\..\..\common\dln_adc.h"
#pragma comment(lib, "..\\..\\..\\bin\\dln.lib")

int _tmain(int argc, _TCHAR* argv[])
{
	// Open device
	HDLN device;
	DlnOpenUsbDevice(&device);

	// Set ADC resolution
	DlnAdcSetResolution(device, 0, DLN_ADC_RESOLUTION_10BIT);
	// Enable ADC channel 0
	DlnAdcChannelEnable(device, 0, 0);
	// Enable ADC port 0
	uint16_t conflict;
	DlnAdcEnable(device, 0, &conflict);

	// Read ADC value
	uint16_t value;
	DlnAdcGetValue(device, 0, 0, &value);
	printf("ADC value = %d\n", value);

	// Disable ADC
	DlnAdcDisable(device, 0);
	DlnAdcChannelDisable(device, 0, 0);
	// Close device
	DlnCloseHandle(device);
	return 0;
}

  • Line 1:#include "..\..\..\common\dln_generic.h"

    The dln_generic..h header file declares functions and data structures for the generic interface. In current example this header is used to call DlnOpenUsbDevice() and DlnCloseHandle() functions.

  • Line 2:#include "..\..\..\common\dln_adc.h"

    The dln_uart.h header file declares functions and data structure specific for ADC interface. By including this header file you are able to call DlnAdcSetResolution(), DlnAdcChannelEnable(), DlnAdcEnable(), DlnAdcGetValue(), DlnAdcDisable(), DlnAdcChannelDisable().

  • Line 3:#pragma comment(lib, "..\\..\\..\\bin\\dln.lib")

    Use dln.lib library while project linking.

  • Line 9:DlnOpenUsbDevice(&device);

    The function establishes the connection with the DLN adapter. This application uses the USB connectivity of the adapter. For additional options, refer to the Device Opening & Identification section.

  • Line 12:DlnAdcSetResolution(device, 0, DLN_ADC_RESOLUTION_10BIT);

    This functions sets ADC resolution to 10 bit.

  • Line 14:DlnAdcChannelEnable(device, 0, 0);

    This function enable ADC channel.

  • Line 17:DlnAdcEnable(device, 0, &conflict);

    This function enables ADC port 0.

  • Line 21:DlnAdcGetValue(device, 0, 0, &value);

    This function gets ADC value of port 0 channel 0.

  • Line 22:printf("ADC value = %d\n", value);

    Print retrieved ADC value to console.

  • Line 25:DlnAdcDisable(device, 0);

    This function disables ADC port 0.

  • Line 26:DlnAdcChannelDisable(device, 0, 0);

    This function disables ADC channel 0 of port 0.

  • Line 28:DlnCloseHandle(device);

    The application closes handle to the DLN adapter.

Rating: 
No votes yet

ADC Functions

This section describes the ADC functions. They are used to control and monitor the ADC module of a DLN-series adapter.Actual control of the device is performed by use of commands and responses. Each function utilizes respective commands and responses. You can send such commands directly if necessary.

Rating: 
No votes yet

DlnAdcEnable() Function

The DlnAdcEnable() function activates the corresponding ADC port of your DLN-series adapter. This function is defined in the dln_adc.h file.

Syntax
C/C++
DLN_RESULT DlnAdcEnable(
    HDLN handle, 
    uint8_t port,
    uint16_t* conflict
);
Parameters
handle

A handle to the DLN-series adapter.

port

A number of the ADC port to be activated.

conflict

A pointer to an unsigned 16-bit integer. The integer will be filled with the number of the conflicted pin, if any.A conflict arises if a pin is already assigned to another module of the DLN-series adapter and cannot be used for the ADC module. To fix this a user has to disconnect a pin from a module that it has been assigned to and call the DlnAdcEnable() function once again. In case there still are conflicted pins, only the number of the next one will be returned.

Return Value
DLN_RES_SUCCESS

The operation completed successfully and I2C master port was successfully disabled.

DlnAdcDisable() Function

The DlnAdcDisable() function deactivates the specified ADC port of your DLN-series adapter. This function is defined in the dln_adc.h file.

Syntax
C/C++
DLN_RESULT DlnAdcDisable(
    HDLN handle,
    uint8_t port
);
Parameters
handle

A handle to the DLN-series adapter.

port

A number of the ADC port to be deactivated.

Return Value
DLN_RES_SUCCESS

Function was successfully executed.

DlnAdcIsEnabled() Function

The DlnAdcIsEnabled() function retrieve information, whether the specified ADC port is activated. This function is defined in the dln_adc.h file.

Syntax
C/C++
DLN_RESULT DlnAdcIsEnabled(
    HDLN handle,
    uint8_t port,
    uint8_t* enabled
);
Parameters
handle

A handle to the DLN-series adapter.

port

A number of the ADC port to retrieve the information from.

enabled

A pointer to an unsigned 8-bit integer. The integer will be filled with the information whether the specified ADC port is activated. There are two possible values:

  • 0 or DLN_ADC_DISABLED - ADC port is deactivated.

  • 1 or DLN_ADC_ENABLED - ADC port is activated.

Return Value
DLN_RES_SUCCESS

Function was successfully executed.

DlnAdcChannelEnable() Function

The DlnAdcChannelEnable() function activates the specified channel from the corresponding ADC port of your DLN-series adapter. This function is defined in the dln_adc.h file.

Syntax
C/C++
DLN_RESULT DlnAdcChannelEnable(
    HDLN handle,
    uint8_t port,
    uint8_t channel
);
Parameters
handle

A handle to the DLN-series adapter.

port

A number of the ADC port to be used.

channel

A number of the channel to be enabled.

Return Value
DLN_RES_SUCCESS

Function was successfully executed.

DlnAdcChannelDisable() Function

The DlnAdcChannelEnable() function deactivates the specified channel from the corresponding ADC port of your DLN-series adapter.

This function is defined in the dln_adc.h file.

Syntax
C/C++
DLN_RESULT DlnAdcChannelDisable(
    HDLN handle,
    uint8_t port,
    uint8_t channel
);
Parameters
handle

A handle to the DLN-series adapter.

port

A number of the ADC port to be used.

channel

A number of the channel to be disabled.

Return Value
DLN_RES_SUCCESS

Function was successfully executed.

DlnAdcChannelIsEnabled() Function

The DlnAdcChannelIsEnabled() retrieves information, whether the specified ADC channel is activated. This function is defined in the dln_adc.h file.

Syntax
C/C++
DLN_RESULT DlnAdcChannelIsEnabled(
   HDLN handle, 
   uint8_t port,
   uint8_t channel,
   uint8_t* enabled
);
Parameters
handle

A handle to the DLN-series adapter.

port

A number of the ADC port to retrieve the information from.

channel

A number of the ADC channel to retrieve the information from.

enabled

A pointer to an unsigned 8-bit integer. The integer will be filled with the information whether the specified ADC channel is activated. There are two possible values:

  • 0 or DLN_ADC_DISABLED - ADC channel is deactivated.

  • 1 or DLN_ADC_ENABLED - ADC channel is activated.

Return Value
DLN_RES_SUCCESS

Function was successfully executed.

DlnAdcGetPortCount() Function

The DlnAdcGetPortCount() function retrieves the number of ADC ports available in your DLN-series adapter. This function is defined in the dln_adc.h file.

Syntax
C/C++
DLN_RESULT DlnAdcGetPortCount(
    HDLN handle,
    uint8_t* count
);
Parameters
handle

A handle to the DLN-series adapter.

count

A pointer to an unsigned 8-bit integer. The integer will be filled with the number of available ADC ports after the function execution.

Return Value
DLN_RES_SUCCESS

Function was successfully executed.

DlnAdcGetChannelCount() Function

The DlnAdcGetChannelCount() function retrieves the number of ADC channels, available in the specified ADC-port of your DLN-series adapter.This function is defined in the dln_adc.h file.

Syntax
C/C++
DLN_RESULT DlnAdcGetChannelCount(
    HDLN handle,
    uint8_t port,
    uint8_t* count
);
Parameters
handle

A handle to the DLN-series adapter.

port

An ADC port to retrieve the number of channels from.

count

A pointer to an unsigned 8-bit integer. The integer will be filled with the available number of channels in the specified ADC port of the DLN-series adapter.

Return Value
DLN_RES_SUCCESS

Function was successfully executed.

DlnAdcChannelSetCfg() Function

The DlnAdcChannelSetCfg() function changes the configuration of a single GPIO pin and set the conditions of DLN_ADC_CONDITION_MET_EV event generation. This function is defined in the dln_adc.h file.

Syntax
C/C++
DLN_RESULT DlnAdcChannelSetCfg(
   HDLN handle, 
   uint8_t port,
   uint8_t channel,
   uint8_t eventType,
   uint16_t eventPeriod,
   uint16_t thresholdLow,
   uint16_t thresholdHigh
);
Parameters
handle

A handle to the DLN-series adapter.

port

A number of the ADC port to be configured.

channel

A number of the ADC channel to be configured.

eventType

Must contain the event generation condition for the ADC channel. The following values are available:

ValueDescription

0 or DLN_ADC_EVENT_NONE

No events are generated for the current channel.

1 or DLN_ADC_EVENT_BELOW

Events are generated when voltage level on the ADC channel crosses the lower threshold.

2 or DLN_ADC_EVENT_LEVEL_ABOVE

Events are generated when voltage level on the ADC channel crosses the higher threshold.

3 or DLN_ADC_EVENT_OUTSIDE

Events are generated when voltage level on the ADC channel falls outside of the specified range between thresholds.

4 or DLN_ADC_EVENT_INSIDE

Events are generated when voltage level on the ADC channel falls within the specified range between thresholds.

5 or DLN_ADC_EVENT_ALWAYS

Events are sent periodically with predefined repeat interval. The non-zero interval must be specified for this event type.

eventPeriod

Must contain the repeat interval for DLN_ADC_CONDITION_MET_EV event generation on the pin. The repeat interval is set in ms (1 to 65,535ms). If the repeat interval is set to 0, the DLN-series adapter will send a single event when the level on the line changes to meet the specified conditions.

thresholdLow

The lower threshold value, specified in bits.

thresholdHigh

The higher threshold value specified in bits.

Return Value
DLN_RES_SUCCESS

Function was successfully executed.

DlnAdcChannelGetCfg() Function

The DlnAdcChannelGetCfg() function retrieves the current configuration settings of a single ADC channel.

This function is defined in the dln_adc.h file.

Syntax
C/C++
DLN_RESULT DlnAdcChannelGetCfg(
    HDLN handle,
    uint8_t port, 
    uint8_t channel,
    uint8_t* eventType,
    uint16_t* eventPeriod,
    uint16_t* thresholdLow,
    uint16_t* thresholdHigh
 );
Parameters
handle

A handle to the DLN-series adapter.

port

A number of the ADC port to retrieve the information from.

channel

A number of the ADC channel to retrieve the information from.

eventType

A pointer to an unsigned 8-bit integer. The integer will be filled the currently set event generation condition for the ADC channel after the function execution. The following values are available:

ValueDescription

0 or DLN_ADC_EVENT_NONE

No events are generated for the current channel.

1 or DLN_ADC_EVENT_BELOW

Events are generated when voltage level on the ADC channel crosses the lower threshold.

2 or DLN_ADC_EVENT_LEVEL_ABOVE

Events are generated when voltage level on the ADC channel crosses the higher threshold.

3 or DLN_ADC_EVENT_OUTSIDE

Events are generated when voltage level on the ADC channel falls outside of the specified range between thresholds.

4 or DLN_ADC_EVENT_INSIDE

Events are generated when voltage level on the ADC channel falls within the specified range between thresholds.

5 or DLN_ADC_EVENT_ALWAYS

Events are sent periodically with predefined repeat interval. The non-zero interval must be specified for this event type.

eventPeriod

A pointer to an unsigned 16-bit integer. The integer will be filled with the repeat interval for DLN_ADC_CONDITION_MET_EV event generation on the pin after the function execution. The repeat interval is set in ms (1 to 65,535ms). If the repeat interval is set to 0, the DLN-series adapter will send a single event when the level on the line changes to meet the specified conditions.

thresholdLow

A pointer to an unsigned 16-bit integer. The integer will be filled with the lower voltage threshold value, specified in bits, after the function execution.

thresholdHigh

A pointer to an unsigned 16-bit integer. The integer will be filled with the higher voltage threshold value, specified in bits, after the function execution.

Return Value
DLN_RES_SUCCESS

Function was successfully executed.

DlnAdcSetResolution() Function

The DlnAdcSetResolution() function sets the ADC resolution value of your DLN-series adapter. This function is defined in the dln_adc.h file.

Syntax
C/C++
DLN_RESULT DlnAdcSetResolution(
    HDLN handle,
    uint8_t port,
    uint8_t resolution
 );
Parameters
handle

A handle to the DLN-series adapter.

port

A number of the ADC port to be configured.

resolution

The new resolution in bits. For the list of compatible values see table below.

Return Value
DLN_RES_SUCCESS

Function was successfully executed.

DlnAdcGetResolution() Function

The DlnAdcGetResolution() function retrieves the currently set ADC resolution of your DLN-series adapter.

This function is defined in the dln_adc.h file.

Syntax
C/C++
 DLN_RESULT DlnAdcGetResolution(
   HDLN handle,
   uint8_t port,
   uint8_t* resolution
);
Parameters
handle

A handle to the DLN-series adapter.

port

A number of the ADC port to retrieve the information from.

resolution

The current ADC resolution in bits.

Return Value
DLN_RES_SUCCESS

Function was successfully executed.

DlnAdcGetSupportedResolutions() Function

The DlnAdcGetSupportedResolutions() function returns all supported resolution values for connected DLN-series adapter.

This function is defined in the dln_adc.h file.

Syntax
C/C++
DLN_RESULT DlnAdcGetSupportedResolutions(
    HDLN handle, 
    uint8_t port, 
    DLN_ADC_RESOLUTIONS *supportedResolutions
);
Parameters
handle

A handle to the DLN-series adapter.

port

A number of the ADC port to be used.

supportedResolutions

Pointer to DLN_ADC_RESOLUTIONS structure, which will be filled by supported resolution values.

Return Value
DLN_RES_SUCCESS

Function was successfully executed.

DlnAdcGetValue() Function

The DlnAdcGetValue() function retrieves current voltage on the specified ADC channel of your DLN-series adapter.

This function is defined in the dln_adc.h file.

Syntax
C/C++
DLN_RESULT DlnAdcGetValue(
    HDLN handle,
    uint8_t port,
    uint8_t channel,
    uint16_t* value
);
Parameters
handle

A handle to the DLN-series adapter.

port

A number of the ADC port to retrieve the information from.

channel

A number of the ADC channel to retrieve the information from.

value

A pointer to an unsigned 8-bit integer. The integer will be filled with the voltage level on the specified ADC channel.

Return Value
DLN_RES_SUCCESS

Function was successfully executed.

DlnAdcGetAllValues() Function

The DlnAdcGetAllValues() function retrieves current voltage values from all enabled ADC channels of your DLN-series adapter.

This function is defined in the dln_adc.h file.

Syntax
C/C++
DLN_RESULT DlnAdcGetAllValues( 
   HDLN handle,
   uint8_t port,
   uint16_t* channelMask,
   uint16_t* values
);
Parameters
handle

A handle to the DLN-series adapter.

port

A number of the ADC port to retrieve the information from.

channelMask

A pointer to an unsigned 16-bit integer. This integer will be filled with a bit mask, each of the bits corresponding to an ADC channel of the port. This parameter contains currently enabled ADC channels of your DLN-series device.

values

A pointer to an unsigned 16-bit integer. This integer will be filled with a bit mask, each of the bits corresponding to an ADC value of the ADC channel of the port.

Return Value
DLN_RES_SUCCESS

Function was successfully executed.

DlnAdcGetSupportedEventTypes() Function

The DlnAdcGetSupportedEventTypes() function returns all supported ADC event types for opened DLN-series adapter.

This function is defined in the dln_adc.h file.

Syntax
C/C++
DLN_RESULT DlnAdcGetSupportedEventTypes(
    HDLN handle, 
    uint8_t port, 
    DLN_ADC_EVENT_TYPES *supportedEventTypes
);
Parameters
handle

A handle to the DLN-series adapter.

port

A number of the ADC port to be used.

supportedEventTypes

Pointer to DLN_ADC_EVENT_TYPES structure, which will be filled by supported event type values.

Return Value
DLN_RES_SUCCESS

Function was successfully executed.