?? sfunusbadda.m
字號:
function [sys,x0,str,ts] = sfunUsbADDA(t,x,u,flag,chin,chout,tempT,detaT)
%sfunUsbADDA 是利用www.dzmi.com網(wǎng)站設(shè)計的usb數(shù)據(jù)采集卡的AD采集的S函數(shù)。
%chin為采集的通道號。1-4代表AD的ch1到ch4。
%chout為DA的通道號。1-2代表DA的ch1到ch2。
%tempT為采集卡的實際采樣速率。detaT為采樣速率微調(diào)。
switch flag,
%%%%%%%%%%%%%%%%%%
% Initialization %
%%%%%%%%%%%%%%%%%%
case 0,
[sys,x0,str,ts] = mdlInitializeSizes(tempT);
%%%%%%%%%%
% Update %
%%%%%%%%%%
case 2,
sys = mdlUpdate(t,x,u,chin,chout,tempT,detaT);
%%%%%%%%%%
% Output %
%%%%%%%%%%
case 3,
sys = mdlOutputs(t,x,u);
%%%%%%%%%%%%%
% Terminate %
%%%%%%%%%%%%%
case 9,
sys=mdlTerminate(t,x,u,chout);
otherwise
error(['unhandled flag = ',num2str(flag)]);
end
%end sfundsc2
%
%=============================================================================
% mdlInitializeSizes
% Return the sizes, initial conditions, and sample times for the S-function.
%=============================================================================
%
function [sys,x0,str,ts]=mdlInitializeSizes(tempT)
sizes = simsizes;
sizes.NumContStates = 0;
sizes.NumDiscStates = 1;
sizes.NumOutputs = 1;
sizes.NumInputs = 1;
sizes.DirFeedthrough = 0;
sizes.NumSampleTimes = 1;
sys = simsizes(sizes);
x0 = 0;
str = [];
ts = [tempT 0]; % Sample period of 0.1 seconds (10Hz)
% end mdlInitializeSizes
%
%=======================================================================
% mdlUpdate
% Handle discrete state updates, sample time hits, and major time step
% requirements.
%=======================================================================
%
function sys = mdlUpdate(t,x,u,chin,chout,tempT,detaT)
Send_Data(u,chout); %發(fā)送數(shù)據(jù)u給OUT1通道。
pause(tempT-detaT);%此處和Ts的區(qū)別在于:Ts確定了示波器橫坐標(biāo)數(shù)值,
%而此處是采集速度的真實時間值。嚴(yán)格來講,此處應(yīng)該減去一個常量:usb傳輸和采集的時間。以
%確保Ts和采集速度的盡量一致。假定此常量為0.008.可通過仿真時間與實際時間的比較估算.
sys= Read_Data(chin); %讀取CH通道的數(shù)據(jù)。每次讀一個點
%end mdlUpdate
%=======================================================================
% mdlOutputs
% Return the output vector for the S-function
%=======================================================================
function sys = mdlOutputs(t,x,u)
sys = x;
%=======================================================================
% mdlTerminate
%=======================================================================
function sys=mdlTerminate(t,x,u,chout)
sys = [];
Send_Data(0,chout); %發(fā)送數(shù)據(jù)0給OUT1通道。
%———以下函數(shù)不要輕易改動———————————————————————————
function Send_Data(temp,channel)
%函數(shù)名稱:function Send_Data(temp,channel)
%入口參數(shù):temp為輸入數(shù)據(jù),10位精度。channel為通道選擇。
%出口參數(shù):無
%函數(shù)功能:發(fā)送指定電壓到DA。
tempH=fix(temp/256);%將一個10進(jìn)制數(shù)轉(zhuǎn)化成兩個字節(jié)。
tempL=mod(temp,256);
dzmiUSB(1,channel,tempH,tempL);
%--------------------------------------------------------------------------
function data = Read_Data(channel)
%函數(shù)名稱:function data=Read_Data(channel)
%入口參數(shù):channel為通道選擇
%出口參數(shù):data為接收到的數(shù)據(jù)。10位精度。
%函數(shù)功能:讀取指定通道的數(shù)據(jù)。
data=dzmiUSB(0,channel);
%--------------------------------------------------------------------------
?? 快捷鍵說明
復(fù)制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -