ni.com is currently experiencing unexpected issues.

Some services may be unavailable at this time.

Multifunction DAQ

取消
顯示結果 
搜尋替代 
您的意思是: 

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 積分
1 條訊息(共 7 條)
3,750 檢視

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 積分
2 條訊息(共 7 條)
3,737 檢視

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 積分
3 條訊息(共 7 條)
3,710 檢視
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 積分
4 條訊息(共 7 條)
3,697 檢視

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 積分
5 條訊息(共 7 條)
3,680 檢視
Yes, everything worked fine.
0 積分
6 條訊息(共 7 條)
3,668 檢視
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 積分
7 條訊息(共 7 條)
3,467 檢視