USB-1608G CSharp (C#) Duty Cycle Pulse Ouput
The following example demonstrates how to use the timer output. The program uses the keys 1 - 4 to set various duty cycles on a 1000Hz TTL waveform. In this example, the count parameter is set to zero to make the frequency continuous.
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.
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; //added to project references namespace TimerPulseOutput { class Program { public const string DEVICE = "1608G"; //identify the board using string name static void Main(string[] args) { MccDaq.ErrorInfo RetVal; double tmrFreq = 100.0f; double tmrDuty = 0.50f; uint tmrCount = 0; int tmrNum = 0; double tmrDelay = 0; IdleState idleState = IdleState.Low; PulseOutOptions options = PulseOutOptions.Default; int BoardNum = GetBoardNum(DEVICE); System.ConsoleKeyInfo cki = new ConsoleKeyInfo(); if (BoardNum == -1) Console.WriteLine("No USB-{0} detected!\n", DEVICE); else { MccDaq.MccBoard daq = new MccDaq.MccBoard(BoardNum); Console.WriteLine( "Frequency Pulse Ouput Example\n" + "\nUse the up & down arrow keys to control duty cycle\n" + "\nUse the left & right arrow keys to control frequency\n" + "\nPress <SpaceBar> to exit loop..." ); RetVal = daq.DConfigPort(DigitalPortType.AuxPort, DigitalPortDirection.DigitalOut); IsError(RetVal); do { if (Console.KeyAvailable) { cki = Console.ReadKey(); switch (cki.Key) { case ConsoleKey.UpArrow: tmrDuty = tmrDuty + 0.01; RetVal = daq.PulseOutStart(tmrNum, ref tmrFreq, ref tmrDuty, tmrCount, ref tmrDelay, idleState, options); IsError(RetVal); break; case ConsoleKey.DownArrow: tmrDuty = tmrDuty - 0.01; RetVal = daq.PulseOutStart(tmrNum, ref tmrFreq, ref tmrDuty, tmrCount, ref tmrDelay, idleState, options); IsError(RetVal); break; case ConsoleKey.RightArrow: tmrFreq = tmrFreq + 10; RetVal = daq.PulseOutStart(tmrNum, ref tmrFreq, ref tmrDuty, tmrCount, ref tmrDelay, idleState, options); IsError(RetVal); break; case ConsoleKey.LeftArrow: tmrFreq = tmrFreq - 10; RetVal = daq.PulseOutStart(tmrNum, ref tmrFreq, ref tmrDuty, tmrCount, ref tmrDelay, idleState, options); IsError(RetVal); break; default: tmrFreq = 100; tmrDuty = 0.5; break; } Console.WriteLine("Duty = {0:F}\t Frequency = {1:F}\r", tmrDuty, tmrFreq); //clear keyboard buffer while (Console.KeyAvailable) { Console.ReadKey(true); } } System.Threading.Thread.Sleep(100); } while (cki.Key != ConsoleKey.Spacebar); IsError(daq.PulseOutStop(tmrNum)); } } 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("USB-{0} board number = {1}\n", dev, BoardNum.ToString()); daq.FlashLED(); return BoardNum; } } return -1; } public static int IsError(ErrorInfo e) { if (e.Value != 0) { Console.WriteLine(e.Message); return 1; } return 0; } } }
Attachments
TimerPulseOutput.zip