Multifunction DAQ

cancel
Showing results for 
Search instead for 
Did you mean: 

Syncing AI and DO on M-6221

Hi there!

I cant imagine i'm the first person to ask this but unfortunately cant find the right fitting answer.

I have the problem, that i want to sync my analog input and digital output.

At this moment i am reading data from the ADC und logging how many samples i aquired by the variable "readvalues". And every 32 aquired samples i want to set my digital output (dividing down the ai/sampleclock by 32). This works fine so far. But now i see that, dependent on how much load i have on my windows pc, the digital out isnt set sometimes. I can see on the scope, that (while my pc is doing other things than my program e.g. opening another program) the DO is shifted and the pulses are getting longer or shorter but not constant.
What can i try to do? Im actually out of ideas...

Here is an excerpt of my code:
[quote]

while(stoptest!=true)
    {
        samplecounter = 0;
        while (samplecounter == 0)
            errorcode = DAQmxReadAnalogF64(InputHandle, sampsperchan, timeout, DAQmx_Val_GroupByScanNumber, analoginputdata, buffersize, &samplecounter, NULL);

        readvalues++;
        if (readvalues > 32768)
            readvalues = 0;

        if (readvalues % 32 == 0)
        {
            sampswritten = 0;
            while (sampswritten == 0)
                printError(DAQmxWriteDigitalLines(DOHandle1, 1, 1, timeout, DAQmx_Val_GroupByChannel, DOarray, &sampswritten, NULL), "Error writing digital lines on Port 1!");
               
            sampswritten = 0;
            while (sampswritten == 0)
                printError(DAQmxWriteDigitalLines(DOHandle2, 1, 1, timeout, DAQmx_Val_GroupByChannel, DOarray2, &sampswritten, NULL), "Error writing digital lines on Port 2!");
}
[/quote]

Timing is configured for the InputHandle by

[quote]
printError(DAQmxCfgSampClkTiming(InputHandle, NULL, samplerate, DAQmx_Val_Rising, DAQmx_Val_ContSamps, buffersize), "Problem setting up the sampleclock...");
[/quote]

Your help is greatly appreciated.

Installed Driver is NI-DAQ 8.0
Installed Hardware M-6221
Used Software: Visual C++
In order to understand the recursion, you have to understand the recursion at first!
0 Kudos
Message 1 of 4
(3,151 Views)
Hi!

The best way do synchronize the AI with the DO is to use the same clock for both. In your case you can use the clock of AI for your DO task.
You can implement:

errorcode = DAQmxCfgSampClkTiming (dotask, "/Dev1/ai/SampleClock", 100, DAQmx_Val_Rising, DAQmx_Val_ContSamps, 100);

In this case the clock for your digital output task will be the same of the clock used from AI.
Preparing 16 HIGHs and 16LOWs will create a pulse train 32 time slower as your AI Clock.

So the all synchronisation is made in Hardware and not in Software.
In Software you have the problem, that other processes use the CPU. That brings your DO process to be delayed.

Here an example for AI-AO Synchronisation. In your case you have to change AO with DO.
http://zone.ni.com/devzone/cda/epd/p/id/2352

Best regards

Ken

Message 2 of 4
(3,125 Views)
Hi Ken!

Great Idea! I will try that asap!

Yesterday i tried my luck with rewiring my board and integrating a counter as the clock for DO. That worked so far. But i like your Idea much more! It's a lot easier! I will report on next problems and/or success.
In order to understand the recursion, you have to understand the recursion at first!
0 Kudos
Message 3 of 4
(3,118 Views)
It's me again.

I tried using the ai/sampleclock for DO. That works fine so far.

Unfortunatley i have 2 DO task. I wanted to equip both task with the /ai/sampleclock timing. Now DAQmx tells me of Error -50103

"The specified resource is reserved. The operation could not be completed as specified."

When i equip only one task with the sampleclock everything works fine.

I can imagine putting the 2 DO Task together in one. This would do the same. But it's getting a bit confusing because they dont really belong to each other. Why doesn't that work.


In order to understand the recursion, you have to understand the recursion at first!
0 Kudos
Message 4 of 4
(3,099 Views)