TACHYON V4 IFDEF i2c_charLCD.fth @rest org FORGET i2c_charLCD.fth } pub i2c_charLCD ." i2c_charLCD 2017/06/16 14:21:10 " ; @org W@ == @rest --- remember { 2017/06/16 14:16:28 Translated sw to Tachyon i2c-charcterLCD made by using GPIO_Expander(PCF8574) Used 4bit-mode on characterLCD Character-LCD PCF8574 module Propeller Vcc ---- 5V 3.3V GND ------------------------ GND SDA --------Level-shifter--- P29 SCL --------Level-shifter--- P28 DB4 -------------- P0 DB5 -------------- P1 DB6 -------------- P2 DB7 -------------- P3 RS -------------- P4 R/W -------------- GND E -------------- P5 E ----5V B ------ P6 LED+ --- C PNP Tr LED+ ------GND E ----5V B ------ P6 Buzzer --- C (with osc) PNP Tr | GND A0 ----- GND A1 ----- GND A2 ----- GND } \ =========================================================================== \ Constants \ =========================================================================== \ Slave addres h20 for PCF8574 A2=A1=A0=0 \ slaveAddr+wr = $40 \ CharacterLCD $10 == RS $20 == E { Set position to (x,y) x -- horizontal pos : 1 to 16 y -- line number : 1 to 4 DDRAM addres of 16X4 CHaracter-LCD $80=write command to DDRAM 1Line [h00 ------ h0F] 0+$7F 2Line [h40 ------ h4F] $40+$7F 3Line [h10 ------ h1F] $10+$7F 4Line [h50 ------ h5F] $50+$7F } TABLE ycur 0 | $7F | $BF | $8F | $CF | \ =========================================================================== \ Variables \ =========================================================================== long tmp2 long dir1 long dir2 word char \ character number for using LCD word line \ line number for using LCD word cur_line \ current line number word sleep_on \ sleep flag word sound_on \ sound flag word result word tmp1 \ =========================================================================== \ Command for PCF8574 \ =========================================================================== \ Backlight and Buzzer \ ( -- n1 ) n1:data for Backlight and Buzzer : BL&BZ sleep_on W@ IF $40 ELSE 0 ENDIF sound_on W@ IF 0 ELSE $80 ENDIF OR ; \ Output buzzer and backlight \ ( n1 -- ) n1:data for Backlight and Buzzer : out BL&BZ $1F OR ; \ Wite data/command to characterLCD \ [E(bit5) + RS(bit4) + lcd4bit(bit0-3)] to ports of PF8574 \ ( n1 -- ) n1:RS+data : GPIO DUP E OR ; \ : GPIO DUP DUP ; { : i2cScan 6 SPACES ." 0 1 2 3 4 5 6 7 8 9 A B C D E F" CR 0 0 8 #16 DOFOR I .BYTE 3 SPACES 0 #16 1 DOFOR ?DUP IF 1 \ ( start length cnt ) DOFOR I C@ lcd_char LOOP \ Print string ELSE 2DROP ENDIF ; \ Rdirect string to charLCD \ How to use: LCD PRINT" Hellow" CON : LCD ' lcd_char uemit W! ; \ Print cr \ ( -- ) : lcd_cr cur_line W@ 1+ DUP line W@ > \ Check if current_line+1 is bigger than using LCD-line IF DROP 1 1 lcd_pos 1 cur_line W! \ Move to lcd's up-left ELSE DUP 1 SWAP lcd_pos cur_line W! \ Move to lower line ENDIF ; \ Display decimal-number from Left \ d987654321 lcd_dec --> [987654321] \ d4321 lcd_dec -------> [4321] \ (n -- ) n:number : lcd_dec 0 tmp1 W! #1000000000 tmp2 ! DUP $80000000 AND IF NEGATE $2D lcd_char ENDIF \ Check if minus and print "-" #10 FOR DUP tmp2 @ => \ Check if n is bigger than tmp IF tmp2 @ U/MOD $30 + lcd_char 1 tmp1 W! \ Print number-char ELSE tmp1 W@ tmp2 @ 1 = OR IF $30 lcd_char ENDIF \ Print "0" ENDIF tmp2 @ #10 U/ tmp2 ! \ Divide tmp by d10 NEXT DROP ; \ Print spaces \ ( n -- ) n:space's number printing on LCD : lcd_bl ?DUP IF FOR $20 lcd_char NEXT ELSE DROP ENDIF ; \ LCD Off (not erase display-data) \ ( -- ) : sleep 1 sleep_on W! 8 lcd_com ; \ Wake up LCD \ ( -- ) : wakeup 0 sleep_on W! $C lcd_com ; \ Buzzer onoff \ ( n1 -- ) n1:1=on 0=off : buzzer sound_on W! BL&BZ out ; \ =========================================================================== \ demo \ =========================================================================== \ Set bar_graph_font(horizontal) to CG-RAM \ ( -- ) : set_bar_graph1 $20 $40 6 FOR \ set 6 charcters 8 FOR \ set 8 lines for 1 character DUP lcd_com \ command for character to CG-RAM SWAP DUP lcd_char SWAP \ write data 1+ NEXT $F8 AND SWAP DUP 1 SHR OR SWAP NEXT 2DROP ; \ Set bar_graph_font(vertical) to CG-RAM \ ( -- ) : set_bar_graph2 $40 0 8 1 DOFOR \ Set 8 charcters 0 8 1 DOFOR \ Set 8 lines for 1 character DUP lcd_com \ command for character to CG-RAM \ J:Outer loop index(CG RAM addr) \ I:inner(current) loop index (each line for character) 7 J - I < IF $1F ELSE 0 ENDIF lcd_char \ Write data 1+ LOOP $F8 AND LOOP DROP ; \ Print all characters and Bar Graph on 2x14 charLCD : demo lcd_init \ d16 4 lcd_setup \ 16characters 4line 1 cur_line W! \ ----- display character-code [0x20 - 0x7f] & [0xa0 - 0xff] ----- $20 \ 1st character BEGIN 1 cur_line W@ lcd_pos \ Set position #16 FOR DUP lcd_char 1+ #10 ms NEXT \ Print characters on LCD-Line 1 cur_line W+! \ \increment line-number cur_line W@ line W@ > IF 1 cur_line W! ENDIF \ Check line number DUP $80 = IF DROP $A0 ENDIF DUP $100 = \ Finish? #100 ms \ Dealy UNTIL DROP #500 ms \ ----- Bar Graph ----- lcd_clr LCD PRINT" Bar Graph" CON set_bar_graph1 1 #16 FOR 0 6 1 DOFOR I lcd_char \ Print bar_graph character DUP 2 lcd_pos \ Set same x-position LOOP 1+ DUP 2 lcd_pos \ Increment x-position #100 ms \ Delay NEXT DROP #16 2 lcd_pos #16 #16 FOR 5 6 -1 DOFOR I lcd_char DUP 2 lcd_pos LOOP 1- DUP 2 lcd_pos \ Decrement x-position #100 ms \ Delay NEXT DROP #100 ms \ WAVE lcd_clr LCD PRINT" WAVE" CON set_bar_graph2 1 dir1 ! 0 \ First character addrss LAP #256 FOR DUP \ Print character toward horizontal on 2nd line 1 2 lcd_pos dir1 @ dir2 ! #16 FOR DUP lcd_char \ Write char DUP 7 = dir2 @ 1 = AND IF DROP $FF ELSE DUP $FF = IF DROP 7 -1 dir2 ! ELSE DUP 0 = IF 1+ 1 dir2 ! ELSE dir2 @ + ENDIF ENDIF ENDIF NEXT DROP \ Set next character on start-position(1,2) DUP 7 = dir1 @ 1 = AND IF DROP $FF ELSE DUP $FF = IF DROP 7 -1 dir1 ! ELSE DUP 0= IF 1+ 1 dir1 ! ELSE dir1 @ + ENDIF ENDIF ENDIF NEXT LAP .LAP DROP ; END \ ?BACKUP