Thursday, 27 December 2007

Conclusion To LCD PIC Project

This is the final post in this series of articles documenting the process of designing and developing a PIC controlled LCD module. I will outline the process of using the PC to dynamically display text on the Screen. This includes illustrating how to display the ?Now Playing? data from Windows Media Player as well as the drive capacities of all available drives.

Programming the Serial Port in Microsoft Visual Studio 2005

In the previous article in this series I used Windows HyperTerminal to send ASCI characters to the PIC which in turn sends them on to the LCD. HyperTerminal, although useful is limited in its operation. You may either enter data via the keyboard, or by selecting a pre-prepared text file to send. Therefore it is not possible to send dynamic (changing) information to the screen using this program. I chose to use C# as my programming language, which is part of Microsoft?s .NET 2.0. My reasons were that C# has a very comprehensive library for the serial port, more than necessary for this project. Added to this, I am currently learning C# for my university degree.

A Reminder about the PIC Buffer

The PIC program, once it has initialised the LCD, listens out for 8bits of RS232 (Serial Communication Protocol) at a time and stores each in its memory until it has 32 bytes of data. As soon as this happens, it prepares the data for sending to the LCD using its 4-bit wide bus and prints it onto the LCD. It then clears the buffer and listens out for the next 32 bytes. If the byte 0x10 is sent this is interpreted by the PIC as an instruction to clear the LCD by sending a special series of bits to the screen.

An Outline of the necessary Program

A successful program for sending text to the LCD in C# should:

  • Safely attempt to open a connection on the serial port (COM1 on my computer)

  • Send byte 0x10 to clear the screen

  • Wait for 70ms to allow LCD to clear screen

  • Print a test message to the screen to show the user that connection is established

  • Close the port safely

  • End with a message in the console window to show user that program terminated successfully

In addition to the above which only sends a static message to the screen the program must provide classes and methods to:

  • Accept a string of user entered text, shorten it to the maximum number of characters for the display, or if the string is shorter it should be padded with extra ?spaces? to fill up all the characters on the screen

  • Produces a variable length delay in program flow for use when clearing screen, and in between changing the display to give user long enough to read display

  • Get the current now playing information from Windows Media Player via the registry.

  • Format the Artist, Title, Album information so suitable for sending to the LCD. This means scrolling longer strings so that the user can view the whole of the string even if display is only 16 characters wide

  • Check the registry repeatedly to see if the song has changed and if display needs updating.

  • Get the drive information of all available drives on the computer and display the free space remaining.

A Word About The Code

The program is built up of four classes. The first is the Program class which contains the static method 'Main' which is the point at which the program starts executing. Secondly the class 'Connection' deals with establishing a connection and sending raw strings of text. It also formats the array of media information and sends/scrolls it where necessary. Strictly speaking this should be in another class if this program were to be a stunning example of encapsulation, which fortunately it isn't. The third class is 'Display' and handles the formatting of general strings of text (not media info) so that they are suitable for display on just two lines. It also stores the formatted text (both media info and general) in some object variables which can be accessed by other parts of the program. Finally the 'Now Playing' class deals with opening a connection to the registry and extracting the current Artist, Title, Album and Duration info. It also provides methods for checking if the registry has changed (i.e. the song has ended/changed).


The result is very pleasant to watch. If the correct WMP plug-in is installed (WMP Blogging Plug-in available from the official WMP site, and a song is being played the display cycles through all the available info showing the type of info on the top line (Artist, Album etc.) and the actual info on the bottom line, scrolling it neatly when necessary. As soon as the user closes WMP or disables the plug-in, the program switches to 'Drive Info' mode, displaying the free space available on all recognised drives. The program and screen then shut down.

Last Words

The code at first looks very long, but most of it is comments and half of what?s left is for formatting the Now Playing information and dealing with the registry. Of the remaining half, a quarter is the try{} catch{} statements for dealing with Exceptions. What?s left is the code that actually sends data to the port which I will summarise here: All code used in this project series is available in the DocDrop section of this site in the folder LCD Project.

Using System.IO.Ports;

SerialPort lcd =
new SerialPort("COM1", 4800,
Parity.None, 8, StopBits.One);
lcd.Open(); //open port
lcd.Write(new byte[] { 0x10 }, 0, 1);
//send hex instruction to return cursor to home
Delay(70); //allow LCD time to refresh
lcd.Write(message); //send string to LCD
lcd.Close(); //close port

Obviously it would be nice to build a visual Windows forms based interface, however that's a little beyond my reach at present and distracts from the job in hand which is PC->PIC interface. If in the future I create one I will of course upload it. Please comment on this app and offer suggestions in the comments section below.

No comments: