Using LCD Displays With The Stamp
---------------------------------

In a stand-alone application, it's hard to beat the versatility of an
LCD display for the Stamp.  The displays are easy to find, come in a
variety of configurations, and easily connect to the Stamp.

Because the Stamp has a limited number of I/O pins, using the LCD in
its 4-bit mode is necessary.  The LCD will actually require six lines:
four for data and two for control.  This leaves two of the Stamps pins
completely free for other interfacing.  This may not sound like much,
but the Stamp is a very capable beast.  Application Note #1 shows how
to connect an LCD to the Stamp and still use these lines for inputs!

Along with this file you should find two companion programs:

 - LCDDEMO1.BAS   Standard features
 - LCDDEMO2.BAS   Builds and displays custom characters

Standard features of the LCD include:

  - Display clear
  - Cursor home
  - Display ON/OFF
  - Cursor ON/OFF
  - Display character blink
  - Cursor shift
  - Display shift

Both demos assume that you need only to write to the display.  They 
are heavily commented and will provide you with what you need to 
effectively use an LCD display in this mode.  The rest of this text 
will cover material that is not contained within the demo programs.


Unseen characters

The LCD is capable of storing more characters than it can physically
display.  A typical 16x1 LCD contains enough RAM for 80 (addressed
as 00 to 4F hex) characters but only 16 are visible at any given time.  
Note that when the display is shifted the DD RAM is treated as a circ-
ular buffer with 16 characters visible through the LCD window.

        Normal window (DD RAM addresses in hex)
        00 01 02 03 04 05 06 07 08 09 0A 0B 0C 0D 0E 0F

        After left shift
        01 02 03 04 05 06 07 08 09 0A 0B 0C 0D 0E 0F 10

        After right shift
        4F 00 01 02 03 04 05 06 07 08 09 0A 0B 0C 0D 0E


Multi-line Mode

After the 4-bit interface is set in the initialization sequence, the
function set command can be issued to cause a multi-line display in 
some units.  It is important to understand that the DD (Display Data)
RAM addressing is not always contiguous (because of the ability to
store non-visible characters).

        Addressing (addresses in hex)

        16x1:
        00 01 02 03 04 05 06 07 08 09 0A 0B 0C 0D 0E 0F

        8x2 (configured physically as 16x1):
        00 01 02 03 04 05 06 07 40 41 42 43 44 45 46 47

        16x2:
        00 01 02 03 04 05 06 07 08 09 0A 0B 0C 0D 0E 0F 
        40 41 42 43 44 45 46 47 48 49 4A 4B 4C 4D 4E 4F


Character Generator RAM

LCDDEMO2.BAS shows how to contruct and display custom characters on 
the LCD display.  With the typical 5x7 font, the LCD has enough RAM
to build eight custom characters.  Each character requires eight
bytes of RAM.  The layout below shows how the character is mapped
in these eight bytes (X = bit used, . = not used):

Byte 1  . . . X X X X X
        . . . X X X X X
        . . . X X X X X
        . . . X X X X X
        . . . X X X X X
        . . . X X X X X 
        . . . X X X X X 
Byte 8  . . . X X X X X  (cursor line - not normally used)

With this scheme, custom characters are mapped into the CG RAM 
(addresses in hex) as follows:

        Character       CG RAM 
        ---------       ---------
            0           00 - 07
            1           08 - 0F
            2           10 - 17
            3           18 - 1F   (last char if 5x10 font used)
            4           20 - 27
            5           28 - 2F
            6           30 - 37
            7           38 - 3F


LCDDEMO2.BAS builds and displays 3 custom characters for an animation
sequence.  The character maps (with hex equivalents are):

. = 0 bit
X = 1 bit

Char 0  . . . . X X X .  $0E
        . . . X X X X X  $1F
        . . . X X X . .  $1C
        . . . X X . . .  $18
        . . . X X X . .  $1C
        . . . X X X X X  $1F
        . . . . X X X .  $0E
        . . . . . . . .  $00

Char 1  . . . . X X X .  $0E
        . . . X X X X X  $1F
        . . . X X X X X  $1F
        . . . X X . . .  $18
        . . . X X X X X  $1F
        . . . X X X X X  $1F
        . . . . X X X .  $0E
        . . . . . . . .  $00
                         
Char 2  . . . . X X X .  $0E
        . . . X X X X X  $1F
        . . . X X X X X  $1F
        . . . X X X X X  $1F
        . . . X X X X X  $1F
        . . . X X X X X  $1F
        . . . . X X X .  $0E
        . . . . . . . .  $00


The demo program stores this data in the EEPROM and then moves it to
the LCD during the initialization sequence.

The animation routine is probably worth a bit of explanation.  Here
it is, lifted from the program:

        FOR index1 = 0 TO 15            ' cover 16 characters
          FOR index2 = 0 TO 4           ' 5 characters for animation
            char = %10000000 | index1   ' set DD RAM address
            GOSUB LCDcmd                ' move cursor to new addr
            LOOKUP index2,(0,1,2,1," "),char
            GOSUB WrLCD                 ' write animation character
            PAUSE 75                    ' delay between chars
          NEXT index2
        NEXT index1

(a)     The outer loop (index1) is setup to cover the 16 characters
        in the display.  0 TO 15 is used so that the value of index1
        can be used to move the cursor to the proper position.

(b)     The inner loop (index2) is used to cycle through the anima-
        tion characters.  Within this loop we move to the correct
        display position, get the character to display from a LOOKUP
        table (we could have used 32 [ASCII code] for the space char-
        acter but " " is more readable), write the character and then
        pause a bit so that we can see what happened.

        Important note: The display position must be set in the inner
        loop because the LCD initialization causes the DD RAM address 
        (cursor position) to be incremented after each character write.



I hope that you find this information and the enclosed files of value.
It should, at least, save you an hour or so of wading through the 
Hitatchi manual.  If you have any comments, suggestions or questions, 
please post a message on the Parallax BBS or to my CompuServe address.

Jon Williams
Riverside, CA
CIS 73110,3272
