Using SUB-20 .NET interface in Matlab
Posted: Tue Oct 13, 2009 11:34 am
Example of reading CH1 of the SUB-20 ADC
Tested with the Matlab R2009a
%---------------------------------------------------------------
disp 'SUB-20 ADC sample. Read 16 samples from CH1'
SamplesToRead = 16;
NET.addAssembly('C:\Program Files\SUB-20\bin\sub20dnc.dll');
sub20 = Xdimax.Sub20();
% simulate do { ... } while(0) loop
for k=0:0
% Open first available SUB-20 device
if ~ sub20.Open(0)
break;
end
% Enable the ADC and set Vref=Vcc
if ~ sub20.ADC_SetConfig(sub20.AdcEnable+sub20.AdcRefVcc)
break;
end
% Create .NET arrays
Data = NET.createArray('System.Int32', SamplesToRead);
Mux = NET.createArray('System.Int32', SamplesToRead);
% Setup Mux array to CH1
for i=0:(SamplesToRead-1)
Mux.Set(i,1);
end
% ADC read
if ~ sub20.ADC_Read(Data, Mux)
break;
end
vref = 5.0;
Samples=zeros(1, SamplesToRead);
% Convert ADC samples to Volts
for i=1:SamplesToRead
Samples(i)= ( double(Data.Get(i-1)) * vref ) /1023 ;
end
end
err = sub20.GetLastError();
% Close SUB-20 device
sub20.Close()
if err > 0
fprintf('Error %d\n', err )
error('Error!')
end
Tested with the Matlab R2009a
%---------------------------------------------------------------
disp 'SUB-20 ADC sample. Read 16 samples from CH1'
SamplesToRead = 16;
NET.addAssembly('C:\Program Files\SUB-20\bin\sub20dnc.dll');
sub20 = Xdimax.Sub20();
% simulate do { ... } while(0) loop
for k=0:0
% Open first available SUB-20 device
if ~ sub20.Open(0)
break;
end
% Enable the ADC and set Vref=Vcc
if ~ sub20.ADC_SetConfig(sub20.AdcEnable+sub20.AdcRefVcc)
break;
end
% Create .NET arrays
Data = NET.createArray('System.Int32', SamplesToRead);
Mux = NET.createArray('System.Int32', SamplesToRead);
% Setup Mux array to CH1
for i=0:(SamplesToRead-1)
Mux.Set(i,1);
end
% ADC read
if ~ sub20.ADC_Read(Data, Mux)
break;
end
vref = 5.0;
Samples=zeros(1, SamplesToRead);
% Convert ADC samples to Volts
for i=1:SamplesToRead
Samples(i)= ( double(Data.Get(i-1)) * vref ) /1023 ;
end
end
err = sub20.GetLastError();
% Close SUB-20 device
sub20.Close()
if err > 0
fprintf('Error %d\n', err )
error('Error!')
end