Example Code

Generate Multiple Channels of Digital Output with Different Frequencies and Duty Cycles

Products and Environment

This section reflects the products and operating system used to create the example.

To download NI software, including the products shown below, visit ni.com/downloads.

    Hardware

  • Data Acquisition (DAQ)

    Software

  • LabVIEW

    Driver

  • NI DAQmx

Code and Documents

Attachment

Overview
Demonstrates the generation of multi channels Digital Output with various parameters for the frequencies and duty cycles

Description:
The following example shows how one might build and generate a digital waveform using non-regeneration so that you can update frequency and duty cycle on-the-fly with M or X series DAQ devices.  The example uses digital output rather than counters to achieve this, so if you need more outputs than counters available this would be a good option (note:  on M Series devices an external sample clock must be provided, this can come from one of the counters if desired).

For best results, use a Sample Clock that is an integer multiple of all desired frequencies.  The effects of not doing so will be more prominent as the desired output frequency approaches the rate of the sample clock.
To run the code you will need to download the Generate PWM Sub VI

Steps to implement or execute code
To implement this example:
  1. Define the physical channels to be used
  2. (Optional) Set the rest of the parameters as needed
  3. Run the VI
  4. (Optional) Turn on the Highlight Execution to see the flow of the VI

 

To execute this example:

  1. Install the required software.
  2. Connect the DAQ hardware that supports the DIO features
  3. Confirm the connection with the MAX with TestPanel
  4. Open the VI and refer the Implement Steps


Requirements
Software
LabVIEW 2012 or compatible
NI-DAQmx 16.0 or compatible

Hardware
cDAQ with C series Digital IO Module

 

fp.JPG

 

bd.JPG

 

**This document has been updated to meet the current required format for the NI Code Exchange.**
John Passiak

Example code from the Example Code Exchange in the NI Community is licensed with the MIT license.

Comments
EET9
Member
Member
on

Hello I am Antonio Smith. This is a great example. I'm trying to use it, but I am keep on getting this error:

EET9
Member
Member
on

Do you believe you can help me?

John_P1
Trusted Enthusiast
Trusted Enthusiast
on

What error are you getting?  It didn't seem to come through.

John Passiak
chucky9
Member
Member
on

It's a good example! I'm trying to add phase input to the VI by modifying the square wave sub-vi to give 120 degree phase shift for the output three signals, but without ant luck. Does ant one know how to modify the above VI to have phase shift feature?

John_P1
Trusted Enthusiast
Trusted Enthusiast
on

Try looking at this example (one of the download links includes phase settings).  It's analog output instead, but it should be easy to convert the analog output signal into a digital one like I did in the example on this page.  Building the waveform in this way is certainly not the most efficient, but the simplicity is nice.

John Passiak
chucky9
Member
Member
on

I modified the example you stated to generate 3 analog signals with 120 degree out of phase and worked well. The only thing I don't like is the slew rate with analog pulses; thus, I was trying to the above VI and add the PHASE terminal in the square wave sub-vi and than create a Phase control on the front panel. I step through the operations, phase data feed to the Square wave sub-vi fine but for some reason, no phase info comes out. I looked through the Square wave sub-sub-vi too, but couldn't find any problem there. Any suggestion is appreciated.

John_P1
Trusted Enthusiast
Trusted Enthusiast
on

You should be able to configure the analog wave form to "square wave" as the example is currently written (the "Basic Function Generator.vi" allows you to specify the waveform type).  You can use the same logic as "Generate PWM.vi" attached on this page to convert the analog waveform into a digital one for use with digital output lines.

John Passiak
Ember5
Member
Member
on

Hello,

I am extremely interested in using this example, but I keep getting the error message listed at the bottom of this post. I assume it is an issue with the sample clock, but I cannot fix it. As far as I can tell, my device *should* be able to handle an output clock and I have tried  leaving the "select clock source" blank to use to the device's internal clock, selecting "onboard clock," and selecting individual lines that appear in the dropdown menu. The device works fine for other examples I have tried, although admittedly I have been unable to get a multi-channel output working with a sample clock- which is why I downloaded this example in the first place. If there is any help you could offer it would be most appreciated.

sincerely,

~ Sarah

Error -200846 occurred at Cont Gen Dig Wfm Non Regeneration Mult Channel.vi

Possible reason(s):

Write cannot be performed when the auto start input to DAQmx Write is false, task is not running, and timing for the task is not configured or Timing Type is set to On Demand.

Set auto start to true, start the task, or configure timing and specify Timing Type other than On Demand.

kjschmidt
Member
Member
on

The problem is that you write to the DAQ before you call the VI that starts the DAQ. I have moved that VI to before the "DAQmx write" and that solve this i problem. But then I encountered a new problem...

John_P1
Trusted Enthusiast
Trusted Enthusiast
on

The write has to occur before starting the task.  This configures the output buffer.

The only way I could see Ember5 getting that error is if she deleted the part of the code that configures the sample clock timing.  This of course is mandatory as well.

John Passiak
kjschmidt
Member
Member
on

John_P1, here is a screenshot of my code, and i get the same error as Ember5. Any clue?
And also does the "lines" on the wavechart graph first appear when the code is running?

Block diagram.pngFront panel.png

aysljc
Member
Member
on

Hello John,

I'm trying to impletment your sample on my device. I kept getting the error 200898 as follows:

"

Possible reason(s):

Partial use of physical lines within a physical port is not supported by this device, given the requested Sample Timing Type.

Consider specifying the entire port and tristating the lines you do not want driven.

Property: SampTimingType

Requested Value: Sample Clock

Output Channels: PXI1Slot2/port0/line0, PXI1Slot2/port0/line1, PXI1Slot2/port0/line2

Task Name: _unnamedTask<145EF4>

"

Do you know where this error comes from and how to fix it? I didn't modify the block diagram. Only the front panel.sample.png

kojsni
Member
Member
on

You are amazing, your code is amazing and you should feel good about yourself.
Saved me quite some time.

vomojo
Member
Member
on

Hey, John, thanks for the example code! I've been trying to get this functionality on my DAQ for the longest time.  That said, I do have some questions about the PWM part of the code. For my project I need to create 3 digital waveforms for 3 separate lines, two of which are identical and one that runs at twice the frequency of the other two. This is easy enough to do with what you've provided, but I need the two identical waveforms to be offset such that when one is high, the other is low. 

 

I know NI-MAX allows you to invert lines when creating tasks; is there any way to modify this example to include a similar functionality?

Thanks in advance. 

msd.iitcbe
Member
Member
on

Hello,

 

I could not able to find the PWM subvi to use this code. Can anyone share it with with me?

 

Thanks.

SUJITniser
Member
Member
on

Dear sir,

             I want to write a program where there will be 3-4 channels to generate digital outputs which can be started and stop at a specific time. When I try to do it for two then only one channel is getting started but the other is showing ERROR-50103 as both the channels are using the same clock which is the onboard clock. We are using PXI 6361. Can you please help me to solve this issue? I am attaching the Vi s here.1615283910613.jpg