Tel: 440-232-8590
Fax: 440-232-8593
E-mail: sales@LiquidCrystalTechnologies.com

Basic Stamp Code

Basic Stamp Code for Driving an LCD Module You can pretty much figure out how to initialize and send date to a standard LCD character module by inspecting this code. It was written so that it is easy to follow, not necessarily because this is the best way to do it, or that this is the fanciest code.

We hope this will get you started, but as is usual in these situations, we assume no responsibility for any trouble you might get in by using this code. We have however used this code many times over the years, and there's not a lot that can really go wrong.

'{$STAMP BS2}
'DATA 0-7 ON STAMP OUTPUTS P0-P7
'RS ON P13 R/W ON P14 E ON P15
NUMROWS VAR BYTE
NUMCOLS VAR BYTE

' ' ****************************************************************************
NUMROWS = 4 ' This must be the number of rows in the module
NUMCOLS = 20 ' This must be the number of columns
'
ROW1 DATA "                     " ' DATA FOR ROW 1 - These must be 20
ROW2 DATA "                     " ' DATA FOR ROW 2 - characters wide
ROW3 DATA "                     " ' DATA FOR ROW 3 - for a 20 character
ROW4 DATA "                     " ' DATA FOR ROW 4 - module ' ****************************************************************************
ROWCOUNT VAR BYTE
CHARCOUNT VAR BYTE
REPEAT VAR BYTE
ROWNUM VAR BYTE
CMDVALUE VAR BYTE
DIRS = %1111111111111111 ' ALL PINS SET TO OUTPUT
OUTS = %1111111111111111 ' ALL PINS SET HIGH
PAUSE 50
OUTS = %0000000000000000' ALL PINS SET LOW

PAUSE 50

CMDVALUE = %00111000 ' SET 8 BIT MODE
GOSUB SETCOMMAND
MDVALUE = %00001110 ' ENABLE DISPLAY
GOSUB SETCOMMAND
CMDVALUE = %00000110 ' SET DATA ENTRY MODE
GOSUB SETCOMMAND
CMDVALUE = %00000001 ' CLEAR DISPLAY
GOSUB SETCOMMAND
CMDVALUE = %00000010 ' HOME THE CURSOR
GOSUB SETCOMMAND

PAUSE 50

FILLCHARS: ' FILLS THE ROWS
ROWNUM = %10000000 ' SET ROW 1 &H00 ADDRESS
GOSUB SETROW

PAUSE 20

FOR REPEAT = 0 TO NUMCOLS-1
READ REPEAT,CHARCOUNT
OUTL = CHARCOUNT
PAUSE 1
OUTH = %10100000 'SETS E AND RS HIGH
PAUSE 1
OUTH = %00100000 'SETS E LOW AND RS HIGH
PAUSE 1
NEXT

IF NUMROWS < 2 THEN CURSOROFF
ROWNUM = %11000000 ' SET ROW 2 &H40 64DEC ADDRESS
GOSUB SETROW
PAUSE 20
FOR REPEAT = 20 TO 20+NUMCOLS-1
READ REPEAT,CHARCOUNT
OUTL = CHARCOUNT
PAUSE 1
OUTH = %10100000 'SETS E AND RS HIGH
PAUSE 1
OUTH = %00100000 'SETS E LOW AND RS HIGH
PAUSE 1
NEXT

IF NUMROWS < 3 THEN CURSOROFF
ROWNUM = %10010100 ' SET ROW 3 &H14 20DEC ADDRESS
GOSUB SETROW
PAUSE 20
FOR REPEAT = 40 TO 40+NUMCOLS-1
READ REPEAT,CHARCOUNT
OUTL = CHARCOUNT
PAUSE 1
OUTH = %10100000 'SETS E AND RS HIGH
PAUSE 1
OUTH = %00100000 'SETS E LOW AND RS HIGH
PAUSE 1

NEXT IF NUMROWS < 4 THEN CURSOROFF
ROWNUM = %11010100 ' SET ROW 4 &H54 84DEC
GOSUB SETROW
PAUSE 20
FOR REPEAT = 60 TO 60+NUMCOLS-1
READ REPEAT,CHARCOUNT
OUTL = CHARCOUNT
PAUSE 1
OUTH = %10100000 'SETS E AND RS HIGH
PAUSE 1
OUTH = %00100000 'SETS E LOW AND RS HIGH
PAUSE 1
NEXT
CURSOROFF: CMDVALUE = %00001100 ' TURN THE CURSOR OFF
GOSUB SETCOMMAND
END ' THE END OF THE PROGRAM

SETROW:
OUTL = ROWNUM
OUTH = %10000000
PAUSE 1
OUTH = %00000000
OUTL = %00000000
PAUSE 1
RETURN

SETCOMMAND:
OUTL = CMDVALUE
OUTH = %10000000
PAUSE 1
OUTH = %00000000
OUTL = %00000000
PAUSE 1
RETURN