09-25-2009 04:55 AM
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
Solved! Go to Solution.
09-28-2009 12:06 PM
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
09-29-2009 03:09 AM
Hi Peter,
this works well. I am using DAQmxWriteCtrFreq now since I have to update several channels at the same time.
Thank you,
Christian