Hi Andrew,
Thank you very much, I have modified my code as follows, plz have a look:
#include <NIDAQmx.h>
#include <windows.h>
#include <stdio.h>
#include <conio.h>
#define DAQmxErrChk(functionCall) if( DAQmxFailed(error=(functionCall)) ) goto Error; else
int32 CVICALLBACK DoneCallback(TaskHandle taskHandle, int32 status, void *callbackData);
int main()
{
int32 error=0;
TaskHandle taskHandle=0;
TaskHandle off;
int32 written;
char errBuff[2048]={'\0'};
float64 data[10];
float64 OFF[1]={0.00};
int i,j,k,kmax;
double inc, ninc;
/* kmax=200, inc=0.05, ninc=-0.05 */
//kmax = 200;
//inc = 0.05;
//ninc = inc*(-1);
/* kmax=1000, inc=0.05, ninc=-0.05 */
kmax = 1000;
inc = 0.01;
ninc = inc*(-1);
/*********************************************/
// DAQmx Config for OFF 0V
/*********************************************/
DAQmxErrChk (DAQmxCreateTask("",&off));
DAQmxErrChk (DAQmxCreateAOVoltageChan(off,"Dev1/ao0","",-10.0,10.0,DAQmx_Val_Volts,NULL));
DAQmxErrChk (DAQmxStartTask(off));
DAQmxErrChk (DAQmxWriteAnalogF64(off,1,1,10.0,DAQmx_Val_GroupByChannel,OFF,NULL,NULL));
DAQmxErrChk (DAQmxStopTask(off));
DAQmxErrChk (DAQmxClearTask(off));
/*********************************************/
// DAQmx Config for SQUARE-WAV generation
/*********************************************/
//DAQmx config initial array
for (j=0; j<10; j+=2)
{
for (i=j; i<j+1; i++)
data[i]=ninc;
for (i=j+1; i<j+2; i++)
data[i]=inc;
}
DAQmxErrChk (DAQmxCreateTask("",&taskHandle));
DAQmxErrChk (DAQmxCreateAOVoltageChan(taskHandle,"Dev1/ao0","",-10.0,10.0,DAQmx_Val_Volts,NULL));
DAQmxErrChk (DAQmxCfgSampClkTiming(taskHandle,"",4000.0,DAQmx_Val_Rising,DAQmx_Val_ContSamps,1000));
DAQmxErrChk (DAQmxCfgOutputBuffer (taskHandle,1000));
DAQmxErrChk (DAQmxSetWriteRegenMode(taskHandle,DAQmx_Val_DoNotAllowRegen));
DAQmxErrChk (DAQmxSetAODataXferMech(taskHandle,"Dev1/ao0",DAQmx_Val_DMA));
DAQmxErrChk (DAQmxWriteAnalogF64(taskHandle,10,0,10.0,DAQmx_Val_GroupByChannel,data,&written,NULL));
DAQmxErrChk (DAQmxStartTask(taskHandle));
printf("\nGenerating square-wave with increasing amplitude continuously...\n");
for(k=0; k<kmax; k++)
{
//config arrays
for (j=0; j<10; j+=2)
{
for (i=j; i<j+1; i++)
data[i]=k*ninc;
for (i=j+1; i<j+2; i++)
data[i]=k*inc;
}
DAQmxErrChk (DAQmxWriteAnalogF64(taskHandle,10,0,10.0,DAQmx_Val_GroupByChannel,data,&written,NULL));
Sleep(500);
}
Error:
if( DAQmxFailed(error) )
DAQmxGetExtendedErrorInfo(errBuff,2048);
if( taskHandle!=0 ) {
/*********************************************/
// DAQmx Stop Code
/*********************************************/
DAQmxStopTask(taskHandle);
DAQmxClearTask(taskHandle);
}
if( DAQmxFailed(error) )
printf("DAQmx Error: %s\n",errBuff);
/*********************************************/
// DAQmx Config for OFF 0V
/*********************************************/
DAQmxErrChk (DAQmxCreateTask("",&off));
DAQmxErrChk (DAQmxCreateAOVoltageChan(off,"Dev1/ao0","",-10.0,10.0,DAQmx_Val_Volts,NULL));
DAQmxErrChk (DAQmxStartTask(off));
DAQmxErrChk (DAQmxWriteAnalogF64(off,1,1,10.0,DAQmx_Val_GroupByChannel,OFF,NULL,NULL));
DAQmxErrChk (DAQmxStopTask(off));
DAQmxErrChk (DAQmxClearTask(off));
printf("\nEnd of program, press Enter key to quit\n");
_getch();
return 0;
}
int32 CVICALLBACK DoneCallback(TaskHandle taskHandle, int32 status, void *callbackData)
{
int32 error=0;
char errBuff[2048]={'\0'};
// Check to see if an error stopped the task.
DAQmxErrChk (status);
Error:
if( DAQmxFailed(error) ) {
DAQmxGetExtendedErrorInfo(errBuff,2048);
DAQmxClearTask(taskHandle);
printf("DAQmx Error: %s\n",errBuff);
}
return 0;
}
I am not sure if this is ok since I have not tested it on a hardware DAQ but just using a DAQmx simulated device.
Besides, here are some conceptual questions I want to clarify:
(1) DAQmxCfgSampClkTiming - setting 4000 as output rate and 2 samples per period, I will have sq-wave of 2KHz?
(2) DAQmxWriteAnalogF64 - setting numSamsPerChan must agree with the array to be written? i.e. If float64 data[10] then numSampsPerChan must set to 10? otherwise, DAQmx prompts error??
(3) What effects will be increasing data[10] to data[1000] or decreasing data[10] to data[2] in my case?
(4) Is Sleep(500) too long for this case? I have notice, DAQmx will prompt error if insufficient data is written to the buffer? What will be the max Sleep() workable?
Thank you very much & Merry Xmas!
Rolly