Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #!/usr/bin/python3
- # This is a more complicated example utilizing a 74HC595 shift register
- # to control a 7 segment display. The wiring from the NodeMCU to the
- # shift register is described below.
- from nodemcu import NodeMcu, NodeMcuError, D
- from time import sleep
- from sys import exit
- from common import getIPPort
- from time import sleep
- # Wiring Names from the TI data sheet
- DS = D[0] # SER
- ST_CP = D[1] # RCLK
- SH_CP = D[2] # SRCLK
- E = D[5]
- RW = D[6]
- RS = D[7]
- def init_lcd(mcu):
- sleep(0.01)
- lcdCommand(mcu, 0x38)
- sleep(0.01)
- lcdCommand(mcu, 0x38)
- sleep(0.01)
- lcdCommand(mcu, 0x38)
- sleep(0.01)
- lcdCommand(mcu, 0x06)
- sleep(0.01)
- lcdCommand(mcu, 0x0c)
- sleep(0.01)
- lcdCommand(mcu, 0x01)
- def lcdCommand(mcu, cmd):
- mcu.write(RS, False)
- mcu.write(RW, False)
- mcu.write(E, True)
- lcdDataWrite(mcu, cmd)
- sleep(0.01)
- mcu.write(E, False)
- def lcdData(mcu, data):
- mcu.write(RS, True)
- mcu.write(RW, False)
- mcu.write(E, True)
- lcdDataWrite(mcu, data)
- sleep(0.01)
- mcu.write(E, False)
- def lcdDataWrite(mcu, data):
- move = 0x80
- print("write ", data)
- for i in range(0,8):
- mcu.write(DS, data & move != 0)
- mcu.write(SH_CP, False)
- mcu.write(SH_CP, True)
- move >>= 1
- mcu.write(ST_CP, True)
- mcu.write(ST_CP, False)
- mcu.write(SH_CP, False)
- def lcdXY(mcu, x, y):
- x += 0x80
- if y == 1:
- x += 0x40
- lcdCommand(mcu, x)
- def lcdString(mcu, s, x=-1, y=-1):
- if x >= 0 and y >= 0:
- lcdXY(mcu, x, y)
- for c in s:
- lcdData(mcu, ord(c))
- sleep(0.05)
- def lcdChar(mcu, x, y, ch):
- lcdXY(mcu, x, y)
- lcdData(mcu, ord(ch))
- try:
- mcu = NodeMcu(getIPPort())
- for o in [DS, ST_CP, SH_CP, E, RW, RS]:
- mcu.setOutput(o)
- mcu.write(SH_CP, False)
- mcu.write(ST_CP, False)
- init_lcd(mcu)
- while True:
- lcdCommand(mcu, 0x01) # clear screen
- lcdString(mcu, "Hello ESP!", 0, 0)
- sleep(10)
- except NodeMcuError as e:
- print("error:", e)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement