Leader

Friday 19 April 2013

Multiple Velleman K8055D USB Boards in Matlab

Download
K8055_multiple.zip

Containing...
K8055D_connect.m - The main script used to connect to the boards, modified from this article from the hack hole to allow up to four Velleman K8055D boards to interface with matlab. 
K9055D.h and K8055D0-3.dll - files required to connect to the boards.
setdigital.m - script used to set the digital channels on the board to represent a decimal in 8 bit binary
dec2bin2.m -  used in setdigital.m to convert decimal to binary.


Connecting...

K8055D_connect.m
The first line of this function should be edited so that it points to the folder containing the .h and .dll files.

This script accepts a vector input, CardAddress which specifies the addresses of the cards to connect to (0, 1, 2, 3) and returns a list of board it successfully connects to. For example, to connect to four cards in one go:

>>CardAddress=0:3;
>>K8055D_connect(CardAddress
ans = 
      0 1 2 3

After connecting a window pops up for each board with possible commands. These can be set to the board using the CALLLIB function, and the .dll for a single board, for example:

>> calllib('K8055D0', 'ClearAllDigital');
Sends the command to clear all the digital channels (set them to 0) for board 0.


setdigital.m
This script takes a decimal as an input, converts it to binary and sets the 8 digital channels on the specified board to a 8-bit binary representation of the decimal. Uses dec2bin2.m. For example,

>> setdigital.m(0,100)
Sets the digital channels on board 0 to represent the number 100 in binary.



Code run-through
K8055D_connect.m
The first line adds the folder containing the .h and .dll files to the Matlab path.

addpath('D:\K8055D');

A quick check is then performed on the input to make sure it contains values between 0 and 3.


if max(ChannelAddress) > 3 || min(ChannelAddress) < 0
    disp('Error: 4 cards max!')
    return
end

A for loop is used to run through each card address specified, and the LOADLIBRARY function is used to load the .dll. After this the CALLLIB function is used to send command to the board via the .dll.

    s = ['loadlibrary(''K8055D',num2str(ChannelAddress(i)), '.dll'',''K8055D.h'');']; 
    eval(s);
    s = ['tmp = calllib(''K8055D',num2str(ChannelAddress(i)),''',  ... ''OpenDevice'',ChannelAddress(i));']; 
    eval(s);

The specific command to load each board is generated and stored in a string, s, which is then evaluated by the EVAL function.


setdigital.m
The two lines of this script initially set all channels to 1.

s = ['calllib(''K8055D',num2str(number),''', ''SetAllDigital'');'];
eval(s);

The command addressing the correct .dll is generated, stored in a string and then evaluated using EVAL.

The decimal input is then converted to binary.

bin = dec2bin2(att);

And a for loop runs through each of the 8 channels, setting them to 0 where appropriate.

for i = 1:8
    if bin(i) == 0
        s = ['calllib(''K8055D',num2str(number),''', ''ClearDigitalChannel'',',num2str((8-i+1)),');'];
        eval(s);
    end
end


No comments:

AdSense