Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import machine
- import time
- import os
- rs = machine.Pin( 5, machine.Pin.OUT, value = 0 ) #D1(5)
- en = machine.Pin( 4, machine.Pin.OUT, value = 0 ) #D2(4)
- d4 = machine.Pin( 0, machine.Pin.OUT, value = 0 ) #D3(0)
- d5 = machine.Pin( 2, machine.Pin.OUT, value = 0 ) #D4(2)
- d6 = machine.Pin( 14, machine.Pin.OUT, value = 0 ) #D5(14)
- d7 = machine.Pin( 12, machine.Pin.OUT, value = 0 ) #D6(12)
- def nibbles( n, lsb = 0 ):
- d7.value( n >> 7 & 1 )
- d6.value( n >> 6 & 1 )
- d5.value( n >> 5 & 1 )
- d4.value( n >> 4 & 1 )
- time.sleep_ms( 5 )
- en.value( 1 )
- time.sleep_ms( 2 )
- en.value( 0 )
- if ( lsb == 0 ):
- nibbles( n << 4, 1 )
- time.sleep( 1 )
- nibbles( 0x33 )
- nibbles( 0x32 )
- nibbles( 0x28 )
- nibbles( 0x06 )
- nibbles( 0x0C )
- nibbles( 0x01 )
- nibbles( 0x02 )
- rs.value( 1 )
- nibbles( 0x4F ) #O
- nibbles( 0x4B ) #K
- rs.value( 0 )
- ts = 0
- chars = 32 * [ 0x20 ] #' '
- pos = 0
- def processChar( chr ):
- global ts, pos, chars
- ts = max( 1, time.ticks_ms() )
- if chr == 0x0C: #^L
- chars = 32 * [ 0x20 ]
- pos = 0
- elif chr == 0x0A: #^J=\n
- pos = 16
- elif 32 <= chr <= 126: #printables
- if pos == 31:
- chars[16:31] = chars[17:32]
- chars[31] = chr
- else:
- chars[pos] = chr
- pos = pos + 1
- def render():
- ts = 0
- nibbles( 0x80 ) # line0
- rs.value( 1 )
- for b in chars[0:16]:
- nibbles( b )
- rs.value( 0 )
- nibbles( 0xC0 ) # line1
- rs.value( 1 )
- for b in chars[16:32]:
- nibbles( b )
- rs.value( 0 )
- def loop():
- arr = uart.read()
- if arr:
- for i in range( len( arr ) ):
- processChar( arr[ i ] )
- if ts and time.ticks_diff( time.ticks_ms(), ts ) > 200:
- render()
- test = machine.Pin( 13, machine.Pin.IN, machine.Pin.PULL_UP )
- if ( test.value() == 1 ):
- os.dupterm( None, 1 )
- uart = machine.UART( 0, 115200 )
- while 1:
- loop()
- # Save the code above as main.py
- # Connect ESP8266 to PC using USB cable while pressing Flash button on ESP8266
- # Download ESP8266 micropython firmware (for example esp8266-20170108-v1.8.7.bin)
- # $ pip install esptool
- # $ esptool.py --port /dev/ttyUSB0 erase_flash</code>
- # $ esptool.py --port /dev/ttyUSB0 --baud 115200 write_flash --flash_size=detect 0 esp8266-20170108-v1.8.7.bin</code>
- # $ sudo apt install picocom
- # $ picocom /dev/ttyUSB0 -b115200
- # Try some python code like "1 + 1" Enter
- # Replug the USB cable
- # $ pip install adafruit-ampy
- # $ ampy --port /dev/ttyUSB0 put main.py main.py
- # $ ampy --port /dev/ttyUSB0 ls
- # Unplug the ESP8266 from PC
- # Connect lcd1602 and ESP8266:
- # 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)
- # Connect esp8266 with lcd1602 to PC
- # $ picocom /dev/ttyUSB0 -b115200
- # 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)
Advertisement
Add Comment
Please, Sign In to add comment