Friday, February 12, 2010

Homemade USB interface board using a PIC

Hello folks,

Here's what I've been working on for the last couple of weeks:





This is a USB interface board I've built around a PIC 18f4550 microcontroller from Microchip. As many of you probably know, I've used my computer's parallel port to connect many things to my computer. However, most laptops don't have parallel ports these days, so I needed a way to connect things without a parallel port. This board does exactly that (and actually much, much more). I found a really nice tutorial on building this board here - http://eegeek.net/content/view/13/32/






You can make your C#, VB, C++ programs communicate with the chip using the open-source MCHPFSUSB Framework from Microchip, or the Window's HID drivers. The framework comes with lots of samples to get you started.


I've put a bootloader on the chip for the sake of convenience. With a bootloader, you don't have to take the chip off the board and put it in a separate programmer for programming. I have to sometimes re-program the chip every 10-15 minutes during development (especially when there's a hard to find bug), and the bootloader really makes it easy.

No project is compete until you record a video! So here's one:




Please excuse the background noise in the video. There was some construction work going on while I was recording this. Anyway, I'm using the board to control some LEDs, a servo, and reading the value of a potentiometer. Controlling the servo was the trickiest part because servo's are sensitive to timing. They expect to receive a pulse every 20 ms, and the duration of the pulse determines how much they will turn. The length of the pulse usually varies between 1-2 ms. Setting up the timers on the chip to work properly was a bit challenging, but I finally got it working. This sound card based oscilloscope really helped! - http://www.zeitnitz.de/Christian/scope_en.

Hope you enjoyed this post. I will be using this board in future projects. Keep checking!

Thursday, January 28, 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#