Experiments with LCD03 4x20 LCD display and 12key keypad - using the i2c bus

Objective
Learn to drive the LCD03 and keypad from
robot-electronics using the i2c option (RS232 is also possible)
The LCD03 carries a small daughter board that also connects to the keypad and the i2c lines
Tested on Debian nslu2 (Slug) - should also be OK on Sweex/Edimax routers etc using OpenWrt and OpenWrt Slug.

Method
I use the tools from i2ctools in lm-sensors
i2cget and i2cset to drive the LCD03 interface (they can be used on any Linux)

It should be possible to use "apt-get install i2c-tools" (originally part of the lm-sensors project)
for Debian Slug but mine refused to create i2cget - so I used the SVN to compile my own - very easy - see the README in the package.

The Blassic basic program below is a demonstrator to remind me how to use the LCD03 in future (!)
It shells to the bash script environment to run the i2cget and i2cset utilities.

If you press a key on the pad the number appears on the LCD and espeak speaks the value.
If you press # the program exits

#!/usr/sbin/blassic
'i2c_keyboard_speak.bas
'i2c driver for LCD03 display and keypad
'press a key on the keypad - display it and speak its value

LABEL Again

'send the low and high bytes to the ramdisk then bring it into Blassic
SHELL "i2cget -y 0 0x63 0x1 b > /var/www/ramdisk/lo_byte.txt"
OPEN "/var/www/ramdisk/lo_byte.txt" FOR INPUT AS #1
INPUT #1,lo_byte$
CLOSE #1
SHELL "i2cget -y 0 0x63 0x2 b > /var/www/ramdisk/hi_byte.txt"
OPEN "/var/www/ramdisk/hi_byte.txt" FOR INPUT AS #1
INPUT #1,hi_byte$
CLOSE #1

'PRINT lo_byte$
'PRINT hi_byte$

IF lo_byte$ = "0x01" THEN key$ = "1": hex_key$ = "31"
IF lo_byte$ = "0x02" THEN key$ = "2": hex_key$ = "32"
IF lo_byte$ = "0x04" THEN key$ = "3": hex_key$ = "33"
IF lo_byte$ = "0x08" THEN key$ = "4": hex_key$ = "34"
IF lo_byte$ = "0x10" THEN key$ = "5": hex_key$ = "35"
IF lo_byte$ = "0x20" THEN key$ = "6": hex_key$ = "36"
IF lo_byte$ = "0x40" THEN key$ = "7": hex_key$ = "37"
IF lo_byte$ = "0x80" THEN key$ = "8": hex_key$ = "38"
IF hi_byte$ = "0x01" THEN key$ = "9": hex_key$ = "39"
IF hi_byte$ = "0x02" THEN key$ = "stah": hex_key$ = "2A"
IF hi_byte$ = "0x04" THEN key$ = "0": hex_key$ = "30"

'temporary exit - press # to stop the program
IF hi_byte$ = "0x08" THEN key$ = "hash": hex_key$ = "23":SHELL "espeak -s 140 'gud bye' > /dev/null 2>&1" :SYSTEM

IF lo_byte$ = "0x00" AND hi_byte$ = "0x00" THEN key$ = "no key pressed":hex_key$ = "7E"

'PRINT "key pressed is ",key$

'send error reports below to stdout and then to null
SHELL "i2cset -y 0 0x63 0x0 0x0C b > /dev/null 2>&1"
SHELL "i2cset -y 0 0x63 0x0 0x01 b > /dev/null 2>&1"
SHELL "i2cset -y 0 0x63 0x0 0x"+hex_key$+" b > /dev/null 2>&1"

'espeak always works but gives an error message - dump the message
IF hex_key$ <> "7E" THEN SHELL "espeak -s 140 "+ key$ +" > /dev/null 2>&1"

PAUSE 300
GOTO Again

The ramdisk is used to transfer data from bash scripts to Blassic basic
I do mount -o size=2M -t tmpfs tmpfs /var/www/ramdisk in a startup script to create the ramdisk in memory

in -
i2cset -y 0 0x63 0x0 0x0C b > /dev/null 2>&1
-y stops the "are you sure" request
0 is the i2c dev number
0x63 is the i2c address
0x0 is the register address (anything sent to register 0 goes to the LCD)
0x0C is (in this case) the character to clear the screen (decimal 12) see this table
b means use bytes
2>&1 means 'send error messages to stdout' , > /dev/null dumps them
stah makes espeak say "star" with an english accent!
gud bye is "Goodbye"

A simple screen driver
Print a string of any length to the i2c LCD panel - top left corner is line-1/column-1, choose to clear the screen or not (1/0)
If the string is longer than a line it wraps to the next line

Example print_word.bas Hello 2 6 1 - print Hello at line 1, column 6, clear the screen first

#!/usr/sbin/blassic
'/home/graham/i2c/print_word.bas <string> <line> <column> <1=clear/0-noclear>
' send a string to the i2c LCD (no spaces in the string)

word$ = PROGRAMARG$(1)
word_length = LEN(word$)
line$ = PROGRAMARG$(2) 'line to print to (1 to 4)
starting_column = VAL(PROGRAMARG$(3))-1 'starting column (1 to 20)

IF PROGRAMARG$(4) = "1" THEN SHELL "i2cset -y 0 0x63 0x0 0x0C" 'clear screen

for character_number = 1 to word_length
character$ = MID$ (word$, character_number, 1)
column$ = HEX$(starting_column + character_number)
' now go to the starting cell
SHELL "i2cset -y 0 0x63 0x0 0x03": SHELL "i2cset -y 0 0x63 0x0 0x0"+line$: SHELL "i2cset -y 0 0x63 0x0 0x0"+column$
ch$ = "0x" + HEX$(ASC(character$))
SHELL "i2cset -y 0 0x63 0x0 "+ch$+ " b"
next character_number

SYSTEM

Sunspot Home
Slug index