LED through IO ports

Discuss Programming
shuiend
scripter
scripter
Posts: 91
Joined: Mon Apr 28, 2003 8:05 pm

LED through IO ports

Post by shuiend » Tue Sep 09, 2003 9:20 am

Void Main didnt u say on the MES one time that you could program leds to light up through a printer port or something? In skool something like that came up as a project and i immdeitely thought of you and that u might have done it. If you have any info on the subject or know who did that if it wasent you i would like it.

User avatar
Void Main
Site Admin
Site Admin
Posts: 5716
Joined: Wed Jan 08, 2003 5:24 am
Location: Tuxville, USA
Contact:

Post by Void Main » Tue Sep 09, 2003 9:42 am

Sure I did it. It was pretty simple actually and I can give you my source code when I get home. I just created a little stub file in C to do the low level stuff (just a few lines of C) and then I used Perl-TK to create a graphical interface that would display which leds were turned on and off at the same time the leds were on or off. I also added in a way to create a song and have it play the song through the PC speaker and light the colored leds at the appropriate time. Really basic stuff (more of a way for me to get a taste of Perl-Tk really). Of course this was all done on Linux. You might check the programming section at MES (I haven't been back there since I left way back when). If you find the thread it might be worth copying out, I wouldn't mind seeing it as well to refresh my memory.

Here is another thread where I posted the C stub to send data to the parallel port:
http://voidmain.is-a-geek.net/forums/vi ... hlight=led

I can expand on it later...

Master of Reality
guru
guru
Posts: 562
Joined: Thu Jan 09, 2003 8:25 pm

Post by Master of Reality » Tue Sep 16, 2003 3:18 pm

i always liked the HOWTO at tldp.org about how to run a coffee machine via your computer.

User avatar
Void Main
Site Admin
Site Admin
Posts: 5716
Joined: Wed Jan 08, 2003 5:24 am
Location: Tuxville, USA
Contact:

Post by Void Main » Tue Sep 16, 2003 5:04 pm

Hey, that is pretty much everything you need:

http://en.tldp.org/HOWTO/Coffee.html

Good call!

shuiend
scripter
scripter
Posts: 91
Joined: Mon Apr 28, 2003 8:05 pm

Post by shuiend » Mon Sep 29, 2003 9:40 am

LSD will design and program and LED Light system with the following specifications. We will define, design, and develop at least an 8 LED light display system controllable via use input. The user will have to option to turn on 1, multiple, or all lights at one. They will be able to do this from a GUI and it will be able to be reused in other programs. All materials should be provided to use. The final system will include detailed instructions itemizing: materials purchased, how to assemble/disassemble the LED board, connection specifications, I/O drivers details and source code for the GUI. The required system will be able to be reused in other programs and used for learning.

That is the exact program I need to right for school. Right now I need to figure out what i need to make the LED Light Board thing. I have next tono knowledge of electronics so I am stumped here.

For the GUI i am going to Use QT. I am getting 2 books on QT from amazon.com and should not have any trouble with that.

I also do not know anything about I/O driver programming. My teacher says it is possible to do all in c++ and not have any assembly. I would like to know if that is true? Also voidmain i searched the MES and could not find your code for the lights. I would like to know if you still have it and if i could get a copy of it. Also do youknow of any websites that have any tutorials for I/O drivers?

User avatar
Void Main
Site Admin
Site Admin
Posts: 5716
Joined: Wed Jan 08, 2003 5:24 am
Location: Tuxville, USA
Contact:

Post by Void Main » Mon Sep 29, 2003 11:05 am

That is no problem, the C code I use to control the LEDs is in the thread I linked to about 4 messages above this one. It's just a few lines and once you compile it you just execute the program and pass it a number from 0 through 255 as a parameter. 0 turns all 8 LEDs off, 255 turns all LEDs on. To tell which LEDs will be turned on or off convert the number to binary. For instance, 0 is:

00000000

no LEDs on.

255 is:

11111111

all LEDs on.

4 is:
00000100

the 6th LED is on all others off.

128 is:

01000000

The first 2nd LED is on, all others off.

128+4=132 is:

01000100

The 2nd and 6th LEDs on, all others off, etc, etc.

I used a simple Perl/Tk script for the GUI to manipulate the lights. If I remember I will post that script in a message this evening for you.

I bought a small breadboard, some various colored LEDs, 400 Ohm resistors (that I did not use), wire, and a paralell end to connect the wires to from Radio Shack. There is a document out there somewhere that describes this but it is very simple. Try this Google search.

shuiend
scripter
scripter
Posts: 91
Joined: Mon Apr 28, 2003 8:05 pm

Post by shuiend » Mon Sep 29, 2003 3:26 pm

Thanks for that quick reply. I have compiled and tried that code and it seems it would work as soon as i get the breadbox and LEDS from radio shack. I found the article you talked about that tells howto do it and as soon as my school approves of the purchase i will be making it.

When i was looking at the code trying to understand how it really worked i was a bit confused. My teacher told me that i had to do alot of intialization of the divice and what not. It seemed like your code only had a couple of lines to make the lights turn on.
outb((unsigned char)value, base);
That line confuses me on how it works. All other lines i can work out.

Could you also explain the way you choose diffrent leds. I dont quite understand it. I know howto convert the number to binary it is just how do i reach the number.

The code for your GUI would be nice just so i could look at it and how you set it up to choose which leds. I plan on doing it in QT just cause i will have other projects later on that i will need to do GUI's in and QT seems the easyist to work with in c++.

User avatar
Void Main
Site Admin
Site Admin
Posts: 5716
Joined: Wed Jan 08, 2003 5:24 am
Location: Tuxville, USA
Contact:

Post by Void Main » Mon Sep 29, 2003 3:52 pm

Maybe the man page for outb() will help. Then again, maybe it won't. :) It's really a very simple system call that outputs a byte of data to a specified I/O port address. If you use the printer port I/O address, that's where your byte will go.

Regarding the value that you pass as a parameter it's just a byte (0-255). You have 8 data pins on your parallel port, each one turned on or off by the value of the bits in the byte that you pass. For instance, you pass 255 which is the same as "11111111" in binary, that would turn all 8 data pins on and thus light up all 8 LEDs. If you pass the number "3" to the program which is the same as "00000010" in binary you would just turn on the 7th data pin and all others would be off, thus the 7th LED light will turn on and all others will be turned off. Pass a "0" and all lights will be off. Each pin is represented by each bit of the byte that you pass. If you have a multimeter you can test this without even hooking up your LEDs. Set it to something like 10Volt DC scale (I believe the pins are 6vDC but you might want to set your multimeter to a higher scale at first just in case I am wrong) and carefully touch each of the data pins with the red wire while touchin the black wire to the ground pin (you might want to find some thin wire to wrap around your leads to poke in the holes. Run the program and pass different values. You should see the appropriate data pins energize. And no, in Linux you don't have to go through a complicated set of instructions to access the ports directly as you can see.

Again, hopefully I'll remember to paste up my Tk code this evening, have a lot of work to get done after my weekend away (my son blew the engine in his dirt bike so I have to tear it apart and get it running for next weekend).

User avatar
Void Main
Site Admin
Site Admin
Posts: 5716
Joined: Wed Jan 08, 2003 5:24 am
Location: Tuxville, USA
Contact:

Post by Void Main » Mon Sep 29, 2003 8:49 pm

First read the message right above this one (if you haven't already). Ok, I put my perl stuff and the C code for the lptout program here. wget the lights.tgz and put them in a directory called "lights" under your home directory, compile the lpt file and SUID root it:

Code: Select all

$ mkdir ~/lights
$ cd ~/lights
$ wget http://voidmain.is-a-geek.net/files/lights/lights.tgz
$ tar -xvzf lights.tgz
$ make lptout
$ su
(enter root's password)
# chown root lptout
# chmod u+s lptout
# exit
$ ./xlights
Hack up the xlights script or the xlights.pl Perl/Tk script however you want. I originally wrote this to have the LED lights flash and play a little tune through the PC speaker. It may be a little much for what you are doing but it's a very short script. You may have to install a Perl module or two if it complains when you try and run it:

Code: Select all

# rpm -Uvh ftp://216.254.0.38/linux/contrib/libc6/i386/perl-Tk-800.024-2.i386.rpm

shuiend
scripter
scripter
Posts: 91
Joined: Mon Apr 28, 2003 8:05 pm

Post by shuiend » Tue Sep 30, 2003 2:59 pm

Thanks i will be looking through all that tommorow at school during the class i am doing it in. I was wondering though about the C code is it GPL? Also could i get your name so i could put that on it for my teacher. I dont intend to just copy your code in the end i am more using it to figure out how to do it in my program and i dont want to be sued or anyhting for copyright infrigement or such.

User avatar
Calum
guru
guru
Posts: 1349
Joined: Fri Jan 10, 2003 11:32 am
Location: Bonny Scotland
Contact:

Post by Calum » Tue Sep 30, 2003 3:41 pm

as an aside, i dimly remember in high school writing a simple program to manipulate LEDs from a printer port but we wrote it in machine code (which i have totally forgotten now! such a shame, if only i had known i would want this skill later!) we did ours on the Acorn BBC i think, a computer most americans will never have seen i bet!

User avatar
Void Main
Site Admin
Site Admin
Posts: 5716
Joined: Wed Jan 08, 2003 5:24 am
Location: Tuxville, USA
Contact:

Post by Void Main » Tue Sep 30, 2003 3:49 pm

The C code is not under any license, it's just common code that could be used for anything really. There is only one line that is key and that is the "outb()" function, the rest is just checking for a valid parameter being passed, you can change all that you want. The outb() system call itself is really nothing more than an ASM instruction that directly correlates to an x86 processor instruction. Take the code, use it, be happy. As far as my name, it's spelled V-o-i-d M-a-i-n. :)

X11
guru
guru
Posts: 676
Joined: Sun Jan 19, 2003 11:09 pm
Location: Australia
Contact:

Post by X11 » Wed Oct 01, 2003 8:12 am

I am thinking of wiring up all my systems reset buttons to another computer, that will reboot them if they dont seem to be working, that would be cool.

One day, when I have time.

shuiend
scripter
scripter
Posts: 91
Joined: Mon Apr 28, 2003 8:05 pm

Post by shuiend » Wed Oct 01, 2003 9:27 am

Well i talked to my teacher about the outb() function and she will not let me use it. So i will have to actually figure out how to right an I/O driver. So would you happen to have any information on writing an I/O driver?

User avatar
Void Main
Site Admin
Site Admin
Posts: 5716
Joined: Wed Jan 08, 2003 5:24 am
Location: Tuxville, USA
Contact:

Post by Void Main » Wed Oct 01, 2003 12:00 pm

shuiend wrote:Well i talked to my teacher about the outb() function and she will not let me use it. So i will have to actually figure out how to right an I/O driver. So would you happen to have any information on writing an I/O driver?
Huh? What the heck does she want you to use? On what operating system? You aren't going to a Microsoft class are you?

Post Reply