LCDCMD

BS2p icon BS2pe icon BS2px icon

LCDCMD Example

 

 

 

Syntax: LCDCMD Pin, Command

Function

Send a command to an LCD display.

Quick Facts

  BS2p, BS2pe, and BS2px
Values for Pin 0, 1, 8, or 9
I/O pin arrangement when Pin is 0 or 1 0 or 1 (depending on Pin) : LCD Enable (E) pin

2 : LCD Read/Write (R/W) pin

3 : LCD Register Select (RS) pin

4 - 7 : LCD Data Bus (DB4 - DB7, respectively) pins
I/O pin arrangement when Pin is 8 or 9 8 or 9 (depending on Pin) : LCD Enable (E) pin

10 : LCD Read/Write (R/W) pin

11 : LCD Register Select (RS) pin

12 - 15 : LCD Data Bus (DB4 - DB7, respectively) pins
Special Notes LCDCMD is designed to use the LCD's 4-bit mode only.
Related Commands

LCDIN, LCDOUT

Explanation

The three LCD commands (LCDCMD, LCDIN and LCDOUT) allow the BS2p, BS2pe, or BS2px to interface directly to standard LCD displays that feature a Hitachi 44780 controller (part #HD44780A). This includes many 1 x 16, 2 x 16, and 4 x 20 character LCD displays.

The Hitachi 44780 LCD controller supports a number of special instructions for initializing the display, moving the cursor, changing the default layout, etc. The LCDCMD command is used to send one of these instructions to the LCD. It is most commonly used to initialize the display upon a power-up or reset condition. Note that LCDCMD, LCDIN and LCDOUT use a 4-bit interface to the LCD which requires a specific initialization sequence before LCDIN and LCDOUT can be used. Specifics on the initialization sequence will follow.

The following is an example of the LCDCMD command:

  LCDCMD 1, 24

The preceding code will send the Scroll Left command (represented by the number 24) to the LCD whose enable pin is connected to I/O pin 1. This will cause the LCD display to scroll, or shift, the entire display one character to the left.

You may have noticed that the Pin argument in the example above was 1. The LCDCMD command actually uses more than just this I/O pin, however. The LCDCMD command requires seven I/O pins. This is because the standard LCD displays have a parallel interface, rather than a serial one. The Pin argument can be the numbers 0, 1, 8, or 9 and will result in the use of the I/O pins shown above. The figure below shows the required wiring for the above command to work.

Note that we could have used 0 for the Pin argument and moved the LCD's Enable pin (pin 6) to I/O pin 0. Similarly, using 9 for the Pin argument would have required us to wire the LCD's pins to I/O pins 9 through 15, rather than I/O pins 1 through 7.

When the LCD is first powered-up, it will be in an unknown state and must be properly configured before sending commands like the one shown above. This process is known as initializing the LCD and is the first thing your program should do upon starting up. The following code is a good example of LCD initialization. (Refer to Hitachi documentation for details).

Init_LCD:
  PAUSE 1000                            ' allow LCD to power-up
  LCDCMD 1, 48                          ' Send wake-up sequence (3x)
  PAUSE 5
  LCDCMD 1, 48
  PAUSE 1
  LCDCMD 1, 48
  PAUSE 1
  LCDCMD 1, 32                          ' Set data bus to 4-bit mode
  LCDCMD 1, 40                          ' Set 2-line mode with 5x8 font
  LCDCMD 1, 8                           ' Turn display off
  LCDCMD 1, 12                          ' Turn display on without cursor
  LCDCMD 1, 6                           ' Auto-increment cursor 
  LCDCMD 1, 1                           ' Clear the display

This initialization code is the most commonly used sequence for a 2 x 16 and 4 x 20 LCD display (the 2-line mode instruction sets the 4 x 20 to 4-line mode). The PAUSE 1000 command is optional, but only if your program takes more than approximately 700 ms before it executes the Init_LCD code above. Without it, upon powering your circuit, the BASIC Stamp may talk to the LCD too early, the LCD will then miss some of the commands and the display will operate strangely, or not at all.

Do not change the "wake-up" and "4-bit mode" sequence commands. However, the commands below the line that says, "Set data bus to 4-bit mode" may be modified to set other desired modes.

The table below shows the most commonly used LCD commands. Here's an example:

  LCDCMD 1, 128 + 64

The preceding command will move the cursor to the first character position on the second line (on a 2 x 16 display). 128 is the Move To Display Address command and 64 is the location number. See the "Character Positioning" section below, for more information.

Common LCD Commands

  Command

(Decimal)
Description
Do Nothing 0 Don't perform any special function.
Clear Display 1 Move cursor and display to home position.
Home Display 2 Move cursor and display to home position.
Inc Cursor 6 Set cursor direction to right, without a display shift.
Display Off 8 Turn off display (display data is retained).
Display On 12 Turn on display without cursor (display is restored).
Blinking Cursor 13 Turn on display with blinking cursor.
Underline Cursor 14 Turn on display with underline cursor.
Cursor Left 16 Move cursor left one character.
Cursor Right 20 Move cursor right one character.
Scroll Left 24 Scroll display left one character.
Scroll Right 28 Scroll display right one character.
Move To CGRAM Address 64 + address Move pointer to Character Generator RAM location
Move To DDRAM Address 128 + address Move cursor to Display Data RAM location

 

While most users will only need the commands shown above, the table below details all of the instructions supported by the LCD (for advanced users). Many instructions are multipurpose, depending on the state of special bits. Clever manipulation of the instruction bits will allow for powerful control of the LCD.

  Command Code (bits) Description
7 6 5 4 3 2 1 0
Clear Display 0 0 0 0 0 0 0 1 Clear entire display and move cursor home (address 0)
Home Display 0 0 0 0 0 0 1 0 Move cursor home and return display to home position.
Entry Mode 0 0 0 0 0 1 M S Sets cursor direction (M: 0=left, 1=right) and display scrolling (S: 0=no scroll, 1=scroll)
Display/Cursor 0 0 0 0 1 D U B Sets display on/off (D), underline cursor (U) and blinking block cursor (B). (0=off, 1=on)
Scroll Display / Shift Cursor 0 0 0 1 C M 0 0 Shifts display or cursor (C: 0=cursor, 1=display) left or right (M: 0=left, 1=right).
Function Set 0 0 1 B L F 0 0 Sets bus size (B: 0=4-bits, 1=8-bits), number of lines (L: 0=1?line, 1=2-lines) and font size (F: 0=5x8, 1=5x10)
Move To CGRAM Address O 1 A A A A A A Move pointer to Character Generator RAM location specified by address (A)
Move To DDRAM Address 1 A A A A A A A Move cursor to Display Data RAM location specified by address (A)

 

The last command shown above (Move To DDRAM Address) is used to move the cursor to a specific position on the LCD. The LCD's DDRAM (Display Data RAM) is a fixed size with unique position number for each character cell. The viewable portion of the DDRAM depends on the LCD's logical view position (which can be altered with the Scroll Display command). The default view position is called the Home position; it means that the display's upper left character corresponds to DDRAM location 0. The diagrams below indicates the position numbers for characters on the LCD screen.

Note that the diagram shows the most common DDRAM mapping, though some LCD's may have organized the DDRAM differently. A little experimentation with your LCD may reveal this.

2 x 16 Display

  On-screen Positions* Off-screen
Line 1 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 ... 39
Line 2 64 65 66 67 68 69 79 71 72 73 74 75 76 77 78 79 80 ... 103

* Assuming the display is in the home position

4 x 20 Display

Line 1 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
Line 2 64 65 66 67 68 69 79 71 72 73 74 75 76 77 78 79 80 81 82 83
Line 3 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39
Line 4 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103

 

On a standard 2 x 16 character display, the following command would move the cursor to the third column of the second line:

  LCDCMD 1, 128 + 66

The number 128 tells the LCD we wish to move the cursor and 66 is the location number of the desired position. Similarly, sending just 128 (128 + 0) would move the cursor to the first character of the first line (the upper left character if the display is at the home position).

You may have noticed that the 2 x 16 display has many locations that are not visible; they are to the right of the edge of the screen. These locations (16 - 39 and 80 to 103) become important for scrolling operations. For example, it is possible to move the cursor to location 16, print some text there and then issue a number of Scroll Left instructions (LCDCMD 1, 24) to slowly scroll the text onto the display from right to left. If you did so, the DDRAM positions that were on the left of the screen would now be past the left edge of the screen. For example:

  LCDCMD 1, 24
  LCDCMD 1, 24

...would cause the screen to scroll to the left by two characters. At this point, the upper-left character in the display would actually be DDRAM location 2 and the lower-left character would be DDRAM location 66. Locations 0, 1, 64 and 65 would be off the left edge of the LCD and would no longer be visible. Some interesting effects can be achieved by taking advantage of this feature.

The 4 x 20 LCD has a strange DDRAM map. The upper-right character is location 19 and the next location, 20, appears as the first character of the third line. This strange mapping is due to constraints in the LCD controller and the manufacturers design, and unfortunately makes the scrolling features virtually useless on the 4 x 20 displays.

Even though the LCD requires many pins to talk to it, only the Enable pin needs to remain dedicated to the LCD and all the other pins can be multiplexed (shared) with certain other devices (if wired carefully). In addition, the I/O pin connected to the LCD's R/W pin is only necessary if the LCDIN command will be used in the application. If the LCDIN command will not be used, LCD pin 5 (R/W pin) can be connected to ground and I/O pin 2 (shown above) may be left disconnected. I/O pin 2 will still be set to output mode for each LCDCMD and LCDOUTcommand executed, however.

Go to PBASIC Language home page

Open Getting Started with Stamps in Class

Open Connection Troubleshooting

BASIC Stamp Help Version 2.5.4

Copyright © Parallax Inc.

8/21/2013