Measurement Studio for VC++

cancel
Showing results for 
Search instead for 
Did you mean: 

Changing duty cycle of PWM on while running

Solved!
Go to solution

Hello,

 

I am trying to generate a PWM signal from my C-Code.

It is working quite well except the fact that I can not change the duty cycle while the PWM signal generation is running.

Here is what I do:

 

//Init

DAQmxCreateTask("masterP",&task_pwmout); DAQmxCreateCOPulseChanFreq(task_pwmout,taskchans1,NULL,DAQmx_Val_Hz , DAQmx_Val_Low , 0.0,freq,duty); DAQmxCfgImplicitTiming(task_pwmout,DAQmx_Val_ContSamps,1000); DAQmxStartTask(task_pwmout); ...

//Stop

DAQmxStopTask(task_pwmout);

 This is working well but the duty cycle is fixed.

 

I tried to change the duty cycle but it seems that the only way this is working is like this:


 

//Init

DAQmxCreateTask("masterP",&task_pwmout); DAQmxCreateCOPulseChanFreq(task_pwmout,taskchans1,NULL,DAQmx_Val_Hz , DAQmx_Val_Low , 0.0,freq,duty); DAQmxCfgImplicitTiming(task_pwmout,DAQmx_Val_ContSamps,1000); DAQmxStartTask(task_pwmout); ...

//Change Duty Cycle

DAQmxStopTask(task_pwmout); DAQmxSetCOPulseDutyCyc(task_pwmout,taskchans1,duty); DAQmxStartTask(task_pwmout); ...

//Stop

DAQmxStopTask(task_pwmout);

I have to stop the task, set the new duty cycle and start the task again.

Unfortunately, this takes some time (~100ms) which means that during this time, the PWM is switched off.

 

Is there a way to change the duty cycle smoother, so that there is no gap between?

 

Thanks in advance.

 

Christian

 

0 Kudos
Message 1 of 3
(7,614 Views)
Solution
Accepted by ChristianEC

Hi Christian,

you are right. Stopping an restarting the task is a very slow way to adjust the pulse width. This page describes an option adjusting the duty cycle during runtime using the "DAQmxWriteCtrFreqScalar" function. By calling this function in a loop after your task has started you will be able to adjust the frequency and the duty cycle of your task, simply by changing the parameters. 

 

Let me know if this works for you. 

 

Regards

--
Peter A.
Field Sales Engineer, NI Germany
Message 2 of 3
(7,597 Views)

Hi Peter,

 

this works well. I am using DAQmxWriteCtrFreq now since I have to update several channels at the same time.

 

Thank you,

 

Christian

0 Kudos
Message 3 of 3
(7,588 Views)