1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
|
function [] = config_IOs()
%cCONFIG_IOS Configurates the input and output digital channels
%
% Starts tasks for 5 digital channels, 3 output and 2 input channels.
% There is one channel per task.
%
% These channels corresponds to:
%
% Output channels:
% SCLK - Serial Clock Input.
% CS_INV - Active-Low Chip Select. Data will not be clocked into DIN
% unless CS is low. When CS is high, DOUT is high impedance.
% DIN - Digital Serial Input. Data is clocked in at the rising edge of
% SCLK.
%
% Input channels:
% SSTRB - Serial Strobe Output. In internal clock mode, SSTRB goes low
% when the MAX186/MAX188 begin the A/D conversion and goes high
% when the conversion is done. In external clock mode, SSTRB
% pulses high for one clock period before the MSB decision. High
% impedance when CS is high (external mode).
% DOUT - Serial Data Output. Data is clocked out at the falling edge of
% SCLK. High impedance when CS is high.
%
% These channels corresponds to pin 19 - 15 on the ADC MAXIM MAX186 chip.
%
% Tobias Lindell - 2013-02-12
global mytaskh
global lib
DAQmx_Val_ChanPerLine =0; % One Channel For Each Line
DAQmx_Val_ChanForAllLines =1; %#ok<NASGU> % One Channel For All Lines
if isempty(lib)
lib = 'myni'; % library alias
if ~libisloaded(lib)
disp('Matlab: Load nicaiu.dll')
funclist = loadlibrary('c:\windows\system32\nicaiu.dll','C:\Program Files (x86)\National Instruments\Shared\ExternalCompilerSupport\C\include\nidaqmx.h','alias',lib); %#ok<NASGU>
%if you do NOT have nicaiu.dll and nidaqmx.h
%in your Matlab path,add full pathnames or copy the files.
%libfunctions(lib,'-full') % use this to show the...
%libfunctionsview(lib) % included function
disp('Matlab: Nicaiu.dll loaded!')
end
end
% DOlines = {'Dev1/port0/line0','Dev1/port0/line1','Dev1/port0/line2'};
lineGrouping = DAQmx_Val_ChanPerLine; % One Channel For Each Line
mytaskh.DO_SCLK = DAQmxCreateDOChan(lib,'Dev1/port0/line0',lineGrouping);
mytaskh.DO_CS_INV = DAQmxCreateDOChan(lib,'Dev1/port0/line1',lineGrouping);
mytaskh.DO_DIN = DAQmxCreateDOChan(lib,'Dev1/port0/line2',lineGrouping);
% DIlines = {'Dev1/port0/line3','Dev1/port0/line4'};
mytaskh.DI_SSTRB = DAQmxCreateDIChan(lib,'Dev1/port0/line3',lineGrouping);
mytaskh.DI_DOUT = DAQmxCreateDIChan(lib,'Dev1/port0/line4',lineGrouping);
end
function taskh = DAQmxCreateDIChan(lib,lines,lineGrouping)
% function taskh = DAQmxCreateDIChan(lib,lines,lineGrouping)
%
% this function creates a task and adds digital output line(s) to the task
%
% inputs:
% lib - .dll or alias (ex. 'myni')
% lines - line(s) to add to task
% 1 line example: 'Dev1/port0/line0'
% 2 lines example: {'Dev1/port0/line0','Dev1/port0/line1'}
% passing as .../line0-1 probably also works, but I didn't test
% lineGrouping - either DAQmx_Val_ChanPerLine or DAQmx_Val_ChanForAllLines
%
%
% C functions used:
% int32 DAQmxCreateTask (const char taskName[],TaskHandle *taskHandle);
% int32 DAQmxCreateDIChan (TaskHandle taskHandle,const char lines[],const char nameToAssignToLines[],int32 lineGrouping);
% int32 DAQmxTaskControl (TaskHandle taskHandle,int32 action);
%
% written by Tobias Lindell
% inspired by Nathan Tomlin (nathan.a.tomlin@gmail.com)
% v0 - 1302
% create task
taskh = [];
name_task = ''; % recommended to avoid problems
[err,~,taskh] = calllib(lib,'DAQmxCreateTask',name_task,uint32(taskh));
DAQmxCheckError(lib,err);
% % check whether done
% [err,b,istaskdone] = calllib(lib,'DAQmxIsTaskDone',(taskh),0);
% DAQmxCheckError(lib,err);
% create DI channel(s) and add to task
% numchan = numel(lines);
name_line = ''; % recommended to avoid problems
if ~iscell(lines) % just 1 channel
[err,~,~,~] = calllib(lib,'DAQmxCreateDIChan',taskh,lines,name_line,lineGrouping);
DAQmxCheckError(lib,err);
else % more than 1 channel to add to task
for m = 1:numel(lines) % loop to add channels
[err,~,~,~] = calllib(lib,'DAQmxCreateDIChan',taskh,lines{m},name_line,lineGrouping);
DAQmxCheckError(lib,err);
end
end
% verify everything OK
DAQmx_Val_Task_Verify =2; % Verify
[err,~] = calllib(lib,'DAQmxTaskControl',taskh,DAQmx_Val_Task_Verify);
DAQmxCheckError(lib,err);
end
function taskh = DAQmxCreateDOChan(lib,lines,lineGrouping)
% function taskh = DAQmxCreateDOChan(lib,lines,lineGrouping)
%
% this function creates a task and adds digital output line(s) to the task
%
% inputs:
% lib - .dll or alias (ex. 'myni')
% lines - line(s) to add to task
% 1 line example: 'Dev1/port0/line0'
% 2 lines example: {'Dev1/port0/line0','Dev1/port0/line1'}
% passing as .../line0-1 probably also works, but I didn't test
% lineGrouping - either DAQmx_Val_ChanPerLine or DAQmx_Val_ChanForAllLines
%
%
% C functions used:
% int32 DAQmxCreateTask (const char taskName[],TaskHandle *taskHandle);
% int32 DAQmxCreateDOChan (TaskHandle taskHandle,const char lines[],const char nameToAssignToLines[],int32 lineGrouping);
% int32 DAQmxTaskControl (TaskHandle taskHandle,int32 action);
%
% written by Tobias Lindell
% inspired by Nathan Tomlin (nathan.a.tomlin@gmail.com)
% v0 - 1302
% create task
taskh = [];
name_task = ''; % recommended to avoid problems
[err,~,taskh] = calllib(lib,'DAQmxCreateTask',name_task,uint32(taskh));
DAQmxCheckError(lib,err);
% % check whether done
% [err,b,istaskdone] = calllib(lib,'DAQmxIsTaskDone',(taskh),0);
% DAQmxCheckError(lib,err);
% create DO channel(s) and add to task
name_line = ''; % recommended to avoid problems
if ~iscell(lines)
[err,~,~,~] = calllib(lib,'DAQmxCreateDOChan',taskh,lines,name_line,lineGrouping);
DAQmxCheckError(lib,err);
else % more than 1 channel to add to task
for m = 1:numel(lines)
[err,~,~,~] = calllib(lib,'DAQmxCreateDOChan',taskh,lines{m},name_line,lineGrouping);
DAQmxCheckError(lib,err);
end
end
% verify everything OK
DAQmx_Val_Task_Verify = 2; % Verify
[err,~] = calllib(lib,'DAQmxTaskControl',taskh,DAQmx_Val_Task_Verify);
DAQmxCheckError(lib,err);
end
function DAQmxCheckError(lib,err)
% function DAQmxCheckError(lib,err)
%
% read error code
% zero means no error - does nothing
% nonzero - find out error string and generate error
%
% inputs:
% lib = .dll or alias (ex. 'myni')
% err = DAQmx error
%
% written by Tobias Lindell
% inspired by Nathan Tomlin (nathan.a.tomlin@gmail.com)
% v0 - 1302
if err ~= 0
% find out how long the error string is
[numerr,~] = calllib(lib,'DAQmxGetErrorString',err,'',0);
% get error string
errstr = char(1:numerr); % have to pass dummy string of correct length
[~,errstr] = calllib(lib,'DAQmxGetErrorString',err,errstr,numerr);
% matlab error
error(['DAQmx error - ',errstr])
end
end
|