Guest User

Lcd.py

a guest
Sep 15th, 2015
108
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #!/usr/bin/python
  2. #
  3. # HD44780 LCD Test Script for
  4. # Raspberry Pi
  5. #
  6. # Author : Matt Hawkins
  7. # Site : http://www.raspberrypi-spy.co.uk
  8. #
  9. # Date : 03/08/2012
  10. #
  11.  
  12. # The wiring for the LCD is as follows:
  13. # 1 : GND
  14. # 2 : 5V
  15. # 3 : Contrast (0-5V)*
  16. # 4 : RS (Register Select)
  17. # 5 : R/W (Read Write) - GROUND THIS PIN
  18. # 6 : Enable or Strobe
  19. # 7 : Data Bit 0 - NOT USED
  20. # 8 : Data Bit 1 - NOT USED
  21. # 9 : Data Bit 2 - NOT USED
  22. # 10: Data Bit 3 - NOT USED
  23. # 11: Data Bit 4
  24. # 12: Data Bit 5
  25. # 13: Data Bit 6
  26. # 14: Data Bit 7
  27. # 15: LCD Backlight +5V**
  28. # 16: LCD Backlight GND
  29.  
  30. #import
  31. import RPi.GPIO as GPIO
  32. import time
  33.  
  34. # Define GPIO to LCD mapping
  35. LCD_RS = 26
  36. LCD_E = 19
  37. LCD_D4 = 13
  38. LCD_D5 = 6
  39. LCD_D6 = 5
  40. LCD_D7 = 11
  41. LED_ON = 15
  42.  
  43. # Define some device constants
  44. LCD_WIDTH = 16 # Maximum characters per line
  45. LCD_CHR = True
  46. LCD_CMD = False
  47.  
  48. LCD_LINE_1 = 0x80 # LCD RAM address for the 1st line
  49. LCD_LINE_2 = 0xC0 # LCD RAM address for the 2nd line
  50.  
  51. # Timing constants
  52. E_PULSE = 0.00005
  53. E_DELAY = 0.00005
  54.  
  55. def main():
  56. # Main program block
  57.  
  58. # Initialise display
  59. lcd_init()
  60.  
  61. # Toggle backlight on-off-on
  62. GPIO.output(LED_ON, True)
  63. time.sleep(1)
  64. GPIO.output(LED_ON, False)
  65. time.sleep(1)
  66. GPIO.output(LED_ON, True)
  67. time.sleep(1)
  68.  
  69. # Send some centred test
  70. lcd_byte(LCD_LINE_1, LCD_CMD)
  71. lcd_string("Rasbperry Pi",2)
  72. lcd_byte(LCD_LINE_2, LCD_CMD)
  73. lcd_string("Model B",2)
  74.  
  75. time.sleep(3) # 3 second delay
  76.  
  77. # Send some left justified text
  78. lcd_byte(LCD_LINE_1, LCD_CMD)
  79. lcd_string("1234567890123456",1)
  80. lcd_byte(LCD_LINE_2, LCD_CMD)
  81. lcd_string("abcdefghijklmnop",1)
  82.  
  83. time.sleep(3) # 3 second delay
  84.  
  85. # Send some right justified text
  86. lcd_byte(LCD_LINE_1, LCD_CMD)
  87. lcd_string("Raspberrypi-spy",3)
  88. lcd_byte(LCD_LINE_2, LCD_CMD)
  89. lcd_string(".co.uk",3)
  90.  
  91. time.sleep(30)
  92.  
  93. # Turn off backlight
  94. GPIO.output(LED_ON, False)
  95.  
  96. def lcd_init():
  97. GPIO.setmode(GPIO.BCM) # Use BCM GPIO numbers
  98. GPIO.setup(LCD_E, GPIO.OUT) # E
  99. GPIO.setup(LCD_RS, GPIO.OUT) # RS
  100. GPIO.setup(LCD_D4, GPIO.OUT) # DB4
  101. GPIO.setup(LCD_D5, GPIO.OUT) # DB5
  102. GPIO.setup(LCD_D6, GPIO.OUT) # DB6
  103. GPIO.setup(LCD_D7, GPIO.OUT) # DB7
  104. GPIO.setup(LED_ON, GPIO.OUT) # Backlight enable
  105. # Initialise display
  106. lcd_byte(0x33,LCD_CMD)
  107. lcd_byte(0x32,LCD_CMD)
  108. lcd_byte(0x28,LCD_CMD)
  109. lcd_byte(0x0C,LCD_CMD)
  110. lcd_byte(0x06,LCD_CMD)
  111. lcd_byte(0x01,LCD_CMD)
  112.  
  113. def lcd_string(message,style):
  114. # Send string to display
  115. # style=1 Left justified
  116. # style=2 Centred
  117. # style=3 Right justified
  118.  
  119. if style==1:
  120. message = message.ljust(LCD_WIDTH," ")
  121. elif style==2:
  122. message = message.center(LCD_WIDTH," ")
  123. elif style==3:
  124. message = message.rjust(LCD_WIDTH," ")
  125.  
  126. for i in range(LCD_WIDTH):
  127. lcd_byte(ord(message[i]),LCD_CHR)
  128.  
  129. def lcd_byte(bits, mode):
  130. # Send byte to data pins
  131. # bits = data
  132. # mode = True for character
  133. # False for command
  134.  
  135. GPIO.output(LCD_RS, mode) # RS
  136.  
  137. # High bits
  138. GPIO.output(LCD_D4, False)
  139. GPIO.output(LCD_D5, False)
  140. GPIO.output(LCD_D6, False)
  141. GPIO.output(LCD_D7, False)
  142. if bits&0x10==0x10:
  143. GPIO.output(LCD_D4, True)
  144. if bits&0x20==0x20:
  145. GPIO.output(LCD_D5, True)
  146. if bits&0x40==0x40:
  147. GPIO.output(LCD_D6, True)
  148. if bits&0x80==0x80:
  149. GPIO.output(LCD_D7, True)
  150.  
  151. # Toggle 'Enable' pin
  152. time.sleep(E_DELAY)
  153. GPIO.output(LCD_E, True)
  154. time.sleep(E_PULSE)
  155. GPIO.output(LCD_E, False)
  156. time.sleep(E_DELAY)
  157.  
  158. # Low bits
  159. GPIO.output(LCD_D4, False)
  160. GPIO.output(LCD_D5, False)
  161. GPIO.output(LCD_D6, False)
  162. GPIO.output(LCD_D7, False)
  163. if bits&0x01==0x01:
  164. GPIO.output(LCD_D4, True)
  165. if bits&0x02==0x02:
  166. GPIO.output(LCD_D5, True)
  167. if bits&0x04==0x04:
  168. GPIO.output(LCD_D6, True)
  169. if bits&0x08==0x08:
  170. GPIO.output(LCD_D7, True)
  171.  
  172. # Toggle 'Enable' pin
  173. time.sleep(E_DELAY)
  174. GPIO.output(LCD_E, True)
  175. time.sleep(E_PULSE)
  176. GPIO.output(LCD_E, False)
  177. time.sleep(E_DELAY)
  178.  
  179. if __name__ == '__main__':
  180. main()
RAW Paste Data