04-01-2009 10:34 AM
Following a Labwindows example for analog output of voltage, I am trying to output a square wave using the code below. My problem is no output except a brief disturbance on my external oscilloscope. I verified my setup is working by using the Measurement & Automation test panel.
sampsPerBuffer = 1000; //1000
cyclesPerBuff = 5; //5
desiredFrequency = 200; //Hz
if( (dataDmx=malloc(sampsPerBuffer*sizeof(float64)))==NULL ) {
MessagePopup("Error","Not enough memory");
goto Error;
}
DAQmxErrChk (DAQmxCreateTask("",&gTaskHandle));
DAQmxErrChk (DAQmxCreateAOVoltageChan(gTaskHandle,"Dev1/ao0","VoltageOut",
0,10,DAQmx_Val_Volts,NULL));
sampsPerCycle = sampsPerBuffer / cyclesPerBuff;
desiredSampClkRate = desiredFrequency * sampsPerCycle;
DAQmxErrChk (DAQmxSetTimingAttribute(gTaskHandle,DAQmx_SampClk_Rate,desiredSampClkRate));
DAQmxErrChk (DAQmxGetTimingAttribute(gTaskHandle,DAQmx_SampClk_Rate,&resultingSampClkRate));
resultingFrequency = resultingSampClkRate / sampsPerCycle;
DAQmxErrChk (DAQmxCfgSampClkTiming (gTaskHandle, "OnboardClock",
resultingSampClkRate, DAQmx_Val_Rising,
DAQmx_Val_ContSamps, sampsPerBuffer)); // samples per channel
DAQmxErrChk (DAQmxRegisterDoneEvent(gTaskHandle,0,DoneCallback,NULL));
// generate square wave
for(;i<sampsPerBuffer;++i) {
double phase_i=fmod(phase+360.0*1/sampsPerCycle*i,360.0);
dataDmx[i] = phase_i/360.0<=50.0/100.0 ? 10 : 0; // 0 or -amplitude; 50.0 is duty cycle
// 10 is amplitude
}
phase = fmod(phase+resultingFrequency*360.0*sampsPerBuffer,360.0);
DAQmxErrChk (DAQmxStartTask(gTaskHandle));
DAQmxErrChk (DAQmxWriteAnalogF64(gTaskHandle,
sampsPerBuffer, // samples per channel
1, // autostart?
10.0, // timeout
DAQmx_Val_GroupByChannel,
dataDmx,
&written,
NULL));
04-02-2009 01:40 PM
Hello,
Have you tried running the example named Cont Gen Volt Wfm-Int Clk ? And does this one work? It also appears to me, looking at your code, is that you may be starting the task before you actually write any data to the buffer. The last two lines of the code you posted should probably be in the opposite order, or else you'll have started the task to write the waveform before you've actually loaded the buffer with your waveform.
Also, if you're going to post code, could you please attach the source code instead of posting all the code on your post. It just helps to keep things a little more readable. Thanks.
Chris W
04-06-2009 01:57 PM
Yes, that's the most likely reason. I will try to put the task start at the end and make a comment if it doesn't work.
04-06-2009 03:54 PM
04-07-2009 03:57 PM
I'm sorry to hear that. I looked into it and it appears that there are known issues with a couple of the CVI examples. Did you have any luck with your code? Did you give my suggestion a try?
Chris
04-08-2009 08:27 AM
07-08-2009 10:02 AM