LCD Displays
This section is about driving the standard character LCD displays using Basic. There are some useful techniques used including using constants so that it is easy to use the program presented on any ports without the need to re-write the program.
The most common variety of LCD displays are the character displays that come in 1,2 and 4 line formats. They are nearly always controlled by HD44780 controller.

This is the kind of display at the subject of this text. This particular one has 2 lines of 16 characters and can be driven useing 8 or 4 data lines.
| Pin number | Symbol | Level | I/O | Function |
| 1 | Vss | - | - | Power supply (GND) |
| 2 | Vcc | - | - | Power supply (+5V) |
| 3 | Vee | - | - | Contrast adjust |
| 4 | RS | 0/1 | I | 0 = Instruction input 1 = Data input |
| 5 | R/W | 0/1 | I | 0 = Write to LCD module 1 = Read from LCD module |
| 6 | E | 1, 1->0 | I | Enable signal |
| 7 | DB0 | 0/1 | I/O | Data bus line 0 (LSB) |
| 8 | DB1 | 0/1 | I/O | Data bus line 1 |
| 9 | DB2 | 0/1 | I/O | Data bus line 2 |
| 10 | DB3 | 0/1 | I/O | Data bus line 3 |
| 11 | DB4 | 0/1 | I/O | Data bus line 4 |
| 12 | DB5 | 0/1 | I/O | Data bus line 5 |
| 13 | DB6 | 0/1 | I/O | Data bus line 6 |
| 14 | DB7 | 0/1 | I/O | Data bus line 7 (MSB) |
This table shows the pin outs of a typical display but some vary slightly There are two main options for using this controller, using all 8 data lines or just using 4 data lines. The example we will go through will use just 4 data lines, this is more complex to set up but once done it is worth the effort as 4 data lines are saved from a precious resource.
The Code
To make the code more versatile constants have been used so that at a later date a different port or bits of that port can be used without re-writing the code. This does make it less readable though.
// Example lcd connections on port D
// D1 LCD data line 4 pin 11
// D2 LCD data line 5 pin 12
// D3 LCD data line 6 pin 13
// D4 LCD data line 7 pin 14
// D5 LCD RS pin 4
// D6 LCD RW pin 5
// D7 LCD E pin 6
// LCD pin 1 GND
// LCD pin 2 + 5V
// LCD pin 3 GND (contrast)
//
constant LCD_PORT$ "d"
constant LCD_RS_BIT 5
constant LCD_RW_BIT 6
constant LCD_E_BIT 7
constant LCD_BSY_MASK 0x10 // set to mask off all but bit 7
constant LCD_IO_MASK 0xfe // this should be all of the 7 port bits
// D4 to D7 on LCD must be contiguous, data_start is the data bit of D4 on
// the LCD, 5 will be the next bit etc. e.g. if lcd data lines are connected
// to say RB4-7 then LCD_DATA_START will be 4 constant LCD_DATA_START 1
Normally when addressing a particular port a character 'b' to 'g' represents that port so you would write for example:
portb "dos" 12
However it is just as valid to make the string directive up of data items and so using the constant will enable the port to be easily changes at some future date thus:
portb LCD_PORT$+"os" 12
This is exactly the same as above, the 'lcd.bas' (downloads section) uses this technique. The display is initialised as 4 bits in the lcd_init function and lcd_cmd & lcd_data are used to 'talk' to the display. There is also and example of how to write a string to the display, this is given here:
function lcd_type(a$)
dim j
for j=1 to len(a$)
lcd_data(asc(a$,j))
next
endf
Because of the automatic type conversion it is also possible to use this to send numbers as well. The full code is in the downloads section. It uses a delay to ensure that the display si not busy which will be fine for most application, it can be improved by added code to detect the busy flag. There are many references on the net to these display types and the commands that they will except, this site is worth a look. http://ouwehand.net/~peter/lcd/lcd0.shtml#TOC