sohotcall

LCD1602 Micropython ESP8266

Sep 21st, 2023
1,057
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 2.86 KB | None | 0 0
  1. import machine
  2. import time
  3. import os
  4.  
  5. rs = machine.Pin( 5, machine.Pin.OUT, value = 0 ) #D1(5)
  6. en = machine.Pin( 4, machine.Pin.OUT, value = 0 ) #D2(4)
  7. d4 = machine.Pin( 0, machine.Pin.OUT, value = 0 ) #D3(0)
  8. d5 = machine.Pin( 2, machine.Pin.OUT, value = 0 ) #D4(2)
  9. d6 = machine.Pin( 14, machine.Pin.OUT, value = 0 ) #D5(14)
  10. d7 = machine.Pin( 12, machine.Pin.OUT, value = 0 ) #D6(12)
  11.  
  12. def nibbles( n, lsb = 0 ):
  13.     d7.value( n >> 7 & 1 )
  14.     d6.value( n >> 6 & 1 )
  15.     d5.value( n >> 5 & 1 )
  16.     d4.value( n >> 4 & 1 )
  17.     time.sleep_ms( 5 )
  18.     en.value( 1 )
  19.     time.sleep_ms( 2 )
  20.     en.value( 0 )
  21.     if ( lsb == 0 ):
  22.         nibbles( n << 4, 1 )
  23.  
  24. time.sleep( 1 )
  25. nibbles( 0x33 )
  26. nibbles( 0x32 )
  27. nibbles( 0x28 )
  28. nibbles( 0x06 )
  29. nibbles( 0x0C )
  30. nibbles( 0x01 )
  31. nibbles( 0x02 )
  32. rs.value( 1 )
  33. nibbles( 0x4F ) #O
  34. nibbles( 0x4B ) #K
  35. rs.value( 0 )
  36.  
  37. ts = 0
  38. chars = 32 * [ 0x20 ] #' '
  39. pos = 0
  40. def processChar( chr ):
  41.     global ts, pos, chars
  42.     ts = max( 1, time.ticks_ms() )
  43.     if chr == 0x0C: #^L
  44.         chars = 32 * [ 0x20 ]
  45.         pos = 0
  46.     elif chr == 0x0A: #^J=\n
  47.         pos = 16
  48.     elif 32 <= chr <= 126: #printables
  49.         if pos == 31:
  50.             chars[16:31] = chars[17:32]
  51.             chars[31] = chr
  52.         else:
  53.             chars[pos] = chr
  54.             pos = pos + 1
  55.  
  56. def render():
  57.     ts = 0
  58.     nibbles( 0x80 ) # line0
  59.     rs.value( 1 )
  60.     for b in chars[0:16]:
  61.         nibbles( b )
  62.     rs.value( 0 )
  63.     nibbles( 0xC0 ) # line1
  64.     rs.value( 1 )
  65.     for b in chars[16:32]:
  66.         nibbles( b )
  67.     rs.value( 0 )
  68.  
  69. def loop():
  70.     arr = uart.read()
  71.     if arr:
  72.         for i in range( len( arr ) ):
  73.             processChar( arr[ i ] )
  74.     if ts and time.ticks_diff( time.ticks_ms(), ts ) > 200:
  75.         render()
  76.  
  77. test = machine.Pin( 13, machine.Pin.IN, machine.Pin.PULL_UP )
  78. if ( test.value() == 1 ):
  79.     os.dupterm( None, 1 )
  80.     uart = machine.UART( 0, 115200 )
  81.     while 1:
  82.         loop()
  83.  
  84. # Save the code above as main.py
  85. # Connect ESP8266 to PC using USB cable while pressing Flash button on ESP8266
  86. # Download ESP8266 micropython firmware (for example esp8266-20170108-v1.8.7.bin)
  87. # $ pip install esptool
  88. # $ esptool.py --port /dev/ttyUSB0 erase_flash</code>
  89. # $ esptool.py --port /dev/ttyUSB0 --baud 115200 write_flash --flash_size=detect 0 esp8266-20170108-v1.8.7.bin</code>
  90. # $ sudo apt install picocom
  91. # $ picocom /dev/ttyUSB0 -b115200
  92. # Try some python code like "1 + 1" Enter
  93. # Replug the USB cable
  94. # $ pip install adafruit-ampy
  95. # $ ampy --port /dev/ttyUSB0 put main.py main.py
  96. # $ ampy --port /dev/ttyUSB0 ls
  97. # Unplug the ESP8266 from PC
  98. # Connect lcd1602 and ESP8266:
  99. # VSS<->Gnd, Vcc<->Vin(5V), V0<->Gnd, RS<->D1(GPIO5), E<->D2(GPIO4), D0<->NC, D1<->NC, D2<->NC, D3<->NC, D4<->D3(GPIO0), D5<->D4, D6<->D5(GPIO14), D7<->D6(GPIO12), A<->3V3, K<->Gnd (Note: NC means Not Connected)
  100. # Connect esp8266 with lcd1602 to PC
  101. # $ picocom /dev/ttyUSB0 -b115200
  102. # Type some text (Use Ctrl+L (formfeed) to clear screen and put cursor to first line, Ctrl+J (newline) to put cursor to second line)
  103.  
Advertisement
Add Comment
Please, Sign In to add comment