Shopping Cart

Your Cart is currently empty.

Home The Insider Blogs Blink your LEDs
Blink your LEDs

Did I mention April 27th for my next blog? Well, I forgot two things. Number one is a classic one: not bearing in mind that after returning from holiday, there is always a pile of work waiting on your desk. You wonder why one goes on holiday anyway (not that I won't do it again Cool). Then my second error was forgetting that last week also has a public holiday (at least here in the Netherlands): Queen's day on April 30th. And somehow, public holidays on a Thursday always manage to render the subsequent Friday rather useless.

Nevertheless, back to business. I'll start with an update of development. We were busy designing the PCB for the MODRC1 extension board. The first prototype arrived last week and it's not entirely working OK. There are only two small problems, but the character display won't work because of it (in fact it will work perfectly but you can't see anything because the contrast circuitry is not correct). So as I speak / write, the design is being corrected and will be submitted again for prototype production. Not too bad though for a first proto.

In my blog about Controlling a Digit-LS from a PC I said I would give a real life example using the LED example shown in my posting Digit-LS Programming, so let's have a look at that now. As you may recall, the Digit-LS configuration of that example expects to receive 8 bytes, each of which will contain the desired setting for the corresponding LED. A value of 0 means "LED OFF" and anything else means "LED ON". So from the PC we need to send 8 bytes of data where each byte is essentially just 1 or 0 depending on whether or not we want that LED to be on.

We will start off with determining the device path and getting a file handle to it. Please note that I omitted any error handling to make it more readable for you.

FIND_DEV_PATH ((LPGUID)&GUID_CLASS_DIGITLS_1_0, DeviceName, sizeof (DeviceName));
wcscpy (FullName, DeviceName);
wcscat (FullName, L"\\");
wcscat (FullName, L"LEDS");
hDevice = CreateFile(FullName,

Now let's fill an array of bytes with a pattern. Let's say we put the binary equivalent of 42 on the 8 LEDS, so that would be 00101010. We fill the byte array accordingly. Bear in mind that the LEDs are layed out 1 to 8 from left to right, so in terms of bit index it's the wrong way around (as least significant bit would be displayed on the right).

UCHAR WriteBytes[8];
WriteBytes[0] = 0;
WriteBytes[0] = 0;
WriteBytes[0] = 1;
WriteBytes[0] = 0;
WriteBytes[0] = 1;
WriteBytes[0] = 0;
WriteBytes[0] = 1;
WriteBytes[0] = 0;

And then send this data to the Digit-LS:


Now let's look at what happens inside the Digit-LS. The data sent from the PC will run through the USB cable and then end up in the endpoint buffer. The corresponding transaction handler will be called which is listed below. Every byte that was transferred is inspected and the corresponding output pin of Port D (to which the LEDs are connected) is switched on or off depending on the value of that byte.

int EP1Transaction(byte EPno, PacketT TransType)
   PORTDbits.RD0 = (EP01Buf._byte[0] != 0);
   PORTDbits.RD1 = (EP01Buf._byte[1] != 0);
   PORTDbits.RD2 = (EP01Buf._byte[2] != 0);
   PORTDbits.RD3 = (EP01Buf._byte[3] != 0);
   PORTDbits.RD4 = (EP01Buf._byte[4] != 0);
   PORTDbits.RD5 = (EP01Buf._byte[5] != 0);
   PORTDbits.RD6 = (EP01Buf._byte[6] != 0);
   PORTDbits.RD7 = (EP01Buf._byte[7] != 0);
   APICalls->EPSetOUTReady (EPno, DTS_TOGGLE);
   return 1;

And now let's have a look at the result:

Yippee, it works! But before going off to celebrate this succesful LED switching action, remember to close the file handle on the PC end of things. If you don't, the driver will keep the LEDs endpoint locked and other applications will not be able to access this endpoint anymore. So this will finalize the transaction:

CloseHandle (hDevice);

Well, hopefully this real-life example of interaction between PC software, device firmware and LED hardware will give you a good idea of what can be done with a Digit-LS and how it can be done. Also understanding what needs to be done on the PC and what goes in the device firmware is very helpful to come up with powerful solutions. Hope you enjoyed it and see you next week!

The Insider

Last Updated on Sunday, 17 May 2009 20:14
Copyright Sofrides IO