Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- \ LCDworkfile.txt ESP32FORTH (Version tested on: 7.0.6.19)
- \ Jan Langevad - August 10th. 2024 *** Work in progress *** E.g. Add cursor control words.
- \ *** August: Added method to program own 5*8 chars. ***
- \ Words to control a "Standard" 16x2 SERIAL (I2C) LCD display - in 4-bit mode!
- \ 16*2 LCDs are normally a HD44780 compatible, and the serial controller a
- \ PCF8574T (w. deafult addr. 0x27), or PCF8574AT (w. default addr. 0x3f)
- \ I2C handling here implemented in approx 5 lines of code, using ESP32FORTH's builtin I2C/Wire :-)
- \ OBS: When LCD is using a 5V power supply, the display defaults to 4-BIT MODE! Used here!
- \ Tips on LCD: https://www.glennklockwood.com/electronics/hd44780-lcd-display.html
- \ A lot more to Google
- \ https://controllerstech.com/custom-characters-in-lcd-1602-stm32/
- \ https://maxpromer.github.io/LCD-Character-Creator/
- FORTH
- HEX \ <---------- OBS All here is done in HEX mode
- variable ByteForLCD \ Our ESP32FORTH's I2C words needs adresses, not actual values to send!
- 0 value Integer
- 0 value CMD
- 0 value BUF
- 0 value DATA
- 0 value BYTE
- 0 value COMMAND? \ -1=yes / 0=no, data
- 0 value MASK \ used as bit mask when sending command- or data-byte
- 8 value BLen \ Back Light ON is deafault here
- : BLon 8 is BLen ; \ On/Off changes when something is actually sent to the dispaly
- : BLoff 0 is BLen ;
- variable WireOK? \ save wire.prep stack result here
- 0 WireOK? !
- WIRE \ <<<<<<<<<<<<<<<<<<<< change vocabulary to use FORTH's I2C words
- : Wire.Prep 15 16 Wire.begin WireOK? ! ; \ initialize IC2 pins SDA SCL ---
- : WriteByte ( bytevalue --- )
- BLen or \ Set the Back-Light-ENable/Disable bit
- 27 Wire.beginTransmission \ LCD's I2C ADDRESS 27 ( OR often 3F )
- \ OBS: Wire.write in ESP32Forth requires adresses, and not values, on stack
- ByteForLCD ! \ CMD --- store byte in an Variable, that will return an address when "called"
- ByteForLCD 1 Wire.write drop \ adr n ---
- 1 Wire.endTransmission drop \ 1 = stop option, drop return 0=ok / 1..4 see FORTH doc!
- ;
- FORTH \ <<<<<<<<<<<<<<<<<<<< change back vocabulary
- : SendBYTE ( DataOrCommandByte --- )
- to BYTE \ save Byte to send
- COMMAND? \ TRUE =
- if 04
- ELSE 05
- THEN is MASK
- \ Send bit 7..4 first:
- BYTE F0 and to BUF
- BUF MASK or to BUF
- BUF WriteByte
- BUF FB and to BUF
- BUF WriteByte
- \ Send bit 3..0
- BYTE 0F and to BUF
- BUF 10 * to BUF \ shift 4 bits to left = 10 * (hex)
- BUF MASK or to BUF
- BUF WriteByte
- BUF fb and to BUF
- BUF WriteByte
- ;
- : SendCommand ( CMDbyte --- ) True is COMMAND? SendBYTE ;
- : SendData ( databyte --- ) False is COMMAND? SendBYTE ;
- : LCDCLEAR 01 SendCommand 2 MS ; \ Clear Screen
- : LCDOFF 08 SendCommand ; \ Dispaly OFF
- \ Cursor related commands
- : ToLine0 80 SendCommand ;
- : ToLine1 AA SendCommand ;
- : LCDgoTo ( Line Col --- ) \ OBS: ZERO based Line 0/1, and Col 0..F
- SWAP \ L C --- C L
- if C0 \ 80 + 40 *** OBS: Second line starts at Position 64 = 0x40 ! ***
- else 80
- then
- + \ add Column offset to the line start position
- SendCommand
- ;
- : LCDstring. ( addr count --- ) \ use S" stringtext..."
- \ usage e.g.: ToLine1 s" Hello FORTH fans" LCDstring.
- 10 MIN \ limit to max. 16 chars!
- swap is Integer \ save start address
- 0 do Integer I + c@
- SendDATA
- loop
- ;
- \ Add CURSOR Control words here! *)
- \
- : LCDcursorON 0F SendCommand ; \ Enable display with blinking cursor. Good for development work!
- : LCDcursorOFF 0C SendCommand ; \ Enable display without cursor
- : LCDinit \ Cryptic, but needed settings. Find/See online doumentation.
- 33 SendCommand \ Must initialize to 8-line mode at first!.
- 32 SendCommand \ Then initialize to 4-line, and finally:
- 28 SendCommand \ initialize to our 4 bits, 2 lines, 5*8 dots
- LCDcursorON
- LCDclear
- ;
- : StarUptLCD
- Wire.Prep \ 15 16 hex = 21 22 decimal GPIO pin #
- CR ." Wire.Prep - "
- WireOK?
- if ." OK"
- else ." FAILED!"
- then CR
- LCDinit
- BLon \ Back Light ON!
- ;
- StarUptLCD \ <<<<<<<<<<<<<<<<<<<<<<<<< Still in HEX mode.
- \ EOF
- \ ********************************************************
- \ Workarea:
- ToLine0 S" Hi FORTH fans " LCDstring. ( addr count --- ) \ use S" stringtext..."
- ToLine1 S" 0123456789abcdef" LCDstring.
- : Hi LCDCLEAR
- WireOK?
- ToLine0
- if S" OK"
- else S" FAILED!"
- then LCDstring.
- ;
- : LCDstoreSMILEY
- 40 SendCommand \ 40 = Char 00 We have 8 CGRAM locations for own chars. Add 8 to next address....
- 00 SendData \ .....
- 1B SendData \ ¤¤ ¤¤
- 04 SendData \ ..¤..
- 04 SendData \ ..¤..
- 04 SendData \ ..¤..
- 11 SendData \ ¤...¤
- 11 SendData \ ¤...¤
- 0E SendData \ .¤¤¤.
- ;
- : Smiley. 0 SendData ;
- : LCDstoreANGRY \ char 01
- 48 SendCommand
- 00 SendData \ .....
- 1B SendData \ ¤¤ ¤¤
- 04 SendData \ ..¤..
- 04 SendData \ ..¤..
- 00 SendData \ .....
- 04 SendData \ ..¤..
- 1B SendData \ ¤¤.¤¤
- 11 SendData \ ¤...¤
- ;
- : LCDstoreBOX \ char 02
- 50 SendCommand
- 1F SendData \ .....
- 1F SendData \ .....
- 1F SendData \ .....
- 1F SendData \ .....
- 1F SendData \ .....
- 1F SendData \ .....
- 1F SendData \ .....
- 1F SendData \ .....
- ;
- : LCDstoreFRAME \ char 03
- 58 SendCommand
- 1F SendData \ .....
- 11 SendData \ . .
- 11 SendData \ . .
- 11 SendData \ . .
- 11 SendData \ . .
- 11 SendData \ . .
- 11 SendData \ . .
- 1F SendData \ .....
- ;
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement