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.
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.
#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.
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.
The DlnAdcEnable()
function activates the corresponding ADC port of your DLN-series adapter. This function is defined in the dln_adc.h
file.
The DlnAdcDisable()
function deactivates the specified ADC port of your DLN-series adapter. This function is defined in the dln_adc.h
file.
The DlnAdcIsEnabled()
function retrieve information, whether the specified ADC port is activated. This function is defined in the dln_adc.h
file.
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.
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.
The DlnAdcChannelIsEnabled()
retrieves information, whether the specified ADC channel is activated. This function is defined in the dln_adc.h
file.
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.
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.
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.
The DlnAdcChannelGetCfg()
function retrieves the current configuration settings of a single ADC channel.
This function is defined in the dln_adc.h
file.
The DlnAdcSetResolution()
function sets the ADC resolution value of your DLN-series adapter. This function is defined in the dln_adc.h
file.
The DlnAdcGetResolution()
function retrieves the currently set ADC resolution of your DLN-series adapter.
This function is defined in the dln_adc.h
file.
The DlnAdcGetSupportedResolutions()
function returns all supported resolution values for connected DLN-series adapter.
This function is defined in the dln_adc.h
file.
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.
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.
The DlnAdcGetSupportedEventTypes()
function returns all supported ADC event types for opened DLN-series adapter.
This function is defined in the dln_adc.h
file.