Advertisement
Jan-Langevad

LCDworkfile.txt

Jul 25th, 2024 (edited)
271
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.39 KB | Software | 0 0
  1. \ LCDworkfile.txt ESP32FORTH (Version tested on: 7.0.6.19)
  2. \ Jan Langevad - August 10th. 2024 *** Work in progress *** E.g. Add cursor control words.
  3. \ *** August: Added method to program own 5*8 chars. ***
  4. \ Words to control a "Standard" 16x2 SERIAL (I2C) LCD display - in 4-bit mode!
  5. \ 16*2 LCDs are normally a HD44780 compatible, and the serial controller a
  6. \ PCF8574T (w. deafult addr. 0x27), or PCF8574AT (w. default addr. 0x3f)
  7. \ I2C handling here implemented in approx 5 lines of code, using ESP32FORTH's builtin I2C/Wire :-)
  8. \ OBS: When LCD is using a 5V power supply, the display defaults to 4-BIT MODE! Used here!
  9.  
  10. \ Tips on LCD: https://www.glennklockwood.com/electronics/hd44780-lcd-display.html
  11. \ A lot more to Google
  12.  
  13. \ https://controllerstech.com/custom-characters-in-lcd-1602-stm32/
  14. \ https://maxpromer.github.io/LCD-Character-Creator/
  15.  
  16. FORTH
  17.  
  18. HEX \ <---------- OBS All here is done in HEX mode
  19.  
  20. variable ByteForLCD \ Our ESP32FORTH's I2C words needs adresses, not actual values to send!
  21.  
  22. 0 value Integer
  23. 0 value CMD
  24. 0 value BUF
  25. 0 value DATA
  26. 0 value BYTE
  27. 0 value COMMAND? \ -1=yes / 0=no, data
  28. 0 value MASK \ used as bit mask when sending command- or data-byte
  29.  
  30. 8 value BLen \ Back Light ON is deafault here
  31. : BLon 8 is BLen ; \ On/Off changes when something is actually sent to the dispaly
  32. : BLoff 0 is BLen ;
  33.  
  34. variable WireOK? \ save wire.prep stack result here
  35. 0 WireOK? !
  36.  
  37. WIRE \ <<<<<<<<<<<<<<<<<<<< change vocabulary to use FORTH's I2C words
  38.  
  39. : Wire.Prep 15 16 Wire.begin WireOK? ! ; \ initialize IC2 pins SDA SCL ---
  40.  
  41. : WriteByte ( bytevalue --- )
  42.  
  43. BLen or \ Set the Back-Light-ENable/Disable bit
  44.  
  45. 27 Wire.beginTransmission \ LCD's I2C ADDRESS 27 ( OR often 3F )
  46.  
  47. \ OBS: Wire.write in ESP32Forth requires adresses, and not values, on stack
  48. ByteForLCD ! \ CMD --- store byte in an Variable, that will return an address when "called"
  49. ByteForLCD 1 Wire.write drop \ adr n ---
  50.  
  51. 1 Wire.endTransmission drop \ 1 = stop option, drop return 0=ok / 1..4 see FORTH doc!
  52. ;
  53.  
  54. FORTH \ <<<<<<<<<<<<<<<<<<<< change back vocabulary
  55.  
  56. : SendBYTE ( DataOrCommandByte --- )
  57.  
  58. to BYTE \ save Byte to send
  59.  
  60. COMMAND? \ TRUE =
  61. if 04
  62. ELSE 05
  63. THEN is MASK
  64.  
  65. \ Send bit 7..4 first:
  66. BYTE F0 and to BUF
  67. BUF MASK or to BUF
  68.  
  69. BUF WriteByte
  70.  
  71. BUF FB and to BUF
  72. BUF WriteByte
  73.  
  74. \ Send bit 3..0
  75. BYTE 0F and to BUF
  76. BUF 10 * to BUF \ shift 4 bits to left = 10 * (hex)
  77. BUF MASK or to BUF
  78. BUF WriteByte
  79.  
  80. BUF fb and to BUF
  81. BUF WriteByte
  82. ;
  83.  
  84. : SendCommand ( CMDbyte --- ) True is COMMAND? SendBYTE ;
  85.  
  86. : SendData ( databyte --- ) False is COMMAND? SendBYTE ;
  87.  
  88. : LCDCLEAR 01 SendCommand 2 MS ; \ Clear Screen
  89.  
  90. : LCDOFF 08 SendCommand ; \ Dispaly OFF
  91.  
  92. \ Cursor related commands
  93. : ToLine0 80 SendCommand ;
  94. : ToLine1 AA SendCommand ;
  95.  
  96. : LCDgoTo ( Line Col --- ) \ OBS: ZERO based Line 0/1, and Col 0..F
  97.  
  98. SWAP \ L C --- C L
  99. if C0 \ 80 + 40 *** OBS: Second line starts at Position 64 = 0x40 ! ***
  100. else 80
  101. then
  102. + \ add Column offset to the line start position
  103. SendCommand
  104. ;
  105.  
  106. : LCDstring. ( addr count --- ) \ use S" stringtext..."
  107.  
  108. \ usage e.g.: ToLine1 s" Hello FORTH fans" LCDstring.
  109.  
  110. 10 MIN \ limit to max. 16 chars!
  111. swap is Integer \ save start address
  112.  
  113. 0 do Integer I + c@
  114. SendDATA
  115. loop
  116. ;
  117.  
  118. \ Add CURSOR Control words here! *)
  119. \
  120.  
  121. : LCDcursorON 0F SendCommand ; \ Enable display with blinking cursor. Good for development work!
  122. : LCDcursorOFF 0C SendCommand ; \ Enable display without cursor
  123.  
  124. : LCDinit \ Cryptic, but needed settings. Find/See online doumentation.
  125.  
  126. 33 SendCommand \ Must initialize to 8-line mode at first!.
  127. 32 SendCommand \ Then initialize to 4-line, and finally:
  128. 28 SendCommand \ initialize to our 4 bits, 2 lines, 5*8 dots
  129.  
  130. LCDcursorON
  131. LCDclear
  132. ;
  133.  
  134. : StarUptLCD
  135.  
  136. Wire.Prep \ 15 16 hex = 21 22 decimal GPIO pin #
  137.  
  138. CR ." Wire.Prep - "
  139. WireOK?
  140. if ." OK"
  141. else ." FAILED!"
  142. then CR
  143.  
  144. LCDinit
  145. BLon \ Back Light ON!
  146. ;
  147.  
  148. StarUptLCD \ <<<<<<<<<<<<<<<<<<<<<<<<< Still in HEX mode.
  149.  
  150. \ EOF
  151.  
  152. \ ********************************************************
  153. \ Workarea:
  154.  
  155. ToLine0 S" Hi FORTH fans " LCDstring. ( addr count --- ) \ use S" stringtext..."
  156. ToLine1 S" 0123456789abcdef" LCDstring.
  157.  
  158.  
  159. : Hi LCDCLEAR
  160. WireOK?
  161. ToLine0
  162. if S" OK"
  163. else S" FAILED!"
  164. then LCDstring.
  165. ;
  166.  
  167. : LCDstoreSMILEY
  168.  
  169. 40 SendCommand \ 40 = Char 00 We have 8 CGRAM locations for own chars. Add 8 to next address....
  170.  
  171. 00 SendData \ .....
  172. 1B SendData \ ¤¤ ¤¤
  173. 04 SendData \ ..¤..
  174. 04 SendData \ ..¤..
  175. 04 SendData \ ..¤..
  176. 11 SendData \ ¤...¤
  177. 11 SendData \ ¤...¤
  178. 0E SendData \ .¤¤¤.
  179. ;
  180. : Smiley. 0 SendData ;
  181.  
  182. : LCDstoreANGRY \ char 01
  183.  
  184. 48 SendCommand
  185.  
  186. 00 SendData \ .....
  187. 1B SendData \ ¤¤ ¤¤
  188. 04 SendData \ ..¤..
  189. 04 SendData \ ..¤..
  190. 00 SendData \ .....
  191. 04 SendData \ ..¤..
  192. 1B SendData \ ¤¤.¤¤
  193. 11 SendData \ ¤...¤
  194. ;
  195.  
  196. : LCDstoreBOX \ char 02
  197.  
  198. 50 SendCommand
  199.  
  200. 1F SendData \ .....
  201. 1F SendData \ .....
  202. 1F SendData \ .....
  203. 1F SendData \ .....
  204. 1F SendData \ .....
  205. 1F SendData \ .....
  206. 1F SendData \ .....
  207. 1F SendData \ .....
  208. ;
  209.  
  210. : LCDstoreFRAME \ char 03
  211.  
  212. 58 SendCommand
  213.  
  214. 1F SendData \ .....
  215. 11 SendData \ . .
  216. 11 SendData \ . .
  217. 11 SendData \ . .
  218. 11 SendData \ . .
  219. 11 SendData \ . .
  220. 11 SendData \ . .
  221. 1F SendData \ .....
  222. ;
  223.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement