#define E OUT(2) ' Operation enable signal. Falling edge triggered.
#define RW OUT(7) ' Read/Write select signal, R/W=1: Read R/W: =0: Write
#define RS OUT(6) ' Register select signal. RS=0: Command, RS=1: Data
#define DB7 OUT(17)
#define DB6 OUT(16)
#define DB5 OUT(15)
#define DB4 OUT(14)
#define DB3 OUT(13)
#define DB2 OUT(12)
#define DB1 OUT(9)
#define DB0 OUT(8)
#define NIBBLE_MODE
' 4-bit Initialization:
' send a command to the LCD controller
SUB LCD_CMD(D as integer)
RS = 0 ' RS=LOW : Command
RW = 0 ' RW=LOW : Write
#ifdef NIBBLE_MODE
GPIO_SET0 = (D AND &HF0) << 10
GPIO_CLR0 = ((D AND &HF0) XOR &HF0) << 10
E = 1 ' enable pulse width >= 300ns
E = 0 ' Clock enable: falling edge
GPIO_SET0 = (D AND &H0F) << 14
GPIO_CLR0 = ((D AND &H0F) XOR &H0F) << 14
#else
' Send data bits
GPIO_SET0 = (D AND &HFC) << 10
GPIO_CLR0 = ((D AND &HFC) XOR &HFC) << 10
GPIO_SET0 = (D AND &H03) << 8
GPIO_CLR0 = ((D AND &H03) XOR &H03) << 8
#endif
E = 1 ' enable pulse width >= 300ns
E = 0 ' Clock enable: falling edge
WAITMICRO(40) 'wait > 37 us
END SUB
' write a character to where ever the current position is
' NOTE: remember to delay 40 us between characters
SUB LCD_DATA(D as integer)
RS = 1 ' RS=HIGH : Data
RW = 0 ' RW=LOW : Write
' Send data bits
#ifdef NIBBLE_MODE
GPIO_SET0 = (D AND &HF0) << 10
GPIO_CLR0 = ((D AND &HF0) XOR &HF0) << 10
E = 1 ' enable pulse width >= 300ns
E = 0 ' Clock enable: falling edge
GPIO_SET0 = (D AND &H0F) << 14
GPIO_CLR0 = ((D AND &H0F) XOR &H0F) << 14
#else
' Send data bits
GPIO_SET0 = (D AND &HFC) << 10
GPIO_CLR0 = ((D AND &HFC) XOR &HFC) << 10
GPIO_SET0 = (D AND &H03) << 8
GPIO_CLR0 = ((D AND &H03) XOR &H03) << 8
#endif
E = 1 ' enable pulse width >= 300ns
E = 0 ' Clock enable: falling edge
WAITMICRO(40) 'wait > 37 us
END SUB
' write line 0 or line 1 of the LCD display
SUB WRITE_LINE(line_num as integer, line(20) as string)
dim i, j as integer
#ifdef LINES_2
if line_num = 0 then i = 0 else i=40
#else
select line_num
case 0
i = 0
case 1
i = 40
case 2
i = 20
case else
i = 84
endselect
#endif
' set the position
LCD_CMD(&H80 + i)
i = 0
WHILE (i < 20) and (line(i) > 0)
LCD_DATA (line(i))
i += 1
LOOP
END SUB
FUNCTION READ_LCD (readData)
DIM x
INPUT(17) ' D7
INPUT(16) ' D6
INPUT(15) ' D5
INPUT(14) ' D4
RS = readData ' RS=HIGH : Data
RW = 1 ' RW=LOW : read
#ifdef NIBBLE_MODE
E = 1 ' enable pulse width >= 300ns
x = ((GPIO_PIN0 >> 10 ) AND &HF0)
E = 0 ' Clock enable: falling edge
E = 1 ' enable pulse width >= 300ns
x = x + ((GPIO_PIN0 >> 14 ) AND &H0F)
E = 0 ' Clock enable: falling edge
#else
#endif
OUTPUT(17) ' D7
OUTPUT(16) ' D6
OUTPUT(15) ' D5
OUTPUT(14) ' D4
return x
ENDFUNCTION
SUB INIT_LCD
IO(3)=1 'turn on backlight
OUTPUT(2) ' E
OUTPUT(7) ' RW
OUTPUT(6) ' RS
OUTPUT(17) ' D7
OUTPUT(16) ' D6
OUTPUT(15) ' D5
OUTPUT(14) ' D4
#ifndef NIBBLE_MODE
OUTPUT(13) ' D3
OUTPUT(12) ' D2
OUTPUT(9) ' D1
OUTPUT(8) ' D0
#endif
E = 0
RS = 0
RW = 0
wait(50) ' Wait > 40 msec after power is applied
#ifdef NIBBLE_MODE
LCD_CMD(&H2C)
wait(5) ' must wait 5ms, busy flag not available
LCD_CMD(&H2C)
wait(5) ' must wait 5ms, busy flag not available
LCD_CMD(&H2C) ' Function set: 4-bit/2-line
LCD_CMD(&H2C) ' Function set: 4-bit/2-line
#else
LCD_CMD(&H3C)
wait(5) ' must wait 5ms, busy flag not available
LCD_CMD(&H3C)
wait(5) ' must wait 5ms, busy flag not available
LCD_CMD(&H3C) ' Function set: 8-bit/2-line
LCD_CMD(&H3C) ' Function set: 8-bit/2-line
#endif
'LCD_CMD(&H0F) '0 0 0 0 1 D C B Display ON/OFF control, display on, cursor on and blinking
LCD_CMD(&H0C) '0 0 0 0 1 D C B Display ON/OFF control, display on, no cursor
LCD_CMD(&H01)'0 0 0 0 0 0 0 1 Display Clear
LCD_CMD(&H06)'0 0 0 0 0 1 D S Entry Mode Set
LCD_CMD(&H02)'0 0 0 0 0 0 1 0 Return Home
wait(5) ' must wait 2ms
ENDSUB
/**********************************************************/
#define ESC 27
#define CtlC 3
MAIN:
dim c, i, line_num as integer
dim S(16) as string
print "Coridium Wireless LCD Example"
print " Enter lines to display"
INIT_LCD
WRITE_LINE(0,"CORIDIUM IS COOL")
WRITE_LINE(1,"0123456789ABCDEF")
line_num = 0
i = 0
S = ""
WHILE(1)
i = 0
S = ""
while(1)
c = RXD(0) ' read characters
if c = "!" then exit
if c = 10 OR c = 13 then exit ' carriage return
' accumulate the string, lines of length greater than 20 will be split
if c >= &H20
S(i) = c
i += 1
endif
if i > 18 then exit
LOOP
if c = "!" then exit
while i < 20
S(i) = " " ' pad out with spaces
i += 1
loop
S(19) = 0
print S
WRITE_LINE(line_num, S)
line_num += 1
#ifdef LINES_2
line_num AND= 1
#else
line_num AND= 3
#endif
LOOP
LCD_CMD(1) ' clear display and set pointer to 0
wait(2)
for i=0 to 79
LCD_DATA("0"+i)
next
print "LCD address", hex (READ_LCD(0))