diff options
| author | vikle012 <viktor.leek@liu.se> | 2019-09-11 13:57:14 +0200 |
|---|---|---|
| committer | vikle012 <viktor.leek@liu.se> | 2019-09-11 13:57:14 +0200 |
| commit | e66efbbe8df2dd4e7de0a1e9bd129cf92e00f92f (patch) | |
| tree | 5ec35be0c219a2e6b017e12e450e76157b9de044 /Kod/bilbana/IOFunctions/config_IOs.m | |
| download | tfyy51-e66efbbe8df2dd4e7de0a1e9bd129cf92e00f92f.tar.gz | |
Initializes repo.
Diffstat (limited to 'Kod/bilbana/IOFunctions/config_IOs.m')
| -rw-r--r-- | Kod/bilbana/IOFunctions/config_IOs.m | 191 |
1 files changed, 191 insertions, 0 deletions
diff --git a/Kod/bilbana/IOFunctions/config_IOs.m b/Kod/bilbana/IOFunctions/config_IOs.m new file mode 100644 index 0000000..0a84d2a --- /dev/null +++ b/Kod/bilbana/IOFunctions/config_IOs.m @@ -0,0 +1,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
\ No newline at end of file |
