Multifunction DAQ

cancel
Showing results for 
Search instead for 
Did you mean: 

AO square wave with DAQCard-6024E

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));

 

0 Kudos
Message 1 of 7
(3,710 Views)

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

0 Kudos
Message 2 of 7
(3,697 Views)

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.

0 Kudos
Message 3 of 7
(3,670 Views)
The example you advised me to look at gives A FATAL RUN-TIME ERROR: Unknown source position thread id 0x00020F48. The program has cause a 'General Protection' fault at 001B:0000000000. 
0 Kudos
Message 4 of 7
(3,657 Views)

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

0 Kudos
Message 5 of 7
(3,640 Views)
Yes, everything worked fine.
0 Kudos
Message 6 of 7
(3,628 Views)
I am running exactly into the same problem with a PCI-6221 card. I did a quick change on ContGen-IntClk.c and I am getting the strange behavior when I call DAQmxWriteAnalogF64(taskHandle,totalsamples,0,10.0,DAQmx_Val_GroupByChannel,samples,NULL,NULL); with the variable samples allocated by malloc. If samples is in the program global area or samples is in the program stack, then it works OK. If samples is allocated by malloc the program terminates with an exception. Someone once told me that the NI driver uses DMA to transfer the data from memory to the card. I am wondering if the DMA routine does not like memory allocate with malloc.
0 Kudos
Message 7 of 7
(3,427 Views)