Counter/Timer

cancel
Showing results for 
Search instead for 
Did you mean: 

Gated edge count with NI-DAQmx

Here is a code snippet of how to do a gate counter using C# and the .NET Measurement Studio library.

 

public int GatedCounter(double period)
{
gateTask = new Task();
int lowTime = (int)Math.Round(2e7 * period);
int highTime = (int)Math.Round(2e7 * period);
gateTask.COChannels.CreatePulseChannelTicks("TestUSB/ctr1", "gate", "20MHzTimebase",
COPulseIdleState.Low, 0, lowTime, highTime);
gateTask.COChannels["gate"].PulseTerminal = "/TestUSB/PFI5";
gateTask.Control(TaskAction.Verify);
counterTask = new Task();
CIChannel channel = counterTask.CIChannels.CreateCountEdgesChannel("TestUSB/ctr0","gatedCounter",
CICountEdgesActiveEdge.Rising, 0, CICountEdgesCountDirection.Up);
channel.CountEdgesTerminal="/TestUSB/PFI0"; // edges input in PFI0
// Use a pause trigger attribute as a gate
counterTask.Triggers.PauseTrigger.ConfigureDigitalLevelTrigger("/TestUSB/Ctr1InternalOutput",
DigitalLevelPauseTriggerCondition.Low);
counterTask.Control(TaskAction.Verify);
// Counter is configured, not start counting (I'm assuming Counter 1 was left in a low state)
counterTask.Start();
// Release the pause
gateTask.Start();
gateTask.WaitUntilDone();
CounterSingleChannelReader reader = new CounterSingleChannelReader(counterTask.Stream);
int count = reader.ReadSingleSampleInt32();
counterTask.Stop();
gateTask.Stop();

return count;
}

 

There are a couple of things to watch out for.

 

First, note that the strings for specifying the device somethings need a leading '/' character and sometimes don't. Secondly, it is important to read the counter before calling Stop(). Otherwise the Stop() call will clear the counter.

0 Kudos
Message 11 of 11
(1,054 Views)