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..hheader 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.liblibrary 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.