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
Using SUB-20 .NET interface in Matlab
Moderator: serg
Re: Using SUB-20 .NET interface in Matlab
I'm trying to perform the GPIO functions in Matlab 2009a but seem to be having a bit of trouble. Following your example code
NET.addAssembly('C:\Program Files\SUB-20\bin\sub20dnc.dll');
sub20 = Xdimax.Sub20();
Data = NET.createArray('System.Int32', 1);
===================
sub20.GPIO_Read(Data)
??? No method 'GPIO_Read' with matching signature found for class 'Xdimax.Sub20'.
=====================
I also have a similar problem with the GPIO_Write.
Should I not be using an Int32 for the GPIO_Read and GPIO_Write functions?
I am able to use other methods like the SPI_Write and others.
Any help would be appreciated.
Thanks
NET.addAssembly('C:\Program Files\SUB-20\bin\sub20dnc.dll');
sub20 = Xdimax.Sub20();
Data = NET.createArray('System.Int32', 1);
===================
sub20.GPIO_Read(Data)
??? No method 'GPIO_Read' with matching signature found for class 'Xdimax.Sub20'.
=====================
I also have a similar problem with the GPIO_Write.
Should I not be using an Int32 for the GPIO_Read and GPIO_Write functions?
I am able to use other methods like the SPI_Write and others.
Any help would be appreciated.
Thanks
Re: Using SUB-20 .NET interface in Matlab
Here you go
%-----------------------------------------------------------------------------------------------------------
% This sample code demonstarates accessing the SUB-20 GPIO pins
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
% Create a bitmask for GPIO12
bit_mask=int32(bitshift(1, 12));
% Configure GPIO12 as an output
if ~ sub20.GPIO_SetConfig(bit_mask, bit_mask)
break;
end
% Set GPIO12 to logical '1'
if ~ sub20.GPIO_Write(bit_mask, bit_mask)
break;
end
% Set GPIO12 to logical '0'
if ~ sub20.GPIO_Write(0, bit_mask)
break;
end
% Read all input pins to the 'val' variable
[success val] = sub20.GPIO_Read(0) ;
if ~ success
break;
end
bits = dec2hex(uint32(val));
end
err = sub20.GetLastError();
% Close SUB-20 device
sub20.Close()
if err > 0
fprintf('Error %d\n', sub20.GetLastError() )
error('Error!')
end
%-----------------------------------------------------------------------------------------------------------
%-----------------------------------------------------------------------------------------------------------
% This sample code demonstarates accessing the SUB-20 GPIO pins
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
% Create a bitmask for GPIO12
bit_mask=int32(bitshift(1, 12));
% Configure GPIO12 as an output
if ~ sub20.GPIO_SetConfig(bit_mask, bit_mask)
break;
end
% Set GPIO12 to logical '1'
if ~ sub20.GPIO_Write(bit_mask, bit_mask)
break;
end
% Set GPIO12 to logical '0'
if ~ sub20.GPIO_Write(0, bit_mask)
break;
end
% Read all input pins to the 'val' variable
[success val] = sub20.GPIO_Read(0) ;
if ~ success
break;
end
bits = dec2hex(uint32(val));
end
err = sub20.GetLastError();
% Close SUB-20 device
sub20.Close()
if err > 0
fprintf('Error %d\n', sub20.GetLastError() )
error('Error!')
end
%-----------------------------------------------------------------------------------------------------------