12-19-2016 01:54 AM
I need to sweep frequency continuously without any pauses. I use c++ and nidaqmx.lib
I've figured out how I can generate sinus but it way requires create and clear task for each sinus generation and it is not acceptable for continuity.
int32 error = 0;
TaskHandle taskHandle = 0;
float64 data[BUFFER_SIZE_GENERATOR];
char errBuff[2048] = { '\0' };
for (int i = 0; i < BUFFER_SIZE_GENERATOR; i++)
{
data[i] = voltage*sin((double)(2 * M_PI*freguency*i) / SAMPLE_RATE_GENERATOR);
}
DAQmxErrCheck(DAQmxCreateTask("", &taskHandle));
DAQmxErrCheck(DAQmxCreateAOVoltageChan(taskHandle, "Dev1/ao0", "", -MAX_VOLTAGE_GENERATOR, MAX_VOLTAGE_GENERATOR, DAQmx_Val_Volts, NULL));
DAQmxErrCheck(DAQmxCfgSampClkTiming(taskHandle, "", SAMPLE_RATE_GENERATOR, DAQmx_Val_Rising, DAQmx_Val_FiniteSamps, time*SAMPLE_RATE_GENERATOR));
DAQmxErrCheck(DAQmxWriteAnalogF64(taskHandle, BUFFER_SIZE_GENERATOR, 0, 0/*10.0*/, DAQmx_Val_GroupByChannel, data, NULL, NULL));
DAQmxErrCheck(DAQmxStartTask(taskHandle));
DAQmxErrCheck(DAQmxWaitUntilTaskDone(taskHandle, DAQmx_Val_WaitInfinitely));
DAQmxStopTask(taskHandle);
DAQmxClearTask(taskHandle);
Solved! Go to Solution.
12-20-2016 09:34 AM
Have you tried looking at the built in C examples from DAQmx? If you have DAQmx installed, you should be able to find examples in "Start > All Programs > National Instruments > NI-DAQmx > NI-DAQmx Examples > DAQmx ANSI C". The examples you're looking for would most likely be in "Analog Out > Generate Voltage > Cont Gen Volt Wfm-Int Clk" for Continuous Generation Voltage Waveform Internal Clock. The example code you included is most likely for a single waveform generation rather than continuous. The example above should be the correct format!
Best,
Chris D. | Applications Engineer | National Instruments
12-21-2016 01:09 AM
My code which posted above was developed by using Analog Out > Generate Voltage > Cont Gen Volt Wfm-Int Clkf.
But this example doesn't show how can i change data continual
12-21-2016 06:33 AM
It seems that i've found solution.
This example helps me:
http://download.ni.com/pub/devzone/epd/gen_voltage_non_regen.c
int main(void)
{
int32 error = 0;
TaskHandle taskHandle = 0;
char errBuff[2048] = { '\0' };
time_t startTime;
char chan[] = "Dev1/ao0";
float64 min = -5.0;
float64 max = 5.0;
uInt64 samplesPerChan = SAMPLE_RATE;
float64 sampleRate = SAMPLE_RATE;
#define bufferSize BUFFER_SIZE
float64 data[bufferSize];
int32 pointsWritten;
int32 totalPoints = 0;
float64 timeout = 10.0;
int32 regenMode = 0;
// Create the waveform
int frequency = 2000;
int32 i;
for (i = 0; i<bufferSize; i++)
data[i] = 0/*VOLTAGE*sin((double)(2 * PI*frequency*i) / SAMPLE_RATE)*/;
// Create the task and channel
DAQmxErrChk(DAQmxCreateTask("", &taskHandle));
DAQmxErrChk(DAQmxCreateAOVoltageChan(taskHandle, chan, "", min, max, DAQmx_Val_Volts, NULL));
DAQmxErrChk(DAQmxCfgSampClkTiming(taskHandle, "", sampleRate, DAQmx_Val_Rising, DAQmx_Val_ContSamps, 2*sampleRate));
// Turn off regeneration
DAQmxErrChk(DAQmxSetWriteAttribute(taskHandle, DAQmx_Write_RegenMode, DAQmx_Val_DoNotAllowRegen));
// Get the regeneration mode
DAQmxErrChk(DAQmxGetWriteAttribute(taskHandle, DAQmx_Write_RegenMode, (void*)®enMode));
// Prime the output buffer
DAQmxErrChk(DAQmxWriteAnalogF64(taskHandle, sampleRate, 0, timeout, DAQmx_Val_GroupByChannel, data, &pointsWritten, NULL));
totalPoints += pointsWritten;
// Start the generation
DAQmxErrChk(DAQmxStartTask(taskHandle));
// Continue to write to the buffer since there isn't regeneration
if (regenMode == 10097)
printf("Generating voltage continuously in regeneration mode.\n");
else if (regenMode == 10158)
printf("Generating voltage continuously in non-regeneration mode.\n");
startTime = time(NULL);
// The loop will quit after 10 seconds
int iterator = 1;
while (time(NULL) < startTime + 10)
{
int frequency = 2000 +100* iterator;
int32 i;
for (i = 0; i<bufferSize; i++)
data[i] = VOLTAGE*sin((double)(2 * PI*frequency*i) / SAMPLE_RATE);
printf(" %i samples have been written.\n", totalPoints);
pointsWritten = 0;
DAQmxErrChk(DAQmxWriteAnalogF64(taskHandle, samplesPerChan, 0, timeout, DAQmx_Val_GroupByChannel, data, &pointsWritten, NULL));
totalPoints = totalPoints + pointsWritten;
iterator++;
}
// Clean up
DAQmxErrChk(DAQmxStopTask(taskHandle));
DAQmxErrChk(DAQmxClearTask(taskHandle));
Error:
if (DAQmxFailed(error))
DAQmxGetExtendedErrorInfo(errBuff, 2048);
if (taskHandle != 0) {
DAQmxStopTask(taskHandle);
DAQmxClearTask(taskHandle);
}
if (DAQmxFailed(error))
printf("DAQmxBase Error (%d): %s\n", error, errBuff);
return 0;
}