( This is used to connect to the LCD through port P and S: ( I/O Ports Assign Function ( ~~~ ~~~~~ ~~~~~~~~~~~~~~~ ( O PP4 LCD, DB4 ( O PP5 LCD, DB5 ( O PP6 LCD, DB6 ( O PP7 LCD, DB7 ( O PS2 LCD, RS (register select. LOW: command, HIGH: data) ( O PS3 LCD, R/W (READ: signal high, WRITE: signal low) ( O PS4 LCD, E (chip enable, active high) HEX ( ==== Ports P and S interface ==== ) D6 CONSTANT PORTS D7 CONSTANT DDRS 56 CONSTANT PORTP 57 CONSTANT DDRP : INIT-PORTS ( -- ) 70 PORTS C! ( E, /WR and /RD set to high ) FC DDRS C! ( outputs for /CSO /RD /WR E R/W RS ) F0 DDRP C! ( outputs for DB7-4 ) F0 DDRT C! ( outputs for A3-0 ) ; ( ==== Tools ==== ) : ( d -- d' ) 2/ 2/ 2/ 2/ ; ( ==== LCD interface ==== ) 4 CONSTANT lcddata ( data register of LCD module ) 0 CONSTANT lcdcmd ( command register of LCD module ) : LCD@ ( a -- d ) E8 OR DUP PORTS C! ( rtc off; E off; R/W read; RS = a ) 00 DDRP C! ( data in ) DUP 10 OR PORTS C! ( enable chip ) PORTP C@ F0 AND ( read data bus ) SWAP PORTS C! ; ( disable chip ) : LCD! ( d \ a -- ) E0 OR DUP PORTS C! ( rtc off; E off; R/W write; RS = a ) F0 DDRP C! ( data out ) SWAP F0 AND PORTP C! ( output data ) DUP 10 OR PORTS C! ( enable chip ) PORTS C! ; ( disable chip ) : CMMD> ( -- d ) lcdcmd LCD@ lcdcmd LCD@ NIB> OR ; : DATA> ( -- d ) lcddata LCD@ lcddata LCD@ NIB> OR ; : WNB ( -- ) BEGIN CMMD> 80 AND 0= ?TERMINAL OR UNTIL ; : >CMMD ( d -- ) DUP lcdcmd LCD! WNB LCD ( d -- ) DUP lcddata LCD! WNB CMMD ( reset sequence for 32 >CMMD ( four bit interface 28 >CMMD ( 4 bit data length; 2 lines; 5x7 dots 6 >CMMD ( entry mode set: increment; no shift F >CMMD ( display on; cursor on; blink on 1 >CMMD ( display clear 80 >CMMD ; ( cursor in top left position : LCD-TYPE ( a \ n -- ) 0 DO COUNT >LCD LOOP DROP ; : .LCD ( n -- ) DUP ABS 0 <# #S SIGN #> LCD-TYPE ; : L1 ( -- ) 80 >CMMD ; ( line 1 ) : L2 ( -- ) C0 >CMMD ; ( line 1 )