Friday, January 29, 2010

Adding a small HD44780 LCD display to my PC

I've always felt the need to have a small screen on my computer to show some "extra" information which I don't usually want on my screen all the time. By extra information I mean - news headlines, RSS feeds from my favorite blogs, weather updates, CPU usage information, new e-mail notifications, etc. So, to fulfill this humble need of mine, I bought a small (16x2 character) LCD screen for Rs.90 (approximately USD $2). Very inexpensive!:




(I've received an e-mail from TK Boyd! He's the man who inspired me to connect all sorts of things to my computer!)

This LCD is based on the popular Hitachi HD44780 controller. You can find lots of information about how to communicate with this LCD on the Internet. I connected it to my computer's parallel port and fixed it to my computer's case:







I created this panel by drilling holes into the case, and adding two switches (for the LCD and backlight), and a pot for contrast adjustment. The LCD is powered by 5V from the SMPS.

Here is the pinout for the LCD:



DB0-DB7 is the data bus.

'E' is the enable line. This is used to indicate the start of a transmission of a data byte to the LCD controller. When we start a transmission, this line is brought high. When transmission is complete, this line is brought low.

'RS' is the register select line. This line indicates to the LCD controller whether the data byte is to be treated as a command or as text data to be displayed on the screen. If it is high, the data sent to the LCD displayed on the screen. If it is low, the data is treated as a command.

'R/W' is the read/write line. If it is low, information can be written to the LCD controller. If it is high, data can be read from the LCD. I've kept it permanently low in my circuit.

Here's a screenshot from the HD44780's datasheet (click on the image to enlarge it):






So, this is how a typical command would be executed:
  1. Make 'RS' and 'R/W' low ('R/W' is always low in my circuit).
  2. Set 'E' high to indicate the start of the command.
  3. Make DB7-DB0 equal to binary "00000001" (decimal: 1). This is the clear display command.
  4. Make 'E' low again to execute the command (which in this case would clear the display).
In code, this would look like:


I'm using Inpout32 to access my parallel port. Another point I should mention here is that the 'E' pin on my LCD is connected to C0 on my parallel port and 'RS' is connected to C2. A weird thing I noticed about C0 is that you have to send "1" to it to make it low (and vice versa). This is why I'm sending "1" in the last line of the the code. I have no idea why this is happening. Have any clues?

To write characters on the LCD, you just have to send the ASCII code of the character to the LCD:


Creating custom characters:




Most characters on the LCD (I think there are a total of 248) are stored in what's called a CGROM. This is an acronym for "Character Generator Read Only Memory". Characters inside this memory location are pre-defined, and cannot be changed. So how do we create our own characters if we can't change anything here? Well, there's a 64-byte hunk of RAM called CGRAM in the LCD, and it is write-able! Characters on an LCD can be up to 8 pixels high, and 5 pixels wide. Each row consumes 1 byte of memory. Since there are 8 rows, one character takes up 8 bytes. So, a total of 8 custom characters can be defined in 64 bytes. Here's the pixel map of a bell pattern I created:



I have written the decimal and hex values for each row. To store this custom character in the CGRAM, I would have to go to the CGRAM address (see the "Set CGRAM address" command in the datasheet) and write these values. The address of the first byte of the first character is 64 (hex: 0x40). How in the world did I get that value? Well if you look at the command for setting the CGRAM address in the datasheet, it says we have to set DB6 to "1". The rest of the bits, DB0-DB5, determine the CGRAM address. If we were to set DB0 to DB5 "0", we would be able to set the first first byte of the first character. The complete command would be binary "1000000" (DB6-DB0). This is equal to 64 in decimal. Similarly, the address of the first bye of the second character would be 64 + 8 = 72 (remember each character consumes 8 bytes).

Here's a code sample to draw this bell character on the LCD:



I need to do some explaining here! Well, first we define the bytes for the bell character in an array. Then we set the CGRAM address to 64 (first byte of the first custom character). Then we write the values in the array to the CGRAM in the for-loop. Notice that we don't have to set the CGRAM address every time we have to go to a different byte (65,66,67 etc). The LCD controller auto increments the CGRAM address everytime we write a byte. To display this newly created character, we first have to switch back from the CGRAM to the display area. We do this by setting the DDRAM address to 128 (first character of the first line). 129 would be the second character, and so on. The second line starts at DDRAM address 192. We display the custom character by displaying ASCII code "0". ASCII codes 0 through 7 are for custom characters. They normally serve as control codes for marking the beginning of a serial transmission, but since these have no meaning to an LCD module, the designers reserved them for CGRAM characters. It took me a long time to figure that out!



I find it convenient to create custom characters on a sheet of graph paper before I begin coding:



A note on creating animations:

You can create animation by rapidly printing custom characters. If you have a custom character on your LCD, and you modify it's bytes by going to the CGRAM, the character will change. In fact, all occurrences of that character will change. Here's another cool thing. You can create more frames for your animation than the eight character limit. This is because you can load new bit patterns from your computer without having to store it in the LCD's memory. Neat!

This post is getting long, so I'll end my discourse here. :) My LCD project is a WIP right now. I'm still adding more features. Anyway, I hope you find this post helpful in your projects. Have fun!


Links:

How to control a HD44780-based Character-LCD

Defining Custom Characters

Creating custom characters tutorial

CodeProject article on controlling LCDs using C#

Share/Bookmark

Monday, July 27, 2009

Reed Switch Motor and Parallel Port Tachometer



Hello readers! I'm posting a project here after quite some time. After moving to Bangalore, I had slowed down. Well, now I am building stuff again so I'll be posting much more often!

A few days ago, my brother Amrit Derhgawen, cousin Anand Karpatne, and I decided to make a simple reed switch motor. My brother is a character animator at DreamWorks, but he also happens to be good with electronic things. So, after a few hours of construction and troubleshooting, he finally got the motor working. JOY!

A reed switch motor consists of a rotor made up of 2 (or 4) permanent magnets. An electromagnet and reed switch are placed close to the rotor on opposite sides. I got my electromagnet by stripping a relay. When a magnet on the rotor gets close to the reed switch, the reed switch gets magnetized and allows current to pass through and turn on the electromagnet. The electromagnet pushes the magnet closest to it, and makes the rotor turn. The electromagnet is switched off when there is no magnet close to the reed switch, and this allows the rotor to spin freely from inertia. When the other magnet gets in working range of the reed switch, the electromagnet pushes again.



After making the motor, I decided to measure it's RPM (Revolutions Per Minute) using my computer for the fun of it. So, I connected another reed switch to my computer's parallel port as shown here:



By default, status port 7 (S7) is high on my computer. When a magnet comes close to the reed switch, S7 becomes low. I placed this reed switch very close to the motor's rotor. The software is a C# Console Application which uses Inpout32.dll to read the parallel port. For about every 60 revolutions of the motor, it estimates its RPM.

The motor usually spins at around 3000 to 4000 RPM depending on voltage. Here is a graph I generated using values from the program:



I turned the motor off and on 3 times quickly, and also played with the voltage. You can see all this in the graph!

Do watch the video to see it in action!

For more information on how to make reed switch motors, check out this wonderful tutorial - http://www.simplemotor.com/rsmotor.htm.

Cheers,
Ashish :)

Share/Bookmark

Saturday, July 12, 2008

A new place, a new beginning...

My family has just moved from Delhi to Bangalore after a tiring 36 hour train journey. Wow..can you even believe that? That's the longest I've ever been on a train! Our movers haven't arrived yet, and our apartment is pretty much empty. There's no refrigerator, no TV, and no bed to sleep on! Well, hopefully everything should arrive within a day or two. The weather here is so much better compared to Delhi (which was very hot and humid when we left).

Moving can be stressful, especially since you have to part with lots of people and friends. Thankfully, this isn't anything new to me as I've always been wandering and on the move. If I remember correctly, I've moved a total of 10 times across 2 countries (U.S. and India), and 6 cities in the 20 years of my existence in this life. I wonder what's after Bangalore! :)

Share/Bookmark

Friday, April 11, 2008

Coding4Fun: Webcam Based Laser Tracking for Human-Computer Interaction


Here's a new Coding4Fun article I did - Webcam Based Laser Tracking for Human-Computer Interaction.

In this article, we will put together a program which will allow us to move the mouse cursor on our computers with a laser pointer, and even generate mouse clicks using only a webcam for computer vision.

You can also use it for impressing your friends by drawing things on your computer with a laser.

Enjoy! :)

Share/Bookmark