
Introduction.
The PIC-n-LCD was originally developed by Dale Wheat (http://www.cyberramp.net/~dale). BG Micro in Dallas (http://www.bgmicro.com) sells this unit in kit form. My students have made some minor modifications to the design and assembled and tested the unit. In my mind, it is the most robust unit on the market.
The offering of this module is a part of a greater project to develop educational materials and low cost designs which enable hobbyists to tinker in the area of embedded processor control. Much of this work appears on my web page.
Profits arising from the sale of manuals, kits and this LCD module are used to buy new items to explore and to pay students to investigate some subject related to embedded processor control.
If you are not completely satisfied with this unit, please return it to the above address for a full refund.
Overview of the Module.
The PIC-n-LCD is a fully assembled and tested unit in a neat package measuring 3.75 inches X 2.0 inches X 1.0 inch.
The unit receives serial (9600 baud) text from a PIC, Basic Stamp, PC or similar and displays the text on the 20X4 LCD panel. The unit may be interfaced with TTL, CMOS or RS232 logic levels.
In addition to printable text characters, the unit responds to control characters for functions such as tab, line feed, form feed, carriage return and others. An on-board speaker is provided to permit the bell function.
In addition to normal LCD functions, the unit is capable of displaying an 8-bit quantity in either a signed or unsigned decimal format.
Further, four TTL level outputs are provided which may be used to remotely drive solenoids or alarms.
Two models are available. They differ only in the method of providing power. The DC model is equipped with two power leads which are to be connected to +5VDC and ground. Reverse polarity and over voltage protection is provided to avoid permanent damage to the unit.
Current drain is 70 mA. However, this may be reduced to 10 mA by disabling the LEDs on the unit. This permits powering the unit with a PIC or Stamp output in applications where low power consumption is critical.
The AC model is equipped with a wall plug in unit and an onboard 5 Volt regulator.
Connection to the PIC, Stamp or similar is facilitated by a DB9 female connector.
Familiarization.
Please take a moment to examine the unit and make a mental note of;
1. Small Speaker. This is used to cause a short beep by sending a special control code.
2. Reset Switch. The module may be manually reset using this switch or by momentarily disconnecting power.
3. 4-LED Block. In addition to providing LCD functions, the unit permits the user to force logic states on four independent outputs. These states may be observed using these LEDs. The outputs are available at the 4X2 pin array J3. Turn the board over and note that one row of pins are connected to ground and the other row to outputs on the PIC.
4. Potentiometer. This is intended to provide LCD contrast.
5. Serial Connector. Only two leads are used. Terminal 3 is RX-DATA and terminal 5 is GRD.
6. Power Connector (AC Model Only). For users in the United States and Canada, a wall transformer unit is supplied. For other users, be sure to use a transformer which is between 9 and 12VDC capable of supplying 100 mA. Be certain that the center terminal is positive.
7. Power ON LED. This enables the user to quickly verify the unit is powered.
8. Wire Leads (DC Model Only). Red (+5VDC) and black (GRD) leads are provided to power the unit. A DC supply in the range of 4.8 to 5.5 VDC capable of providing 70 mA is required.
In inspecting the unit, you will probably note that there are some empty holes.
The original design used a MAX232 to convert RS232 levels from a PC (typically plus and minus 8 VDC) to TTL. However, the nature of the PIC inputs is such that this conversion may be done with a series limiting resistor. In addition, the elimination of the MAX232 permits Stamp and PIC users to directly interface with the unit.
On the DC model, a series positive temperature coefficient (PTC) thermistor and shunt zener diode are provided to protect the unit from permanent damage if the unit is accidentally connected to +12 VDC, -12VDC or if the power leads are accidentally reversed. It happens!
Use of the Unit.
Connection of the unit to a Basic Stamp, PIC or to a PC serial port is illustrated in Figure #1.
Rather than listing all of the special control codes, I have opted to discuss them in the context of a Basic Stamp 2 program. The Englishlike nature of Basic makes it readily understandable and I am hopeful the user can map this over to suit their needs on a PIC, 68HC11, PICSTIC or PC Serial Port using C, QBASIC or Visual Basic. A complete listing of the control codes is contained in Appendix 1.
In the following program, 9600 Baud, Inverted serial is used is used.
' SERLCD_1.BS2 ' ' Illustrates various features of the PIC-n-LCD ' ' copyright, Peter H. Anderson, Feb, '98 N_9600 CON $4000+84 ' 9600 baud inverted T VAR BYTE ' variables P VAR BYTE N VAR BYTE DIRS = $0001 ' make P0 an output TOP: T = 74 ' dummy up some variables for later display P = 30 SEROUT 0, N_9600, [$0C] ' clear the LCD and locate cursor ' to upper left PAUSE 1000 SEROUT 0, N_9600, [$19, $02] ' define cursor as blinking block ' $00 is no cursor, $01 is blinking block ' $02 is underline, $03 is blinking block ' and under line PAUSE 1000 SEROUT 0, N_9600, ["Hello", $0A, $0D] ' $0D - move cursor to extreme left ' $0A - move cursor to next line SEROUT 0, N_9600, ["World", $0A, $0D]Control code $0C (form feed) is used to clear the LCD and locate the cursor to the beginning of the first line. The cursor defaults to a blinking block.
The cursor may be modified by sending control code $19, followed by either a 0, 1, 2 or 3.
The string "Hello" is displayed on the first line followed by a "newline" which consists of control codes $0D (carriage return) and $0A (line feed). Note that a carriage return positions the cursor at the beginning of the current line. The line feed positions the cursor at the same location on the next line.
The string "World" is then displayed on the second line.
SEROUT 0, N_9600, ["T=", DEC T, $09, "P=", DEC P, $05, $0A, $0D] ' display some variables, $09 is tab ' $05 is save cursor SEROUT 0, N_9600, ["Rochanda", $06] ' print some more text on line 4 ' restore cursor to the end of line 3 PAUSE 4000This snippet illustrates how variables may be displayed. Note that control code $09 is a horizontal tab.
Control $05 is used to save the cursor position. Control code $06 is used to restore the cursor. That is, the cursor will relocate to the point it was at when the $05 code was saved.
SEROUT 0, N_9600, [$0C] ' clear the LCD SEROUT 0, N_9600, [$19, $00] ' disable cursor FOR N = 1 TO 20 ' twirl a wheel SEROUT 0, N_9600, 50, ["|", $03, "/", $03, "-", $03, "\", $03] NEXT ' $03 is non destructive back space PAUSE 4000This code illustrates a cute application of the nondestructive backspace (code $03). The characters "|", "/", "-" and "\" are each output and each is followed by a nondestructive backspace ($03) with a 50 msec intercharacter delay. The effect is to create a spinning wheel.
SEROUT 0, N_9600, [$0D, $0A] ' new line T=$81 ' dummy in a value SEROUT 0, N_9600, 1000, [$17, T, $09, $16, T] ' prints T as unsigned quantity (129) and as signed (-127) PAUSE 4000Control codes $16 and $17 are used to indicate that the next character is to be displayed in either signed or unsigned decimal formal, respectively.
In the above, T is dummied to a value of $81 (hexadecimal) or 129 decimal.
Control code $17 indicates the next value is to be displayed in unsigned decimal format. Thus, the number 129 appears on the LCD.
In applications using signed 8-bit numbers, $81 (or %10000001), the one in the most significant bit is interpreted as meaning the remaining seven bits represent a negative number and the PIC-n- LCD performs a two's compliment conversion and displays -127.
FOR N=1 TO 5 SEROUT 0, N_9600, 300, [$0F] ' $0F is shift right NEXT ' pull the whole display to the right 5 places FOR N=1 TO 5 SEROUT 0, N_9600, 300, [$0E] ' $0E is shift left NEXT ' pull the whole display back to the left PAUSE 4000 SEROUT 0, N_9600, 100, [$0B, $0B] ' two vertical tabs PAUSE 4000Codes $0F and $0E cause the display to shift the left and to the right by one position.
Code $0B causes the lines to move up. Note that line 1 disappears off the top of the LCD.
FOR N=1 TO 5 ' beep the speaker 5 times SEROUT 0, N_9600, [$07] PAUSE 100 NEXTCode $07 (bell) causes a short burst of 1000 HZ tone on the speaker.
FOR N=0 TO 15 ' output to the GP Outputs
SEROUT 0, N_9600, [$15, N]
PAUSE 1000
NEXT
SEROUT 0, N_9600, [$15, $00] ' turn GP Outs off
PAUSE 4000
In addition to providing LCD functions, the unit provides four general
purpose outputs. Control code $15 is used to indicate that the next
character is the output pattern. The above code will cause the four LEDs
on the unit to "count" up. The general purpose outputs are discussed in
greater detail elsewhere.
SEROUT 0, N_9600, [$0C, "BEST WISHES", $0A, $0D] ' clear LCD and output message SEROUT 0, N_9600, ["73", $0A, $0D] DONE: GOTO DONEThe following, program SERLCD_2.BS2 illustrates how the cursor may be positioned using control code $01 followed by the cursor position. For a 20X4 LCD display, there are 80 RAM locations in the LCD. Each RAM location corresponds to a location on the LCD.
$00 - $13 First Line $40 - $53 Second Line $14 - $27 Third Line $54 - $67 Fourth LineThus, position 0 on line 4 is $54 + 0. Position 4 on line 2 is $40 + 4.
' SERLCD_2.BS2
'
' Illustrates how to display four quantities on different lines on a
' 4-line display.
'
' P0 (term ) -------------------------- To RX on LCD
'
' Peter H. Anderson, Feb, '98
N_9600 CON 84+$4000
Q1 VAR WORD ' some dummy quantities
Q2 VAR WORD
Q3 VAR WORD
Q4 VAR WORD
N VAR BYTE
DIR0=1 ' define serial output to LCD
MAIN:
SEROUT 0, N_9600, [$0C] ' clear LCD
' display Q1=
' Q2=
' Q3=
' Q4=
SEROUT 0, N_9600, [$01, $00, "Q1="] ' line 1, pos 0
SEROUT 0, N_9600, [$01, $40, "Q2="] ' line 2, pos 0
SEROUT 0, N_9600, [$01, $14, "Q3="] ' line 3, pos 0
SEROUT 0, N_9600, [$01, $54, "Q4="] ' line 4, pos 0
FOR N=0 to $FF
Q1=N ' give quantities some varying values
Q2=100+N
Q3=200+N
Q4=300-N
SEROUT 0, N_9600, [$01, $00+4, dec Q1]
SEROUT 0, N_9600, [$01, $40+4, dec Q2]
SEROUT 0, N_9600, [$01, $14+4, dec Q3]
SEROUT 0, N_9600, [$01, $54+4, dec Q4]
PAUSE 300
NEXT
STOP
The above Basic Stamp 2 programs may be downloaded from my WWW
page.
Interfacing with a PC Serial Port.
The LCD unit may be directly interfaced with a PC serial communications port as shown in Figure #1. The LCD may be located several hundred feet from the PC.
Interfacing with a Com port in this manner may be useful in applications where there is no need for a conventional monitor or in providing a remote display. The unit may have value in applications where the user desires to simply control a device at a distant point using the general purpose outputs.
In writing a program similar to that discussed above for the Basic Stamp, remember to set the COM port to 9600 Baud, 8 data bits, no parity. The number of "stop" bits may be one or two.
The user may also control the serial LCD from the keyboard using such terminal emulator packages as Procomm or Hyper Terminal which is a part of the Windows 95 package. Configure as a direct connection to the Com port you are using.
Clearly, displaying text characters is simply a matter of hitting the appropriate key. Twenty six control characters $01 through $1A may be sent as ctrl-a though ctrl-z.
For example, to clear the LCD ($0C), send ctrl-l. To ring the bell ($07), send ctrl-g.
A full table appears in Appendix 1.
Interfacing with a PIC.
My Web page includes a number of examples of assembly language code for interfacing the PIC16F84 with the unit.
The value of this LCD unit in the PIC environment cannot be overemphasized. I am currently teaching a course in PIC assembly to some 35 eager undergraduate students. This is an intense hands on laboratory type course and I simply do not have enough emulators. Thus, students develop their code, use the MPLAB simulator where applicable, program their 16F84 and run their programs without the ability to set break points and watch variables. I assume this is true of most hobbyists.
Thus, this LCD unit is our primary debugging tool and I am pleased to note that it doing the job.
We use an ever growing utility, LCD_CTRL.ASM, which I have developed to take some of the frustration out of their work. The utility is a collection of subroutines which include;
LCD_CHAR - displays the value in the W register as a character
LCD_VAL - displays the value in the W register in hexadecimal format. That is, the high nibble is converted to a character and displayed, followed by the low nibble.
For example;
MOVLW "A" CALL LCD_CHAR ; the character "A' is displayed MOVLW "A" CALL LCD_VAL ; 41 is displayed (the ASCII value of "A")
DEBUG.
A call to DEBUG causes the value of W, and the state of the Zero and C flags to be displayed on line 1. The value in RAM location 40H is displayed as a character on line 2. The values in RAM locations 10 through 17H are displayed on lines 3 and 4 in hexadecimal format. The routine includes a second delay.
This tool permits students to selectively insert calls to DEBUG in their code and observe the values in critical registers. The idea behind displaying the character on line 2 is that in some applications, the debugging may be limited to determining just what code the PIC is executing.
For example, at one point in the code, the student might have;
MOVLW "!" MOVWF 40 CALL DEBUGAt another point they may put a "." in RAM location 40H and call DEBUG.
The LCD then displays either a "!" or a "." to indicate where their program is going.
LCD_CTRL.ASM is included by the students at the end of their program using the #include directive.
In implementing this, I have reserved RAM locations 40- 4FH and program memory locations 300 - 3FFH. That is, these locations are not available to my students.
In calls to all of the above routines, the W and STATUS registers are saved and restored prior to returning to the calling routine. The DEBUG routine uses four levels of nesting, including the call to DEBUG. Thus, students are restricted from calling DEBUG at levels of nesting greater than four.
Power Considerations. (DC Modules Only).
There may be applications where power consumption is a real consideration. An example is a battery operated data logger where the LCD is only required when the user desires to examine the data.
In the above, I have indicated the power requirements as 5.0 VDC @ 70 mA. However, this assumes, the "on" LED is used and all four of the LEDs associated with general purpose outputs are also on. If the LEDs are disabled, power dissipation is limited to the onboard PIC, the LCD and the contrast potentiometer and the current drain is reduced to 10 mA
This permits powering the unit using an output from a Stamp or PIC as shown in Figure #2.
When the LCD is not required;
DIR15=0 ' make the lead supplying power an input SLEEPWhen the LCD is required;
DIR0=1 ' make the serial lead an output OUT0=0 ' exert a zero on the serial lead DIR15=1 ' power lead an output OUT15=1 ' power the unitNote that there is a subtlety here.
The PIC associated with the LCD unit automatically detects whether the serial data format is inverted or noninverted. It does this by checking the state of the serial lead on "booting" up as the result of power being applied or a momentary depression of the manual reset button.
Unfortunately, too many "automatic" features can get one into trouble and sending inverted data is recommended. That is, in the idle condition, the serial lead is near ground.
In the above, note that the Stamp exerts a logic zero on the serial lead, prior to applying power to the PIC-n-LCD to assure the LCD does boot in the inverting mode.
(Note to PC Users). Don't be too concerned with this discussion of inverted and non inverted serial data. The PC Com port includes an RS232 level converter that causes less than minus three volts to appear on the serial lead when idle. As far as the LCD unit is concerned, you are always sending data in an inverted format.
General Purpose Outputs.
As noted above, four general purpose outputs are provided.
These are normally at a logic zero on boot up. They are addressed using control code $15 (ctrl-u) followed by the pattern. For example; $15 followed by $03 (or binary 0000 0011) will cause GPO bits 3 and 2 to be at a zero and bits 1 and 0 to be at logic one. A logic one is near +5VDC,
To make matters a bit more complex, there is a fifth general purpose output (GPO4) which is used to control the speaker when control code $07 is sent. However, it may alternately used as a fifth GPO output.
However, recognize that if control code $07 is sent to the unit for the bell function, this lead will pulse at 1000 Hz for about 0.1 seconds. Thus, if GPO bit 4 is to be used as a general purpose output, avoid use of the bell function.
GPO bits 0, 1, 2 and 3 are capable of assuming either a logic zero (near ground) or a logic one (near +5 V). All are capable of sinking and sourcing 20 mA.
GPO bit 4 differs slightly in that it is an open collector output. That is, a logic zero is near ground and a logic one is an open. This output is capable of sinking 20 mA when in the logic zero state.
Note that access to general purpose outputs GPO0-GPO3 is facilitated by wrapping wires to the 4X2 pin field near the four LEDS. Note that one row of this field is a common ground. The other row of pins are the actual outputs.

$01 (ctrl-a), Set Cursor Address.
This is followed by the location on the LCD panel.
$00 Beginning of Line 1 $40 Beginning of Line 2 $14 Beginning of Line 3 $54 Beginning of Line 4For example;
SEROUT 0, N_9600, [$01, $40+4, "Hello"]Move the cursor to position 4 on line 2 and display "Hello".
$02 (ctrl-b), Home Cursor. This was not illustrated in the above sample routines.
Moves the cursor to the upper left of the display. The content of the display is not affected.
For example;
SEROUT 0, N_9600, [$02]$03 (ctrl-c), Cursor Left.
These commands advance the cursor one position to the left or to the right. The character under the cursor is not affected. Note that $03 is a nondestructive backspace.
$05 (ctrl-e), Save Cursor.
Save the current position of the cursor. Any previously saved cursor position is overwritten.
$06 (ctrl-j), Restore Cursor.
Restore the cursor at the previously saved address.
$07 (ctrl-g), Bell.
"Rings" bell, 1000 Hz for nominally 0.1 seconds.
$08 (ctrl-h), Destructive Backspace.
The cursor is moved one position to the left and the character which is in that position is replaced with a space.
$09 (ctrl-i), Horizontal Tab.
This advances the cursor four places to the right.
$0A (ctrl-j), Line Feed.
The cursor is moved to the same position on the next line. This is usually used in conjunction with control code $0D to implement a "new line" function.
$0B (ctrl-k), Vertical Tab.
The display is scrolled up one line and the bottom line is cleared. The cursor remains in the same location.
$0C (ctrl-l), Form Feed.
The display is cleared and the cursor is moved to the upper left of the display.
$0D (ctrl-m), Carriage Return.
The cursor is positioned at the beginning of the current line.
$0E (ctrl-n), Shift Display Left
$0F (ctrl-o), Shift Display Right
These control codes shift the entire display one place to the left or right.
$11 (ctrl-q), LCD Instruction.
This control code is followed by an instruction to the LCD. This permits the user to issue commands directly to the instruction register. In most cases, the commands duplicate the rich command set provided by the PIC-n-LCD.
However, this capability does permit the user to place the display in a user defined graphics mode. Please see my Web page in the future for more details.
$12 (ctrl-r), LCD Data.
This control code is followed by a character to be sent directly to the LCD data register.
For example, sending $12 followed by $41 will cause the character "A" to be displayed. However, there is no point in doing this. Simply send the $41.
However, this ability permits the user to display characters which the PIC-n-LCD would otherwise interpret as control codes.
SERLCD_3.BS2 is a simple routine which permits you to see these characters.
' SERLCD_3.BS2
' Displays the characters $00 through $0F
' copyright, Peter H. Anderson, Feb, '98
N_9600 CON $4000+84 ' 9600 baud inverted
N VAR BYTE
DIRS = $0001 ' make P0 an output
TOP:
SEROUT 0, N_9600, [$0C] 'clear the LCD, locate cursor to up left
FOR N=0 TO $1F
SEROUT 0, N_9600, [HEX2 N, $09, $12, N]
PAUSE 2000
SEROUT 0, N_9600, [$0C] ' clear the LCD
NEXT
DONE:
GOTO DONE
Note that the value of N is displayed in hexadecimal format, followed by a
tab, followed by the control code $12, followed by the character that
would normally be interpreted as a control character.
$13 (ctrl-s), Set Number of Lines.
The PIC-n-LCD defaults to formatting for a four line display and thus, this command should not be necessary. However, for the sake of completeness;
$13, $00 No formatting. $13, $01 Format for a one line display. $13, $02 For a two line display. $13, $04 For a four line display (default).$15 (ctrl-u), Set General Purpose Outputs.
The lower five bits of the next character are used to set the general purpose outputs. Note that all five outputs are updated simultaneously. As noted above, GPO4 is an open collector output and is affected by the Bell ($07) command.
$16 (ctrl-v), Print Signed Decimal Number.
Display the next character as a signed decimal number. For example, if the number is $7F, the most significant bit is zero, indicating the number is positive and the number 127 is displayed. If the number is $81, the most significant bit is one, indicating the number is negative. The PIC-n- LCD takes the two's compliment of the lower seven bits and displays the number as -127.
$17 (ctrl-w), Print Unsigned Decimal Number.
Display the next character as an unsigned number. For example, $7F is displayed as 127. $81 is displayed as 129.
$18 (ctrl-y), Set Cursor Type.
This is followed by a character to define the style of the cursor.
$18, $00 No Cursor
$18, $01 Blinking Block
$18, $02 Underline
$18, $03 Blinking Block and Underline
