Advertisement
Guest User

Untitled

a guest
May 19th, 2025
6
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.03 KB | None | 0 0
  1. This the code I'm trying to run it with
  2. import RPi.GPIO as GPIO
  3. import time
  4. LCD_RS = 18
  5. LCD_E = 23
  6. LCD_D4 = 12
  7. LCD_D5 = 16
  8. LCD_D6 = 20
  9. LCD_D7 = 21
  10.  
  11. LCD_WIDTH = 20
  12. LCD_CHR = True
  13. LCD_CMD = False
  14.  
  15. LCD_LINE_1 = 0x80
  16. LCD_LINE_2 = 0xC0
  17. LCD_LINE_3 = 0x94
  18. LCD_LINE_4 = 0xD4 # Often the start of the fourth line
  19.  
  20. E_PULSE = 0.0005
  21. E_DELAY = 0.0005
  22.  
  23. def lcd_init():
  24. # Initialize GPIO
  25. GPIO.setmode(GPIO.BCM)
  26. GPIO.setwarnings(False)
  27. GPIO.setup(LCD_E, GPIO.OUT)
  28. GPIO.setup(LCD_RS, GPIO.OUT)
  29. GPIO.setup(LCD_D4, GPIO.OUT)
  30. GPIO.setup(LCD_D5, GPIO.OUT)
  31. GPIO.setup(LCD_D6, GPIO.OUT)
  32. GPIO.setup(LCD_D7, GPIO.OUT)
  33.  
  34. # Power on delay
  35. time.sleep(0.1)
  36.  
  37. # Initialization sequence
  38. lcd_byte(0x33, LCD_CMD)
  39. time.sleep(0.005)
  40. lcd_byte(0x33, LCD_CMD)
  41. time.sleep(0.005)
  42. lcd_byte(0x32, LCD_CMD)
  43. time.sleep(0.005)
  44. lcd_byte(0x28, LCD_CMD) # 4-bit mode, 2 lines, 5x7 font
  45. lcd_byte(0x08, LCD_CMD) # Display off, cursor off, blink off
  46. lcd_byte(0x01, LCD_CMD) # Clear display
  47. time.sleep(0.005)
  48. lcd_byte(0x06, LCD_CMD) # Entry mode: increment cursor, no shift
  49. lcd_byte(0x0C, LCD_CMD) # Display on, cursor off, blink off
  50.  
  51. def lcd_byte(bits, mode):
  52. # Send byte to data pins
  53. GPIO.output(LCD_RS, mode)
  54.  
  55. # High bits
  56. GPIO.output(LCD_D4, False)
  57. GPIO.output(LCD_D5, False)
  58. GPIO.output(LCD_D6, False)
  59. GPIO.output(LCD_D7, False)
  60. if bits & 0x10 == 0x10:
  61. GPIO.output(LCD_D4, True)
  62. if bits & 0x20 == 0x20:
  63. GPIO.output(LCD_D5, True)
  64. if bits & 0x40 == 0x40:
  65. GPIO.output(LCD_D6, True)
  66. if bits & 0x80 == 0x80:
  67. GPIO.output(LCD_D7, True)
  68.  
  69. # Toggle Enable pin
  70. time.sleep(E_DELAY)
  71. GPIO.output(LCD_E, True)
  72. time.sleep(E_PULSE)
  73. GPIO.output(LCD_E, False)
  74. time.sleep(E_DELAY)
  75.  
  76. # Low bits
  77. GPIO.output(LCD_D4, False)
  78. GPIO.output(LCD_D5, False)
  79. GPIO.output(LCD_D6, False)
  80. GPIO.output(LCD_D7, False)
  81. if bits & 0x01 == 0x01:
  82. GPIO.output(LCD_D4, True)
  83. if bits & 0x02 == 0x02:
  84. GPIO.output(LCD_D5, True)
  85. if bits & 0x04 == 0x04:
  86. GPIO.output(LCD_D6, True)
  87. if bits & 0x08 == 0x08:
  88. GPIO.output(LCD_D7, True)
  89.  
  90. # Toggle Enable pin
  91. time.sleep(E_DELAY)
  92. GPIO.output(LCD_E, True)
  93. time.sleep(E_PULSE)
  94. GPIO.output(LCD_E, False)
  95. time.sleep(E_DELAY)
  96.  
  97. def lcd_string(message, line):
  98. # Send string to LCD at specified line
  99. message = message.ljust(LCD_WIDTH, " ")
  100. lcd_byte(line, LCD_CMD)
  101. for i in range(LCD_WIDTH):
  102. lcd_byte(ord(message[i]), LCD_CHR)
  103.  
  104. def main():
  105. # Main program block
  106. lcd_init()
  107.  
  108. try:
  109. lcd_string("Line 1: Hello!", LCD_LINE_1)
  110. lcd_string("Line 2: From Pi", LCD_LINE_2)
  111. lcd_string("Line 3: 20x4 LCD", LCD_LINE_3)
  112. lcd_string("Line 4: Test!", LCD_LINE_4)
  113.  
  114. time.sleep(5) # Keep the message displayed for longer
  115.  
  116. finally:
  117. GPIO.cleanup()
  118.  
  119. if __name__ == '__main__':
  120. main()
  121.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement