\ lcd.fs
\ Nibble mode driver for 300 chip  17Apr06 rjn/cws

0 [if]

LCD 	PORT	300    	FUNCTION
PIN		PIN
---	-------	-----	-------------------------------------
 1	----   	11	GND
 2	----   		+5 Volts (used 78L05 on Gadget MB)
 3	----		Contrast Voltage 0-5 Volts (10K pot)
 4	P0.7	10	RS - Instruction Register Select
 5	GND	11	R/W - H=READ L=WRITE Registers (GND)
 6	P0.6	9	E - Enable  P0.6
 7	GND	11	byte DB0
 8	GND	11	byte DB1		
 9	GND	11	byte DB2
10	GND	11	byte DB3
11	P0.0	1	byte DB4 - nibble DB0 
12	P0.1	2	byte DB5 - nibble DB1
13	P0.2	4	byte DB6 - nibble DB2
14	P0.3	5	byte DB7 - nibble DB3

[then]

\ ---------- I/O ----------
$e1 constant XBR2
\ $a4 constant PRT0CF

:m .E  6 .P0 m;
4 constant enable-mask
:m .RS  7 .P0 m;
:m pins  P0 m;
\ :m dirs	  PRT0CF   m;  	\ 017
:m dirs  P0MDOUT m;  \ Need number for 300.

] \ Target Forth

: us  ( n - )   for 3 # for next next ;
: ms  ( n - )   for 100 # for 80 # for next next next ;

: instruction  [ .RS clr ] ;
: data  [ .RS set ] ;
: pulseout-E-10  [ .E toggle ] 10 # us [ .E toggle ] ;

: lcd  ( c - )
    $0b # pins #and!
    dup $f0 # and pins #ior!
    pulseout-E-10
    2* 2* 2* 2*
    $f0 # pins #and! pins #ior!
    pulseout-E-10 ;

: clear-lcd  instruction 1 # lcd data 100 # ms ;

: no-pullups  $80 # XBR2 #ior! ;

: init-lcd 
    $03 # pins #!
    $fc # dirs #ior!
    200 # ms
    .s cr
    $30 # pins #!
    .s cr
    pulseout-E-10 100 # ms
    .s cr
    pulseout-E-10 100 # ms
    pulseout-E-10 100 # ms
    $20 # pins #!
    pulseout-E-10 100 # ms
    $28 # lcd 100 # ms
    $0e # lcd 10 # ms
    $01 # lcd 100 # ms
    $02 # lcd 10 # ms
    data ;

: init  no-pullups init-lcd ;

: high  [ .RS set ] ;
: low   [ .RS clr ] ;

: us-loop  begin  10 # us high 50 # us low  again ;
: ms-loop  begin  10 # ms high 50 # ms low  again ;


\ ************************************************************

\ a: .E		6 .P0 	 ;a
\ a: .RS  	7 .P0 	 ;a
\ a: pins		P0 	 ;a
\ a: dirs	  PRT0CF   ;a  	\ 017
\ a: dirs 	P0MDOUT  ;a  	\ 300
a: data 	.RS setb ;a
a: instruction 	.RS clr  ;a

a: 1us  ( - )   6 # R7 mov  begin R7 -zero until ;a
code us  ( c - )  begin  1us  ACC -zero until  ' drop jump c;
: ms  ( n - )   for 250 us  250 us  250 us  250 us next ;

: strobe 
	[asm .E setb ]  100 us  [asm .E clr ]
	160 us  160 us  \ may be ok at half this value
; 

code write ( c - )
	A R2 mov		\ temp save A in R2
	A swap	$0F # A anl	\ work on high nibble first
	%10110000 # pins anl	\ clear data pins
	A pins orl		\ set data pins from high nibble
	' strobe call
	R2 A mov		\ restore original byte
	$0F # A anl		\ work on low nibble
	%10110000 # pins anl	\ clear data pins
	A pins orl		\ set data pins from low nibble
	' strobe call
	' drop jump c;

: init 
[asm	%00110000 # pins mov   	\ RS=0, instruction mode
	%11001111 # dirs orl ]	\ config pins as outputs
	30 ms			\ power on delay
[asm	%00000011 # pins mov ]	\ initialization pattern
	strobe	10 ms
	strobe
	strobe
[asm 	%00000010 # pins mov ]
	strobe  10 ms
	$28 write  10 ms	\ nibble mode, 2 lines, 5X7 font
	$0E write  		\ enable display and cursor
	$01 write  10 ms	\ clear display
	$02 write  		\ home cursor
[asm	data ]  10 ms
;

: clear [asm instruction ]
	1 write
	[asm data ] ;

: print  ( a len - ) for count write next drop ;

: hello  s" Hello Universe " ;
 
