USB-CTR04 USB-CTR08 Measuring Frequency using CInScan

The following example demonstrates how to measure frequency using either the USB-CTR04 or CTR-08. Furthermore it demonstrates the use of CConfigScan(…) and CInScan(…).

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.

public const int CHANCOUNT = 1;
        public const int BUFFERSIZE = 2;
        public const int HALFBUFFSIZE = BUFFERSIZE / 2;
        public const string DEVICE = "USB-CTR";


        public const int                    CounterNum = 0;
        public const int                    OutTimerNum = 0;
        public const CounterMode            Mode = CounterMode.Period;
        public const CounterDebounceTime    DebounceTime = CounterDebounceTime.Debounce500ns;
        public const CounterDebounceMode    DebounceMode = CounterDebounceMode.TriggerAfterStable;
        public const CounterEdgeDetection   EdgeDetection = CounterEdgeDetection.RisingEdge;
        
        public const CounterTickSize        TickSize = CounterTickSize.Tick20pt83ns;    // 0 equates to 20.833 nS ticksize
        public const double                 tick = 0.000000020833f;

        public const int                    MapCounter = 0;
        public static bool                  ReadLower = true;
        /*////////////////////////////////////////////////////////////////////////////////////*/

        static void Main(string[] args)
        {


            int BoardNum = 0;
            int Rate = 1;
            double freq = 1000.0f;
            double duty = 0.50f;
            double intDelay = 0;
            short daqStatus;
            int Count;
            int Index;

            Console.WriteLine("Locating Device...Please wait\n");

            BoardNum = GetBoardNum(DEVICE);

            if (BoardNum == -1)
            {
                Console.WriteLine("No {0} detected!", DEVICE);
                WaitForKey();
                return;
            }
            else
            {
                MccBoard daq = new MccDaq.MccBoard(BoardNum);

                //allocate buffer
                IntPtr buffer = MccService.WinBufAlloc32Ex(BUFFERSIZE);

                if (buffer == IntPtr.Zero)
                {
                    Console.WriteLine("Bad Handle");
                    return;
                }

                IsError(daq.CConfigScan(CounterNum, 
                                        Mode, 
                                        DebounceTime, 
                                        DebounceMode, 
                                        EdgeDetection, 
                                        TickSize, 
                                        MapCounter));

                //for test purposes,jumper a wire from timer zero over to counter in zero.
                //use timer as test signal

                IsError(daq.PulseOutStart(OutTimerNum,
                                            ref freq,
                                            ref duty, 0,
                                            ref intDelay,
                                            IdleState.Low,
                                            PulseOutOptions.Default));

     
                ScanOptions Options = ScanOptions.Default
                                      | ScanOptions.Ctr64Bit
                                      | ScanOptions.Continuous 
                                      | ScanOptions.Background;

                IsError(daq.CInScan(CounterNum, 
                                    CounterNum, 
                                    BUFFERSIZE, 
                                    ref Rate, 
                                    buffer, 
                                    Options));


                System.ConsoleKeyInfo cki = new System.ConsoleKeyInfo();
                Console.WriteLine("Retrieving Data\n");
                Console.WriteLine("Press any key to Exit...\n");

                Int64[] longArray = new Int64[BUFFERSIZE];

                do
                {

                    IsError(daq.GetStatus(out daqStatus, out Count, out Index, FunctionType.CtrFunction));
                    if ((Index >= HALFBUFFSIZE) & ReadLower) //check for 50% more data
                    {
                        //get lower half of buffer
                        IsError(MccService.WinBufToArray64(buffer, longArray, 0, HALFBUFFSIZE));
 
                        DisplayData(longArray, HALFBUFFSIZE / CHANCOUNT);
                        ReadLower = false; //flag that controls the next read
                    }
                    else if ((Index < HALFBUFFSIZE) & !ReadLower)
                    {
                        //get the upper half
                        IsError(MccService.WinBufToArray64(buffer, longArray, HALFBUFFSIZE, HALFBUFFSIZE));

                        DisplayData(longArray, HALFBUFFSIZE / CHANCOUNT);
                        ReadLower = true;//flag that controls the next read
                    }
             
                } while (!Console.KeyAvailable);
                cki = Console.ReadKey();
                
                //stop output timer
                daq.PulseOutStop(OutTimerNum);

                IsError(daq.StopBackground(FunctionType.CtrFunction));
                IsError(daq.PulseOutStop(OutTimerNum));

                //free up memory
                IsError(MccService.WinBufFreeEx(buffer));

                WaitForKey();
            }

        }

Attachments
CSharp_USB_CTR_PeriodMode_Using_CInScan.zip