Serial LCD Library using PIC16C84
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:
- LCDlib.tar.gz is my entire LCD library and associated test source code in gzip’d tar format.
- LCDlib.zip is my entire LCD library and associated test source code in zip format for Windows PCs.
- lcd.c the library C source.
- lcdtest1.c tests printing an integer in decimal and hex format (lcdtest1.hex).
- lcdtest2.c tests positioning the cursor for printingt (lcdtest2.hex).
- lcdtest3.c tests scrolling the LCD display left and rightt (lcdtest3.hex).
- lcdtest.jpg a schematic showing how to wire the PIC to the serial LCD module.
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.
For more detail: Serial LCD Library using PIC16C84