Basic LCD Control using CCS


Ok.... before we go on ... It is a heckuva lot easier to buy a serial LCD. Scott Edwards had a really good deal on LCD screens ($29) for a while (apparently the sale is over). Crystalfontz has some also (Crystalfontz) for about the same prices (haven't tried Crystalfontz). Can't beat those prices. You will spend more than $30.00 worth of time putting one together. However, it is still fun to get one going. So here's an easy one to start.

The above diagram shows the hook up for a simple serial LCD. This hookup is described in the file lcd.c which comes with the examples provided with CCS. The file is copyrighted and cannot be reproduced here. So, if you have CCS, then read on.

The difference between the above drawing and the CCS example is that I've added a serial input onto A0. Now all that has to be done is wait for a serial input and then put it into the LCD program. Each input must be terminated with a carriage return (char 13). That means, in C code, you should end your string with an '\r', not an '\n'. .

I found some really cheap LCD modules at BGMicro ($2.70 each) that were only 1 X 16 - so this code only expects that. If you have a different LCD, then modify the code a little and set the appropriate flags in the LCD.C include file. Now, I'm going to emphasize this.... be sure to modify the flags in the LCD.C file for your particular LCD!

Once you get this sucker running, you use the pot to adjust for contrast. Several example LCD projects just say tie this to ground. I have NEVER gotten that to work well (always too dark). Contrast has always had to be adjustable for the LCDs that I've tried. Get it going and keep hitting reset/adjusting the pot until you can clearly see the 'Ready . . ." message. Because the A0 pin is floating, the program will periodically crash if you don't have the input tied to your controlling program output. That's ok... it makes a nice recycle effect when it crashes (the Ready . . . message keeps blinking). I planned it that way .... (heh, heh).

My code looks a little like this:

#include < 16F84.H >
#fuses HS,NOPROTECT,NOWDT

#use delay(clock=4000000)
//
// the following file is included with the examples that come
// with CCS. You should modify it for your type of LCD before you
// use it!

#include < lcd.c >

//
// declare the rs232 parameters. Leave off the INVERT if you are feeding
// this from a PC through a MAX232 or similar
//

#use rs232(baud=9600, rcv=PIN_A0,parity=n,bits=8,INVERT)

main() {

//
//  If you feed the buffer with more than 16 characters, it will crash
//  So you might want to bullet proof this a whole lot more. I didn't
//  and my prototypes work fine - because I don't send it more than 
//  16 characters (grin). PS: the extra characters in the array are for
//  the carriage return/null on the end of the string.
//
//  The array initialization is not needed, but shown as a reminder of
//  the space being used.
//
   char buf[18]={32,32,32,32,32,32,32,32,32,32,32,32,32,32,32,32,32,32};
   unsigned xc=0;
   lcd_init();            // mandatory

   lcd_putc("\fReady . . .\n");  // nice and friendly
   delay_ms(2000);
   lcd_putc("\f");	// clear the screen (uses codes in lcd.c)
  while(1)
  {
   gets(buf);           // puts data into buf[] until it reaches a char 13 (cr)
   lcd_putc("\f");	// clears screen before printing buffer - remove if more
		 	// than one line LCD. Clear it with your input code instead.
			//
   for(xc=0;xc<16;xc++)
    {
      if(buf[xc]==0) buf[xc]=32; // eliminate the null character
      if(buf[xc]==13) buf[xc]=32; // not needed if using gets to get string
      if(buf[xc]==10) buf[xc]=32; // not needed if using more than 1X16
      lcd_putc(buf[xc]); //
      buf[xc]=32;       // resets buffer contents to spaces
    }
  }
}

Back to start of this section