Pulse counter interface allows to count with pulses and set timers.
To start using Pulse counter module you need to enable Pulse counter port. It can be done with the DlnPlsCntEnable() function. Also pulse counter module mode need to be set with DlnPlsCntSetMode() function.
At any moment pulse counter or timer value can be read with function DlnPlsCntGetValue().
You can also enable the events to get alert when condition is met. One of the few conditions can be selected: pulse counter/timer overflow, when it matches selected value or repeat event for selected period. Use DlnPlsCntSetEventCfg() to setup events.
There are 3 available modes: free run mode, time-based mode and pulse-based mode.
In "Free Run Mode" pulses are counted continuously. You can suspend, resume or reset the counter and get the number of pulses at any time.
In "Time-Based Mode" pulses are counted during the user-defined time period. When the predefined time period (limit) is exceeded, the counting starts again from 0. The pulse counter can send a match event to PC if activated. The event contains the number of pulses detected during this period.
In "Pulse-Based Mode" Pulses are counted until the number of pulses reaches the user-defined value (limit). Then the counting starts again from 0. The counter can send an event to PC if activated. The event contains time elapsed from the moment you started the counter.
Pulse counter module mode can be set or changes by calling DlnPlsCntSetMode() function. You can also retrieve the current mode by calling DlnPlsCntGetMode() function.
There are also constants available for each mode in the dln_pls_cnt.h
header file: DLN_PLS_CNT_MODE_FREE_RUN(0)
, DLN_PLS_CNT_MODE_TIME_BASED(1)
, DLN_PLS_CNT_MODE_PULSE_BASED(2)
.
This example shows how to setup pulse counter module, read timer and pulse counter values. 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_pls_cnt.h" #pragma comment(lib, "..\\..\\..\\bin\\dln.lib") int _tmain(int argc, _TCHAR* argv[]) { // Open device HDLN device; DlnOpenUsbDevice(&device); // Set Free Run mode DlnPlsCntSetMode(device, 0, DLN_PLS_CNT_MODE_FREE_RUN, 0); // Enable counter uint16_t conflict; DlnPlsCntEnable(device, 0, &conflict); // Do some delay for (int i = 0; i < 10000; i++); // Read counter values uint32_t timerValue, counterValue; DlnPlsCntGetValue(device, 0, &timerValue, &counterValue); printf("Timer Value = %u, Counter Value = %u\n", timerValue, counterValue); // 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_pls_cnt.h"
The dln_pwm.h header file declares functions and data structures for the PWM interface. In current example this header is used to call DlnPlsCntSetMode(), DlnPlsCntEnable(), DlnPlsCntGetValue() functions.
Line 3:#pragma comment(lib, "..\\..\\..\\bin\\dln.lib")
Use dln.lib
library while project linking.
Line 10: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 13:DlnPlsCntSetMode(device, 0, DLN_PLS_CNT_MODE_FREE_RUN, 0);
The function sets pulse counter mode to “Free run mode”. You can read more about pulse counter modes at Pulse Counter Modes section.
Line 16:DlnPlsCntEnable(device, 0, &conflict);
The function enables pulse counter module.
Line 19:for (int i = 0; i < 10000; i++);
Performing delay for more continuous mode of counter.
Line 23:DlnPlsCntGetValue(device, 0, &timerValue, &counterValue);
The function retrieves timer and pulse counter values.
Line 24:printf("Timer Value = %u, Counter Value = %u\n", timerValue, counterValue);
Printing retrieved timer and pulse counter values to console.
Line 27:DlnCloseHandle(device);
The application closes handle to the DLN adapter.
This section describes the Pulse Counter module functions. They are used to control Pulse Counter interface and modify its settings.
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 DlnPlsCntGetPortCount()
function retrieves the number of pulse counter ports available in your DLN-series adapter.
The DlnPlsCntEnable()
function configures a port as Pulse counter.
The DlnPlsCntDisable()
function releases a port from Pulse counter.
The DlnPlsCntIsEnabled()
function informs whether a port is currently configured as Pulse counter.
The DlnPlsCntSetMode()
function is used to set mode and limit parameters of specified Pulse Counter port.
The DlnPlsCntGetMode()
function is used to get mode and limit parameters of the specified Pulse Counter port.
The DlnPlsCntGetResolution()
function retrieves the current Pulse Counter Module resolution.
The DlnPlsCntGetValue()
function retrieves the current timer and counter values of the specified Pulse Counter port.
The DlnPlsCntReset()
function resets the timer and/or counter on the Pulse Counter port.
The DlnPlsCntResume()
function resumes the pulse counter operation state on the Pulse Counter port.
The DlnPlsCntSuspend()
function suspends pulse counter operation state on the Pulse Counter port.
The DlnPlsCntIsSuspended()
function informs whether a port is currently suspended.
The DlnPlsCntSetEventCfg()
function defines the event configuration for the specified Pulse Counter port.
The DlnPlsCntGetEventCfg()
function is used to get event configuration of the specified Pulse Counter port.