|
Serial LCD Library
Here is a library to interface your PIC code to an LCD that is
controlled via a serial line. One such LCD is available from
Scott Edwards Electronics. There are
many others, and the code shown here can easily be adpated to other
LCD displays by changing some defines.
The following files are referenced below, but for
simplicity I've listed them here for download:
The following routines are defined:
- void lcd_putc(char c) send the byte c to the LCD
- void lcd_clear(void) clears the LCD screen
- void lcd_home(void) homes the LCD cursor
- void lcd_puts() print a string **UNIMPLEMENTED**
- void lcd_printhex(char c) prints byte c as a 2 digit hex character
- void lcd_printdec(char c) prints byte c as a decimal digit string
- void lcd_printbin(char c) prints byte c as a binary string
- void lcd_scroll_left(char n) scrolls LCD screen left n positions
- void lcd_scroll_right(char n) scrolls LCD screen right n positions
- void lcd_goto(char row, char col) goto a location
The LCDPORT and LCDPIN constants define which port
and logical pin the serial LCD is connected to. By default they are
set to PORTA and pin 1 which is the RA1 output on phsyical pin 17 of a
PICC84.
If you connect the serial LCD to a different port and pin then you
must change the definitions in the lcd.c file.
- void lcd_putc(char c) - send a byte to the LCD
- This routine is the heart of the LCD library: it will send a byte
specified in the argument c to the LCD connected to the port defined
by LCDPORT and pin LCDPIN. All the other routines use this code.
lcd_putc() assumes that the LCD is running at 9600 baud, 8-N-1 serial
settings. If your LCD is running at a different baud rate, or you have
a faster PIC, you will have to adjust the timing loop starting at
lcdtxloop. The lcd_putc() routine places a 10ms pacing
delay between each character.
- void lcd_clear(void) - clear the LCD screen
- Building upon the lcd_putc() routine is easy, as this routine
shows. We simply send the byte sequence that clears the display.
- void lcd_home(void) - home the LCD cursor
- Sends the byte sequence that homes the LCD cursor to the top left
hand corner.
- void lcd_printhex(char c) - print byte as hex
- Print a byte value on the display as 2 hex characters. For
example, the value 255 would be printed as FF. The code prints the hi
nibble first (the upper 4 bits of the byte), followed by the lower
nibble.
- void lcd_printdec(char c) - print byte as decimal
- Print a byte value as a decimal string. For example, the value 123
is printed as the characters "123". This function performs repeated
subtractions: starting at 100, we subtract until the value left is
less than 100. We then repeat the loop for 10's. Whatever is left is
the units value. Effectively, this routine performs division by
repeated subtraction. The output of lcd_printdec() is always 3 digits
long: 001 as against just 1.
- void lcd_printbin(char c) - print byte as binary
- This routine prints a binary representation of a byte. For
example, 129 would be printed as 10000001. 8 bits are always printed,
which means that the value may have leading zeros: 5 would be printed
as 00000101.
- void lcd_scroll_left(char n) - scroll screen left
- Scroll the LCD display left n positions. The effect of this
command depends a lot on the type of LCD display you have.
- void lcd_scroll_right(char n) - scroll screen right
- Scroll the LCD display right n positions. The effect of this
command depends a lot on the type of LCD display you have.
- void lcd_goto(char row, char col) - goto a position
- Positions the cursor at the specified row and column. The row
argument starts at 0. So the first line on a two line LCD is row 0,
and the second line is row 1. Similarly, the col argument starts at 0.
If you print off the screen, it is not displayed, but is stored in the
LCD's RAM. Use the lcd_scroll_left and lcd_scroll_right commands to
access the data.
Using the library
The Makefile for this library is as follows:
#
# Makefile for LCD test code
#
# Mark Crosbie 9/15/98
# mark@mastincrosbie.com
LIBS=lcd.lib
ASMINCS= p16c84.inc
C2CINCS=
all: lcdtest1.hex lcdtest2.hex lcdtest3.hex
lcdtest1.hex: lcdtest1.asm
gpasm -r dec -p 16c84 -t lcdtest1.asm
lcdtest1.asm: lcdtest1.c lcd.lib delays.lib
c2c -olcdtest1.asm lcd.lib delays.lib lcdtest1.c
lcdtest2.hex: lcdtest2.asm
gpasm -r dec -p 16c84 -t lcdtest2.asm
lcdtest2.asm: lcdtest2.c lcd.lib delays.lib
c2c -olcdtest2.asm lcd.lib delays.lib lcdtest2.c
lcdtest3.hex: lcdtest3.asm
gpasm -r dec -p 16c84 -t lcdtest3.asm
lcdtest3.asm: lcdtest3.c lcd.lib delays.lib
c2c -olcdtest3.asm lcd.lib delays.lib lcdtest3.c
lcdtest4.hex: lcdtest4.asm
gpasm -r dec -p 16c84 -t lcdtest4.asm
lcdtest4.asm: lcdtest4.c lcd.lib delays.lib
c2c -olcdtest4.asm lcd.lib delays.lib lcdtest4.c
lcd.lib: lcd.c
c2c -lib -olcd.lib lcd.c
delays.lib: delays.c
c2c -lib -odelays.lib delays.c
clean:
rm *.hex *.lib core *.asm *.lst
Example 1 - Printing numbers
The lcdtest1.c test prints the numbers
between 32 and 128 as an ASCII character, a decimal number and a hex
number. There is a 1 second delay each time through the loop so you
can see the output. The code is quite simple: first the PORTA pin to
which the LCD is connected is set to be an output. The code then
delays for a second to give the LCD a chance to initialize (it really
only needs a few 100 milliseconds). Then the LCD is cleared using
lcd_clear().
The loop iterates through the ASCII characters between 32 and 128 and
prints each one as a character, a decimal value and a hex value.
The hex output file is available for a PIC
16C84.
Example 2 - Printing at a location
lcdtest2.c shows how to position the cursor
at a specific location and print information. It uses the previous
example's loop and adds lcd_goto() calls between each print.
The assembly output is available, as is the
hex output file for a PIC 16C84.
Example 3 - Scrolling the LCD
We now test the scrolling commands to scroll the LCD left and
right. My LCD can only display 16 columns of text, but if you continue
to print, the extra characters are still stored in the LCD's RAM, but
are not displayed. However, if you scroll the LCD left you can display
the characters off the screen.
This simple example prints 32 characters to the first row on the
LCD. It then scrolls the LCD left a character at a time, and then
right a character at a time.
The source code is available, as is the
generated assembly output and the
hex output file.
|
PIC Microcontroller Project Book
Lot's of great PIC project ideas!
Programming and Customizing the Pic Microcontroller
If you are learning to program microcontrollers then Myke's book is good start.
The Art of Electronics
A classic in the field. Teaches you the art and science of linear and digital electronic design.
If you want to learn why your circuit is not working, read this book and you'll know why.
Mobile Robots: Inspiration to Implementation
A very readable introduction to the art of robotic design and implementation from the best
practioners in the field: the MIT Artificial Intelligence Lab!
Microcontroller Cookbook
A set of cookbook style designs for the 8051 and PIC microcontrollers.
A handy reference to have if you need a quick solution to a problem.
|