using System;
using MccDaq; // also, add reference to MccDaq to the project
namespace OneShotExample
{
class Program
{
public const string DEVICE = "USB-CTR"; //works for USB-CTR04 or USB-CTR08
public const int OutTimerZero= 0; //Frequency sources - connect to counter inputs 0 & 1
public const int OutTimerOne = 1;
public const CounterMode Mode = CounterMode.OutputInitialStateLow | //counter output low
CounterMode.OutputOn | //enable the gate
CounterMode.GateClearsCtr | //clear counter on gate signal
CounterMode.Totalize;
/*////////////////////////////////////////////////////////////////////////////////////*/
static void Main(string[] args)
{
int BoardNum = 0;
double freq = 1000000.0f; //1MHz
double freq2 = 5000.0f; //5000Hz
double duty = 0.50f;
double intDelay = 0;
int CounterNum = 0;
int MapCounter = 0;
Console.WriteLine("Locating Device...Please wait\n");
BoardNum = GetBoardNum(DEVICE);
if (BoardNum == -1)
{
Console.WriteLine("No {0} detected!\n", DEVICE);
WaitForKey("Press any key to exit\n");
return;
}
else
{
MccBoard daq = new MccDaq.MccBoard(BoardNum);
CounterNum = 0;
IsError(daq.CConfigScan(CounterNum,
Mode,
CounterDebounceTime.Debounce500ns,
CounterDebounceMode.TriggerAfterStable,
CounterEdgeDetection.RisingEdge,
CounterTickSize.Tick20pt83ns,
MapCounter));
IsError(daq.CLoad32(CounterRegister.OutputVal0Reg0, 99)); //delay the one shot pulse for 100uS after gate strobe
IsError(daq.CLoad32(CounterRegister.OutputVal1Reg0, 199));//after that output 100uS pulse
//connect timer output 0 to counter input 0 so that counter have something to count.
//counter will count 1uS pulses.
IsError(daq.PulseOutStart(OutTimerZero,
ref freq,
ref duty,
0,
ref intDelay,
IdleState.Low,
PulseOutOptions.Default));
//timer output 1 will be used to strobe the gate to clear the counter
for (int i = 0; i < 4; i++)
{
//test loops
WaitForKey("Press any key to start output\n");
IsError(daq.PulseOutStart(OutTimerOne,
ref freq2,
ref duty,
1,
ref intDelay,
IdleState.Low,
PulseOutOptions.Default));
}
IsError(daq.PulseOutStop(OutTimerZero)); //disable frequency source
IsError(daq.PulseOutStop(OutTimerOne)); //disable frequency source
IsError(daq.CClear(0));//clear counter 0
}
}
/*////////////////////////////////////////////////////////////////////////////////////*/
public static int GetBoardNum(string dev)
{
for (int BoardNum = 0; BoardNum < 99; BoardNum++)
{
MccDaq.MccBoard daq = new MccDaq.MccBoard(BoardNum);
if (daq.BoardName.Contains(dev))
{
Console.WriteLine("board number = {0}\n", BoardNum.ToString());
return BoardNum;
}
}
return -1;
}
/*////////////////////////////////////////////////////////////////////////////////////*/
public static void WaitForKey(string s)
{
Console.WriteLine(s);
do
{ //idle loop
System.Threading.Thread.Sleep(10);
} while (!Console.KeyAvailable);
Console.ReadKey();
}
/*////////////////////////////////////////////////////////////////////////////////////*/
public static int IsError(ErrorInfo e)
{
if (e.Value != 0)
{
Console.WriteLine(e.Message);
WaitForKey("Press any key to continue\n");
return 1;
}
return 0;
}
/*////////////////////////////////////////////////////////////////////////////////////*/
}
}