Counter/Timer

cancel
Showing results for 
Search instead for 
Did you mean: 

Generate pulse frequency with usb-6259 on matlab

Hi,

 

I would like to create a pulse with the ni usb-6259 counter via Matlab (R2007b). As Matlab Data Acquisition Toolbox dosen't support the access to the counter/timer of the 6259, I call 'DAQmxCreateCOPulseChanFreq' from the library. However, it returns a negative status indicating that the channel was not created. Can someone tell me why I have a negative status?

 

Thanks,

Miko

 

Here is the code:

 

% %load libraries nicaiu.dll,nidaqmx.h if not loaded
clc
if ~libisloaded('myni')
    disp('Matlab: Load nicaiu.dll')
    funclist = loadlibrary('nicaiu.dll', ...
        'C:\Program Files\National Instruments\NI-DAQ\DAQmx ANSI C Dev\include\nidaqmx.h', ...
        'alias', ...
        'myni' );
end
disp('Matlab: dll loaded')
disp('')

% Counter must be driven and read through 'c' function calls

% Define variable for taskhandle
taskh1 = uint32(1);

% Create task
[a,b,taskh1] = calllib('myni', 'DAQmxCreateTask', 'master', taskh1);

% Create channel name for counter input edge count
taskchans1 = ['Dev1/ctr0'];

%define required properties for pattern generation
DAQmx_Val_Hz = 10373; % a pre-scaled unit (aka DEFINE)
DAQmx_Val_Low = 10214; % resting state, pre-define unit
initialDelay = 0; % sec
freq = 16000; % Hz
dutyCycle = 0.5; % width / period. This ratio, combined with frequency, determines pulse width and intervals

% Create channel: specify duty cycle and frequency
nStatus = calllib('myni', 'DAQmxCreateCOPulseChanFreq',...
    uint32(taskh1), taskchans1, '', DAQmx_Val_Hz, DAQmx_Val_Low, initialDelay, freq, dutyCycle);

if nStatus == 0     % channel created successfully
    disp('Success')
elseif nStatus < 0  % error: channel not created
    disp('Error')
elseif nStatus > 0  % channel created with warning
    disp('Warning')
end

 

 

 

Miko
0 Kudos
Message 1 of 6
(6,781 Views)

Miko,

 

Unfortunately, I am not familiar enough with Matlab or have the tools to help troubleshoot your specific code.    I did find an example of a similar task performed in C.  The example is attached.

 

I hope this helps

 

 

-Pete

 

 

Peter C.
Applications Engineer
National Instruments
0 Kudos
Message 2 of 6
(6,765 Views)

Hi Peter,

 

The task is indeed the same but as I am not familiar with C, I can't see the little differences compared to my matlab code. It seems that I should change the Taskhandle name and type but I don't really understand how.

Anyway, I'll make some try.

Thanks for your help.

 

Miko
0 Kudos
Message 3 of 6
(6,763 Views)

Miko,

 

I found some general information on calling DAQmx functions in matlab.   You are using different hardware, but you may be able to use some parts of their code to help you.

 

 

http://forums.ni.com/t5/Multifunction-DAQ/Use-M-Series-cards-with-DAQmx-in-Matlab/td-p/185232

 

 

 

 

I hope this proves useful!

 

 

 

-Pete

 

Peter C.
Applications Engineer
National Instruments
0 Kudos
Message 4 of 6
(6,758 Views)

Thanks Peter!

 

It indeed proved useful, thanks to the last comment (slastuka p.10).

It seems that the input and output type depend on NIDAQmx version.

 

Here is the code (that works for me), in case help someone else:

clear all, close all
clc
if ~libisloaded('myni')
    disp('Matlab: Load nicaiu.dll')
    loadlibrary('nicaiu.dll', ...
        'C:\Program Files\National Instruments\NI-DAQ\DAQmx ANSI C Dev\include\nidaqmx.h', ...
        'alias', ...
        'myni');
end
disp('Matlab: dll loaded')
% libfunctionsview('myni')
% Counter must be driven and read through 'c' function calls

% Define variable for taskhandle
Taskh1 = [];

% Create task
[a,b,Taskh1] = calllib('myni', 'DAQmxCreateTask', 'Counter', Taskh1);
Taskh1 = libpointer('voidPtrPtr',Taskh1);

% Create channel name for counter input edge count
Taskchans1 = libpointer('cstring','Dev1/ctr0');%Counter name
NameChan = libpointer('cstring','');%Channel name

%define required properties for pattern generation
DAQmx_Val_Hz = int32(10373); % Units: Hz <-> 10373
DAQmx_Val_Low = int32(10214); % Resting state: Low <-> Terminal is at a low state at rest. Pulses move the terminal to a high state.  
InitialDelay = 0; % Delay (sec) from the start call to the beginning of the pulse train ; this is currently set to 0.0 by default. 
Freq = 16000; % Hz
DutyCycle = 0.5; % width / period. This ratio, combined with frequency, determines pulse width and intervals

% Create channel: specify duty cycle and frequency
nStatus = calllib('myni', 'DAQmxCreateCOPulseChanFreq',...
    Taskh1, Taskchans1, NameChan, DAQmx_Val_Hz, DAQmx_Val_Low, InitialDelay, Freq, DutyCycle);

if nStatus == 0     % channel created successfully
    disp('Success')
elseif nStatus < 0  % error: channel not created
    disp('Error')
elseif nStatus > 0  % channel created with warning
    disp('Warning')
end

 I don't know for the following, but at least I can create the channel.

 

Miko
0 Kudos
Message 5 of 6
(6,749 Views)

Great!

 

I'm glad it helped.

 

 

 

-Peter

Peter C.
Applications Engineer
National Instruments
0 Kudos
Message 6 of 6
(6,742 Views)