ADC20/24 Hight Resolution Data Logger MATLAB code (working)

Post your MATLAB discussions here
Post Reply
harrissimo
Newbie
Posts: 0
Joined: Mon Oct 18, 2010 10:15 am

ADC20/24 Hight Resolution Data Logger MATLAB code (working)

Post by harrissimo »

Hi everyone,

I don't know if this would be useful to anyone but I've not been able to find any working MATLAB code for the ADC20/24 data logger. As such I've written the following code which works.

This code only records data from the devices. I haven't actually attempted to get the digital I/O working as we didn't need this for what we are doing! I might update this when I get time as well and possibly a GUI.

Hope this helps!



Code: Select all

%<<<<<<<<  Pico Data Logger ADC-20/24 MATLAB Code >>>>>>>>

%==========================================================================

%You need to have HRDL.dll (rename PicoHRDL.dll to HRDL.dll) and HRDL.h in work path directoy
%
%A full description of the header file HRDL.h is contained in the ADC20.en.pdf
%document under the 'programmer's reference' section
%
%Running the MATLAB command y = libfunctions(LibName, '-full') (having 
%loaded the header HRDL.h) brings up a list of the header file functions 
%and the associated variable types that go with each command

%==========================================================================

clear
clc

%Define your workpath
WorkPath = 'C:\......\PicoHRDL';
cd C:\......\PicoHRDL

% Load the PicoHRDL library;
 LibName = 'HRDL';
 fprintf('\n%s','Loading PicoHRDL library: ')
 loadlibrary([LibName '.dll'],[LibName '.h']);
 if libisloaded(LibName) == 1;
     fprintf('\t%s','OK')
 else
     fprintf('\t%s','FAILED')
 end
 
 %Start up the unit and test it is functioning correctly
 fprintf('\n%s','Checking unit runs ok:')
 OpenHRDL = calllib(LibName,'HRDLOpenUnit');
 if OpenHRDL >= 1;
     fprintf('\t%s','OK')
 else
     fprintf('\t%s','FAILED')
 end
 
%==========================================================================

% This just gives information about the unit 
 
%  %Get unit info
%  USBType='';
%  USBPtr = libpointer('cstring',USBType);
%  [SLength, USBType] = calllib(LibName,'HRDLGetUnitInfo',OpenHRDL,USBPtr,255,1);
%  SerialNo='';
%  SerPtr = libpointer('cstring',SerialNo);
%  [SLength, SerialNo] = calllib(LibName,'HRDLGetUnitInfo',OpenHRDL,SerPtr,255,4);
%  OSVersion='';
%  OSPtr = libpointer('cstring',OSVersion);
%  [SLength, OSVersion] = calllib(LibName,'HRDLGetUnitInfo',OpenHRDL,OSPtr,255,6);
%  
%  fprintf('\n%s','USB:   ')
%  fprintf(USBType)
%  fprintf('\n%s','Serial Number:   ')
%  fprintf(SerialNo)
%  fprintf('\n%s','OS  Version:   ')
%  fprintf(OSVersion)

%==========================================================================

%Disable all channels
for DisChannel=1:1:8
ChanDisMessage = ['Disbaling channel number ',num2str(DisChannel),':  '];
fprintf('\n%s',ChanDisMessage)
DisChannelError = calllib(LibName,'HRDLSetAnalogInChannel',OpenHRDL,DisChannel,0,0,0);
if DisChannelError == 1
fprintf('\t%s','OK')
else
fprintf('\t%s','FAILED')
end
end

%Define how many channels you wish to use, the volatge range and the mode you wish to run the datalogger in 
fprintf('\n%s')
NoChannelsEn = input('Please input the number of channels you would like to use (1 to 8):   ');
VoltageRange = input('Please input the voltage range you would like to use (0 = 1250mv, 1 = 2500mv):   ');
Mode = input('Please indicate which mode you would like the data logger to run in (0 = Differential, 1 = Single Ended):    ');

%Enable all channels you wish to use
for EnChannel=1:1:NoChannelsEn
ChanEnMessage = ['Enabling channel number ',num2str(EnChannel),':  '];
fprintf('\n%s',ChanEnMessage)
EnChannelError = calllib(LibName,'HRDLSetAnalogInChannel',OpenHRDL,EnChannel,1,VoltageRange,Mode);
if EnChannelError == 1
fprintf('\t%s','OK')
else
fprintf('\t%s','FAILED')
end
end

%Check the correct number of channels have been enabled
NoEnChannels = int16(zeros(1,1));
NoEnChannelsPointerType = 'int16Ptr';
NoEnChannelsPointer = libpointer(NoEnChannelsPointerType, NoEnChannels);
[NoEnChannelsError,NoEnChannels] = calllib(LibName,'HRDLGetNumberOfEnabledChannels',OpenHRDL,NoEnChannelsPointer);
NoEnChannelsMessage = ['Number of enabled Channels: ',num2str(NoEnChannels)];
fprintf('\n%s', NoEnChannelsMessage)

%Define the interval between readings
fprintf('\n%s')
Interval = input('Please input the interval you would like between readings in ms:    ');
fprintf('\n%s','Loading delay:  ')
DelayError = calllib(LibName,'HRDLSetInterval',OpenHRDL,Interval,0);
if DelayError == 1
fprintf('\t%s','OK')
else
fprintf('\t%s','FAILED')
end

%Define the number of readings you would like to take and how you would
%like the datalogger to record the data
fprintf('\n%s')
Number = input('Please input the number of readings you would like to take:    ');

Type = input('Please input the type of readout you would like to use (0 = continous, 1 = burst, 2 = single):    ');

%The first mode (continuous) records data to MATLAB as it is generated in the
%datalogger.  The time between readings will not correspond exactly to the
%interval you have set due to the time taken to run the MATLAB code (~200ms)
if Type == 0
    DataBuffer = uint32(zeros(Number,NoEnChannels));
    OverflowBuffer = uint16(zeros(Number,1));
    
for n = 1:1:Number
    
    tic;
    TimeBuffer(n,:) = clock;
    pause(Interval/1000)
    BufferPointerType = 'int32Ptr';
    DataPointer = libpointer(BufferPointerType,DataBuffer(n,:));
    OverflowPointerType = 'int16Ptr';
    OverflowPointer = libpointer(OverflowPointerType,OverflowBuffer(n));
    [UsbGrabError, DataBuffer(n,:), OverflowBuffer(n)]= calllib(LibName,'HRDLGetValues',OpenHRDL,DataPointer,OverflowPointer,1);
    toc;
    
end

%The second mode stores the data internally in the datalogger's buffer and
%then reads out the entire data set at once
elseif Type == 1
    
DataBuffer = uint32(zeros(1,NoEnChannels*Number));
TimeBuffer = uint32(zeros(1,Number));
OverflowBuffer = uint16(zeros(1,Number));
ClockStart = clock;   
    DataError = calllib(LibName,'HRDLRun',OpenHRDL,Number,0);
    pause(Number *(Interval/1000));
    ReadyError = calllib(LibName,'HRDLReady',OpenHRDL);
    if ReadyError == 0
        DataErrorMessage = ['There was a problem accessing the data.  Please press any key to carry on.'];    
        fprintf('\n%s',DataErrorMessage)
        pause
    elseif ReadyError == 1
        BufferPointerType = 'int32Ptr';
        DataPointer = libpointer(BufferPointerType,DataBuffer);
        TimePointer = libpointer(BufferPointerType,TimeBuffer);
        OverflowPointerType = 'int16Ptr';
        OverflowPointer = libpointer(OverflowPointerType,OverflowBuffer);
        [UsbGrabError, TimeBuffer, DataBuffer, OverflowBuffer]= calllib(LibName,'HRDLGetTimesAndValues',OpenHRDL,TimePointer,DataPointer,OverflowPointer,Number);
    end
end   

%This closes the unit down.  WARNING not closing the unit before trying to
%open it again in MATLAB will cause MATLAB to crash
CloseUnitError = calllib(LibName,'HRDLCloseUnit',OpenHRDL);
if CloseUnitError == 1
fprintf('\t%s','Unit closed correctly')
else
fprintf('\t%s','There was a problem')
end

end

Chris
Site Admin
Site Admin
Posts: 169
Joined: Tue Aug 17, 2010 9:00 am
Location: St. Neots

Re: ADC20/24 Hight Resolution Data Logger MATLAB code (worki

Post by Chris »

Thank you for this contribution.

Regards,

-Chris.

Post Reply