Advertisement
Guest User

Untitled

a guest
Jan 14th, 2020
103
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 2.53 KB | None | 0 0
  1.  
  2. import smbus
  3. import time
  4. import time
  5. import RPi.GPIO as GPIO
  6. from keypad import keypad
  7.  
  8. # Define some device parameters
  9. I2C_ADDR  = 0x27 # I2C device address, if any error, change this address to 0x3f
  10. LCD_WIDTH = 16   # Maximum characters per line
  11.  
  12. # Define some device constants
  13. LCD_CHR = 1 # Mode - Sending data
  14. LCD_CMD = 0 # Mode - Sending command
  15.  
  16. LCD_LINE_1 = 0x80 # LCD RAM address for the 1st line
  17. LCD_LINE_2 = 0xC0 # LCD RAM address for the 2nd line
  18. LCD_LINE_3 = 0x94 # LCD RAM address for the 3rd line
  19. LCD_LINE_4 = 0xD4 # LCD RAM address for the 4th line
  20.  
  21. LCD_BACKLIGHT  = 0x08  # On
  22. #LCD_BACKLIGHT = 0x00  # Off
  23.  
  24. ENABLE = 0b00000100 # Enable bit
  25.  
  26. # Timing constants
  27. E_PULSE = 0.0005
  28. E_DELAY = 0.0005
  29.  
  30. #Open I2C interface
  31. #bus = smbus.SMBus(0)  # Rev 1 Pi uses 0
  32. bus = smbus.SMBus(1) # Rev 2 Pi uses 1
  33.  
  34. def lcd_init():
  35.   # Initialise display
  36.   lcd_byte(0x33,LCD_CMD) # 110011 Initialise
  37.   lcd_byte(0x32,LCD_CMD) # 110010 Initialise
  38.   lcd_byte(0x06,LCD_CMD) # 000110 Cursor move direction
  39.   lcd_byte(0x0C,LCD_CMD) # 001100 Display On,Cursor Off, Blink Off
  40.   lcd_byte(0x28,LCD_CMD) # 101000 Data length, number of lines, font size
  41.   lcd_byte(0x01,LCD_CMD) # 000001 Clear display
  42.   time.sleep(E_DELAY)
  43.  
  44. def lcd_byte(bits, mode):
  45.   # Send byte to data pins
  46.   # bits = the data
  47.   # mode = 1 for data
  48.   #        0 for command
  49.  
  50.   bits_high = mode | (bits & 0xF0) | LCD_BACKLIGHT
  51.   bits_low = mode | ((bits<<4) & 0xF0) | LCD_BACKLIGHT
  52.  
  53.   # High bits
  54.   bus.write_byte(I2C_ADDR, bits_high)
  55.   lcd_toggle_enable(bits_high)
  56.  
  57.   # Low bits
  58.   bus.write_byte(I2C_ADDR, bits_low)
  59.   lcd_toggle_enable(bits_low)
  60.  
  61. def lcd_toggle_enable(bits):
  62.   # Toggle enable
  63.   time.sleep(E_DELAY)
  64.   bus.write_byte(I2C_ADDR, (bits | ENABLE))
  65.   time.sleep(E_PULSE)
  66.   bus.write_byte(I2C_ADDR,(bits & ~ENABLE))
  67.   time.sleep(E_DELAY)
  68.  
  69. def lcd_string(message,line):
  70.   # Send string to display
  71.  
  72.   message = message.ljust(LCD_WIDTH," ")
  73.  
  74.   lcd_byte(line, LCD_CMD)
  75.  
  76.   for i in range(LCD_WIDTH):
  77.     lcd_byte(ord(message[i]),LCD_CHR)
  78.  
  79.  
  80. if __name__ == '__main__':
  81.  
  82.   lcd_init()
  83.   kp = keypad(columnCount = 4)
  84.  
  85.       ###### Wait for command ending with # and print to LCD
  86.   digit=None
  87.   command = []
  88.   while digit !="#":
  89.       digit = None
  90.       while digit == None:
  91.           digit = kp.getKey()
  92.       seq.append(digit)
  93.       lcd_string(''.join(str(e) for e in command),LCD_LINE_1)
  94.       time.sleep(0.4)
  95.  
  96.   print(command)
  97.  
  98.   #try:
  99.   #  main()
  100.   #except KeyboardInterrupt:
  101.   #  pass
  102.   #finally:
  103.    # lcd_byte(0x01, LCD_CMD)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement