VB Form to read ADC results

All about Sub-20 Multi Interface USB Adapter USB to I2C, SPI, GPIO, RS232, RS485, Ir, LCD

Moderator: serg

Post Reply
xol
Site Admin
Posts: 241
Joined: Sat Aug 29, 2009 8:04 am

VB Form to read ADC results

Post by xol »

Below code is a VB sample to read 8 ADC channels with ADC_Read

Code: Select all

Public Class Form1
' Read all the 8 ADC channels 3 times i.e 8*3=24

Private Const SamplesToRead As Integer = 24
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click

Dim dev As New Sub20
Dim Data(SamplesToRead) As Integer
Dim Mux(SamplesToRead) As Integer
Dim i As Integer

' Open SUB-20 device
If Not dev.Open(0) Then
   MsgBox(dev.GetStrError(dev.GetLastError()))
   Exit Sub
End If

Do
   ' Enable the ADC and set Vref=Vcc
    If Not dev.ADC_SetConfig(Xdimax.Adc.Enable Or Xdimax.Adc.RefVcc) Then
      Exit Do
   End If

    ' Setup the Mux to read all the channels, i.e Ch0,Ch1,Ch2...Ch7,Ch0,Ch1.. etc
   For i = 0 To Data.GetLength(0) - 1
      Mux(i) = i Mod 8
      Next

    ' Read samples
   If Not dev.ADC_Read(Data, Mux) Then
      Exit Do
   End If

    Loop While False
      If dev.GetLastError() > 0 Then
         MsgBox(dev.GetStrError(dev.GetLastError()))
      End If
      
   ' Close SUB-20 device
   dev.Close()
   
End SUB
End Class

sasha.tr
Posts: 1
Joined: Mon Aug 31, 2009 6:10 pm

Re: VB Form to read ADC results

Post by sasha.tr »

Hi.
What is A/D precesion?

xol
Site Admin
Posts: 241
Joined: Sat Aug 29, 2009 8:04 am

Re: VB Form to read ADC results

Post by xol »

The A/D resolution is 10bit. Precision is +-2 lsb. For 3.3V reference it will be +-6.4mV

serg
Posts: 143
Joined: Mon Aug 31, 2009 9:17 pm

Re: VB Form to read ADC results

Post by serg »

Reading ADC from Matlab R2009a, Sample code

%---------------------------------------------------

disp 'SUB-20 ADC sample. Read 16 samples from CH1'

SamplesToRead = 16;

NET.addAssembly('C:\Program Files\SUB-20\bin\sub20net.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', sub20.GetLastError() )
error('Error!')
end

%---------------------------------------------------

Post Reply