Archive for May, 2009

May 28, 2009: 12:02 pm: Dan.Net, Electronics, Programming

A little while ago I wrote about an electronics kit from kitusrus that interfaces an LCD to a PC through its parallel port. Like all kitsrus stuff it was fun to build, and the instructions included with the kit do a great job of explaining the design of the circuit.

However, I was a little surprised that they didn’t include the source code for the Windows program that communicates with the board. Sending a “hello world” message to the LCD is fine for verifying that the kit works, but an LCD connecting to a PC could be used for so much more.

When Googling for some .Net code that I could use as a starting point for writing my own interface, I was delighted to find that somebody had already done most of the work for me. An article on The Code Project site describes the construction of a homebrew parallel port LCD connection, and includes the C# .Net code that the creator used to send data to the LCD. Out of curiosity I downloaded the code and ran the .exe to see what happened, and I was astounded to see it successfully writing The Code Project’s RSS feed to the kitsrus LCD, under Vista no less. “Hello World”, indeed.

The Code Project article’s creator wasn’t using the kitrsrus kit, but because his LCD is based on a similar controller chip, and by a lucky coincidence in the selection of parallel port pins, the code mostly worked. The only problem was that the data wasn’t scrolling across the LCD as intended, but was stuck in the last column of the display, writing only to that one position. The data was getting across, it just wasn’t being positioned correctly. This is a pretty common and easily solved problem when writing data to an LCD, as I’ll explain below.

(In order to preserve your sanity and mine, I’ll refrain some referring to “the Code Project project” and the “kitsrus kit”, and call them the “CP project” and “the kit”. Apologies to their respective trademark owners.)

Although there is no “standard interface” for connecting an LCD to a parallel port, there is a fairly close correspondence between the parallel port’s pins and the pins used by all LCDs that use an HD44780-compatible controller chip. (Most LCDs intended for use by hobbyists are compatible with this standard, dating back to the late 90s. The Wikipedia entry for HD44780 links to an Everyday Practical Electronics article from 1997.)

The CP project used an LCD based on an old Samsung KS0066 chip, and the kit contains an LCD based on the KS0070 and manufactured in 1999, and both of those are HD44780-compatible.

The Code Project author wrote 2 articles that contain all the technical information you need to know in order to understand his .Net source code: this article covers the parallel port’s pins, and this one covers the LCD’s. The illustration below is linked to his second article.

Both the parallel port and the LCD use 8 data pins, D0 through D7. Not surprisingly, both the CP project and the kit wire the 2 sets of pins in a one-to-one correspondence, D0 to D0, D1 to D1, etc. The data sent by the .Net code arrives just fine at the LCD, then.

LCDs based on the HD44780 interface also have 3 control pins: RS (register select), R/W (read/write) and E (enable). Since LCDs don’t have much to say, both the CP project and the kit do the same thing with the R/W pin – wire it to ground, putting it in a permanent write state.

By a happy coincidence, both the CP project and the kit happened to wire parallel port pin C0 to the E pin of the LCD – without pin C0 turning things off and on, nothing would have made it to the LCD. There is a non-intuitive pattern of setting the enable pin low then high then low again that is necessary to write data to the LCDs — this seems to be the thing that trips up most novice LCD programmers, but the CP project’s source code does a nice job of commenting this code so that you can understand what’s going on.

The only area where the 2 approaches differ is in the selection of the RS pin: the CodeProject board uses pin C2 while the kitsrus board uses C1. (See the CP project’s pinout diagram below). Since this pin is used to tell the LCD whether it is receiving data or instructions, and the pin needs to be high for data, this difference should have caused everything being sent to the LCD to the treated as instructions, resulting in a jumpy cursor but no characters on the screen. However, by another lucky coincidence, the parallel port pin used by the kit, C1, is reversed – it is normally high and is set to low by sending it a “1″. This results in everything sent to the LCD being treated as data — characters with no cursor control, exactly what we got.

Parallel port connections to the LCD, by Levent Saltuklaroglu, courtesy of The Code Project
Parallel port connections to the LCD, by Levent Saltuklaroglu, courtesy of The Code Project

To adapt the CP project’s code to the kit, the only change required is to redirect all signals intended for C2 to C1, and flip the bit from allow for the fact that kit’s pin is reversed.

I decided to implement this change by adding a go-between method that would convert the instructions sent by the CodeProject code to the ones required by the kitsrus board:

        private void writeToControl(int intValue)
        {
            int intModifiedValue = intValue;
            if ((intValue & 4) > 0)
				// if C2 is being set  high, set C1 high by sending it a 0
                intModifiedValue = intModifiedValue & 253;
            else
				// else, if C2 is being set low, set C1 low by sending it a 1
                intModifiedValue = intModifiedValue | 2;

            PortAccess.Output(intControl, intModifiedValue);
        }

And that’s it — the kit can now be fully controlled by the CP project’s code.

This is quite cool, since it breathes new life into a 10-year old kit. When originally introduced the kit had only a command line interface that would only run in DOS – real DOS, not the command line in Windows XP. However, the .Net code works just fine under Vista and Windows 7. By tinkering with the code you can now use the LCD as a remote display for whatever you like: RSS feeds, e-mail, twitter. A poor geek’s Chumby!

May 6, 2009: 7:46 pm: DanUncategorized
Empty nest, shortly after the eggs hatched
Empty nest, shortly after the eggs hatched

In the spring a GigaMegaGeek’s fancy stubbornly stays focused on gadgets and coding, but some of my neighbours have other ideas.

For the second year in a row, the company where I work has played host to a Canada Goose nest right next to our parking lot.  The eggs hatched yesterday, and Mother and Father Goose have been showing the neighbourhood to their 6 kids.  I suppose everybody thinks their geese are special, but mine really are!  As proof, here are some fascinating facts about my geese:

1) The location of the nest itself is somewhat of a surprise — 2 feet from a very busy parking lot, as you can see from the photo — but I’m quite surprised that the exact same location has been used for the next both times.  I have no idea whether this year’s parents are the same as last year’s, but there was definitely no sign of any geese in this area while the ground was covered in snow, from about December to March.  That spot seems to be a dubious location for a nest, yet clearly my geese know otherwise.  And you can’t argue with their success rate –two for two.

Hey, don't leave us here with that creepy dead thing!
Hey, don't leave us here with that creepy dead thing!

2) While geese are said to be  monogamous, they sure aren’t liberated — Mom did all of the nest sitting as far as I could tell, and was faithfully sitting on that nest on all but one time that I passed by over the last month.  On the one instance when she had wandered a few feet away to graze the lawn, about 10 days ago, I peeked in the nest and was dismayed to see nothing but a layer of goose feathers.  I was afraid that we had a seriously deluded goose on our hands, but it turns out that she just did a clever job of covering the eggs before stretching her legs.

An evening stroll - less than 1 day old
An evening stroll - less than 1 day old

3) Our company is not only hosting a nest, but last year we hosted a Goose Day Care.  A couple of days after the eggs hatched last year, we were astounded to see a herd of 24 goslings being guided through the parking lot by 4 adults.  I have no idea how many goose families were involved — our pair only had 6 young last year, same as this year — but that herd stayed together in the area under about October.     This year’s pair has 4 other interested adults hanging around the nest (older brothers and sisters of this year’s goslings, I suspect), and we may have a new herd forming.  I just took a walk around the area and came across another group of 5 goslings about 100 yards from here.

Baby sitting, literally
Baby sitting, literally

4) The goslings get shoved out of the nest very quickly — they are up and walking within a few hours of being born — but the family returns to the nest at night.  I just took the above photo, and you’ll have to take my word for it that tucked underneath Mom are 6 squirming goslings.  (You can see a fragment of one of the eggs to the left of the nest).

Meanwhile, in the west end of Toronto, the 2 peregrine falcons that inhabit the highrise condo where I live are at it again.  They laid 3 eggs last year, and seemed to have done the same again this year until a 4th egg suddenly became visible yesterday.  As in 2008, fellow resident Matt Rossi is taking some amazing photos of the pair, and has just setup a web cam pointed at the nest.  Since the first of the eggs is expected to hatch anytime now, I’d urge you to keep an eye on  Matt’s web site to see how things go.

Peregrine falcons are gradually rebounding from the brink of extinction in Canada, but the odds are  against the young making it past the “fledgling” stage (that is, learning to fly).  Too many things can go wrong, both in the wild and in the city.  Sadly, we found that out firsthand last year.  All three of last year’s chicks successfully learned to fly and went off to explore the world, but all three died later in the year in collisions, 2 of them at Pearson airport.

There’s no way of knowing how things will turn out this year, but thanks to Matt and his incredible camera work we’ll all have the opportunity to find out over the coming months.