USB-CTR04 & CTR08 CSharp One-Shot Pulse
The example below demonstrates how to use a USB-CTR04/08 to fire off a one shot pulse when a trigger signal is detected. The complete project is attached below as a zip file. This example uses two of the frequency output timers - one to serve as a frequency source and the other to generate a trigger signal. Connect the TMR0 to C0In and TMR1 to C0GT. When the counter detects the pulse on its gate it will delay a specified time before putting out the pulse of a specified width - see picture below.
To access the API, add a reference to the MccDaq object. Adding the reference is usually accomplished by right clicking the Project [under the Project Explorer] and selecting Add Reference. The complete project is attached to the article and was created with Visual Studio 2008 using CSharp. Compiling and execution is quick as there is minimal Windows overhead and associated code.
To make the code more readable a few convenience functions were added such as IsError, GetBoardNum, and WaitForKey. The IsError function checks the error number in the ErrorInfo object and if not zero displays the error message. The GetBoardNum function begins reading the Device strings from each number location and as soon as it finds one that contains the identifying string it exits. The WaitForKey does just that - waits for someone to press a key. To view the complete program project download the attachment below.
Disclaimer: The attached Code or Example is provided As Is. It has not been tested or validated as a product, for use in a deployed application or system, or for use in hazardous environments. You assume all risks for use of the Code or Example.
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; } /*////////////////////////////////////////////////////////////////////////////////////*/ } }
Attachments
CSharp_USB_CTR_OneShot.zip