Measurement Studio for .NET Languages

cancel
Showing results for 
Search instead for 
Did you mean: 

Updating PWM duty cycle without stoping and recreating task

I'm using VS.NET 2005 and a ni-6229 daq.  I need to have 6 simultaneous analog outputs.  So right now I am using the 4 analog outputs and 2 pwm outputs.  The generated pwm signal will go through an RC network.  It has a basic UI with a start and stop button and an UpDown box to vary the duty cycle.  Right now if you want to update the pwm and change the duty cycle you have to stop the task and recreate the channel with the new duty cycle.  My question is, is there a way to update the duty cycle without having the stop and recreate the task?  The reason is I dont want the signal to stop and then start back up and let the capacitor lose its charge and then recharge.  Is this possible?
 
Here a a piece of the code that handles one PWM:
 
PRivate myPTask1 As Task

Private Sub Start1btn_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Start1btn.Click
        Try
            myPTask1 = New Task
            myPTask1.COChannels.CreatePulseChannelFrequency("Dev1/ctr0", "", COPulseFrequencyUnits.Hertz, COPulseIdleState.Low, 0.0, 1000.0, Convert.ToDouble(Duty1UpDown.Value))
           
          PTask1.Timing.ConfigureImplicit(SampleQuantityMode.ContinuousSamples, 1000)
            myPTask1.Start()
            Start1btn.Enabled = False
            Stop1btn.Enabled = True
        Catch ex As System.Exception
            MessageBox.Show(ex.Message)
            myPTask1.Dispose()
            Start1btn.Enabled = True
            Stop1btn.Enabled = False

        End Try
End Sub
Private Sub Stop1btn_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Stop1btn.Click
        myPTask1.Stop()
        myPTask1.Dispose()
        Start1btn.Enabled = True
        Stop1btn.Enabled = False
End Sub
0 Kudos
Message 1 of 5
(4,144 Views)
Hi BienoJ,
 
Assuming that you just have one channel, you can change the duty cycle on the fly by setting the following two properties:
 
myPTask1.COChannels(xx).PulseDutyCycle = <new duty cycle>
myPTask1.COChannels(xx).PulseFrequency = <new frequency>
 
where xx is the index of the channel. If you wanted to set all your counter output channels, you could set these properties on the entire collection like:
 
myPTask1.COChannels.All.PulseDutyCycle = ....
myPTask1.COChannels.All.PulseFrequency = ....
 
Note NI-DAQmx requires that when you update the duty cycle of a pulse train, you must also supply an update to the frequency. 
 
Hope this helps!
 
Best Regards,


Message Edited by Jonathan N on 04-03-2008 09:08 PM
Jonathan N.
National Instruments
0 Kudos
Message 2 of 5
(4,138 Views)
I actually am running two different PWM's with the same frequency but I need to be able to run them at the same time with different duty cycles.
0 Kudos
Message 3 of 5
(4,135 Views)
Hi Bienoj,

Just set those properties on both channels as I showed earlier (i.e. myTask.COChannels(0)..... and myTask.COChannels(1).....).   Are you getting an error when you set those properties?

Best Regards,
Jonathan N.
National Instruments
0 Kudos
Message 4 of 5
(4,109 Views)

I did it for one of the channels and it works perfectly.  I'm assumming it will work for the other as well but I will post back if I get an error.

 

Thanks!

 

BienoJ

0 Kudos
Message 5 of 5
(4,107 Views)