Digital I/O

cancel
Showing results for 
Search instead for 
Did you mean: 

multiple pulse trains on 6221

I have 6 devices, each connected to a different digital output channel.
I need to send a pulsetrain with a different frequency to each device.
How can I do this using DAQmx with the 6221 board?

Thanks in advance,

DRT
0 Kudos
Message 1 of 10
(4,157 Views)

Hi DRT-

Your 6221 digital output can be clocked to provide digital outputs with hardware-timed precision.  Do you need to be able to change pulse specs on the fly?  This would be considerably more involved, so for the interest of discussion I'll address the case of constant frequencies.

First, there is no dedicated timing engine for the digital output hardware on the board so you will need to provide an update clock from elsewhere.  The most popular choice is to use an onboard counter from the device to generate a sample clock for the digital hardware.  I'm not sure which development environment you are using, but there are pulse generation examples for LabVIEW ( Help>>Find Examples ), ANSI C ( Program Files>>NI-DAQ>>Examples>>DAQmx ANSI C), or .NET ( Program Files\National Instruments\MeasurementStudioVS2003\DotNET\Examples\DAQmx ).

Once you have set up the sample clock generation you will need to configure the digital output task for hardware timing with external clock control.  The "external" sample clock source will then be "/Dev1/ctr0InternalOutput" and should be supplied to the pertinent DAQmx Timing function for the digital output task you choose.  Good examples would be ( Hardware Input and Output>>DAQmx>>Digital Generation>>Write Dig Chan-Ext Clk.vi ), ( \Digital\Generate Values\Write Dig Chan-Ext Clk ), or ( Digital\Generate Values\WriteDigChan_ExtClk ) for each of the aforementioned development environments.

The only challenge left is to actually generate the sample data.  Your data to write should consist of appropriately padded trains of zeroes and ones to achieve the various frequencies of outputs relative to the master sample clock.  This means that the sample clock rate you choose should be at least twice as fast as the rate you are trying to achieve and that you will need to pad the digital data to be written on the various other channels.

Hopefully this will help get you started- please let us know if we can offer other suggestions along the way.

Tom W
National Instruments
0 Kudos
Message 2 of 10
(4,147 Views)
Tom,
I tried using the C# WriteDigChan_ExtClk sample and entering /Dev1/ctr0InternalOutput in the Clock source text box. When I clicked the Write button, I get the error:

Requested value is not a supported value for this property.

Property: NationalInstruments.DAQmx.Timing.SampleTimingType
You Have Requested: NationalInstruments.DAQmx.SampleTimingType.SampleClock
You Can Select: NationalInstruments.DAQmx.SampleTimingType.OnDemand

Task Name: _unnamedTask<0>

Status Code: -200077

Also, my maximum frequency will be 60 Hz, and I do need to be able to change the frequency on the fly.

Any suggestions?



0 Kudos
Message 3 of 10
(4,141 Views)

Hi DRT-

Is it possible that you're trying to use DIO lines in ports 1 or 2?  I meant to mention it before, but hardware-timed digital operations are only supported on port 0.  If you could provide a short section of your code where you create the digital channels I'd be happy to take a look at it.

Thanks-

Tom W
National Instruments
0 Kudos
Message 4 of 10
(4,140 Views)
Tom,
Here is the code. It is taken from the NI C# example WriteDigChan_ExtClk. I modified it here to hard-code the values that were read from the form controls:

private void writeButton_Click(object sender, System.EventArgs e)
{
  using (Task digitalWriteTask = new Task())
  {
      // Create the digital output channel
      digitalWriteTask.DOChannels.CreateChannel("Dev1/port0","",
         ChannelLineGrouping.OneChannelForAllLines);
      // Verify the task so we can query the channel's properties
          digitalWriteTask.Control(TaskAction.Verify);

      // Create the data to write
      int samples = 1000;
      int lines = (int)digitalWriteTask.DOChannels[0].NumberOfLines;
      bool[] data = new bool[lines];

      // Set up the timing
      digitalWriteTask.Timing.ConfigureSampleClock("Dev1/ctr0InternalOutput",
      Convert.ToDouble(1000.0),
      SampleClockActiveEdge.Rising, SampleQuantityMode.FiniteSamples,
         Convert.ToInt32(samples));

      // Write the data
      DigitalSingleChannelWriter writer = new DigitalSingleChannelWriter(digitalWriteTask.Stream);

      // Loop through every sample
      Random r = new Random();
      for (int i = 0; i < samples; i++)
      {
         // Generate a random set of boolean values
         for (int j = 0; j < lines; j++)
         {
            if (r.Next() % 2 == 0)
               data[j] = true;
            else
               data[j] = false;
          }

         // Write those values
         writer.WriteSingleSampleMultiLine(true, data);
     }
  }
}

0 Kudos
Message 5 of 10
(4,132 Views)

Hi DRT-

Can you please confirm which DAQ device you are using?  If you are using a PCI-6221 the configuration code you posted should work great, but the error you're seeing seems to indicate that you are using an E Series or other board that does not support hardware-timed digital operations.  Can you please check in MAX which one of your boards is referred to be "Dev1" and make sure that it is indeed a PCI-62XX (i.e. M Series) board?

Thanks a lot-

Tom W
National Instruments
0 Kudos
Message 6 of 10
(4,126 Views)
Tom,
Don sheepishly says: "Oops, this PC has the 6035E board, not the 6221 board".
Sorry about that.
My application will be using the 6221 board, when I can get it installed.

But you can still clear up some things for me regarding my application: 6 digital channel outputs, each with different frequencies, and yes, frequency must be able to be changed on the fly.

This is what I was thinking about doing:
1. Use the counter for my timing.
2. Use the asynchronous callback of the counter output to do my work.
3. In the callback (which will be called at a rate of 600 Hz, determine the state of each of the 6 digital lines, based on the frequency required for the device attached to the particular digital channel.
4. Output the new data to the digital port.

Does this sound feasible?
0 Kudos
Message 7 of 10
(4,125 Views)

Hi Don-

Let's take a step back and take a closer look at how the channels relate to one another.  Do all of the channels need to change frequency proportionally to one another or completely independently.  In other words, let's look at two channels:

Channel 1: 50Hz
Channel 2: 95HZ

When you say the frequency must be changed on the fly, do you mean that Channels 1 and 2 will be changed to 100Hz and 190Hz or that they might need to be changed to 50Hz and 100Hz, respectively?  The latter case is more complicated but certainly do-able.

It won't be quite as simple as just verifying states and writing new states.  In order to clock out the digital values you will need to construct and write a multi-point buffer which will then be clocked out by the counter output.  The easiest option would be to construct a 2-dimensional output array that consists of the smallest number of points necessary to divide all digital channels by integer numbers.  So, for an example of

Channel 1: 50Hz
Channel 2: 100Hz

You will need digital patterns of

Channel 1: 1100
Channel 2: 1010

And you should output them at 400Hz.  By default, the output patterns will be continuously "regenerated" from the buffer unless you intervene by writing new buffer values.  The advantage of using a counter for the sample clock is that you can adjust its frequency output while the counter task is running, so you can optimize the update rate and number of digital buffer points as needed.

Hopefully this helps-

Tom W
National Instruments
0 Kudos
Message 8 of 10
(4,122 Views)
Tom,
Thanks for the quick response.
Each channel's frequency can be changed independently. The frequencies can change by any amount, up or down, from 1 to 30 Hz. At 600 Hz, I know I can't cover every frequency, but, for my purposes, the frequencies I can get are close enough.
Can I not just calculate the line state for each every time the counter pulses and then bit-shift and OR the states together and do a digital write at that time?
0 Kudos
Message 9 of 10
(4,109 Views)

Hi DRT-

You could certainly control the lines with software logic, but you will lose the hardware-timed nature of your output operation.  So, since you're programming in Windows it may be relatively unlikely that you will be able to achieve precise timing by using software decisions to update the data point-by-point "on the fly."  I would definitely suggest using the hardware-timed outputs via buffer writes.

Hopefully this helps-

Tom W
National Instruments
0 Kudos
Message 10 of 10
(4,099 Views)