views:

582

answers:

2

Is there an API function call for this board that would allow me to generate a clock signal on an output at 500 kHz while running some other code on the board? Thanks in advance for the advices.

+1  A: 

I found Example: Generating Timer Events in the Data Acquisition Toolbox documentation.

dio = digitalio('nidaq','Dev1');
addline(dio,0:7,'in');
set(dio,'TimerFcn',@daqcallback)
set(dio,'TimerPeriod',5.0)
start(dio)
pause(11)
delete(dio)
clear dio
eed3si9n
Thanks for the answer. It may take me a day or two to get a chance to test it out on the hardware though. I'll let you know how it goes.
stanigator
+2  A: 

According to the Supported Hardware documentation, version 2.8 or greater of the Data Acquisition Toolbox is needed to support a Measurement Computing USB-1024HLS device. Assuming you have version 2.8 or newer, the following should come close to a solution for you...

The first step would be to get the hardware ID for the device. The function DAQHWINFO should help with this:

deviceInfo = daqhwinfo('mcc');

The hardware ID gotten from the structure deviceInfo can then be used to create a Digital I/O Object (DIO) using the DIGITALIO function:

dio = digitalio('mcc',hardwareID);

Next, you have to add two output lines (for a clock signal and a pulse-width modulation (PWM) signal) using ADDLINE:

addline(dio,0:1,'out');

Then, you have to set a few DIO properties.

set(dio,'TimerPeriod',0.000002);  % i.e. 500 kHz
set(dio,'TimerFcn',@update_outputs);

The function update_outputs is called once every timer period and should set the output pins to the appropriate values. The clock signal is simply going to switch back and forth between 0 and 1 every timer period. The PWM signal will likely alternate between 0 and 1 as well, but it will not change every timer period, remaining in each state for a set amount of time based upon the sort of pulse-width modulation you want. Here's what your update_outputs function may end up looking like:

function update_outputs(obj,event)
  currentValues = getvalue(obj);
  clockValue = ~currentValues(1);
  pwmValue = pwm_compute();
  putvalue(obj,[clockValue pwmValue]);
end

Note that this uses PUTVALUE and GETVALUE to set/get the values of the output pins. You will have to write the function pwm_compute such that it computes a new PWM value for each time period. Since pwm_compute will likely have to know how many values have been output already (i.e. how many times it has already been called), you can track that using a persistent variable:

function newValue = pwm_compute
  persistent nValues;
  if isempty(nValues)
    nValues = 0;
  else
    nValues = nValues+1;
  end
  ...
  % Compute the new value for the (nValues+1) time period
  ...
end

This is just one possible solution. You could potentially precompute the PWM signal and pull the value for each timer period from a vector or data file, or you could potentially use the event data structure passed to update_outputs to get the time of the timer event (relative to the DIO timer start, I believe).

Finally, you have to start the DIO:

start(dio);

...and, once you're finished using it, delete it and clear it from memory:

delete(dio);
clear dio;

One potential stumbling block...

Generating a 500 kHz signal could be difficult. It's such a high frequency that you may run into problems, specifically with the 'TimerFcn', which is called once every timer period. If the 'TimerFcn' takes longer than 0.000002 seconds to run, some timer events may not be processed, leading to an output that is actually of a lower frequency. I have a feeling you may have to use a lower signal frequency for things to work properly, but I could be wrong. =)

gnovice
According to http://www.mathworks.com/access/helpdesk/help/toolbox/daq/index.html?/access/helpdesk/help/toolbox/daq/daq_product_page.html, the newest version of Data Acquisition Toolbox is 2.14, but I have still managed to communicate to my DAQ somehow. I just haven't implemented the clock signal yet (that you have suggested). It would be great if I can hear your thoughts about the Data Acquisition Toolbox version from what you have read. Thanks for the effort.
stanigator
If you have the newest version, you're good. It's "two-point-fourteen" as opposed to "two-point-one-four", if that was the confusion (i.e. it is 6 better than "two-point-eight"). =)
gnovice