Advertisement
ceterumcenseo

LCD1602.py

Mar 28th, 2019
291
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 2.40 KB | None | 0 0
  1. #!/usr/bin/python3
  2.  
  3. # This is a more complicated example utilizing a 74HC595 shift register
  4. # to control a 7 segment display. The wiring from the NodeMCU to the
  5. # shift register is described below.
  6.  
  7. from nodemcu import NodeMcu, NodeMcuError, D
  8. from time import sleep
  9. from sys import exit
  10. from common import getIPPort
  11. from time import sleep
  12.  
  13. # Wiring       Names from the TI data sheet
  14. DS = D[0]    # SER
  15. ST_CP = D[1] # RCLK
  16. SH_CP = D[2] # SRCLK
  17.  
  18. E = D[5]
  19. RW = D[6]
  20. RS = D[7]
  21.  
  22. def init_lcd(mcu):
  23.         sleep(0.01)
  24.         lcdCommand(mcu, 0x38)
  25.  
  26.         sleep(0.01)
  27.         lcdCommand(mcu, 0x38)
  28.  
  29.         sleep(0.01)
  30.         lcdCommand(mcu, 0x38)
  31.  
  32.         sleep(0.01)
  33.         lcdCommand(mcu, 0x06)
  34.  
  35.         sleep(0.01)
  36.         lcdCommand(mcu, 0x0c)
  37.  
  38.         sleep(0.01)
  39.         lcdCommand(mcu, 0x01)
  40.  
  41. def lcdCommand(mcu, cmd):
  42.         mcu.write(RS, False)
  43.         mcu.write(RW, False)
  44.         mcu.write(E, True)
  45.  
  46.         lcdDataWrite(mcu, cmd)
  47.         sleep(0.01)
  48.         mcu.write(E, False)
  49.  
  50. def lcdData(mcu, data):
  51.         mcu.write(RS, True)
  52.         mcu.write(RW, False)
  53.         mcu.write(E, True)
  54.  
  55.         lcdDataWrite(mcu, data)
  56.         sleep(0.01)
  57.         mcu.write(E, False)
  58.  
  59. def lcdDataWrite(mcu, data):
  60.         move = 0x80
  61.  
  62.         print("write ", data)
  63.         for i in range(0,8):
  64.                 mcu.write(DS, data & move != 0)
  65.                 mcu.write(SH_CP, False)
  66.                 mcu.write(SH_CP, True)
  67.                 move >>= 1
  68.  
  69.         mcu.write(ST_CP, True)
  70.         mcu.write(ST_CP, False)
  71.         mcu.write(SH_CP, False)
  72.  
  73.  
  74. def lcdXY(mcu, x, y):
  75.         x += 0x80
  76.         if y == 1:
  77.                 x += 0x40
  78.         lcdCommand(mcu, x)
  79.  
  80. def lcdString(mcu, s, x=-1, y=-1):
  81.         if x >= 0 and y >= 0:
  82.                 lcdXY(mcu, x, y)
  83.         for c in s:
  84.                 lcdData(mcu, ord(c))
  85.                 sleep(0.05)
  86.  
  87. def lcdChar(mcu, x, y, ch):
  88.         lcdXY(mcu, x, y)
  89.         lcdData(mcu, ord(ch))
  90.  
  91.  
  92. try:
  93.         mcu = NodeMcu(getIPPort())
  94.  
  95.         for o in [DS, ST_CP, SH_CP, E, RW, RS]:
  96.                 mcu.setOutput(o)
  97.  
  98.         mcu.write(SH_CP, False)
  99.         mcu.write(ST_CP, False)
  100.  
  101.         init_lcd(mcu)
  102.  
  103.         while True:
  104.                 lcdCommand(mcu, 0x01) # clear screen
  105.                 lcdString(mcu, "Hello ESP!", 0, 0)
  106.                 sleep(10)
  107.  
  108.  
  109. except NodeMcuError as e:
  110.         print("error:", e)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement