Saturday, January 30, 2010

Setting and clearing individual digital channels

Setting and clearing individual channels is straightforward - the appropriate functions take a value between 1 and 8 for output numbers 1-8 (as marked on the board).

Functions used:
  • SetDigitalChannel(int)
  • ClearDigitalChannel(int)


The program is built in the same way as before, but this time we'll have a Knight-Rider style progression of the LEDs

#include <iostream>
#include <time.h>
#include "K8055D_C.h"

using namespace std;


int main(void) {


    OpenDevice(0);        // select device #0

// Knight Rider 1            
    ClearAllDigital();        // set all digital outputs to off
    for (int b=0;b<5;b++){
        for (int a=1;a<9;a++){
            SetDigitalChannel(a);    // set individual digital channel
            cout << "Setting channel (incrementing) "<<a<<endl;
            Sleep(100);
            ClearDigitalChannel(a);    // clear individual digital channel
        }
        for (int a=7;a>1;a--){
            SetDigitalChannel(a);    // set individual digital channel
            cout << "Setting channel (incrementing) "<<a<<endl;
            Sleep(100);
            ClearDigitalChannel(a);    // clear individual digital channel
        }
    }
    ClearAllDigital();        // set all digital outputs to off

    
    CloseDevice();        // close device
    return 0;

}

First post, first program!

Welcome to my blog about the Velleman K8055 USB interface board.

I saw one and had to have it, but didn't have any real idea about what to do with it.  I still don't, but follow me on a journey of discovery as I learn to program it, and find games to play with it!

The first thing I wanted to do was to write a basic C++ (my language of choice for the moment) console app that interfaces with the card.  I've never written any sort of interface like this before - all my previous programming experience has been scientific & numerical.  This involved learning how to reference an external DLL, via a library and a header file.

I'm using Visual Studio 2008 Pro edition, set up for C++, all the experiences I document herein relate to this setup, and I can't comment on any other way of doing things.

The requirement of the first program is simple - make the board respond!  It has LED's to indicate the status of the outputs, so for now, the goal is to control these.

The resource file that came with the board contains three important files:
  • K8055D_C.h
  • K8055D_C.lib 
  • K8055D_C.dll
 The header file has to be #included in your main program, and the lib file has to be added as a Resource file.  This can be done by simply dragging and dropping the file in to the "Resource Files" folder of your project.  The DLL needs to be in the folder from which the program is run - usually the Debug or Release folder of your project.  The library file can be stored anywhere.

From here on out, it's straight forward.  The first program I wrote flashes all the LEDs on and off three times:

<pre style="font-family: Andale Mono, Lucida Console, Monaco, fixed, monospace; color: #000000; background-color: #eee;font-size: 12px;border: 1px dashed #999999;line-height: 14px;padding: 5px; overflow: auto; width: 100%"><code>#include &lt;iostream&gt;
#include &lt;time.h&gt;
#include &quot;K8055D_C.h&quot;

using namespace std;


int main(void) {


    OpenDevice(0);        // select device #0


// Flashing
    for(int a=0;a&lt;3;a++){
        SetAllDigital();    // set all digital outputs to on      
        Sleep(1000);        // wait one second
        ClearAllDigital();    // set all digital outputs to off
        Sleep(1000);        // wait one second
    }

    
    CloseDevice();        // close device
    return 0;

}
</code></pre>

Job jobbed!  Next, I'll look at setting individual LEDs, and the analogue outputs.