Serial LCD Kit - Basic Stamp 2 Routines

Introduction

The examples contained in this discussion were written for the Basic Stamp 2. The reason is that many of the people who are buying this kit are using the Basic Stamp. But, in addition, it is a nice platform in imparting how to use the serial LCD. After all, BASIC is pretty English like.

There are a number of discussions on interfacing the PIC with a serial LCD at http://www.access.digex.net/~pha/PIC. This includes a collection of routines to perform such simple tasks as clearing the LCD, positioning the cursor to Line 1 or Line 2, sending a character, displaying a byte, displaying a string and natural binary to BCD conversion routines.

Power.

Power the serial LCD with a source of 5.0 VDC.

Serial Connection.

The serial LCD may be connected to any serial source capable of generating either 2400 or 9600 baud. This might be a Basic Stamp, a PIC or similar processor. Note that the connection consists of two leads, the signal lead and ground.

Delay between Characters.

Serial characters are received by the serial LCD processor, acted on and the LCD processor then returns to fetch the next character. The amount of time required for the processor to control the LCD varies depending on the operation being performed.

To avoid the processor missing a serial character, it is recommended that there be a minimum 5.0 msec delay between the transmission of each character.

In the case of the Basic Stamp, this may easily be accomplished by setting the "pace" field to 5 or greater.

For example;

	SEROUT 0, 396+$4000, 10, ["ABCDEF"]

			               ^ pace is set to 10 msecs delay
between characters

Sending Instructions to the LCD.

Characters which are sent to the serial LCD may be of two types, either instructions to configure the LCD or actual data to be displayed on the LCD.

Instructions are distinguished from data by first sending the character $FE (or 254 decimal). The actual instruction is the next character.

For example;

	$FE $01		; clears the LCD
	$FE $80		; sets the cursor to the beginning of line 1
	$FE $80+$40	; sets the cursor to the beginning of line 2

Other instruction commands are discussed in the context of examples.

Example SERIAL_1.BS2.

The following program illustrates how to clear the LCD, set the cursor to either the beginning of line 1 or line 2, how to display text and how to display variables in either decimal or hexadecimal format.

A brief word about the Basic Stamp 2's SEROUT command.

	SEROUT pin, bit_delay, intercharacter_delay, [...]

The bit delay determines the baud rate. For example; at 2400 baud, each bit is 1/2400 = 416 usecs. However, the Stamp has a built in overhead of 20 usecs. Thus, the bit delay is specified as 416 - 20 = 396. For 9600 baud, 1/9600 = 104. Subtracting 20 gives 84.

In addition to the delay timing, bit 14 of this parameter controls whether the data is sent inverted or non-inverted. Thus, for inverted;

	0100 0000 0000 0000 or $4000 
' SERIAL_1.BS2
'
' Illustrates how to display simple text strings and variables.
'
' For 2400 Inverted use SEROUT 0, 396+$4000, 10, [...]
' For 9600 Inverted use SEROUT 0, 84+$4000, 10, [...]
' For 2400 Non inverted use SEROUT 0, 396+$0, 10, [...]
' For 9600 Non inverted use SEROUT 0, 84+$0, 10, [...]
'
' P0 (term 5) -------------------------- To RX on LCD
'
' Towanda Malone, Morgan State University, 7 July, '97

	
	B_2400  CON 396
	B_9600  CON 84
	INV	CON $4000
	NON	CON $0000
	
	T_C_WHOLE VAR BYTE
	T_C_DEC VAR BYTE
	N 	VAR BYTE

	DIR0=1  		' define serial output to LCD

TOP:

	SEROUT 0, B_2400+INV, 10, [$FE, $01]	' clear LCD
	SEROUT 0, B_2400+INV, 10, [$FE, $80]	' cursor at line 1
	SEROUT 0, B_2400+INV, 10, ["This is line 1"] ' display some text

	SEROUT 0, B_2400+INV, 10, [$FE, $80+$40, "This is line 2"]
	' locate cursor at the beginning of line 2 and display some text

	PAUSE 10000	' 10 second pause to admire
 
	SEROUT 0, B_2400+INV, 10, [$FE, $01]	' clear the LCD
	
	T_C_WHOLE=23	' dummy up a temperature of 23.17 degrees
	T_C_DEC=17

	SEROUT 0, B_2400+INV, 10, [$FE, $80, "T_C = ", dec T_C_WHOLE]
	SEROUT 0, B_2400+INV, 10, [".", dec T_C_DEC, "Degrees C."]
	' display T_C = 23.17 Degrees C.
	
	PAUSE 10000	' pause to admire

	SEROUT 0, B_2400+INV, 10, [$FE, $01, $FE, $80] ' clear, line 1

	FOR N=0 TO 15	' count up in decimal
               SEROUT 0, B_2400+INV, 10, [dec N, " "]
           NEXT

	SEROUT 0, B_2400+INV, 10, [$FE, $80+$40]	' line 2

	FOR N=0 TO 15 	' repeat in hex
	   SEROUT 0, B_2400+INV, 10, [hex N, " "]
	NEXT

	PAUSE 10000

	GOTO TOP

Positioning the Cursor.

The sequence $FE $80+n where n may be in the range of 00 - $7F determines the location of the cursor. For two line LCDs $80+$00 is the beginning of line 1 and $80+$40 is the beginning of line 2.

One can position the cursor at the 8th position on the first line;

	$FE $80+(8-1)	' Note that the first position is +0
	
	$FE $80+$40+(8-1)	' Eighth position on line 2

Example SERIAL_2.BS2.

This example illustrates how to display four varying quantities at different points on a 2-line display.

For example;

	Q1=	Q2=
	Q3=	Q4=

' SERIAL_2.BS2
'
' Illustrates how to display four quantities at different places on a
' 2-line display.
'
' P0 (term 5) -------------------------- To RX on LCD
'
' Towanda Malone, Morgan State University, 7 July, '97

	
	B_2400  CON 396
	B_9600	CON 84
	INV	CON $4000
	NON	CON $0000
	
	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, B_2400+INV, 10, [$FE, $01]	' clear LCD

	' display Q1=	Q2=
	'	  Q3=   Q4=

	SEROUT 0, B_2400+INV, 10, [$FE, $80, "Q1="]	  ' line 1, pos 0
	SEROUT 0, B_2400+INV, 10, [$FE, $80+(8-1), "Q2="] ' line 1, pos 7
	SEROUT 0, B_2400+INV, 10, [$FE, $80+$40, "Q3="]'  ' line 2, pos 0
	SEROUT 0, B_2400+INV, 10, [$FE, $80+$40+(8-1), "Q4="]' line 2, pos
7

	FOR N=0 to $FF
	   Q1=N		' give quantities some varying values
	   Q2=100+N
	   Q3=200+N
	   Q4=300-N
		
           SEROUT 0, B_2400+INV, 10, [$FE, $80+3, dec Q1]
	   SEROUT 0, B_2400+INV, 10, [$FE, $80+(8-1)+3, dec Q2]
           SEROUT 0, B_2400+INV, 10, [$FE, $80+$40+3, dec Q3]
           SEROUT 0, B_2400+INV, 10, [$FE, $80+$40+(8-1)+3, dec Q4]

	   PAUSE 300
	NEXT
	STOP

The New Line Feature.

Most serial LCD displays cannot handle a newline character ($0D). Rather the LCD simply displays a "-"

This LCD interprets the newline character ($0D) as one would expect.

If the LCD is currently on line 1, the receipt of a new line character causes the cursor to go to the beginning of line 2. However, if the cursor is already on line 2, the text which is currently on line 2 is written to line 1 and the cursor is positioned at the beginning of line 3.

In implementing this feature a 64 byte RAM buffer in the processor is used which stores a copy of the text on line 2. When the newline character is received, the LCD is cleared. Then the buffer is displayed on line 1. Finally, the cursor is located at the beginning of line 2.

All of this requires time; about 150 msecs. Thus, when using the newline feature, please allow 150 msecs before sending the next character.

[Implementation of this feature turned out to be more complex than was first thought. The fact that we were able to cram this feature along with everything else the processor does into a mere 512 program locations is quite a testament to the power of the Microchip PICs.]

Example - SERIAL_3.BS2.

' SERIAL_3.BS2
'
' Illustrates how to use the newline feature.  
'
' Note that this works only with a 2-line LCD display.
'
'
' P0 (term 5) -------------------------- To RX on LCD
'
' Towanda Malone, Morgan State University, 7 July, '97

	
	B_2400  CON 396
	B_9600  CON 84
	INV	CON $4000
	NON	CON $0000
	
	
	N 	VAR BYTE

	DIR0=1  		' define serial output to LCD

MAIN:
	SEROUT 0, B_2400+INV, 10, [$FE, $01, $FE, $80]	' clear LCD

	' now display some strings
	SEROUT 0, B_2400+INV, 10, ["The Rochanda Team"]
	PAUSE 500	' pause to admire

	SEROUT 0, B_2400+INV, 10, [$0D]
	PAUSE 150	' to allow newline feature to execute
	SEROUT 0, B_2400+INV, 10, ["Morgan State University"]
	PAUSE 500	' pause to admire

	SEROUT 0, B_2400+INV, 10, [$0D]
	PAUSE 150	' to allow newline feature to execute
	SEROUT 0, B_2400+INV, 10, ["Dept of EE"]
	PAUSE 500	' pause to admire

	SEROUT 0, B_2400+INV, 10, [$0D]
	PAUSE 150	' to allow newline feature to execute
	SEROUT 0, B_2400+INV, 10, ["Baltimore, MD"]
	PAUSE 500	' pause to admire

	SEROUT 0, B_2400+INV, 10, [$FE, $01, $FE, $80]	' clear LCD

	' illustrates how to also display a variable	
	FOR N=0 to 255
	   SEROUT 0, B_2400+INV, 10, ["Line=", dec N]
	   PAUSE 500	' pause to admire
	   SEROUT 0, B_2400+INV, 10, [$0D]
	   PAUSE 150	' to allow newline feature to execute
           NEXT
	   
	STOP

Four Line Displays.

This processor was specifically designed for 2-line displays.

However, we believe that with the exception of the new line feature, this will work with 4-line LCD displays.

Recall earlier that, the cursor was positioned to any point on line 1 using;

$FE $80+n	; line 1 - where n is in the range 00-3F (0 to 63 decimal)
$FE $80+$40 	; line 2 - n is 00-3F (0 to 63 decimal)

This is applicable to 2-line displays.

For four line displays, the definitions of the beginnings of the lines;

	Line 1	$80+$0	 ($80)
	Line 2  $80+$40	 ($C0)

	Line 3  $80+$0 + 20  ($94) 	
	Line 4 	$80+$40 + 20 ($D4)

Note that Line 1 consists of only 20 bytes. To go further would be to display on Line 3. The same is true of Line 2. To go further would be to display on Line 4.

Again, please note. The "new line" feature will not work. Simply avoid sending the character $0D.

' SERIAL_4.BS2
'
' Illustrates how to display four quantities on different lines on a
' 4-line display.
'
' P0 (term 5) -------------------------- To RX on LCD
'
' Towanda Malone, Morgan State University, 7 July, '97

	
	B_2400  CON 396
	B_9600  CON 84
	INV	CON $4000
	NON	CON $0000
	
	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, B_2400+INV, 10, [$FE, $01]	' clear LCD


	' display 	Q1=
	'	  	Q2=
	'	  	Q3=
	'       	Q4=

	SEROUT 0, B_2400+INV, 10, [$FE, $80, "Q1="]  ' line 1, pos 0
	SEROUT 0, B_2400+INV, 10, [$FE, $C0, "Q2="]  ' line 2, pos 0
	SEROUT 0, B_2400+INV, 10, [$FE, $94, "Q3="]  ' line 3, pos 0
	SEROUT 0, B_2400+INV, 10, [$FE, $D4, "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, B_2400+INV, 10, [$FE, $80+4, dec Q1]
           SEROUT 0, B_2400+INV, 10, [$FE, $C0+4, dec Q2]
           SEROUT 0, B_2400+INV, 10, [$FE, $94+4, dec Q3]
           SEROUT 0, B_2400+INV, 10, [$FE, $D4+4, dec Q4]

	   PAUSE 300
	NEXT
	STOP