TACHYON V4 IFDEF 2wire_charLCD.fth @rest org FORGET 2wire_charLCD.fth } pub 2wire_charLCD.fth ." 2-wire Character LCD 2017/06/10 11:53:58 " ; @org W@ == @rest --- remember \ =========================================================================== \ Constants \ =========================================================================== \ User-defined #P0 == data #P1 == clk \ CharacterLCD $10 == RS $20 == LED- $40 == E \ =========================================================================== \ Variables \ =========================================================================== word char \ character number for using LCD word line \ line number for using LCD word cur_line \ current line number word sleep_on \ sleep_on flag \ =========================================================================== \ Command for 74HC164 \ =========================================================================== : data_l data LOW ; : data_h data HIGH ; : clk_l clk LOW ; : clk_h clk HIGH ; \ Send hi-pulse to Enable-pin \ ( -- ) : lcd_enable data_h 1 us data_l 1 us ; \ Send data to shift-register \ ( n -- ) n:E(bit6) + LED_on(bit5) + RS(bit4) + lcd4bit(bit3-0) : shift_out #24 REV \ flip 8bits so msb will be first to shift right out data MASK SWAP \ create an iomask+data combo for SHRPUT 8 FOR SHROUT clk HIGH clk LOW NEXT 2DROP ; \ reset shift-register \ ( -- ) : reset_sr clk HIGH 1 us clk LOW 1 us ; \ =========================================================================== \ Command for LCD \ =========================================================================== \ Send command to HD44780 \ (n -- ) n:bit4=RS-bit bit3-0=LCD-data : lcd_com reset_sr DUP $100 AND IF RS ELSE 0 ENDIF \ RS bit SWAP 2DUP \ ( 10/0 n 10/0 n ) \ upper 4bit 4 SHR $F AND OR E OR \ Add RS-bit and Enable-bit shift_out lcd_enable reset_sr \ lower 4bit \ ( 10/0 n ) $F AND OR E OR \ Add RS-bit and Enable-bit shift_out lcd_enable reset_sr \ Set RS and DB4-DB7 to Hi, and Set LED_on to hi if sleep is inactive $1F sleep_on W@ IF LED- OR ENDIF \ Add LED_on shift_out ; \ Setup propeller pins and initialize HD44780 \ ( -- ) : lcd_init 0 sleep_on W! \ Clear shift-register output reset_sr \ 8bit mode #100 ms \ wait 100msec $43 shift_out lcd_enable \ FunctionSet 5 ms lcd_enable \ FunctionSet 1 ms lcd_enable \ FunctionSet 5 ms reset_sr \ Clear shift-register \ 4bit mode 2 lcd_com $28 lcd_com \ FunctionSet DL=0 N=1 F=0 $C lcd_com \ DisplaySwitch D=1 C=B=0 1 lcd_com \ ScreenClear 6 lcd_com \ InputSet I/D=1 S=0 reset_sr \ Clear shift-register #100 ms #16 char W! 2 line W! \ defsult setting is 16Charcters & 2Lines ; \ Display character \ ( c -- ) c:character code 'A'=h41 : lcd_char $100 OR \ Add RS=1 to character-code lcd_com ; \ Set position to (x,y) \ x -- horizontal pos : 0x1 to 0x28 (decimal: 1 to 40Characters) \ y -- line number : 0x1 to 0x4 (decimal: 1 to 4Lines) \ DDRAM addres of 16X4 CHaracter-LCD \ 1Line [h00 ------ h0F] \ 2Line [h40 ------ h4F] \ 3Line [h10 ------ h1F] \ 4Line [h50 ------ h5F] \ ( x y -- ) : lcd_pos 2 U/MOD SWAP 0= IF 1 = IF $40 ELSE $50 ENDIF \ Line is 2 or 4 ELSE 0= IF 0 ELSE $10 ENDIF \ Line is 1 or 3 then 1- + $80 OR lcd_com \ Add DDRAM Addr command ; \ Clear LCD-screen \ ( -- ) : lcd_clr 1 lcd_com 2 ms ; \ Setup of display construction \ (x y -- ) : lcd_setup line W! char W! ; \ Display string \ ( n1 -- ) n1:string sample:[ " ABC" ] : lcd_str DUP LEN$ \ ( address length ) DUP 0= IF ADO I C@ lcd_char loop \ Print string ELSE 2DROP ENDIF ; END